Pensieve header: A stability comparison of Euler, Improved Euler, and Runge-Kutta.

Euler[f_,x0_,y0_, x_, n_] := Module[
{h=(x-x0)/n, xj=x0, yj=y0, k1},
Do[
k1=f[xj,yj];
xj=xj+h;
yj=yj+h k1,
{n}
];
yj
];
ImprovedEuler[f_,x0_,y0_, x_, n_] := Module[
{h=(x-x0)/n, xj=x0, yj=y0, k1, k2},
Do[
k1=f[xj,yj];
k2=f[xj+h, yj+h k1];
xj=xj+h;
yj=yj+h (k1+k2)/2,
{n}
];
yj
];
RungeKutta[f_,x0_,y0_, x_, n_] := Module[
{h=(x-x0)/n, xj=x0, yj=y0, k1, k2, k3, k4},
Do[
k1=f[xj,yj];
k2=f[xj+h/2, yj+h k1/2];
k3=f[xj+h/2, yj+h k2/2];
k4=f[xj+h, yj+h k3];
xj=xj+h;
yj=yj+h (k1+2k2+2k3+k4)/6,
{n}
];
yj
]

f[x_,y_] := -λ y;
Euler[f, 0, 1, h, 1]

Plot[Euler[f, 0, 1, 1, 1], {λ,0,2}]

ImprovedEuler[f, 0, 1, h, 1] // Simplify

Plot[ImprovedEuler[f, 0, 1, 1, 1], {λ,0,2}]

RungeKutta[f, 0, 1, h, 1] // Simplify

Plot[RungeKutta[f, 0, 1, 1, 1], {λ,0,2}]