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

We focus on the example subject to the initial condition . We know that the exact solution is .

Let’s compare Euler’s method, the trapezoid method, and the Runge-Kutta algorithm with a step size of , evolving up to time .

The result is the following picture:

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]