% Demonstration of Euler's method for IVP x'=x, x(0)=1. % Cut and paste this whole file into the Matlab command window % or else download it and invoke it as "eulerdemo" % MVW, 2007-11-16 % n=10; % Number of steps x(1)=1; % Initial value h=1/n; % Compute step size t=h*(0:n); % Compute sample times % Generate vector of approximate samples by Euler's method: for k=2:n+1, x(k)=x(k-1)+h*x(k-1); end % Generate exact solution: for k=0:n, y(k+1)=exp(k/n); end % Show differences: figure; plot(t,x,'red',t,y,'blue'); % Euler and exact on the same axes figure; plot(t,y-x); % Difference exact-Euler