Afsnit 19.5 opgave 12

Mindste kvadraters metode. Parabolsk variation.

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

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]

sx=0; Do[sx=sx+x[[i]],{i,5}]

sx2=0; Do[sx2=sx2+x[[i]]^2,{i,5}]

sx3=0; Do[sx3=sx3+x[[i]]^3,{i,5}]

sx4=0; Do[sx4=sx4+x[[i]]^4,{i,5}]

sy=0; Do[sy=sy+y[[i]],{i,5}]

sxy=0; Do[sxy=sxy+x[[i]] y[[i]],{i,5}]

sxxy=0; Do[sxxy=sxxy+x[[i]]^2 y[[i]],{i,5}]

sm={{5,sx,sx2},{sx,sx2,sx3},{sx2,sx3,sx4}};

MatrixForm[sm]

r={sy,sxy,sxxy}

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]