How to make a data set for a polynomial + noise

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

Ndata is the number of data points. A is the ampitude of the noise. xleft & xright determine the

domain of the polynomial.

> Ndata:=100: A:=0.001: xleft:=-3: xright:=3:

Next we creat a function. Notice it does not have to be a polynomial.

> p:=x -> 0.03*x^3-0.2*x^2+2:

> plot(p(x),x=xleft..xright);

We generate some noise using the rand function.

> noise:=A*rand(-100..100):

Set up two data arrays.

> xdata:=array(1..Ndata):
ydata:=array(1..Ndata):

Generate the data.

> for i from 1 to Ndata do xdata[i]:=(i-1)*(xright-xleft)/Ndata +xleft: ydata[i]:=p(xdata[i])+noise(): od:

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

Save Ndata, xdata and, ydata to the disk.

> save Ndata, xdata, ydata, xleft, xright, `dataset.m`;