Afsnit 19.5 opgave 12

Mindste kvadraters metode. Parabolsk variation.

x={0,1,2,4,6}; y={3,1,0,1,4}; n=5;

m=Table[{x[[i]],y[[i]]},{i,5}]

g1=ListPlot[m, 
PlotStyle->{PointSize[0.02],RGBColor[1,0,0]}]

g2=ListPlot[m, PlotJoined->True,
PlotStyle->{RGBColor[0,1,0]}]

Show[g1,g2]

sm = (({{n,  (∑_(i = 1)^n) x[[i]],  
                (∑_(i = 1)^n) x[[i]]^2}, { (∑_(i = 1)^n) x[[i]],  
                (∑_(i = 1)^n) x[[i]]^2, (∑_(i = 1)^n) x[[i]]^3}, { (∑_(i = 1)^n) x[[i]]^2, (∑_(i = 1)^n) x[[i]]^3,  
                (∑_(i = 1)^n) x[[i]]^4}}));

MatrixForm[sm]

r  = 
       { (∑_(i = 1)^n) y[[i]] , 
        (∑_(i = 1)^n) x[[i]] y[[i]] ,   
        (∑_(i = 1)^n) x[[i]]^2 y[[i]]}

v={b0,b1,b2}; Solve[sm .  v==r,v]

v /. %;

v=N[%]; b0=v[[1,1]];b1=v[[1,2]];b2=v[[1,3]];

F[s_] = b0 + b1 s + b2 s^2

g3=Plot[F[x],{x,-1,7}]

Show[g1,g3]