Open access peer-reviewed chapter

Numerical Methods: Euler and Runge-Kutta

Written By

Victor Akinsola

Submitted: 09 August 2022 Reviewed: 10 October 2022 Published: 05 January 2023

DOI: 10.5772/intechopen.108533

From the Edited Volume

Qualitative and Computational Aspects of Dynamical Systems

Edited by Kamal Shah, Bruno Carpentieri and Arshad Ali

Chapter metrics overview

219 Chapter Downloads

View Full Metrics

Abstract

Most real life phenomena change with time, hence dynamic. Differential equations are used in mathematical modeling of such scenarios. Linear differential equations can be solved analytically but most real life applications are nonlinear. Numerical solutions of nonlinear differential equations are approximate solutions. Euler and Runge-Kutta method of order four are derived, explained and illustrated as useful numerical methods for solving single and systems of linear and nonlinear differential equations. Accuracy of a numerical method depends on the step size used and degree of nonlinearity of the equations. Stiffness is another challenge with numerical solutions of nonlinear differential equations. Although better accuracy can be obtained with smaller step size, this takes more computational effort and time. Algorithms and codes can be written using available computer programming software to overcome this challenge and to avoid computational error. The Runge-Kutta method is more applicable and accurate for diverse classes of differential equations.

Keywords

  • numerical solution
  • Euler method
  • Runge-Kutta method of order four (RK4)
  • accuracy

1. Introduction

In numerical analysis, mathematical methods are employed to produce numerical answers to mathematical expressions. It entails developing, examining, and putting into practice computer algorithms to solve continuous mathematics problems numerically [1]. These problems may originate from real-life applications in the natural sciences, social sciences, engineering, medical sciences and business where variables which vary continuously are involved.

Differential equations are the foundation of the majority of mathematical models used in the natural sciences and engineering. The numerical technique finds the solution function at a discrete set of points, approximating the derivatives or integrals in the relevant equation.

In an effort to obtain solutions that are more precise, more affordable, or more resilient to instabilities in the issue data, a number of techniques have been developed. Many of the differential equations that arise in practical problems are usually nonlinear. Nonlinear differential equations are nearly impossible to be solved precisely [2]. Most differential equations that arise in practical problems are typically nonlinear. The computational labour of solving a system of first order equations may be formidable even when the exact solution is possible [3]. It is for these reasons that techniques have been developed for computing to any degree of accuracy the numerical solution of almost any of such problems.

These techniques typically fall into “families” of increasing order of accuracy, with Euler’s method (or a close relative) frequently making up the lowest order. Adams-Bashforth techniques are “multistep” methods, whereas Runge-Kutta methods are “single-step” methods. These methods have been very successful and reliable.

In this chapter, the derivation of Euler and Runge Kutta methods are explained and illustrative examples given to explain how they are used. Algorithms/codes of the examples written with Maple mathematical programming software were also included for better comprehension and further practice.

Advertisement

2. The Euler method

The popular Euler method published in 1768 is attributed to Leonhard Euler (1707–1783). The method is one of the most straightforward and fundamental method of solving single and systems of ordinary differential eqs. [1]. The basic idea is as follows;

Consider initial value system:

dydt=ftxyy0=y0E1
dxdt=gtxyx0=x0E2

By the definition of a derivative,

yt=limh0ft+hfthE3
xt=limh0gt+hfthE4

for small h>0, then, Eqs. (3) and (4) imply that a reasonable difference approximation for yt and xt [4] are.

yt=ft+hfthE5
xt=gt+hfthE6

Substituting Eq. (5) and Eq. (6) into Eq. (1) and Eq. (2) respectively yield the difference equations

ft+hfth=ftxyE7
gt+hfth=gtxyE8

which approximates the differential Eqs. (1) and (2).

According to [1], the Euler’s method approximates a solution (x, y) with step size h by:

yk+1=yk+hfxkykE9
xk+1=xk+hgxkykE10

Expressing Eq. (1) and Eq. (2) in vector notation:

dYdt=FY,Y0E11

Where Y=yx, Y0=y0x0, FY=fxyg(xy) and dYdt=dydtdxdt.

The Euler’s method approximates a solution (x, y) by:

xk+1yk+1=xkyk+hFxkykE12

Illustration 1

Given the initial value problem in [5]:

y=yxy+x,y0=1.

Determine.

y for x=0.1, using the Euler method with the step size h=0.02.

Solution

The Euler formula is.

yn+1=yn+hfxnyn where n is the number of iteration.

For the first iteration which is n=0, the Euler formula becomes

y1=y0+hfx0y0

x0=0 and y0=1 as the given initial condition.

x1=x0+h
xn=x0+nh
n=xnx0h=0.100.02=5
y1=1+0.02f01=1+0.02101+0=1+0.02=1.02

When n=1, x1=x0+h=0+0.02=0.02.

y2=y1+0.02fx1y1=1.02+0.02f0.021.02=1.02+ 0.021.020.021.02+0.02=1.039230769 When n=2, x2=x0+2h=0+20.02=0.04

y3=y2+0.02fx2y2=1.039230769+0.021.0392307690.041.039230769+0.04=1.057748232

When n=3, x3=x2+h=0.04+0.02=0.06

y4=y3+0.02fx3y3=1.057748232+0.021.0577482320.061.057748232+0.06=1.075601058

When n=4, x4=x3+h=0.06+0.02=0.08

y5=y4+0.02fx4y4=1.075601058+0.021.0756010580.081.075601058+0.08=1.092831936

Table 1 gives the numerical solution of Illustration 1 using different step sizes. Obviously this takes more computational effort since more iteration is needed but gives more accurate result.

Xy(x) at h = 0.02, n = 5y(x) at h = 0.01, n = 10y(x) at h = 0.005, n = 20
01.01.01.0
0.021.020000001.0198039221.019706799
0.041.0392307691.0388528861.038665609
0.061.0577482321.0572007041.056929219
0.081.0756010581.0748943341.074543762
0.101.0928319361.0919750651.091549847

Table 1.

Numerical solution of Example on Euler’s method.

The algorithm/ code given below can be adapted to various single differential equations for the Euler method (Figure 1).

Algorithm/ Codes using Maple programming software

  ## Illustration on Euler method#

 ode:=diff(y(x),x)=(y(x)-x)/(y(x)+x);

sol:dsolve({ode,y(0)=1},y(x));

  ##restart:y:=array(0..200):x:=array(0..200):n:=5.0:A:=0.0:h:=0.02:x[0]:=0.0:y[0]:=1.0:

  for m from 0 to n do x[m]:=A+m*h:y[0]:=1.0:od:

  for m from 0 to n do y[m+1]:=y[m]+h*((y[m]-x[m])/(y[m]+x[m])):od:

  for m from 0 to n do print (m, x[m], y[m]);od:

  0, 0., 1.0

  1, 0.02, 1.020000000

  2, 0.04, 1.039230769

  3, 0.06, 1.057748232

  4, 0.08, 1.075601058

  5, 0.10, 1.092831936

Figure 1.

Graph of numerical solution of Illustration 1 for six iterations.

For more on Maple programming codes and algorithms see [6].

Illustration 2

Consider the Lorenz dynamical system given as.

dxdt=σytxt,x0=0.1
dydt=xtzt+rxtyt,y0=0.1
dzdt=xtytbzt,z0=0.1

Where σ=10, r=28 and b=83 with step size 0.01 determine the solution at t=10 using the Euler’s method.

Solution

The Lorenz system is a classical nonlinear dynamical system. Due to the high degree of nonlinearity of the system and the number of iterations required, its numerical solution is only easily tractable using computer programming. The algorithm/Code in Maple mathematical software is given below after the table of numerical solution (Figures 24) and (Table 2).

Figure 2.

Graphical representation of numerical solution of Lorenz system X(t) using Euler method for 1000 iterations as in Table 2.

Figure 3.

Graphical representation of numerical solution of Lorenz system Y(t) using Euler method for 1000 iterations as in Table 2.

Figure 4.

Graphical representation of numerical solution of Lorenz system Z(t) using Euler method for 1000 iterations as in Table 2.

tX(t)Y(t)Z(t)
1.04.2777167831.83544159826.77337837
2.03.3078604455.70792874812.82481891
3.09.0482573503.39424277033.60229026
4.02.9066315324.60630437217.27981641
5.014.2570715818.8565332429.67196074
6.00.14570991241.41906617821.71150940
7.08.80744703911.6884119923.40171689
8.04.0387572580.510495090227.19520327
9.06.2607082169.46124571618.53277577
10.016.6576370218.0080555037.42332818

Table 2.

Numerical solution of the Lorenz system using Euler method at step size 0.01.

tn=t0+nh
n=tnt0h=1000.01=1000

Algorithm/Maple programming Code of the numerical solution of Lorenz system using Euler’s method

## This is the numerical solution of Lorenz system using Euler method##

sys:=diff(x(t),t)=sigma*(y(t)-x(t)),diff(y(t),t)=-x(t)*z(t)+r*x(t)-y(t),

diff(z(t),t)=x(t)*y(t)-b*z(t);

##restart:X:=array(0..2000000):Y:=array(0..2000000):Z:=array

(0..2000000):T:=array(0..2000000):A:=0.0:h:=0.01:N:=1000:sigma:=

10.0:b:=(8/3):r:=28.0: for m from 0 to N do T[m]:=A+m*h:X[0]:=0.1:

Y[0]:=0.1:Z[0]:=0.1:od: for m from 0 to N do X[m+1]:=X[m]+

h*(sigma*(Y[m]-X[m])):Y[m+1]:=Y[m]+h*(-X[m]*Z[m]+r*X[m]-Y[m]):

Z[m+1]:=Z[m]+h*(X[m]*Y[m]-b*Z[m]):od:

for m from 0 by 100 to N do print (m, T[m], X[m],Y[m],Z[m]);od: with(plots):

func1:=listplot([seq(X[m],m=0..N)],style=line,color=black,thickness=1):

display(func1,labels=["t","X(t)"],axes=boxed,caption="Figure1:Euler solution for X(t)");

func2:=listplot([seq(Y[m],m=0..N)],style=line,color=blue,thickness=1):

display(func2,labels=["t","Y(t)"],axes=boxed,caption="Figure2:Euler solution for

Y(t)"); func3:=listplot([seq(Z[m],m=0..N)],style=line,color=red, thickness=1):

display(func3,labels=["t","Z(t)"],axes=boxed,caption="Figure

3:Euler solution for Z(t)");

For more on Lorenz system see [7].

Advertisement

3. The Runge-Kutta method of order four

One of the well-known numerical techniques for solving differential equations is the Runge-Kutta method. The Runge-Kutta method is a family of methods which depend on the order of derivatives involved. The Runge-Kutta method of order four is the classical form of the method in which four values of the derivatives are used for each iteration [1].

3.1 Derivation of Runge-Kutta method of order four

The Taylor series of functions of one variable is:

yx=yx0+yx0xx0+yx02!xx02+yx03!xx03+E13
yx=n=0ynn!xx0nE14

when x=x0+h, xx0=h.

Then Eq. (13) becomes.

yx=yx0+hyx0+h2yx02!+h3yx03!+E15

Taylor series of functions of two variables is:

fxy=i=01n!xx0x+yy0yifxyE16

when x=x0+mh, xx0=mhand yy0=nh

fxy=i=01n!mhx+nhyifxyE17
fx.y=fx0y0+f'x0y0mh+nh+f''x0y0mh+nh22!
+f'''x0y0mh+nh33!+E18
fx.y=fx0y0+mhf'x0y0+nhf'x0y0+f''x0y0mh2+2mhnh+nh22!
+f'''x0y0mh3+3mh2nh+3mhnh2+nh33!+E19
fx.y=fx0y0+hmfx+nfy+h22!m2fxx+2mnfxy+n2fyy+h33!m3fxxx+3m2nfxxy+3mn2fxyy+n3fyyy+E20

where the partial derivatives are all evaluated at the point x0y0.

Consider the differential equation

dydx=y'=fxyE21
df=fxdx+fydyE22
d2fdx2=dfdx=f'=fdxdxdx+fydydxE23
y''=f'=fdx+fydydxE24
y''=f'=fdx+fyfE25
y''=f'=fx+ffyE26
y=f=fx+ffy=fx+ffyE27
fx=fx+ffxyE28
ffy=ffy+fyfE29
y=f=fxx+2ffxy+f2fyy+fxfy+ffy2E30

From Eq. (15).

yxyx0=hyx0+h2yx02!+h3yx03!+E31

Hence

yxyx0=hfx0+h22!fx+ffy+h33!fxx+2ffxy+f2fyy+fxfy+ffy2+E32

The main idea is to select several points so that the Taylor series of expansion Eq. (19), coincide with the terms on the right hand side of Eq. (32).

Supposem1=hfx0y0E33
m2=hfx0+nhy0+nm1E34
m3=hfx0+phy0+pm2E35
m4=hfx0+qhy0+qm3E36

Using Eq. (32), these values become;

m1=hfE37
m2=hf+nhfx+ffy+nh22fxx+2ffxy+f2fyy+E38
m3=hf+phfx+ffy+ph22fxx+2ffxy+f2fyy+2npfxfy+ffy2+E39
m4=hf+qhc+qh22fxx+2ffxy+f2fyy+2pqfxfy+ffy2+E40

where all functions are evaluated at the point (x0, y0).

Considering an expression of the form

am1+bm2+cm3+dm4E41

Equating the expression to the right hand side of Eq. (37) through Eq. (40) to obtain four equations in seven unknowns.

Coefficient ofhf:a+b+c+d=1E42
Coefficient ofhffxfy+ffy2bn+cp+dq=12E43
Coefficient ofhffxx+2ffxy+f2fyy:bn2+cp2+dq2=13E44
Coefficient ofhffxfy+ffy2:cnp+dpq=16E45

Choosing three unknown quantities arbitrarily since there are four equations in seven unknowns.

Let n=p=12 and q=1 in Eq. (42) through Eq. (45). Then

a+b+c+d=1E46
b+c+2d=1E47
3b+3c+12d=4E48
3c+6d=2E49

Solving Eq. (46) through Eq. (47) simultaneously produced.

a=d=16,b=c=13

Connecting Eq. (33) and the expression (41).

yxyx0=am1+bm2+cm3+dm4E50
yxyx0=m16+m23+m33+m46E51
yx=yx0+16m1+2m2+2m3+m4E52

In general

yn+1=yn+16m1+2m2+2m3+m4E53

Where

m1=hfxnyn
m2=hfxn+12hyn+12m1
m3=hfxn+12hyn+12m2
m4=hfxn+hyn+m3

This is the Runge-Kutta formula of order four for a single first order differential equation.

The formula can be generalized for system of differential equations.

Consider the system of two initial-value differential equation:

dxdt=ftxyx0=y0E54
dydt=gtxyy0=x0E55

The Runge-Kutta formula of order four for system of two initial-value differential equation of the form of Eq. (54) and Eq. (55) is

xn+1=xn+16k1+2k2+2k3+k4E56
yn+1=yn+16m1+2m2+2m3+m4E57

Where

k1=hftnxnyn
m1=hgtnxnyn
k2=hftn+12hxn+12k1yn+12m1
m2=hgtn+12hxn+12k1yn+12m1
k3=hftn+12hxn+12k2yn+12m2
m3=hgtn+12hxn+12k2yn+12m2
k4=hftn+hxn+k3yn+m3
m4=hgtn+hxn+k3yn+m3

Generalizations of the formular to system of more than two equations follow a similar pattern.

Further insights and resources on the Runge- Kutta method can be found in [8, 9, 10].

Illustration 3

Given the initial value problem.

y=x+y,y0=1..

Determine.

y for x=0.2, using the Runge-Kutta method of order four (RK4) with the step size h=0.1.

Solution

The RK4 formula is:

yn+1=yn+16m1+2m2+2m3+m4

Where

m1=hfxnyn
m2=hfxn+12hyn+12m1
m3=hfxn+12hyn+12m2
m4=hfxn+hyn+m3
xn=x0+nh
n=xnx0h=0.200.1=2

Hence two iterations shall be needed.

When n=0

y1=y0+16m1+2m2+2m3+m4
m1=hfx0y0=0.1f01=0.10+1=0.1
m2=hfx0+12hy0+12m1=0.1f0+120.11+120.1=0.1f0.051.05=0.10.05+1.05=0.11.1=0.11
m3=hfx0+12hy0+12m2=0.1f0+120.11+120.11=0.1f0.051.055=0.10.05+1.055=0.11.105=0.1105
m4=hfx0+hy0+m3=0.1f0+0.11+0.1105=0.1f0.11.1105=0.10.1+1.1105=0.11.2105=0.12105
y1=y0+16m1+2m2+2m3+m4=1+160.1+20.11+20.1105+0.12105=1.110342

For the second iteration, when n = 1:

y2=y1+16m1+2m2+2m3+m4

Where

x1=x0+h=0+0.1=0.1
m1=hfx1y1=0.1f0.11.110342=0.10.1+1.110342=0.11.210342=0.1210342m2=hfx1+12hy1+12m1=0.1f0.1+120.11.110342+0.12103422=0.1f0.151.1.708591=0.13208591m3=0.1f0.151.176385=0.1326385m4=0.1f0.21.2429805=0.144298049
y2=1.110342+160.1210342+20.13208591+20.1326385+0.144298049=1.242805512

Illustration 4

Consider the Lorenz dynamical system given as.

dxdt=σytxt,x0=0.1.
dydt=xtzt+rxtyt,y0=0.1.
dzdt=xtytbzt,z0=0.1.

Whereσ=10, r=28 and b=83with step size 0.1 determine the numerical solution att=10 using the method of RK4 (Figures 57).

Figure 5.

Graphical representation of numerical solution of Lorenz system X(t) using Runge-Kutta method of order four for 100 iterations as in Table 3.

Figure 6.

Graphical representation of numerical solution of Lorenz system Y(t) using Runge-Kutta method of order four for 100 iterations as in Table 3.

Figure 7.

Graphical representation of numerical solution of Lorenz system X(t) using Runge-Kutta method of order four for 100 iterations as in Table 3.

Solution

The number of iteration needed is determine thus:

tn=t0+nh
n=tnt0h=1000.1=100

The numerical solution is generated using Maple mathematical programming software. The codes and solution are presented below (Table 3).

Algorithm/Maple programming Code of the numerical solution of Lorenz system using RK4 method.

## This is the numerical solution of Lorenz system using RK 4method##    sys1:=diff(x(t),t)=sigma*(y(t)-x(t)),diff(y(t),t)=-x(t)*z(t)+r*x(t)-y(t),diff(z(t),t)=x(t)*y(t)-b*z(t);    ##    restart:X:=array(0..20000):Y:=array(0..20000):Z:=array(0..20000):T:=array(0..20000):A:=0.0:h:=0.1:N:=100:sigma:=10.0:b:=(8/3):r:=28.0:    for m from 0 to N do T[m]:=A+m*h:X[0]:=0.1:Y[0]:=0.1:Z[0]:=0.1:od:    for m from 0 to N-1 do K1:=h*(sigma*(Y[m]-X[m])): M1:=h*(-X[m]*Z    [m]+r*X[m]-Y[m]):N1:=h*(X[m]*Y[m]-b*Z[m]):K2:=h*(sigma*((Y[m]+    M1/2)-(X[m]+K1/2))): M2:=h*(-(X[m]+K1/2)*(Z[m]+N1/2)+r*(X[m]+    K1/2)-(Y[m]+M1/2)):N2:=h*((X[m]+K1/2)*(Y[m]+M1/2)-b*(Z[m]+N1/2))    :K3:=h*(sigma*((Y[m]+M2/2)-(X[m]+K2/2))): M3:=h*(-(X[m]+K2/2)*(Z    [m]+N2/2)+r*(X[m]+K2/2)-(Y[m]+M2/2)):N3:=h*((X[m]+K2/2)*(Y[m]+    M2/2)-b*(Z[m]+N2/2)):K4:=h*(sigma*((Y[m]+M3)-(X[m]+K3))):M4:=h*    (-(X[m]+K3)*(Z[m]+N3)+r*(X[m]+K3)-(Y[m]+M3)):N4:=h*((X[m]+K3)*(Y    [m]+M3)-b*(Z[m]+N3)):X[m+1]:=X[m]+(K1+2*K2+2*K3+K4)/6:Y[m+1]:=Y    [m]+(M1+2*M2+2*M3+M4)/6: Z[m+1]:=Z[m]+(N1+2*N2+2*N3+N4)/6:od:    for m from 0 by 10 to N do print (m, T[m], X[m],Y[m],Z[m]);od:

tX(t)Y(t)Z(t)
1.08.4378531279.71113953325.75673332
2.07.6287157866.96609016026.86095558
3.09.5743365739.69023684428.21573058
4.07.5244942818.10703815624.99141661
5.08.7523008397.56266262228.79099030
6.08.8040611709.97339758725.79606864
7.07.3967862486.82480981826.47905713
8.09.7176084399.62259677628.65893980
9.07.4947440308.27695800924.64927991
10.08.6014219047.22298003928.83748475

Table 3.

Numerical solution of the Lorenz system using RK4 method at step size 0.1.

For further insight on programming with Maple see [6].

Advertisement

4. Conclusions

This chapter discusses the numerical solution of differential equation using Euler and Runge-Kutta methods. The formulas were derived and illustrations given to understand their applications. Algorithms written in Maple computational environment were provided for better understanding and further practice.

References

  1. 1. Akinsola VO, Oluyo TO. Mathematical analysis with numerical solutions of the mathematical model for the complications and control of diabetes mellitus. Journal of Statistics and Management Systems. 2019;22(5):845-869. DOI: 10.1080/09720510.2018.1556409
  2. 2. Bronshtein IN, Semendyayev KA, Musiol G, Mühlig H. Handbook of Mathematics. 6th ed. London: Springer; 2015
  3. 3. Baumann G. Mathematics for Engineers IV: Numerics. München: Oldenbourg Wissenschaftsverlag; 2010
  4. 4. Lynch S. Dynamical Systems with Applications using Maple. 2nd ed. Basel: Birkhäuser Verlag AG, Springer; 2010
  5. 5. Shah NH. Numerical Methods with C + + Programming. New Delhi: PHI Learning; 2009. p. 243
  6. 6. Richard H. Computer Algebra Recipes for Mathematical Physics. Boston: Birkhauser; 2005
  7. 7. Montoya CA, Sánchez RD, Castaño LF. Approach to the numerical solution of Lorenz system on SoC FPGA. In: 2016 XXI Symposium on Signal Processing, Images and Artificial Vision (STSIVA). Colombia: Universidad Pontificia Bolivariana (UPB) Seccional Bucaramanga; 2016. pp. 1-4. DOI: 10.1109/STSIVA.2016.7743305
  8. 8. Enns RH, McGuire GC. Nonlinear Physics with Maple for Scientists and Engineers. 2nd ed. Massachusetts, USA: Birkhäuser Boston, Springer Science+Business Media; 2000
  9. 9. Steele WA, Vallauri R. Computer simulations of pair dynamics in molecular fluids. Molecular Physics. 1987;61(4):1019-1030. DOI: 10.1080/00268978700101621
  10. 10. Zou Y. Multi-Variable Calculus, A First Step. Berlin/Boston: Walter de Gruyter GmbH; 2020

Written By

Victor Akinsola

Submitted: 09 August 2022 Reviewed: 10 October 2022 Published: 05 January 2023