function polycos(m,n); % Polynomial least squares fit of cos(4t). See Tref & Bau exercise 11.3. % Calling: polycos(m,n) for m points and degree n-1 polynomial fit. % Depends on: mgs2 and house.m t=linspace(0,1,m); A=fliplr(vander(t)); A=A(:,1:n); b=cos(4*t'); xa=(A'*A)\(A'*b); [Q R]=mgs2(A); xb=R\(Q'*b); [Q R]=house(A); xc=R\(Q'*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, disp([xa xb xc xd xe xf]')