function [t,y] = euler( f, tspan, y0, n) %EULER Apply Euler's method to solve y' = f(y,t), y(a) = y0, % on the interval a <= t <= b with n steps. a = tspan(1); b = tspan(2); h = (b-a)/n; t = a:h:b; y(1) = y0; for j = 1:n y(j+1) = y(j) + h.*f(t(j),y(j)); end end