(* Exploring linear list square fit *) (* To run on command line: math -run "<{Hue[1], Thickness[0.01]}, PlotLabel->"Original function", DisplayFunction -> Identity ] n=100; 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.0*Random[Real, {-1, 1} ], F[x] + 1.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^2, x^3}, x] (* Do linear least square fit *) Print[fit] plotfit = Plot[fit, {x, a, b}, PlotStyle->{Hue[0.5], Thickness[0.005]}, PlotLabel-> "least square fit", 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]