## Comparing numerical methods

Here is a simple example of comparing different numerical methods for ODEs.

We focus on the example $\frac{dx}{dt} = x^2$ subject to the initial condition $x(0)=1$. We know that the exact solution is $x(t) = \frac{1}{1-t}$.

Let’s compare Euler’s method, the trapezoid method, and the Runge-Kutta algorithm with a step size of $\Delta t = 0.3$, evolving up to time $t=0.9$.

The result is the following picture:

Comparing numerical methods. Dashed line is exact solution; pink is Runge-Kutta algorithm, purple is trapezoid method, red is Euler’s method.

Here is the code which I used to obtain the picture:

f[x_] := x^2;
dt = .3;
n = 3;

solutioncurve =
Plot[(1 - t)^(-1), {t, 0, dt*n}, PlotStyle -> {Thick, Dashed},
PlotRange -> All];

evalues = RecurrenceTable[{
t[0] == 0,
x[0] == 1,
t[k + 1] == t[k] + dt,
x[k + 1] == x[k] + f[x[k]]*dt},
{t, x},
{k, 0, n}];
eplot = ListLinePlot[evalues, PlotStyle -> Red,
PlotMarkers -> Automatic];

trapvalues = RecurrenceTable[
{t[0] == 0,
x[0] == 1,
t[k + 1] == t[k] + dt,
x[k + 1] == x[k] + .5*dt*(f[x[k]] + f[x[k] + f[x[k]]*dt])},
{t, x},
{k, 0, n}];
trapplot =
ListLinePlot[trapvalues, PlotStyle -> Purple,
PlotMarkers -> Automatic];

rkvalues = RecurrenceTable[
{t[0] == 0,
x[0] == 1,
t[k + 1] == t[k] + dt,
x[k + 1] == x[k] + dt*(
(1/6)*(f[x[k]])
+ (1/3)*(f[x[k] + .5*dt*f[x[k]]])
+ (1/3)*f[x[k] + .5*dt*(f[x[k] + .5*dt*f[x[k]]])]
+ (1/6)*
f[x[k] + dt*(f[x[k] + .5*dt*(f[x[k] + .5*dt*f[x[k]]])])]
)
},
{t, x},
{k, 0, n}];
rkplot = ListLinePlot[rkvalues, PlotStyle -> Pink,
PlotMarkers -> Automatic];

Show[solutioncurve, eplot, trapplot, rkplot]

This entry was posted in Differential equations, Mathematica. Bookmark the permalink.