We want to fit a line y=mx+b with the data below. Clearly it does not all fit on a single line, so we look for the line of “best fit”.

S = {{-1.1,3.2},{-0.9,3.0},{-0.6,2.1},{-0.2,0.9},{0.4,-0.5},{0.6,-2.3}}

LPlot1 = ListPlot[S]

We need to create a matrix A corresponding to the matrix equation AX=B, where X = < m, b> is the column vector of unknowns m and b in the equation y=mx+b. (See page 457 on how to create the matrix for this example).

(A = Join[Drop[S,{},{2}], Table[{1},{i,1,6}],2])//MatrixForm

B = Drop[S,{},{1}]

See page 444 for the explanation of this expression

Inverse[Transpose[A].A].Transpose[A].B//MatrixForm

The first number is m, the second is b. So y = -3.05x+0.151667.

LinePlot1 =Plot[-3.05*x+0.151667,{x,-1.5,1.5}, PlotStyle->Red]

Show[LPlot1,LinePlot1, PlotRange->{{-1.5,1.5},{-3,3.2}}]

So was a line.  Can we do something similar with y=ax^2+bx+c?

S

(Ap = Table[{S[[i,1]]^2,S[[i,1]],1},{i,1,6}])//MatrixForm

Note that B is still the same!

Inverse[Transpose[Ap].Ap].Transpose[Ap].B//MatrixForm

Here, the first value is a, the second is b, the third is c.  So y = -0.84x^2-3.44x+0.4462.  Lets plot!

ParabolaPlot1 =Plot[-0.8428754261379737*x^2-3.444044261719502*x+0.44646234695842296,{x,-1.5,1.5}, PlotStyle->Blue]

Show[ParabolaPlot1,LPlot1,LinePlot1, PlotRange->{{-1.5,1.5},{-3,3.2}}]

B//MatrixForm

(Y=Inverse[Transpose[A].A].Transpose[A].B)//MatrixForm

A.Y//MatrixForm

(Yp=Inverse[Transpose[Ap].Ap].Transpose[Ap].B)//MatrixForm

Ap.Yp//MatrixForm

So which one is a better fit? The line or the parabola?  Simply compute the difference in the best fit outputs versus the real data output (y variable).

Norm[A.Y-B]

Norm[Ap.Yp-B]

Let’s try a cubic!!!  Just one more (first) column in the matrix: y= ax^3+bx^2+cx+d

(Ac = Table[{S[[i,1]]^3,S[[i,1]]^2,S[[i,1]],1},{i,1,6}])//MatrixForm

(Yc=Inverse[Transpose[Ac].Ac].Transpose[Ac].B)//MatrixForm

CubicPlot1 =Plot[-1.3991*x^3-1.99994*x^2-2.932500916435742*x+0.6995893220698359,{x,-1.5,1.5}, PlotStyle->Black]

Show[CubicPlot1,ParabolaPlot1,LPlot1,LinePlot1, PlotRange->{{-1.5,1.5},{-3,3.2}}]

Norm[A.Y-B]

Norm[Ap.Yp-B]

Norm[Ac.Yc-B]