%% Strang exercise 2.3/17. Fit a quadratic thru 3 points % % All rights reserved, Matthias Kawski, June 2007. % http://math.asu.edu/~kawski % % Copy and paste the commands into your command window. % % Desired viewpoint (to be much refined over time): % Functions are really long column vectors of their values % % More advanced ways would define sub functions so that there % is no need for retyping the definitions of e.g. f1 and F1. % However, here the emphasis is on functions as column vectors! % x=[1 2 3]' y=[4 8 14]' f1=ones(size(x)) f2=x f3=x.*x A=[f1 f2 f3] c=A\y % % for fun: % plot(x,y,'ro') axis([0,5,0,20]) % xx=[0:0.2:4]' F1=ones(size(xx)) F2=xx F3=xx.*xx AA=[F1 F2 F3] yy=AA*c % hold on plot(xx,yy,'b-') % % Much wilder: There is nothing special about polynomials, % indeed what matters is the linear parameterization only. % Here are a few random points, and some different functions! % x=[1:5]' y=floor(rand(size(x))*10) clf plot(x,y,'ro') axis([0,6,-2,12]) % f1=ones(size(x)) f2=1./(1+4*(x-2).*(x-2)); f3=1./(1+4*(x-4).*(x-4)); f4=sin(x); f5=sin(2*x); A=[f1 f2 f3 f4 f5] c=A\y % xx=[0:0.1:6]'; F1=ones(size(xx)); F2=1./(1+4*(xx-2).*(xx-2)); F3=1./(1+4*(xx-4).*(xx-4)); F4=sin(xx); F5=sin(2*xx); AA=[F1 F2 F3 F4 F5]; size(AA) yy=AA*c; % hold on plot(xx,yy,'b-') plot(xx,F1,'k-',xx,F2,'r-',xx,F3,'m-',xx,F4,'g-.',xx,F5,'c:')