First-order Euler method for the SHO

stepsize

h=.01;

number of steps

n= 1000;

Guess for energy

stepsize h = 0.01, ground state

ϵ= 1.00568791374638089042292;

u=0;

Look for odd solutions

y1=0;
y2=1;

Look for even solutions (this overrides the previous commands)

y1=1;
y2=0;

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

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

These are my first “shots” at ϵ

ϵ= 1.000000000; (*Too small *)

ϵ= 1.100000000;   (*Too big *)

ϵ= 1.0050000000;   (*Too small *)

ϵ= 1.0057000000;  (*Too big *)

ϵ= 1.005687900000;   (*Too small *)