% solution of x'=x using % Euler's algorithm (x) % 2nd order Taylor (y) % 2nd order RK (z) function ode init = .5; % initial condition h = .5; % step size tm = 10; % time value x(1) = init; y(1) = init; z(1) = init; t(1) = 0; for i=1:(tm/h) % Euler x(i+1) = x(i) + h * x(i); % 2nd order Taylor y(i+1) = y(i) + h*y(i) + (h^2/2)*y(i); % 2nd order RK K1 = h * z(i); K2 = h * ( z(i) + K1 ); z(i+1) = z(i) + ( K1 + K2 )/2; t(i+1) = t(i) + h; end plot(t,x,t,y,'-.',t,z,'--') hold on; fplot('.5*exp(x)',[0,tm],'k'); legend('Euler','2nd order Taylor','2nd order RK','Exact solution')