Least-Squares / FindFit

Below, data is generated for (((x-2))^2)-1=(x^2)-4x+3, but fitted to (ax^2)+bx, then plotted.

t=Table[{x,(x-2)^2-1},{x,-10,10}];
fn[x_]:=a x^2+b x
fit=FindFit[t,fn[x],{a,b},{x}];
fn[x]/.fit
Show[ListPlot[t],Plot[fn[x]/.fit,{x,-10,10}],PlotRange->All]

Here, we plot difference between model and observed value as a function of independent variable.

rx=Table[{t[[i,1]],t[[i,2]]-fn[t[[i,1]]]/.fit},{i,1,Length[t]}];
ListPlot[rx]

Here, we plot difference between model and observed value as a function of observed value.

ry=Table[{t[[i,2]],t[[i,2]]-fn[t[[i,1]]]/.fit},{i,1,Length[t]}];
ListPlot[ry]

We want ∑((((Subscript[f, i]) - (OverHat[f])((Subscript[x, i]))))^2) to be minimized for the fit.

For multivariate, we can have:

data=Import["http://www.cs.utexas.edu/~evanott/PHY110C_Textbook/static/data_analysis/_downloads/prac_multi.csv"];
ListPointPlot3D[data,AxesLabel->{"t","x","f(t,x)"}]

ClearAll[a,x,k,t];
FindFit[data,Cos[a x]Exp[k t],{a,k},{x,t}]

But real data is different. This is because doing this is actually a very hard problem. Just because Mathematica converges doesn’t mean it’s doing something meaningful. Be skeptical!

Show[ListPointPlot3D[data],
Plot3D[Cos[0.2 x] Exp[-.05t],{t,0,40},{x,-20,20}],
AxesLabel->{"t","x","f(t,x)"}]

Interpolation/Extrapolation

Clear[t];
t=Table[{x,Sin[2x]},{x,0,2 Pi,.5}];

(*Creates single-variable function,given order i*)

Manipulate[Show[
ListPlot[t,PlotStyle->PointSize[Medium]],Plot[{Interpolation[t,InterpolationOrder->i][x],Sin[2x]},{x,0,2 Pi},PlotLegends->{"Fitted Curve","Sin[2x]"}],PlotRange->{{0,2 Pi},{-1,1}},PlotLabel->"InterpolationOrder->"<>ToString[i]],{i,0,8,1}]

Interpolation[t,InterpolationOrder->i][2Pi]

Series

Creates a second-degree approximation around x=0.

Series[(1+x)^n,{x,0,2}]

Normal[%]