Skip to main content

CLP-2 Integral Calculus

Section D.1 Simple ODE Solvers — Derivation

The first order of business is to derive three simple algorithms for generating approximate numerical solutions to the initial value problem
y(t)=f(t,y(t))y(t0)=y0
The first is called Euler’s method because it was developed by (surprise!) Euler
 1 
Leonhard Euler (1707–1783) was a Swiss mathematician and physicist who spent most of his adult life in Saint Petersberg and Berlin. He gave the name π to the ratio of a circle’s circumference to its diameter. He also developed the constant e.
.

Subsection D.1.1 Euler’s Method

Our goal is to approximate (numerically) the unknown function
y(t)=y(t0)+t0ty(τ)dτ=y(t0)+t0tf(τ,y(τ))dτ
for tt0. We are told explicitly the value of y(t0), namely y0. So we know f(τ,y(τ))|τ=t0=f(t0,y0). But we do not know the integrand f(τ,y(τ)) for τ>t0. On the other hand, if τ is close t0, then f(τ,y(τ)) will remain close
 2 
This will be the case as long as f(t,y) is continuous.
to f(t0,y0). So pick a small number h and define
t1=t0+hy1=y(t0)+t0t1f(t0,y0)dτ=y0+f(t0,y0)(t1t0)=y0+f(t0,y0)h
By the above argument
y(t1)y1
Now we start over from the new point (t1,y1). We now know an approximate value for y at time t1. If y(t1) were exactly y1, then the instantaneous rate of change of y at time t1, namely y(t1)=f(t1,y(t1)), would be exactly f(t1,y1) and f(t,y(t)) would remain close to f(t1,y1) for t close to t1. Defining
t2=t1+h=t0+2hy2=y1+t1t2f(t1,y1)dt=y1+f(t1,y1)(t2t1)=y1+f(t1,y1)h
we have
y(t2)y2
We just repeat this argument ad infinitum. Define, for n=0,1,2,3,
tn=t0+nh
Suppose that, for some value of n, we have already computed an approximate value yn for y(tn). Then the rate of change of y(t) for t close to tn is f(t,y(t))f(tn,y(tn))f(tn,yn) and
This algorithm is called Euler’s Method. The parameter h is called the step size.
Here is a table applying a few steps of Euler’s method to the initial value problem
y=2t+yy(0)=3
with step size h=0.1. For this initial value problem
f(t,y)=2t+yt0=0y0=3
Of course this initial value problem has been chosen for illustrative purposes only. The exact solution is
 3 
Even if you haven’t learned how to solve initial value problems like this one, you can check that y(t)=2+2t+et obeys both y(t)=2t+y(t) and y(0)=3.
y(t)=2+2t+et.
n tn yn f(tn,yn)=2tn+yn yn+1=yn+f(tn,yn)h
0 0.0 3.000 20.0+3.000=3.000 3.000+3.0000.1=3.300
1 0.1 3.300 20.1+3.300=3.100 3.300+3.1000.1=3.610
2 0.2 3.610 20.2+3.610=3.210 3.610+3.2100.1=3.931
3 0.3 3.931 20.3+3.931=3.331 3.931+3.3310.1=4.264
4 0.4 4.264 20.4+4.264=3.464 4.264+3.4640.1=4.611
5 0.5 4.611
The exact solution at t=0.5 is 4.6487, to four decimal places. We expect that Euler’s method will become more accurate as the step size becomes smaller. But, of course, the amount of effort goes up as well. If we recompute using h=0.01, we get (after much more work) 4.6446.

Subsection D.1.2 The Improved Euler’s Method

Euler’s method is one algorithm which generates approximate solutions to the initial value problem
y(t)=f(t,y(t))y(t0)=y0
In applications, f(t,y) is a given function and t0 and y0 are given numbers. The function y(t) is unknown. Denote by φ(t) the exact solution
 4 
Under reasonable hypotheses on f, there is exactly one such solution. The interested reader should search engine their way to the Picard-Lindelöf theorem.
for this initial value problem. In other words φ(t) is the function that obeys
φ(t)=f(t,φ(t))φ(t0)=y0
exactly.
Fix a step size h and define tn=t0+nh. By turning the problem into one of approximating integrals, we now derive another algorithm that generates approximate values for φ at the sequence of equally spaced time values t0, t1, t2, . We shall denote the approximate values yn with
ynφ(tn)
By the fundamental theorem of calculus and the differential equation, the exact solution obeys
φ(tn+1)=φ(tn)+tntn+1φ(t) dt=φ(tn)+tntn+1f(t,φ(t)) dt
Fix any n and suppose that we have already found y0, y1, , yn. Our algorithm for computing yn+1 will be of the form
yn+1=yn+ approximate value of tntn+1f(t,φ(t)) dt
In Euler’s method, we approximated f(t,φ(t)) for tnttn+1 by the constant f(tn,yn). Thus
Euler's approximate value for tntn+1f(t,φ(t)) dt is tntn+1f(tn,yn) dt=f(tn,yn)h
So Euler approximates the area of the complicated region  0yf(t,φ(t)), tnttn+1  (represented by the shaded region under the parabola in the left half of the figure below) by the area of the rectangle  0yf(tn,yn), tnttn+1  (the shaded rectangle in the right half of the figure below).
Our second algorithm, the improved Euler’s method, gets a better approximation by using the trapezoidal rule. That is, we approximate the integral by the area of the trapezoid on the right below, rather than the rectangle on the right above.
The exact area of this trapezoid is the length h of the base multiplied by the average of the heights of the two sides, which is 12[f(tn,φ(tn))+f(tn+1,φ(tn+1))]. Of course we do not know φ(tn) or φ(tn+1) exactly.
Recall that we have already found y0,,yn and are in the process of finding yn+1. So we already have an approximation for φ(tn), namely yn. But we still need to approximate φ(tn+1). We can do so by using one step of the original Euler method! That is
φ(tn+1)φ(tn)+φ(tn)hyn+f(tn,yn)h
So our approximation of 12[f(tn,φ(tn))+f(tn+1,φ(tn+1))] is
12[f(tn,yn)+f(tn+1,yn+f(tn,yn)h)]
and
Improved Euler's approximate value for tntn+1f(t,φ(t)) dt is 12[f(tn,yn)+f(tn+1,yn+f(tn,yn)h)]h
Putting everything together
 5 
Notice that we have made a first approximation for φ(tn+1) by using Euler’s method. Then improved Euler uses the first approximation to build a better approximation for φ(tn+1). Building an approximation on top of another approximation does not always work, but it works very well here.
, the improved Euler’s method algorithm is
Here are the first two steps of the improved Euler’s method applied to
y=2t+yy(0)=3
with h=0.1. In each step we compute f(tn,yn), followed by yn+f(tn,yn)h, which we denote y~n+1, followed by f(tn+1,y~n+1), followed by yn+1=yn+12[f(tn,yn)+f(tn+1,y~n+1)]h.
t0=0y0=3f(t0,y0)=20+3=3y~1=3+30.1=3.3f(t1,y~1)=20.1+3.3=3.1y1=3+12[3+3.1]0.1=3.305t1=0.1y1=3.305f(t1,y1)=20.1+3.305=3.105y~2=3.305+3.1050.1=3.6155f(t2,y~2)=20.2+3.6155=3.2155y2=3.305+12[3.105+3.2155]0.1=3.621025
Here is a table which gives the first five steps.
n tn yn f(tn,yn) y~n+1 f(tn+1,y~n+1) yn+1
0 0.0 3.000 3.000 3.300 3.100 3.305
1 0.1 3.305 3.105 3.616 3.216 3.621
2 0.2 3.621 3.221 3.943 3.343 3.949
3 0.3 3.949 3.349 4.284 3.484 4.291
4 0.4 4.291 3.491 4.640 3.640 4.647
5 0.5 4.647
As we saw at the end of Section D.1.1, the exact y(0.5) is 4.6487, to four decimal places, and Euler’s method gave 4.611.

Subsection D.1.3 The Runge-Kutta Method

The Runge-Kutta
 6 
Carl David Tolmé Runge (1856–1927) and Martin Wilhelm Kutta (1867–1944) were German mathematicians.
algorithm is similar to the Euler and improved Euler methods in that it also uses, in the notation of the last subsection,
yn+1=yn+ approximate value for tntn+1f(t,φ(t)) dt
But rather than approximating tntn+1f(t,φ(t)) dt by the area of a rectangle, as does Euler, or by the area of a trapezoid, as does improved Euler, it approximates by the area under a parabola. That is, it uses Simpson’s rule. According to Simpson’s rule (which is derived in Section 1.11.3)
tntn+hf(t,φ(t)) dth6[f(tn,φ(tn))+4f(tn+h2,φ(tn+h2))+f(tn+h,φ(tn+h))]
Analogously to what happened in our development of the improved Euler method, we don’t know φ(tn), φ(tn+h2) or φ(tn+h). So we have to approximate them as well. The Runge-Kutta algorithm, incorporating all these approximations, is
 7 
It is well beyond our scope to derive this algorithm, though the derivation is similar in flavour to that of the improved Euler method. You can find more in, for example, Wikipedia.
That is, Runge-Kutta uses
  • k1,n to approximate f(tn,φ(tn))=φ(tn),
  • both k2,n and k3,n to approximate f(tn+h2,φ(tn+h2))=φ(tn+h2), and
  • k4,n to approximate f(tn+h,φ(tn+h)).
Here are the first two steps of the Runge-Kutta algorithm applied to
y=2t+yy(0)=3
with h=0.1.
t0=0y0=3k1,0=f(0,3)=20+3=3y0+h2k1,0=3+0.053=3.15 k2,0=f(0.05,3.15)=20.05+3.15=3.05y0+h2k2,0=3+0.053.05=3.1525k3,0=f(0.05,3.1525)=20.05+3.1525=3.0525y0+hk3,0=3+0.13.0525=3.30525k4,0=f(0.1,3.30525)=20.1+3.30525=3.10525y1=3+0.16[3+23.05+23.0525+3.10525]=3.3051708t1=0.1y1=3.3051708k1,1=f(0.1,3.3051708)=20.1+3.3051708=3.1051708y1+h2k1,1=3.3051708+0.053.1051708=3.4604293k2,1=f(0.15,3.4604293)=20.15+3.4604293=3.1604293y1+h2k2,1=3.3051708+0.053.1604293=3.4631923k3,1=f(0.15,3.4631923)=20.15+3.4631923=3.1631923y1+hk3,1=3.3051708+0.13.4631923=3.62149k4,1=f(0.2,3.62149)=20.2+3.62149=3.22149y2=3.3051708+0.16[3.1051708+23.1604293++23.1631923+3.22149]=3.6214025t2=0.2y2=3.6214025
Now, while this might look intimidating written out in full like this, one should keep in mind that it is quite easy to write a program to do this. Here is a table giving the first five steps. The data is only given to three decimal places even though the computation has been done to many more.
n tn yn k1,n yn,1 k2,n yn,2 k3,n yn,3 k4,n yn+1
0 0.0 3.000 3.000 3.150 3.050 3.153 3.053 3.305 3.105 3.305
1 0.1 3.305 3.105 3.460 3.160 3.463 3.163 3.621 3.221 3.621
2 0.2 3.621 3.221 3.782 3.282 3.786 3.286 3.950 3.350 3.949
3 0.3 3.950 3.350 4.117 3.417 4.121 3.421 4.292 3.492 4.291
4 0.4 4.292 3.492 4.466 3.566 4.470 3.570 4.649 3.649 4.648
5 0.5 4.6487206
As we saw at the end of Section D.1.2, the exact y(0.5) is 4.6487213, to seven decimal places, Euler’s method gave 4.611, and improved Euler gave 4.647.
So far we have, hopefully, motivated the Euler, improved Euler and Runge-Kutta algorithms. We have not attempted to see how efficient and how accurate the algorithms are. A first look at those questions is provided in the next section.