x=[-1 0 2 3] format compact y=[0 -1 2 1] plot(x,y,'x') plot(x,y) plot(x,y,'*') A=[x;ones(1,4)]' c=A'*y' B=A'*A B\c d=B\c x0=linspace(-2,4,100); f1=d(1)*x0+d(2); plot(x,y,'*',x0,f1) polyfit(x,y,1) polyfit(x,y,2) polyfit(x,y,3) p=polyfit(x,y,3) p3=polyval(x0,p) p3=polyval(p,x0); plot(x,y,'*',x0,p3) polyfit(x,y,5) polyfit(x,y,4) a=0.25/35.6875 b=(2-13.5*a)/4 f2=a*2.^x0+b; plot(x,y,'*',x0,f2) A=[2.^x;ones(1,4)]' B=A'*A c=A'*y' B\c f2=ans(1)*2.^x0+ans(2); plot(x,y,'*',x0,f2)