euler[df_,xinit_,yinit_,xfin_,n_]:=(y[0]=N[yinit];
stepsize=(xfin - xinit)/n;Do[y[k+1]=y[k]+(df/.x->(xinit+k*stepsize)/.y->y[k])*stepsize,{k,0,n-1}];
N[y[n]])
eulerplot[df_,xinit_,yinit_,xfin_,n_]:=(y[0]=N[yinit];stepsize=(xfin - xinit)/n;Do[y[k+1]=y[k]+(df/.x->(xinit+k*stepsize)/.y->y[k])*stepsize,{k,0,n-1}];
points=Table[{xinit+k*stepsize,y[k]},{k,0,n}];
ListPlot[points,PlotMarkers->{Automatic,Small},PlotStyle->{Black}])
compareplot[f_,xinit_,yinit_,xfin_,n_]:=(y[0]=N[yinit];stepsize=(xfin - xinit)/n;Do[y[k+1]=y[k]+(D[f,x]/.x->(xinit+k*stepsize))*stepsize,{k,0,n-1}];
points=Table[{xinit+k*stepsize,y[k]},{k,0,n}];
Show[{ListPlot[points,PlotMarkers->{Automatic,Tiny},PlotStyle->{Black}],Plot[f,{x,xinit,xfin}]}])
comparewitherrors[f_,df_,xinit_,yinit_,xfin_,n_]:=(y[0]=N[yinit];stepsize=(xfin - xinit)/n;Do[y[k+1]=y[k]+(df/.x->(xinit+k*stepsize)/.y->y[k])*stepsize,{k,0,n-1}];
points=Table[{xinit+k*stepsize,y[k]},{k,0,n}];
Show[{ListPlot[points,PlotMarkers->{Automatic,Tiny},PlotStyle->{Black}],Plot[f,{x,xinit,xfin}]}])

Given  y’=x-y and y(1) =3, find y(2) with 4 steps
euler[ y’, initial x value, initial y value, target x value, number of steps]
eulerplot[ y’, initial x value, initial y value, target x value, number of steps]

euler[x-y,1,3,2,4]

eulerplot[x-y,1,3,2,4]

Use Euler' s method to approximate e^x with the differential equation y’=y and the initial condition e^0 = 1 at x = 5 with 5 steps
comparewitherrors[ y, y’, initial x value, initial y value, target x value, number of steps]

comparewitherrors[E^x,y,0,1,5,5]