Curve fitting for stored data sets

> restart:with(linalg):with(plots):

Read in a data set. (You will have too figure out where they are stored on your computer.)

Each data set gives xdata, ydata and Ndata. The first two are arrays of length Ndata.

> read `dataset.m`;

> Ndata;

> plot([xdata[n],ydata[n],n=1..Ndata],style=POINT);

After plotting the data decide how many terms to use for a polynomial least sqaures fit. then create the Vandermonde matrix and apply linsove. We will try a linear fit.

> Nterms:=2:

> V:=submatrix(vandermonde(convert(xdata,list)),1..Ndata,1..Nterms);

> a:=linsolve(transpose(V)&*V,transpose(V)&*ydata);

> CurvePlot:=plot(sum('a[k]*x^(k-1)','k'=1..Nterms),
x=xleft..xright,color=black,thickness=2):
PointPlot:=plot([xdata[n],ydata[n],n=1..Ndata],style=POINT):
display([PointPlot,CurvePlot]);

Lastly, we compute a messure of how good the fit it. It is just the square root of the sum of the squares of the differences between the curve and the ydata points.

> sqrt( sum(
(sum('a[k]*xdata[n]^(k-1)','k'=1..Nterms) -ydata[n])^2
,n=1..Ndata) );

>

Maybe a linear curve is not what is needed. Try some othhers.