Course 6 : Data Lists

x=Range[-2,2,0.1]

y=Map[(#1+1)^2-#1&,x]

ListPlot[y]

mydata=Table[{x[[i]],y[[i]]},{i,Length[x]}]

ListPlot[mydata]

(* an alternative way *)
ListPlot[Transpose[{x,y}]]

Sampling

Clear[x,y]
y[t_]=Exp[-t/4] Sin[t]
Plot[y[t],{t,0,20},PlotRange->All]

mydata=Table[{t,y[t]},{t,0,10,0.1}]

Ploting

ListPlot[mydata,Joined->False]

Parametric Sampling

x[t_]=Exp[-t/4] Cos[t]
y[t_]=Exp[-t/4] Sin[t]
ParametricPlot[{x[t],y[t]},{t,0,10}]

mydata2=Table[{x[t],y[t]},{t,0,10,0.1}]
gr1=ListPlot[mydata2]

DataFiles I/O

Export["c:\\tmp\\test1.dat", mydata2, "Table"]

Directory[]
SetDirectory["c:\\tmp\\"]
Export["test2.txt", mydata2,"Text"]

Export["plot1.jpg",gr1]

newlist={}
newlist=Import["test1.dat"]

Grids

xmin=-3; xmax=3.0; dx=1.0;
ymin=-3; ymax=3.0; dy=1.0;
mygridmatrix=Table[{x,y},{x,xmin,xmax,dx},{y,ymin,ymax,dy}]

mygriddata=Flatten[mygridmatrix,1]

ListPlot[mygriddata, PlotStyle->PointSize[0.02]]

Interpolating function

example function

f=2 x +x Cos[2π x^2];
Print["y=",f];
xmax=3;  (*repeat with xmax=3*)
gr1=Plot[f,{x,0,xmax},PlotStyle->{RGBColor[0,0,1]}]

Sampling

dx=0.1;
data=Table[{x,f},{x,0,xmax,dx}]
Print["Number of data  ",Length[data]];
gr2=ListPlot[data,PlotStyle->PointSize[0.02]]

Interpolation

fi=Interpolation[data]
(* fi=Interpolation[data, InterpolationOrder->3] *)
(* fi=Interpolation[data, Method->"Spline"] *)

fi[0.25]

gr3=Plot[fi[x],{x,0,xmax},PlotStyle->{Red}]

Show[gr1,gr2,gr3]

Processing of Interpolating function

diff=D[fi[x],x]
Plot[diff,{x,0,xmax}]

integ=Integrate[fi[x],x]
Plot[integ,{x,0,xmax}]

i=Integrate[fi[x],{x,0,0.8}]

Fitting

sample data

dat1=Table[{x,2 x+0.5+0.25 Sin[x]+RandomReal[{0,1}]},{x,0,4,0.1`}]
gr1=ListPlot[dat1,PlotStyle->Red]
dat2=Table[{x,Sin[x]+RandomReal[{0,1}]},{x,0,4,0.1}]
gr2=ListPlot[dat2,PlotStyle->Red]

Linear fitting

y=Fit[dat1,x+1,x]
gr11=Plot[y,{x,0,4}];
Show[gr1,gr11]

myfit=FindFit[dat1,a x +b, {a,b},x]

y=a x +b /.myfit
gr11=Plot[y,{x,0,4}];
Show[gr1,gr11]

quadratic fitting

myfit=FindFit[dat2, a x^2 + b x + c, {a,b,c},x]

y=a x^2 + b x + c /.myfit
gr21=Plot[y,{x,0,4}];
Show[gr2,gr21]