% Explicit RK4 Convergence
%
tmax = 1;
timesteps = 1:10;
for k = timesteps
    dt = 2^(-k);
    n=tmax/dt;
    t=0; u=1;
    for j=1:n
        k1 = dt*f(t,u);
        k2 = dt*f(t+0.5*dt,u+0.5*k1);
        k3 = dt*f(t+0.5*dt,u+0.5*k2);
        k4 = dt*f(t+dt,u+k3);
        u = u + (k1+2*k2+2*k3+k4)/6;
        t = t + dt;
    end
    uf(k) = u;
end
loglog(2.^(-timesteps),abs(uf-1/2),'o-'); xlabel('dt'); ylabel('error');