% demonstration showing global error for 1-step integrators f = inline('x','t','x'); t0 = 0; x0 = 1; tfinal = 5; e_err = []; h_err = []; rk_err = []; h = []; for j=1:10 h(j) = 2^(-j+1); [t,x] = euler(f,[t0 tfinal],x0,h(j)); e_err(j) = abs(exp(t(end))*x0-x(end)); [t,x] = heun(f,[t0 tfinal],x0,h(j)); h_err(j) = abs(exp(t(end))*x0-x(end)); [t,x] = rk4(f,[t0 tfinal],x0,h(j)); rk_err(j) = abs(exp(t(end))*x0-x(end)); end figure(1), clf loglog(h,e_err, 'r.','markersize',15); hold on loglog(h,e_err,'r-', 'linewidth',1.5); loglog(h,h_err, 'b.','markersize',15); hold on loglog(h,h_err,'b-', 'linewidth',1.5); loglog(h,rk_err, 'k.','markersize',15); hold on loglog(h,rk_err,'k-', 'linewidth',1.5); xlabel('step size, h','fontsize',16) ylabel('error at t=5 ','fontsize',16) title('error in one-step methods for various step-sizes, h','fontsize',16) set(gca,'fontsize',16) set(gca,'Xdir','reverse')