%% Runge Kutta method %% %% Written for x'=f(x) with f(x)=-x and x(0)=x0 dt=.1; x0=1; t0=0; endtime=1; t=0:dt:endtime; N=(endtime-t0)/dt; x(1)=x0; for n=1:N k1=(x(n)+exp(-x(n)))*dt; x2=x(n)+.5*k1 k2=(x2+exp(-x2))*dt; x3=x(n)+.5*k2 k3=(x3+exp(-x3))*dt; x4=x(n)+k3 k4=(x4+exp(-x4))*dt; x(n+1)=x(n)+1/6*(k1+2*k2+2*k3+k4); end plot(t,x,'k') hold on %% END RUNGE KUTTA METHOD %%