AMATH 301 - Autumn 2014 Due: 11:59:59pm Friday October 31 , 2014

AMATH 301 - Autumn 2014
Homework 3
Due: 11:59:59pm Friday October 31st , 2014
VERY IMPORTANT: Remember to suppress all output and plotting before submitting your code.
Download data.txt from the course website. Copy and paste the data into the first few lines
of your M file. Do not use the load command here. The data in data1, data2, data3, data4 is
arranged so that (xj , yj ) is stored in the j th column.
Exercise 1: Plot the points in data1 using the ‘o’ linespec command so that Matlab doesn’t
connect the points. Fit the following curves to the data, using polyfit:
f1 (x) = c1 x2 + c2 x + c3
f2 (x) = c1 x4 + c2 x3 + c3 x2 + c4 x + c5
f3 (x) = c1 x6 + c2 x5 + c3 x4 + c4 x3 + c5 x2 + c6 x + c7
Note that you may get some warnings about the polynomial degree (especially when the degree is
greater than N −1). Plot the three curves using polyval along with the data (can use x = -2:0.01:2 ).
For each curve, store the coefficients as a row vector with cj in the j th column.
Answers: Should be written out as A1-A3.dat.
Plot the points in data2 and fit the data with a 10th degree polynomial and a cubic spline. For
the latter, use interp1 along with the ‘cubic’ method specifier. Plot the two curves along with
the data. Note that one of these looks a lot better than the other. Use the polynomial curve to
interpolate the data at x = {0.5, 1.5, 2.5, . . . , 8.5, 9.5}. Store the corresponding values in a row
vector and repeat the process to use the cubic spline to interpolate.
Answers: Should be written out as A4-A5.dat.
The rest of Exercise 1 is optional (not counted towards total by CSB)
For data3, you must decide on what curve to use to fit the data. To do this, you can use the
commands semilogy and loglog. This commands work just like plot (ie loglog(x,y) plots x and y).
The difference between these commands is that they plot the data with different scalings than plot.
To see this, plot f (x) = exp(2x) for x ∈ [0, 10] with the plot, semilogy, and loglog commands. The
semilogy plot should appear as a line because it plots the y-values on a log scale. Note that the
slope is 2, corresponding to the coefficient of x in f (x).
Now try plotting f (x) = 3x4 for x ∈ [0, 10] with the plot, semilogy, and loglog commands. The
loglog plot should appear as a line because it plots both the x and y values on a log scale. Note
that the slope is 4, corresponding to the power of x in f (x).
Use these new commands to determine whether f (x) = c2 exp(c1 x) or f (x) = c2 xc1 is the best
curve to use to fit data3. Use the appropriate Matlab command (fminsearch or polyfit) to obtain c1
and c2 . Plot your curve along with the data to be sure you have a good fit. Compute the values at
x = {0.5, 1.5, 2.5, . . . , 8.5, 9.5} using your curve choice and c1 ,c2 . Store the values in a row vector
Answer: Should be written out as A6.dat.
1
AMATH 301 - Autumn 2014
Due: 11:59:59pm Friday October 31st , 2014
Homework 3
Exercise 2: Verify that data4 should be fit using an exponential by plotting with semilogy. Fit
the data with
f (x) = c2 exp(c1 x)
using fminsearch. Define your objective function to be the least-squares error
between the data and f (x). Store c1 and c2 as a row vector.
q
1
N
PN
j=1 (yj
− f (xj ))2
Answer: Should be written out as A7.dat.
Plot the curve along with the data to verify that your curve is a good fit. Find the value of the
least squares error of the final fit that fminsearch produces. (Hint: you can get this by using
[c,fval,exitflag] = fminsearch(. . . )).
Answer: Should be written out as A8.dat.
Use your curve to obtain values at x = {0.5, 1.5, 2.5, . . . , 8.5, 9.5}. Store these as a row vector.
Answer: Should be written out as A9.dat.
2
AMATH 301 - Autumn 2014
Due: 11:59:59pm Friday October 31st , 2014
Homework 3
Exercise 3: The Alinco costume company is trying to decide how many of each Husky costume
to make for Halloween. There have two models: A (link here) costing $1199.00 and B (link here)
costing $1049.00 (actual data). The company has 3 employees: Harry, Dubs, and George (not
actual data). Harry can only make model A and George can only make model B. Dubs, being a
dog expert of sorts, can make both model A and model B. As such, Harry and George make $100
for each costume made while Dubs makes $150. Come up with the profit function for the company
if HA is how many costumes are made by Harry, GB is how many costumes are made by George,
and DA , DB are how many of each costume are made by Dubs.
On second thought, I’ll do it for you:
p(HA , DA , DB , GB ) = 1199(HA + DA ) + 1049(DB + GB ) − 100HA − 100GB − 150(DA + DB )
Finally, contracts require that at least 150 of model A and 125 of model B must be made and that
each employee is required to make no less than 50 and no more than 100 costumes:
150 ≤HA + DA
125 ≤DB + GB
50 ≤HA ≤ 100
50 ≤DA + DB ≤ 100
50 ≤GB ≤ 100
Let’s find how many of each costume should be assigned to each employee to maximize profit.
First, write the profit function p in terms of a linear minimization problem (min cT x), where
x = [HA , DA , DB , GB ]T . Store the elements of c as a row vector.
Answer: Should be written out as A10.dat.
Next, rewrite the inequality constraints in the form Ax ≤ b. Note that 50 ≤ xj ≤ 100 should
be written as two constraints: 50 ≤ xj (or equivalently −xj ≤ −50) and xj ≤ 50. The matrix
A should then be 10x4 (remember that DA ≥ 0 and DB ≥ 0). Finally, use linprog to solve the
constrained minimization problem and obtain the optimal x = [HA , DA , DB , GB ]T . What was the
maximal profit gained by your solution? Note you should check that these values make sense before
submitting your assignment (are the constraints met? etc).
Because CSB does not have linprog installed, you’ll need to submit your optimal x statically. To
do this, run your code on your machine (or in the lab) and get the values for x. Then comment
out the line that calls linprog. Finally, add lines to your code that are something similar to:
>> xsave = [100; 100; 100; 100];
>> save(0 A11.dat0 ,0 xsave0 ,0 −ascii0 );
but replace the 100s with your values for the optimal x.
3