Using the Euler method in Matlab

You first need to download the m-file Euler1.m. Put this file in the same directory you use for your other m-files (additional help).

Example

y' = -y + t, y(0)=1, find y(t) for t between 0 and 2 using 20 steps of Euler method:
f = @(t,y) -y + t
[ts,ys] = Euler1(f,[0,2],1,20);     
[ts,ys]                             %  table of t and y-values
plot(ts,ys,'o-')
Show direction field and approximations with N=5,10,20 together:
dirfield(f,[0:.1:2.5],[.4:.1:1.5]); hold on
for N = [5,10,20]
  [ts,ys] = Euler1(f,[0,2],1,N);
  plot(ts,ys,'o-')
end
hold off