You are on page 1of 3
svar Rilo clan (Lana square) % MATLAB Demo (Least squares) % File: Isdeno Find the least-squares parabola for the 5 data points: tloy el 3 a] 75 21 25 3 14s 4] 23.5 Formulate problen in terms of finding the least-squares solution to Ax Verify the results, and plot a graph of the data vs. the paratola. Compute the projection matrix for the column space of A. outline: The matrix A has 5 rows; rowk is [2 t{k} t_{k}/2 ]. The vector b has 5 components; component k is y_{k}. Use ref([A b]) to show that Ax = b is NOT solvable. Usc 18.06 MATLAB command Isq(A, b) to find the least-squares solution. verify: p = aXxbar, pre = b, and ¢ is orthogonal to p. Give a plot of data points vs, the least-squares parabola, Compute the projection matrix, or use 18.86 command projmat(A) ne 2 8 2G OE BE OE BE 2G DE 9G AEE DE IE HE OE DERE DE REIS >> diary 1sdemo >> b= [-3; 7.55 2.5; 14.55 23.5]; >> A= (11111; 01234; 0149 16); >> A= A" A= 1 2 @ 1 4 2 4 20098 20 4 46 >> Z= [Ab] ze 1.2200 a @ -3.av00 1.0200 © 1.0008 1.0000 7.5080 1.0200 © 2.0008 © 4.0000 © 2.5280 1.0200 © 3.0008 © 9.0000 14.5080 1.0202 4.2008 16.0000 23.5288 >> Z = ref(Z) Ze 2 2 @ @ @ 41 ea @ oe 1 @ @ oe @ 1 @ eo @ 4 * 3806 This reduced row echelon form [R d] shows that d cannot 980% be expressed as a linear conbination of the calunns of R. 9806 Rx=d (and Ax=b) is not solvable. * >> help 1sq LsQ” Least squares. Dwbar,p,e] = LSQ(A,b) finds a least squares ip ives mitedu1®.0shwww MATLAB Res sions derail 1% svar Rilo clan (Lana square) solution to the overdetermined system A*x == b. xbar = the solution to the normal equations. p = projection of b onto the colum space. e=b-p. >> [ebar, p, e] = 1sa(A, ») xbar 2.0800 5.5@00 4.580 @.5¢00 @.5¢00 3006 Check: A*xbar = >> A* xbar 1 2 RK Check: pee = b >> pie ans -3.0200 7.5000 2.5200 14.5200 23.5000 9904 Check: @ is orthogonal to p >> pire ans e x 48% Plot the data points and parabola over the interval -1 <= t < x >> t= linspace(-1, 5, 21); >> tats >> xbar xbar 300% Compute y(t) for each value of t in the interval [-1, 5]. x >> y= xbar(1) + xbar(2)rt + xbar(a)*tt.*2; >> plot (t,y,'r=")5 >> grid on; hold on ip ives mitedu1®.0shwww MATLAB Res sions derail 23 svar Rilo clan (Lana square) x 900, x >> plot (L@; 15 2; 33 4], bs ‘e+")s >> xlabel(*L'}5 >> ylabel(*y(t)")5 >> title(‘Least-squares parabola: y(t)'); >> print -dps parabola.ps yt the 5 data points, and label the axes. x 9% Compute the projection matrix, P = Atinv(A™*A)*A". 9% Recall that A has 5 rows and 3 colunns. XK% A must have linearly independent columns in order for A'+A 58% to be invertible. x >> format rat >> P= Atinv(A'+A)=A" Pe 31/35, 9/35 -3/35 “7 3/35 9/35 13/35 12/35 6/35 “7 -3/35 12/35 17735 12/35 3/35 1/7 6/35 12/35 13/35, 9/35 3/35 1/7 -3/35 9/35 31/35 380% Check: the colunns of P span a 3-dimensional subspace of {5}. %%% In particular, this subspace is the colunn space of the matrix A. >> -pank(P) 3 >> help projnat PROJMAT Projection matrix onto the colunn space. P = projnat(A) is the symmetric, square matrix that projects any vector onto the column space of A. >> projnat(A) ans 9/35 -3/35 1/7 3/35 13/35, 12/35 6/35 1/7 12/35 17/35 12/35 3/35 6/35 12/35 13/35 9/35 “7 3/35 9/35 31/35, 39% Check: The projection of b onto the column space of A is p. >> projmat(A)*> ans = a 2 7 24 23 >> diary off ip ives mitedu1®.0shwww MATLAB Res sions derail

You might also like