# CS310 Homework 2Lec 1, Spring 2013 Due by 4:00 pm on Friday, February 22

## Announcements

Check here periodically.

 2/14/2013 Homework assigned. To ask questions about the homework and see questions posed by other students and their answers, go to: http://piazza.com/class#spring2013/cs310 and sign-in using your wisc.edu account.

## Overview

This assignment introduces you to linear systems as well as interpolating and approximating data.

### Learning Outcomes

• Model problems as a system of linear equations.
• Transform a system of linear equations into a single matrix equation.
• Solve linear systems in MATLAB using the backslash operator.
• Gain more experience writing user-defined functions.
• Interpolate data using polynomials and splines.
• Use MATLAB's plot command to visualize solution results.
• Use an interpolating function to make estimates of values between data points.
• Fit data using an approximating function that is not a polynomial by transforming the approximating function to a polynomial format.

## Problems

homework2.m

For problems where you are asked to write equations, you may use \$\$eqn\$\$ to enter the equations into your m-file and publish them as part of your MATLAB solution (as you did in Team Lab 1), or you can put them in a comment in your m-file, or you can hand write the equations onto your printed solution (or on a separate sheet). Otherwise, complete ALL your work in MATLAB (in your homework2.m script and any functions you write).

### Problem 1: (4 points) Component Analysis

An electrical engineer supervises the production of three types of electrical components. Three kinds of material -- metal, plastic, and rubber -- are required for production. The amounts needed to produce each component are:

ComponentMetal
(g/component)
Plastic
(g/component)
Rubber
(g/component)
A
15
0.30
1.0
B
17
0.40
1.2
C
19
0.55
1.5

Suppose that totals of 3.89, 0.095, and 0.282 kg of metal, plastic, and rubber, respectively, are used each day. We wish to find how many of each component have been produced per day.

1. Give the equations for this linear system, making sure to identify what the unknowns are.

2. Solve your system from part a using MATLAB by creating the coefficient matrix and data vector and solving the system using the backslash operator.

### Problem 2: (9 points) Heat Plate

In much the same way that we solved the bug on a bar problem in Team Lab 3, we can solve a linear system to determine the temperature at various locations across a heat plate. We have simplified the problem to allow students to focus on the general concepts used when modeling such a problem as a system of linear equations.

Imagine that you wish to know the temperature at all points on a plate that is insulated except for two positions on the edge where heating elements of known temperature are applied. At some point when the temperature at all positions on the plate have stabilized, what will the temperature at each point be?

To solve such a problem, we imagine the plate surface divided into equal sized "tiles" and restate the problem to find the temperature at the center of each tile. We know that the temperature of each tile depends upon its neighboring temperatures. In fact, it is a weighted average of those temperatures based on the proportion of our tile's edge that is shared with its neighbor. Here is a rectangular "plate" divided into 15 equal area tiles. Assume that each edge of each tile has identical length, so that the temperature of any one tile is a simple average of the temperatures of all neighboring tiles that share a side with the tile in question. This creates a system of 15 equations, one for each "tile" area.

TA and TB are the non-insulated positions along the edge where a heat source of known temperature will be applied. The temperatures at T1 through T15 are the unknowns that you wish to find.

1. Define a function named heatPlate that accepts two temperature values TA and TB (representing TA and TB, respectively) and returns a single matrix result that is a 5x3 matrix of temperatures for the unknowns T1 through T15 in the order labeled above. If you can not figure out how to return the values as a 5x3 matrix, return them as a column vector for a minor (1/2 pt) deduction.

To solve this problem, identify 15 equations. Each equation represents the temperature value for one tile based upon the temperature values of each of its neighboring tiles. For example, the temperature at T11 is equal to the average of the temperatures: T8, T10, T12, and T14.

What about the corners? Well, since the gray area is insulation, the only effect is from the non-insulating tiles. Thus, T3 is equal to the average of only T2 and T6.

How do TA and TB factor into the equations? They will be in the equations, but since their values will be known when the function is called, the terms with TA and TB should be on the right-hand side of the equations once the equations are put into general form.

DO NOT SOLVE FOR ANY OF THE TEMPERATURES OF THIS PROBLEM BY HAND. Work out the equations for each tile and then reorder the terms in general form, with the unknown terms on the left-hand side and the known terms on the right-hand side of the equation. Once you have the equations, rewrite the linear system as a matrix equation and put the commands to solve this problem in your heatPlate(TA,TB) function definition.

2. In your homework2.m script, write code to call your heatPlate function to compute the values of T1 through T15 for each of the following conditions. Do not suppress the output of your function calls.

1. Find T1 through T15 when TA = 100 and TB = 100

2. Find T1 through T15 when TA = 0 and TB = 75

3. Find T1 through T15 when TA = 80 and TB = 15

### Problem 3: (12 points) Interpolation

The temperature for a midwest city was collected at one-hour intervals. Data was collected throughout a 24-hour period, starting at 8 AM, and is reported in the following table. Unfortunately, data for five different hours was lost.

 Hours Temp (F) 1 2 3 4 5 6 7 8 9 10 11 12 46.9 53.1 55.9 60.1 63 64.9 69.1 71.1 70 ? 70 69.1

 Hours Temp (F) 13 14 15 16 17 18 19 20 21 22 23 24 ? 66.9 64.9 ? 60 59 53 46 46.9 ? ? 43
1. Interpolate the data using a polynomial that goes through all the data points. (Note: hours with missing temperatures should be ignored).

2. Plot the data points and the interpolating polynomial through all the data points over the range x = 1 to 24 on a single figure. Use diamonds to represent the data points and a solid red line for the polynomial. Use the axis command to scale the plot so that x ranges from -1 to 25 and y ranges from 20 to 90.

3. Plot the spline interpolation through the same data points on the same figure as part b. Use a black dotted line for the spline.

4. For each of the hours with missing temperatures, estimate the temperature three ways:

1. using your polynomial interpolation through all the data points
3. using a polynomial interpolation through the four data points nearest the missing value

Put your results in a matrix with 4 rows and 5 columns where row 1 contains the hours, row 2 contains the results using (i), row 3 contains your results using (ii), and row 4 contains your results using (iii). You can create the results matrix and set the first row using the following:

```results = zeros(4, 5);
results(1, :) = [10, 13, 16, 22, 23];
```

Don't forget to display your final matrix with all the results.

5. Briefly analyze your results from part d. Does one interpolation give better estimates than the others? Are the estimates for some missing hours worse than others?

### Problem 4: (5 points) Approximation

For the data in the following table, the y data obviously decays as x increases:

 x data y data 1 2.12 3.09 5.23 7.64 9.14 11.2 14.3 16.1 18.3 22.7 25 2.43 2.21 2.07 1.79 1.63 1.58 1.49 1.42 1.39 1.34 1.27 1.24
1. Fit this data to a curve of the form:

(Hint: transform the curve to a polynomial.)

2. Plot the data points and approximating function over the range x = 1 to 25 on a single figure. Use stars to represent the data points and a blue dashed line for the approximating curve.

## Handing in

• Do all of your work (except the function definitions) in your homework2.m file. Show all your MATLAB commands as well as the results of the commands. See the information above for more complete details.

• Publish your homework2 (to either HTML or PDF) and print the published results.

• Print your functions definitions separately (use File->Print).

• Make sure to look at your printed work to see that it is formatted the way you want and that everything got printed.

• Staple your work. Put the published script first and then the function print-outs.

• Turn in your work by 4 pm Friday, February 22. You may turn in your work at Team Lab on Thursday, February 21 or make sure it is put in the drop-box outside Beck Hasti's office door by 4 pm on Friday, February 22. Beck's office is on the 5th floor of the Computer Sciences building, room 5375.

• Late policy: work may be submitted up to 24 hours late with a 10% penalty. To turn in work after the due date (i.e., late), publish your homework2 to PDF (and not HTML) and email your PDF file and any function definition m-files to Beck Hasti (hasti@cs.wisc.edu) with the subject "CS 310 Homework 2 late submission".
Last Updated: 2/14/2013     © 2012 Beck Hasti, hasti@cs.wisc.edu; some problems based on problems from Applied Numerical Methods with MATLAB for Engineers and Scientists (2nd ed), Steven C. Chapra, McGraw-Hill (2008)