% This code solves x'=f(x)=x using Euler, % second order RK and plots them along % with the exact solution for comparison. format compact; clear all; % INITIAL CONDITIONS h = 1; time = 5; iter = time / h; y(1)=1; z(1)=1; t(1)=0; % EULER'S METHOD for i=1:iter y(i+1)=y(i)+h*y(i); t(i+1)=i*h; end % SECOND ORDER RUNGE_KUTTA METHOD for i=1:iter k1 = h * z(i); k2 = h * ( z(i) + k1 ); z(i+1) = z(i) + h*(k1+k2)/2; end % Plot solutions hold on; plot(t,z,'b'); plot(t,y,'r'); % Plot exact solution x = 0:(time/100):time; plot( x, exp(x),'k'); legend('2-nd order RK','Euler (1-st order)','Exact Solution',3); hold off;