%% Numerical methods for x'=2x, x(0)=1

N = 10; h = 1/N;
tt = 0:h:1;
xe = zeros(N+1,1); xme = zeros(N+1,1); xrk = zeros(N+1,1);
xe(1) = 1.0; xme(1) = 1.0; xrk(1) = 1.0;

% Euler's method

for j=1:N
    xe(j+1) = xe(j) + h*(2*xe(j));
end

% modified Euler's method

for j=1:N
    aved = 0.5*(2*xe(j)+2*xe(j+1));
    xme(j+1) = xme(j) + h*aved;
end

% Runge-Kutta method

for j=1:N
    k1 = 2*xrk(j);
    k2 = 2*(xrk(j)+h*k1/2);
    k3 = 2*(xrk(j)+h*k2/2);
    k4 = 2*(xrk(j)+h*k3);
    xrk(j+1) = xrk(j) + h*(k1+2*k2+2*k3+k4)/6;
end

% plot the solutions

plot(tt,exp(2*tt),'b-')
hold on
plot(tt,xe,'ro')
hold on
plot(tt,xme,'kd')
hold on
plot(tt,xrk,'b*')
hold off
xlim([0 1])

%%