(* Exploring linear list square fit *) F[x_] := x; (* define function *) a = .0; b= 20.0; (* define the interpolating interval *) plotfunction = Plot[F[x], {x, a, b}, PlotStyle->{Hue[1], Thickness[0.01]}, PlotLabel->"Original function", DisplayFunction -> Identity ] n=20; h = (b-a)/n; (* define euqally spaced data points *) (* data = Table[N[{x, F[x]}], {x, a, b, h}] *) (* now introduce experimental uncertainity *) (* SeedRandom[] data = Table[N[{x + 0.5*Random[Real, {-1, 1} ], F[x] + 0.5*Random[Real, {-1, 1} ]}], {x, a, b, h}] *) (* Set of first n prime numbers *) data = Table[ {k, Prime[k]}, {k, n}] plotdata = ListPlot[data, PlotLabel -> "Data points", Prolog-> AbsolutePointSize [5] ] fit = Fit[data, {1, x}, x] (* Do linear least square fit *) Print[fit] plotfit = Plot[fit, {x, a, b}, PlotStyle->{Hue[0.5], Thickness[0.005]}, PlotLabel-> "Cubic spline", DisplayFunction -> Identity ] (* Show all graphs at once *) (* Show[plotfunction, plotdata, plotfit, PlotLabel -> FontForm["red - original f(x), blue - Least S. Fit ", {"Courier", 15}], Prolog-> AbsolutePointSize[5], DisplayFunction -> $DisplayFunction ] *) (* Show only data and the fit *) Show[plotdata, plotfit, PlotLabel -> FontForm["circles - data, blue - Least S. Fit ", {"Courier", 15}], Prolog-> AbsolutePointSize[5], DisplayFunction -> $DisplayFunction ] Clear[data] Clear[fit]