Curve Fitting Basics
    I. Linear Fits (nonweighted and weighted)
   II. Nonlinear Fits (nonweighted and weighted)

<<"ErrorBarPlots`" (* Necessary for plotting error bars *)

Linear Fitting

Note : The "Linear" in LinearModelFit refers to linearity in the fit parameters; LinearModelFit is not just for fitting straight lines

?LinearModelFit

Fitting data to a line

data = {{2,2},{3,3.1},{5,4.9},{6,6.2}};

lm =LinearModelFit[data,{1,x},x]

Plot[lm[x],{x,0,5}]

Combining data and function plots :

ListPlot[data,PlotStyle->PointSize[0.015]];
Plot[lm[x],{x,0,7}];
Show[%,%%]

lm["BestFitParameters"]

lm["ParameterErrors"]

lm["ParameterTable"]

lm["FitResiduals"]

(* Plot residuals vs. x-values of data *)
ListPlot[Transpose[{Table[data[[i,1]],{i,1,Length[data]}],lm["FitResiduals"]}],PlotStyle->PointSize[0.015]]

LinearModelFit used for something other than a line

Generate some noisy data:

data = Table[{x=i/10., 0.8*x + 1.3*Sin[2. π x]+Random[NormalDistribution[0,0.2]]} ,{i,1,40}];
dataPlot =ListPlot[data]

Fit to function of the form :  a1 + a2*x + a3*Sin[2 π x]

Clear[x]
lm2 =LinearModelFit[data,{x,Sin[2 π x]},x]

fitPlot = Plot[lm2[x],{x,0,4}];
Show[dataPlot,fitPlot]

lm2["ParameterTable"]

NOTE : If you want to fit without including the constant, i.e., if you know that a1 = 0 in the functional form above, use the option “IncludeConstantBasis -> False”

Clear[x]
lm2 =LinearModelFit[data,{x,Sin[2 π x]},x,IncludeConstantBasis->False]

Weighted fitting using LinearModelFit

Use previous example, but also assign error bars (in third column of data) and weights.

data = Table[{x=i/10., 0.8*x + 1.3*Sin[2. π x]+Random[NormalDistribution[0,0.2]],0.2} ,{i,1,40}];
dataPlot =ErrorListPlot[data]

Separate  3-column data into 2-column list with  x-y pairs, and list with weights:

xydata = Table[{data[[i,1]],data[[i,2]]},{i,1,Length[data]}];w=Table[1/data[[i,3]]^2,{i,1,Length[data]}]; (* Weights *)

Fit to function of the form :  a1 + a2*x + a3*Sin[2 π x]

Clear[x]
lm3 =LinearModelFit[xydata,{x,Sin[2 π x]},x,Weights-> w]

fitPlot = Plot[lm3[x],{x,0,4}];
Show[dataPlot,fitPlot]

lm3["ParameterTable"]

Plot normalized residuals

ListPlot[Table[{data[[i,1]],(lm3[data[[i,1]]]-data[[i,2]])/data[[i,3]]},{i,1,Length[data]}]]

Nonlinear Fitting

?NonlinearModelFit

Fitting to a Gaussian

Generate noisy Gaussian "data" :

g[x_]:= 3 Exp[-(x-100)^2/10^2] + 0.5
data = Table[{x=75+i/2.,g[x]+.5 Random[NormalDistribution[0,0.5]]},{i,0,100}];

dataPlot =ListPlot[data]

Clear[a,b,c,d,x]
nlm=NonlinearModelFit[data,a Exp[-(x-b)^2/c^2] + d,{{a,2},{b,98},{c,4},{d,1}},x]

fitPlot = Plot[nlm[x],{x,75,125},PlotRange->{0,4}];
Show[fitPlot,dataPlot]

nlm["BestFitParameters"]

nlm["ParameterErrors"]

nlm["ParameterTable"]

FindFit can also be used, but I don' t think it gives you access to as much information;  I think it' s equivalent to nlm["BestFitParameters"].

FindFit[data,a Exp[-(x-b)^2/c^2] + d,{{a,2},{b,98},{c,4},{d,1}},x]

NOTE : The fit parameters determined by NonlinearModelFit are returned as Mathematica "rules", e.g, a->2.96224.

Weighted fitting using NonlinearModelFit

Use previous example, but also assign error bars (in third column of data) and weights.

g[x_]:= 3 Exp[-(x-100)^2/10^2] + 0.5
data = Table[{x=75+i/2.,g[x]+ Random[NormalDistribution[0,0.5]],0.5},{i,0,100}];

dataPlot =ErrorListPlot[data]

Separate 3-column data into 2-column list with  x-y pairs, and list with weights:

xydata = Table[{data[[i,1]],data[[i,2]]},{i,1,Length[data]}];w=Table[1/data[[i,3]]^2,{i,1,Length[data]}]; (* Weights *)

Clear[a,b,c,d,x]
nlm2=NonlinearModelFit[xydata,a Exp[-(x-b)^2/c^2] + d,{{a,2},{b,98},{c,4},{d,1}},x,Weights->w]

fitPlot = Plot[nlm2[x],{x,70,130}];
Show[dataPlot,fitPlot]

nlm2["ParameterTable"]

Plot normalized residuals

ListPlot[Table[{data[[i,1]],(nlm2[data[[i,1]]]-data[[i,2]])/data[[i,3]]},{i,1,Length[data]}]]