Ordinary differential equations: 

numerical solution

Initial value problem

Comparison of Euler, Heun und Runge−Kutta algorithms

In this simulation the code for the three above mentioned algorithms is demonstrated, as applied to the numerical solution of an explicit, linear ordinary differential equation of first order. Starting from an initial value, a series of consecutive points is calculated and shown in a graphic.

As an example the exponential function is chosen with

differential equation dy/dt  = y ´ = y

analytic solution y (t) = (initial value) * et

For this special function the differential equation does not explicitly depend on the variable t, which allows a simple graphical interpretation of the approach.

When opening the file the abscissa range is divided into 5 intervals. The number of intervals can be chosen between 1 and 24 by means of a slider; the interval widths change correspondingly.

The default initial value for x = 0 is y = 1 . It can be changed with a second slider. The analytic solution curve (blue) changes with y0 . Each change starts a new calculation of the numerical approximations for the three methods.

The discrete points calculated with Euler are drawn blue, those with Heun in green, those with Runge_Kutta (4 step) in red.

There are four lines in cyan in the upper part of the coordinate system. They can be offset in parallel by drawing at their left end, and can be elongated and rotated by drawing at their right end. This way the algorithms can be reconstructed graphically, most easily with a very broad interval.

Back resets the number of intervals and initial value.

A second window shows the relative residual difference between the numerical approximations and the analytic solution. Coordinate scales adjust themselves to the mistake of the Euler approach, for which it is largest.

When you open this file from the EJS console you can easily insert other differential equations instead of the very simple one of the exponential. The corresponding places are highlighted in the code pages.