Alfa Heryudono
MTH540/440 - EAS621/622 - DSC550 Discussion Session
September 23, 2020
Topic: Time Series Prediction Numerical Experiment.
See the discussion video in mycourses.

ClearAll["Global`*"]
f[x_]:=Exp[-x^2]*Sin[15*Pi*x]-x*Cos[0.5*Pi*x]
(*f[x_]:=Exp[-x^2]*Sin[15*Pi*x]-x*Cos[0.5*Pi*x]+x*Sin[2*Pi*x]*)
x=Table[x,{x,-1,1,0.005}];
ww =50;
net=NetChain[{10,10,10,10,10,10,1},"Input"->ww,"Output"->"Scalar"]
NetInitialize[net]
net=NetTrain[net,f[Partition[x[[;;-2]],ww,1]]->f[x[[(ww+1);;]]],MaxTrainingRounds->10000]

g1:=Plot[f[x],{x,-1,2},ImageSize->Large,PlotStyle->Red,GridLines->{{1},{}}]
g2:=ListPlot[Thread[{x,f[x]}],ImageSize->Large];
xe=x[[-ww;;]];
fe =f[x[[-ww;;]]];
Do[
xe = AppendTo[xe,Last[xe]+0.005];
fe = AppendTo[fe,net@fe[[-ww;;]]] ;
,{i,1,200}];
xe=xe[[(ww+1);;]];
fe = fe[[(ww+1);;]];
g3:=ListPlot[Thread[{xe,fe}],ImageSize->Large,PlotStyle->Black];
Show[g1,g2,g3]