First-order Euler method for the SHO

stepsize

h=.01;

number of steps

n= 1000;

Guess for energy

ϵ= 1.0056564;

ϵ= 5.014043;

u=0;

Look for odd solutions

y1=0;
y2=1;

Look for even solutions

y1=1;
y2=0;

For[i=1,i<=n,i++,
u=u+h;
y1=y1 +h*y2;
y2=y2 + h*(u^2-ϵ)*y1;
a[i]=u; b[i]=y1;
]

ListPlot[Table[{a[i],b[i]},{i,1,n}],PlotRange->{-100,100}]

ListPlot[Table[{a[i],b[i]},{i,1,n}],PlotRange->{-5,5}]