You are on page 1of 2

MAD 4401

Project 4
Least Squares Approximation and Numerical Linear Algebra
February 20, 2011

1. Let x=0:.2:1, y=rand(1,6) plot this data plot(x,y,o). Using MATLABs polyfit find the least
squares polynomials of degrees, deg= 1, 2, 3, 4, 5 and plot them all together with the data. Save the plot to
turn in.

2. Collect data from the function y=logistic below. Plot x=1:200 and y and hold the plot. Now find the
regression line through the data. Plot the regression line with the data. Save the plot to turn in.

function y=logistic
for i=1:200,

3. Let F (x) = x3 + 2x2 x + 3 and let f be the vector of coefficients of F (x). Plot the polynomial as follows:
(1) On the interval [1, 3] using the sample vector xs=-1:.01:3 and let ys be the values of F (x) for
this sample.
(2) Add some random noise yn=ys+1-2*rand(1,401) and plot the noisy vector with F (x). Save the
plot to turn in.
(3) Do a degree 3 least squares fit using the data from the vectors xs and yn and call the outcome
f3. Look at max(abs(f-f3/f3(1)) to see how close the regression polynomial is to the original polynomial.
Why did we divide by f3(1)?
(4) Collect 50 regularly spaced knot points from xs and yn and call the outcomes x and y. Do a least
squares cubic fit to this data. Call the outcome g3. How close is g3 to f? (like part (3))
(4) Plot the least squares polynomial by forming y3=polyval(g3,xs) with the plot of the noisy vector,
yn. Save the plot to turn in.
(5) What is the absolute error and the relative error between ys and y3.

4. For the function Runge R(x) = 1+x 2,

(1) Plot R(x) on the interval [10, 10] using a fine resolution sample like xs=-10:.01:10. Let ys be
the values of R(x) on the sample xs.
(2) Now form the knots with x=-10:2:10 and let y be the values of R(x) on x.
plot(xs,ys), hold, plot(x,y,o)
(3) Use MATLABs polyfit to find the regression polynomials f6, f8 and f10 of degrees 6, 8 and 10.
Plot these with your plot of R(x). Call the plotting vectors y6, y8, y10. Save the plot to turn in.
(4) By looking at the absolute errors,
max(abs(ys-y6)), etc
and the relative error,
max(abs((ys-y6)./ys)), etc
which of these polynomials is the better fit to R(x)?

5. The following data is purported to show the growth in participation over a period of 31
months. It was claimed that experienced exponential growth.
x=[1,5,7,11,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28, 29,30,31]
The x vector is in months and the y vector in millons of subscribers.
(1) Plot the data.

(2) Find the least squares quadratic polynomial and plot it with the data. Use a fine resolution sample
for plotting like xs=0:.01:35.
(3) Find the maximum of the absolute error at the knot points.
An exponential polynomial has the form y = aebx where a and b are constants. We can get a least squares
exponential fit by taking the log of both sides to get

log(y) = log(a) + bx

Letting y1 = log(y), a1 = log(a), b1 = b and x1 = x this is a linear equation

y1 = a1 + b1 x1

which has a least squares approximation using our methods. Now recover a = ea1 and b1 = b.
(4) Plot y = aebx using MATLABs exp to compute the exponential. Save the plot to turn in.
(5) Find the maximum of the absolute error at the knot points.
(6) Which curve, the quadratic or the exponential, is the better fit?

6. Write a MATLAB function [L,U]=mylu(A) which finds the LU Decomposition of A. You may assume
that no row switches are needed. Check your mylu on rand(5) and magic(5). You have three things to
check: L is unit lower triangular, U is upper triangular, and A = LU.

7. Write a function [x,count]=ppmysolve(A,b) based on your mysolve which incorporates the partial
pivoting strategy. The output variable count should count the number of row switches. Test your ppmysolve
on these systems. How many row switches are used in each of these?
(a) A=magic(7), b=A*ones(7,1)
(b) A=vander(rand(8,1)), b=A*ones(8,1)
(c) A=A*A where A=vander(rand(8,1)), b=A*ones(8,1)

8. Write a MATLAB function [Q,R]=gramqr(A) which finds a QR Decomposition of A using the Gram
Schmidt Method. Do not, repeat, DO NOT, assign R = QT A. Test your gramqr on these matrices. You
have three things to check for in each.
(a) A=magic(5)
(b) A=pascal(6)
(c) A=hilb(8)
(d) A=ones(5)+eps*eye(5)

9. Write a MATLAB function [Q,R]=mgs(A) which finds a QR Decomposition of A using the Modified
Gram-Schmidt method. Check your mgs on the matrices in problem 8.

10. Using this matrix

A=triu(rand(8),1)+2 b (-20)*eye(8)
compute cond(A) and rcond(A). Read up on these functions using MATLABs help. Set
Use the condition number to predict the number of accurate decimal places in the computed answer.