%% Improved Euler method %% %% Written for x'=f(x) with f(x)=-x and x(0)=x0 clear all dt=.1; x0=0; t0=0; endtime=1; t=0:dt:endtime; N=(endtime-t0)/dt; x(1)=x0; for n=1:N x1=x(n)+dt*(x(n)+exp(-x(n))); x(n+1)=x(n) + 0.5*dt*( (x(n)+exp(-x(n))) + (x1+exp(-x1)) ) end plot(t,x,'k') hold on %% END IMPROVED EULER METHOD %%