{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw3sol

# hw3sol - MIT 18.335 Fall 2007 Homework 3 Solutions...

This preview shows pages 1–3. Sign up to view the full content.

MIT 18.335, Fall 2007: Homework 3, Solutions 1. (Trefethen/Bau 11.3) The following lines of code creates the matrices, computes the six different least squares approximations, and shows the result in a way that makes it easy to compare each of the coefficients. m=50; n=12; t=linspace(0,1,m)’; A=vander(t); A=fliplr(A(:,m-n+1:m)); b=cos(4*t); xa=(A’*A)\(A’*b); [Q,R]=mgs(A); xb=R\(Q’*b); [W,R]=house(A); Q=formQ(W); xc=R\(Q(:,1:n)’*b); [Q,R]=qr(A,0); xd=R\(Q’*b); xe=A\b; [U,S,V]=svd(A,0); xf=V*(S\(U’*b)); format long coeffs=[xa xb xc xd xe xf]’ format short The printout is Columns 1 through 4 1.00000000261118 -0.00000097548021 -7.99995921914247 -0.00065762300158 0.99999999859093 0.00000027088201 -8.00000704985626 0.00005898516353 1.00000000099660 -0.00000042274292 -7.99998123568615 -0.00031876324278 1.00000000099661 -0.00000042274316 -7.99998123568337 -0.00031876324997 1.00000000099661 -0.00000042274332 -7.99998123567749 -0.00031876333094 1.00000000099661 -0.00000042274317 -7.99998123568300 -0.00031876325403 Columns 5 through 8 10.67211753087116 -0.02630342671309 -5.61079800311668 -0.14317854424735 10.66655515934153 -0.00090455925668 -5.68354616965529 -0.00876156857361 10.66943079603079 -0.01382028866396 -5.64707562539748 -0.07531602783445 10.66943079596514 -0.01382028807910 -5.64707562752298 -0.07531602343290 10.66943079654419 -0.01382029054076 -5.64707562089245 -0.07531603505147 10.66943079598727 -0.01382028814670 -5.64707562740522 -0.07531602353876 Columns 9 through 12 1.77528406414325 -0.05507362221192 -0.34838900741980 0.08331520196694 1.61521325639978 0.06358015838896 -0.39818374763656 0.09235164808582 1.69360696748984 0.00603210593626 -0.37424170232631 0.08804057587879 1.69360696192797 0.00603211018556 -0.37424170413275 0.08804057620718 1.69360697513982 0.00603210078168 -0.37424170032592 0.08804057553828 1.69360696194714 0.00603211023566 -0.37424170417595 0.08804057621838 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document