function euler_method2 nn=5; %initial number of subintervals k=5; %number of approximations for m=1:k n=nn*2^(m-1); %number of subintervals e=zeros(n+1); h=5/n; %step size t=0:h:5; for i=1:n %e(i+1)=e(i)+h*subs(f,x,e(i)); e(i+1)=e(i)+h*f(h*i,e(i)); %carrying out Euler method end y=exact(t); %y=subs(exact,x,t); error(m)=abs(y(n+1)-e(n+1));%error at t=5 plot(t,e); hold on; end plot(t,y,'r'); error for m=1:k-1 ratio(m)=error(m)/error(m+1); end ratio function d=f(t,x) d=-1/2*exp(t/2)*sin(5*t)+5*exp(t/2)*cos(5*t)+x; function d=exact(x) d=exp(x/2).*sin(5*x);