bachelor thesis in mathematics/applied...

65
BACHELOR THESIS IN MATHEMATICS/APPLIED MATHEMATICS Comparison of numerical methods for solving a system of ordinary differential equations: accuracy, stability and efficiency by Kolar Amir Taher Kandidatarbete i matematik /till¨ ampad matematik DIVISION OF APPLIED MATHEMATICS M ¨ ALARDALEN UNIVERISTY SE-721 23 V ¨ ASTER ˚ AS, SWEDEN

Upload: others

Post on 28-Feb-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

BACHELOR THESIS IN MATHEMATICS/APPLIED MATHEMATICS

Comparison of numerical methods for solving a system of ordinarydifferential equations: accuracy, stability and efficiency

by

Kolar Amir Taher

Kandidatarbete i matematik /tillampad matematik

DIVISION OF APPLIED MATHEMATICSMALARDALEN UNIVERISTY

SE-721 23 VASTERAS, SWEDEN

Page 2: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Bachelor thesis in mathematics/applied mathematics

Current version:7th June 2020

Project name:Comparison of numerical methods for solving a system of ordinary differential equa-tions: accuracy, stability and efficiency

Author :Kolar Amir Taher

Supervisor :Siyang Wang

Reviewer :Thomas Westerback

Examiner :Doghonay Arjmand

Comprising :15 ECTS credits

Page 3: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Abstract

In this thesis, we compute approximate solutions to initial value problems of first-order linear ODEs using five explicit Runge-Kutta methods, namely the forwardEuler method, Heun’s method, RK4, RK5, and RK8. This thesis aims to comparethe accuracy, stability, and efficiency properties of the five explicit Runge-Kuttamethods. For accuracy, we carry out a convergence study to verify the convergencerate of the five explicit Runge-Kutta methods for solving a first-order linear ODE.For stability, we analyze the stability of the five explicit Runge-Kutta methodsfor solving a linear test equation. For efficiency, we carry out an efficiency study tocompare the efficiency of the five explicit Runge-Kutta methods for solving a systemof first-order linear ODEs, which is the main focus of this thesis. This system offirst-order linear ODEs is a semi-discretization of a two-dimensional wave equation.

Page 4: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Acknowledgments

l would like to express my sincere gratitude to my supervisor, Siyang Wang, who hasprovided me with a deep understanding of MATLAB features. This understandingallowed me to be able to solve initial value problems of first-order linear ODEsin MATLAB. l would also like to thank Siyang Wang and my reviewer ThomasWesterback for their input and guidance on how to develop this thesis.

Page 5: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Contents

1 Introduction 61.1 Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.2 Literature review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.3 Research question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.4 Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2 Differential equations 102.1 Partial differential equations . . . . . . . . . . . . . . . . . . . . . . . 102.2 Ordinary differential equations . . . . . . . . . . . . . . . . . . . . . . 13

3 Numerical methods for ODEs 153.1 Finite difference methods . . . . . . . . . . . . . . . . . . . . . . . . . 15

3.1.1 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.1.2 Numerical properties . . . . . . . . . . . . . . . . . . . . . . . 16

3.2 Stability of explicit Runge-Kutta methods . . . . . . . . . . . . . . . 183.2.1 The forward Euler method . . . . . . . . . . . . . . . . . . . . 193.2.2 Heun’s method . . . . . . . . . . . . . . . . . . . . . . . . . . 203.2.3 The fourth-order Runge-Kutta method . . . . . . . . . . . . . 213.2.4 The fifth-order Runge-Kutta method . . . . . . . . . . . . . . 223.2.5 The eighth-order Runge-Kutta method . . . . . . . . . . . . . 233.2.6 Comparison of stability regions . . . . . . . . . . . . . . . . . 27

4 Numerical experiments 294.1 Convergence study . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294.2 Efficiency study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

4.2.1 Stability limits . . . . . . . . . . . . . . . . . . . . . . . . . . 364.2.2 Time-error efficiency . . . . . . . . . . . . . . . . . . . . . . . 37

5 Conclusion 42

Bibliography 46

A MATLAB code 46A.1 Script 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.2 Script 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.3 Script 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.4 Script 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.5 Script 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.6 Script 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

2

Page 6: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

A.7 Script 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.8 Script 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.9 Script 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.10 Script 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.11 Script 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.12 Script 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.13 Script 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.14 Script 14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.15 Script 15 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.16 Script 16 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.17 Script 17 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.18 Script 18 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46A.19 Script 19 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

B Reflection of thesis objectives 46

3

Page 7: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

List of Figures

3.1 Stability regions of the five explicit Runge-Kutta methods in a com-plex plane . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4.1 Global truncation errors in the numerical solution of y′(t) = −2ty(t),y(0) = 1, for t ∈ [0, T ] using the five explicit Runge-Kutta methodsagainst the step size h on a log-log scale with logarithm of base 10. . 32

4.2 Global truncation errors in the numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability nnumber of time points using the forward Euler method. . . . . . . . . 37

4.3 Time (seconds) required for the forward Euler method to computeglobal truncation errors in the numerical solution of the system ofODEs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

4.4 Global truncation errors in the numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability nnumber of time points using Heun’s method. . . . . . . . . . . . . . . 38

4.5 Time (seconds) required for the Heun’s method to compute globaltruncation errors in the numerical solution of the system of ODEs. . . 38

4.6 Global truncation errors in the numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability nnumber of time points using RK4. . . . . . . . . . . . . . . . . . . . . 38

4.7 Time (seconds) required for RK4 to compute global truncation errorsin the numerical solution of the system of ODEs. . . . . . . . . . . . 38

4.8 Global truncation errors in the numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability nnumber of time points using RK5. . . . . . . . . . . . . . . . . . . . . 39

4.9 Time (seconds) required for RK5 to compute global truncation errorsin the numerical solution of the system of ODEs. . . . . . . . . . . . 39

4.10 Global truncation errors in the numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability nnumber of time points using RK8. . . . . . . . . . . . . . . . . . . . . 39

4.11 Time (seconds) required for RK8 to compute global truncation errorsin the numerical solution of the system of ODEs. . . . . . . . . . . . 39

4

Page 8: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

List of Tables

4.1 Global truncation errors and approximations of the convergence rateof the forward Euler method for solving y′(t) = −2ty(t), y(0) = 1,for t ∈ [0, 1]. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30

4.2 Global truncation errors and approximations of the convergence rateof Heun’s method for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1]. . 30

4.3 Global truncation errors and approximations of convergence rate ofRK4 for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1]. . . . . . . . . 31

4.4 Global truncation errors and approximations of the convergence rateof RK5 for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1]. . . . . . . . 31

4.5 Global truncation errors and approximations of the convergence rateof RK8 for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1.2]. . . . . . . 31

4.6 Stability limit n of the five explicit Runge-Kutta methods for ~y ′(t) =

A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1]. . . . . . . . . . . . . . . . . . 36

5

Page 9: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Chapter 1

Introduction

1.1 Background

A differential equation is an equation that relates one or several functions and itsderivatives [10]. Differential equations are used to model advanced systems, for in-stance, mechanical and electrical systems from fields such as biology, social sciences,engineering, and economics. Mechanical systems and electrical systems are two ex-amples of physical systems that change over time [18]. Differential equations areused to describe a continuous change of physical systems mathematically. Partialdifferential equations (PDEs) and ordinary differential equations (ODEs) are twoclasses of differential equations that are used to model and characterize the beha-vior of physical systems. Parabolic PDEs exist in physical problems such as a heatconduction problem for solid bodies. Heat equations describe how the heat tem-perature distributes in, for instance, a rode as the time changes. Wave equationsexist to be used to model physical problems such as physical systems, where wavemotions are considered [23].

Furthermore, the above-mentioned examples of systems are complex which ac-cording to Houcque [11] are not possible to solve using analytical methods, therefore,numerical methods are used instead. Numerical methods are important to use tosolve those differential equations whose exact solutions are not possible to obtainusing analytical methods. Analytical methods such as the method of separationof variables give us exact solutions of simple PDEs and ODEs, whereas numericalmethods give us approximations of the exact solution. Heath [10] emphasizes thatnumerical methods are viewed as a significant tool to use to solve problems for ex-ample in engineering and industry. Also, Omale et al. [18] describe that numericalmethods have been used to solve differential equations of weather and climate fore-casts. Numerical methods are explicit or implicit computed in one step or multiplesteps. An explicit method computes the numerical solution at the next time pointusing the previous numerical solution at the previous time point. While an implicitmethod evaluates a function using the numerical solution at the next time pointwhich is solved for.

There are various numerical methods for solving PDEs and ODEs [10]. Themethod of lines is an example of a numerical method used to find numerical solu-tions of hyperbolic and parabolic PDEs by transforming the PDEs to a system offirst-order ODEs which approximates the original PDEs. Finite difference meth-ods, finite element methods, and finite volume methods are examples of numerical

6

Page 10: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

methods that are used to approximate the partial derivatives in the PDEs [23].Finite difference methods such as Runge-Kutta methods are essential for findingapproximate solutions of initial value problems of first-order ODEs [3]. High-orderRunge-Kutta methods are used to solve ODEs because of their high accuracy andefficiency [10].

Moreover, C. Runge and M. W. Kutta developed explicit and implicit Runge-Kutta methods [1]. The developments of low-order to high-order Runge-Kutta meth-ods started in 1895. In 1895, C. Runge extended the forward Euler method tomore elaborate Runge-Kutta methods with higher accuracy. Runge developed thesecond-order Runge-Kutta method in 1895. K. Heun introduced the third-orderRunge-Kutta method in 1900 [6]. Runge-Kutta methods became important in thestudies of explicit and implicit methods for solving ODEs through time discretization[7]. In 1901, W. Kutta introduced the fourth-order and the fifth-order Runge-Kuttamethods. F. J. Nystrom developed Runge-Kutta methods to be used to solve sys-tems of second-order differential equations [27]. In 1957, R. H. Merson proposed theidea to combine Runge-Kutta methods of different orders in Butcher tableau. In1964, Butcher introduced the sixth-order Runge-Kutta method with seven stages.The seventh-order Runge-Kutta method with nine stages was recognized from 1968.In 1970, the eighth-order Runge-Kutta method with eleven stages was introducedby Curtis [6].

Runge-Kutta methods are active in research [1] and for the past several years,most research papers have relied on the derivation of new Runge-Kutta methods withhigher derivatives to obtain more accurate Runge-Kutta methods [30]. In severalpapers, researchers have investigated, compared lower-order and high-order Runge-Kutta methods in terms of numerical properties, namely accuracy, stability, andefficiency. In Section 1.2, we review such research papers that have been publishedfor the past several years.

1.2 Literature review

Wusu et al. [30] published a paper on deriving a new Runge-Kutta method involvinghigher derivatives. In their paper, they derived a three-stage multiderivative expli-cit Runge-Kutta method with first-order and second-order derivatives using theTaylor series expansion of a function. They analyzed the multiderivative explicitRunge-Kutta method in terms of stability and consistency. They also solved twofirst-order ODEs to compare the accuracy of the multiderivative explicit Runge-Kutta method with Heun’s method and Goeken’s method. Heun’s method andGoeken’s method are also three-stage explicit Runge-Kutta methods but they in-volve fewer derivatives than the multiderivative explicit Runge-Kutta method. Theauthors computed absolute errors in the numerical solution of the two first-orderODEs using the multiderivative explicit Runge-Kutta method, Heun’s method, andGoeken’s method with different step sizes. An absolute error is the magnitude of thedifference between the numerical solution and the exact solution of an ODE. Theirresearch has shown that the absolute errors of the Goeken’s method and the mul-tiderivative explicit Runge-Kutta method do not increase as fast as Heun’s method.The multiderivative explicit Runge-Kutta method did perform better than Heun’smethod and Goeken’s method. Their research has also shown that the multideriv-ative explicit Runge-Kutta method is stable, it is the most accurate and efficient

7

Page 11: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

three-stage explicit Runge-Kutta method investigated in their paper.

Papadopoulos and Simos [19] have also derived an explicit Runge-Kutta method,namely the fourth-order modified Runge-Kutta Nystrom method. The fourth-ordermodified Runge-Kutta Nystrom method consists of four additional variable coef-ficients than the classical fourth-order Runge-Kutta method. In their research,they have used the fourth-order modified Runge-Kutta Nystrom method to solve aSchrodinger equation, which is a second-order ODE. The authors have compared theaccuracy and efficiency of the fourth-order modified Runge-Kutta Nystrom methodwith three additional types of fourth-order Runge-Kutta Nystrom methods for solv-ing the Schrodinger equation. They verified the efficiency by computing the errorat the final time point. Their research has shown that the fourth-order modifiedRunge-Kutta Nystrom method is more accurate and efficient than the other threetypes of fourth-order Runge-Kutta Nystrom methods.

Another research paper on the accuracy and efficiency of Runge-Kutta methodsis written by Hasan [9]. In his paper, he has compared the accuracy and efficiencyof the following six Runge-Kutta methods for solving an initial value problem ofa first-order linear ODE: the forward Euler method, the backward Euler method,Heun’s method, the fourth-order Runge-Kutta method, ODE23, and ODE45. Hecomputed the numerical solution and exact solution of the initial value problemusing the six Runge-Kutta methods with different step sizes. He verified the ac-curacy and efficiency of the six Runge-Kutta methods by computing the averageabsolute errors and computational time. His research has shown that the ODE45 isthe most accurate Runge-Kutta method. The ODE45 is more efficient in terms offunction evaluations and computational time than the other Runge-Kutta methodsinvestigated in this paper [9].

Moreover, Islam [12] has also written a research paper on the forward Eulermethod and the fourth-order Runge-Kutta method for finding numerical solutionsof two first-order ODEs with four step sizes. He has compared the accuracy of thetwo explicit Runge-Kutta methods by computing the maximum errors. Anothercomparison he did was to compare the efficiency of the two explicit Runge-Kuttamethods by verifying how fast the numerical solution converges to the exact solution.His research has shown that the fourth-order Runge-Kutta method is more accurateand the numerical solution obtained by this method converges faster in comparisonwith the forward Euler method. Also, the fourth-order Runge-Kutta method com-puted the numerical solution of the two first-order ODEs more efficiently than theforward Euler method.

Furthermore, according to Ketcheson and Ahmadia [14] it is significant to findstability polynomials of optimal explicit Runge-Kutta methods for solving first-orderODEs. In their research, they have for instance introduced an approach for how toconstruct stability polynomials of first-order ODEs. The authors have plotted thestability regions of optimal explicit Runge-Kutta methods such as the fourth-orderRunge-Kutta method. Another research paper on stability regions is written bySeka and Assui [24], where they have proved that the evolution of stability regionsdoes not depend on the order of Runge-Kutta methods. They did this proof byanalyzing the stability of the first-order, the second-order, the third-order, and thefourth-order Runge-Kutta method with an eighth-order Runge-Kutta method witheleven stages. Afterwards, they compared the stability regions of the first-order, thesecond-order, the third-order, and the fourth-order Runge-Kutta method with an

8

Page 12: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

eighth-order Runge-Kutta method with eleven stages which they have introduced.Their research has shown that the size of the stability regions of the second-order, thethird-order, and the fourth-order Runge-Kutta methods are larger than the stabilityregion of the eighth-order Runge-Kutta method [24].

In the above-reviewed papers, we see that several researchers have focused toa large extent on finding the accuracy, stability, and efficiency properties of low-order explicit Runge-Kutta methods such as the forward Euler method and Heun’smethod. The researchers have less focused on investigating the accuracy, stability,and efficiency properties of high-order explicit Runge-Kutta methods, especially thefifth-order and the eighth-order Runge-Kutta method. Indeed, there is less researchon stability analysis of high-order Runge-Kutta methods, in particular the fifth-orderRunge-Kutta method. Therefore, in this thesis, we derive the stability analysisfor the following five explicit Runge-Kutta methods: the forward Euler method,Heun’s method, the fourth-order Runge-Kutta method (RK4), fifth-order Runge-Kutta method (RK5) and the eighth-order Runge-Kutta method (RK8). We alsoinvestigate the accuracy and efficiency properties of the five explicit Runge-Kuttamethods.

1.3 Research question

In this thesis, we study the accuracy of the five explicit Runge-Kutta methodsfor solving an initial value problem of a first-order linear ODE by verifying theirconvergence rates. We derive the stability analysis to investigate which one of the fiveexplicit Runge-Kutta methods has a better stability property for solving an initialvalue problem of a linear test equation. This thesis mainly focuses on comparing theefficiency properties of the five explicit Runge-Kutta methods to be able to answerthe research question: Which one of the five explicit Runge-Kutta methods is themost efficient for solving an initial value problem for a system of first-order linearODEs? This system is given and already obtained by the method of lines for a two-space dimensional wave equation, where the wave equation is a hyperbolic PDE.This thesis only focuses on explicit methods to solve this system.

1.4 Outline

The outline of this thesis is as follows. In Chapter 2, we present a theoreticalbackground on PDEs and ODEs. Chapter 3 starts with a theoretical background onfinite difference methods and it ends with the stability analysis for the five explicitRunge-Kutta methods. In Chapter 4, we present, analyze, and discuss the resultsof the convergence study and the efficiency study. Lastly, in Chapter 5 we concludethis thesis and mention future work.

9

Page 13: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Chapter 2

Differential equations

This chapter presents a theoretical background on PDEs and ODEs that are funda-mental in this thesis.

2.1 Partial differential equations

A partial differential equation (PDE) is a differential equation involving an unknownfunction of partial derivatives with respect to more than one independent variable[10].

PDEs are classified into hyperbolic, parabolic, and elliptic PDEs. HyperbolicPDEs model physical processes that are time-dependent and do not develop intoa steady-state. Parabolic PDEs model physical processes that are time-dependentand do develop to a steady-state. Elliptic PDEs model processes that are time-independent and have already obtained a steady-state [10].

Yang et al. [31] describe that a wave equation is an example of a hyperbolic PDEand it is used to model wave motion problems. An amplitude of a wave measuresthe height of the wave. A two-space dimensional wave equation for an amplitudefunction u(x, y, t) in position (x, y) and time t is written as

∂2u(x, y, t)

∂t2= B

(∂2u(x, y, t)

∂x2+∂2u(x, y, t)

∂y2

)(2.1)

for x ∈ [x0, xf ], y ∈ [y0, yf ], t ∈ [0, T ],

where T is a fixed final time point. Furthermore, u(x, y, t) is a solution of Equation(2.1) which we seek for. This solution defines the dependent variable u is a function ofthe spatial independent variables, x, y, and the independent variable t representingtime. This solution describes the waves that move with the constant wave velocityB. In Equation (2.1), x ranges from the initial spatial point x0 to the final spatialpoint xf . Also, y ranges from the initial spatial point y0 to the final spatial point yf .And t ranges from the initial time point t = 0 to the fixed final time point t = T .

We want to find u(x, y, t) that satisfies the boundary conditions and the initialconditions. The boundary conditions are

u(x0, y, t) = bx0(y, t), u(xf , y, t) = bxf (y, t), (2.2)

u(x, y0, t) = by0(x, t), u(x, yf , t) = byf (x, t),

10

Page 14: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

and the initial conditions are

u(x, y, 0) = i0(x, y),∂u

∂t

∣∣∣t=0

(x, y, 0) = i′0(x, y), (2.3)

which should be given in order to solve Equation (2.1). In Equation (2.2), b is aconstant which denotes boundary, u(x0, y, t) is a solution at the boundary pointx0 which is equal to the boundary condition bx0(y, t) in position y and time t.Additionally, u(xf , y, t) is a solution at the boundary point xf which is equal to theboundary condition bxf (y, t). Also, u(x, y0, t) is a solution at the boundary point y0which is equal to the boundary condition by0(x, t) in position x and time t. Also,u(x, yf , t) is a solution at the boundary point yf which is equal to the boundarycondition byf (x, t). In Equation (2.3), u(x, y, 0) is a solution at t = 0 which is equalto the the initial condition i0 of a spatial index i in position x and y. The first-orderderivative of the initial condition i0 is i′0 in position x and y [31].

Furthermore, Yang et al. [31] describe that a heat equation in two space di-mensions is an example of a parabolic PDE. A two space dimensional heat equationdescribes the temperature distribution u(x, y, t) which is written as

∂u(x, y, t)

∂t= B

(∂2u(x, y, t)

∂x2+∂2u(x, y, t)

∂y2

)(2.4)

for x ∈ [x0, xf ], y ∈ [y0, yf ], t ∈ [0, T ].

In this context, the temperature distribution is the temperature that spreads in, forexample, a wire. The temperature distribution is a function of x, y and time t. InEquation (2.4), B is a diffusion constant [31].

Equation (2.4) has the boundary conditions

u(x0, y, t) = bx0(y, t), u(xf , y, t) = bxf (y, t),

u(x, y0, t) = by0(x, t), u(x, yf , t) = byf (x, t),

and the initial condition isu(x, y, 0) = i0(x, y).

Furthermore, u(x, y, t) is a solution of Equation (2.4) we seek for that satisfies theboundary conditions and the initial condition [31].

According to Yang et al. [31] Poisson’s equation in two space dimensions is anelliptic PDE and it is written as

∂2u(x, y)

∂x2+∂2u(x, y)

∂y2= f(x, y), (2.5)

for x ∈ [x0, xf ], y ∈ [y0, yf ],

where f(x, y) is a function of x and y. Equation (2.5) has the boundary conditions

u(x0, y) = bx0(y), u(xf , y) = bxf (y), (2.6)

u(x, y0) = by0(x), u(x, yf ) = byf (x).

We seek for the solution u(x, y) of Equation (2.5) which satisfies the boundaryconditions. In Equation (2.6), u(x0, y) is a solution at the boundary point x0 which

11

Page 15: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

is equal to the boundary condition bx0(y) in position y. Also, u(xf , y) is a solution atthe boundary point xf which is equal to the boundary condition bxf (y). Furthermore,u(x, y0) is a solution at the boundary point y0 in position x. Additionally, u(x, yf )is a solution at the boundary point yf [31].

Furthermore, the method of lines is a method that solves hyperbolic PDEs andparabolic PDEs that are time-dependent PDEs. The method of lines is a procedurethat replaces spatial partial derivatives in a PDE with algebraic approximationsnumerically. Consequently, the spatial derivatives that were written in the explicitform before are not anymore in terms of x and y. Finite difference methods, finiteelement methods, and finite volume methods are examples of numerical methodsthat are used to approximate the spatial derivatives. Consequently, we obtain asystem of ODEs with one independent variable t. Numerical methods are used tosolve the ODEs to compute an approximate solution of the original PDE [23].

Now we consider the following two-dimensional wave equation to explain morehow to proceed with the method of lines

utt = uxx + uyy, (2.7)

The numerical procedure of the method of lines for Equation (2.7) is as follows.In Equation (2.7), we can see that the function u is dependent on t, x, and y.Furthermore, utt, uxx and uyy are also second-order partial derivatives of u withrespect to t, x and y, respectively. Equation (2.7) is changing over time and inspace. We want to approximate the second-order partial derivatives utt, uxx, anduyy using finite differences. Therefore, we need to put discrete points in time andspace. However, in the method of lines, we do spatial discretization which meansthat we only discretize x and y in space and not in time. The time variable t isstill continuous. We discretize the spatial derivatives uxx and uyy. This procedureis called semi-discretization and it gives a system of ODEs. We put discrete pointsin the space x and y on intervals. We use the discrete points to approximate thesecond-order partial derivative utt in time t [23].

Furthermore, after the spatial discretization we get the following system of ODEs

~utt = D~u, (2.8)

where D is the spatial discretization matrix which according to [15] can be con-structed using, for instance, the finite difference methods, finite element methods,or finite volume methods. Equation (2.8) is in the second-order partial derivativewith respect to time t which can’t be solved using the numerical methods for solv-ing ODEs with first-order derivatives. Therefore, we have to transform ~utt to a newequation with first-order derivative with respect to t by introducing a new vector ~vas the following

~v = ~ut. (2.9)

We must take the first-order derivative with respect to t on both sides of Equation(2.9) and we get

~vt = ~utt = D~u.

We write the vectors, ~u and ~v in one vector in first-order derivative with respect tot [

~u~v

]t

=

[~vD~u

]=

[0 ID 0

] [~u~v

], (2.10)

12

Page 16: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

where I is an identity 2 × 2 matrix. The initial value problem of a system of first-order linear ODEs solved in Section 4.2 is in matrix-vector form as Equation (2.10)with

A =

[0 ID 0

],

where A is a 2× 2 matrix. The system of ODEs in Equation (2.8) should be solvedusing explicit methods and not implicit methods.

2.2 Ordinary differential equations

An ordinary differential equation (ODE) is a differential equation involving an un-known function of derivatives with respect to one independent variable usually t.The order of an ODE is determined by the highest derivative in the ODE. Thehighest derivative in a first-order linear ODE is one, therefore this type of ODE isof first-order [10].

Consider this first-order linear ODE taken from [28]

y′(t) = 5y(t), (2.11)

where y is a dependent variable with respect to the independent variable t and y(t) isan unknown solution which we seek for. According to Wang [28] Equation (2.11) issimple, therefore we can use analytical methods to solve it analytically. The generalsolution which is a family of solutions of Equation (2.11) is

y(t) = Ce5t,

where C is a real constant [28]. If we instead want to find an exact solution ofEquation (2.11), then we can impose the following initial condition to determine thevalue of C which according to [28] it is

y(0) = 2.

Wang [28] describes that the initial condition provide us with information that theinitial value of y(t) at the initial time point t0 = 0 is 2. We get that y(0) = Ce0 =C = 2 and the exact solution that satisfies the initial condition of Equation (2.11)is

y(t) = 2e5t, (2.12)

where Equation (2.12) is in agreement with [28]. The first-order ODE with an initialcondition is called an initial value problem [28]. Wang [28] describes that a generalform of an initial value problem is

y′(t) = f(t, y(t)), (2.13)

y(a) = c, for t ∈ [a, b],

where f(t, y(t)) is a function which depend on t and y. From the initial conditionin Equation (2.13), we know that the initial value is c. Also, the time interval is

13

Page 17: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

t ∈ [a, b], the initial time point is t0 = a and the final time point is tn = b, where nis the number of time points.

Heath [10] describes that in most applications of differential equations thereexists more than one ODE which is transformed into a system of ODEs. Jung [13]introduce a system of k first-order linear ODEs with coefficients written in this form

y′1(t) = a11y1(t) + a12y2(t) + · · ·+ a1kyk(t) + b1(t)),

y′2(t) = a21y1(t) + a22y2(t) + · · ·+ a2kyk(t) + b2(t)),...

y′k(t) = ak1y1(t) + ak2y2(t) + · · ·+ akkyk(t) + bk(t)),

where aij(t) and bj(t) are known functions on the time interval t ∈ [a, b], i =1, 2, · · · , n and j = 1, 2, · · · , n. The column vector is ~y(t) of the unknown functionsy1(t), · · · , yk(t) which are dependent on t, where k = 1, 2, · · · , n [13]. Jung [13] writethis system of k first-order linear ODEs with an initial condition in matrix notationform as

~y ′(t) = A~y(t) +~b(t), (2.14)

~y(t) = ~y0, for t ∈ [a, b].

In Equation (2.14), A is a n×n matrix, ~y ′(t) is a column vector and ~b(t) is anothercolumn vector. The column vectors depend on t and the column vectors have alength n. If ~b(t) has length zero, then it is a zero column vector, and Equation(2.14) becomes homogeneous. In Equation (2.14), we have the initial value vector~y0. In the efficiency study, we use numerical methods for ODEs to solve an initialvalue problem of a homogenous system of first-order linear ODEs formulated asEquation (2.14).

14

Page 18: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Chapter 3

Numerical methods for ODEs

In this chapter, we present a theoretical background on finite difference methodsand analyze the stability of the five explicit Runge-Kutta methods.

3.1 Finite difference methods

Finite difference methods solve for instance initial value problems of first-order ODEson the time interval t ∈ [a, b]. The idea of finite difference methods for an initialvalue problem of a first-order ODE is to divide the time interval t ∈ [a, b] into nsubintervals and approximate the first-order derivative in the initial value problemfor every discrete time point tj in t ∈ [a, b]. In time discretization of initial valueproblems of first-order ODEs, we obtain discrete solutions that are approximatevalues of the exact solution of the first-order ODE at discrete time points. Time-discretization is a procedure for solving initial value problems of first-order linearODEs [31].

Now we show how to proceed with time-discretization of Equation (2.13). Wewant to find an approximate solutions of Equation (2.13) by discretizing the timeinterval t ∈ [a, b] into n subintervals using n+ 1 points [28]

a = t0 < t1 < t2 < · · · < tn = b.

Wang [28] describes that the length of each n subinterval is the step size hj = tj−tj−1,where j = 1, 2, · · · , n. In this procedure, the previous computed numerical solutionyj−1 at the previous discrete time point tj−1 is used to compute the next numericalsolution yj at the next discrete time point tj, where j = 0, 1, · · · , n. An approximatesolution of Equation (2.13) at the discrete time point tj is yj ≈ y(tj). The numericalsolution at t0 is y0 = y(t0) = c. We can use the numerical solution y0 = c to computethe next numerical solution y1 at the next discrete time point t1. We can use thisprocedure to get the numerical solution yn at the final time point tn.

One-step methods such as explicit Runge-Kutta methods use the informationfrom the previously computed numerical solution at a discrete time point to computethe next numerical solution at the next discrete time point. Explicit Runge-Kuttamethods are finite difference methods for solving initial value problems of first-order ODEs. Explicit Runge-Kutta methods involve s stages which is equal to thenumber of function evaluations of the function f(t, y(t)) in Equation (2.13) neededto advance the numerical solution in one time step [10].

15

Page 19: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

3.1.1 Errors

Errors occur when one performs numerical computation on a computer. Errors areclassified into round-off errors and truncation errors. Round-off errors are createdfrom the representation of numbers that is approximated. We get round-off errorsfrom numbers such as fractional numbers, π, and the square root of numbers whichcan not be represented exactly in the computer. Truncation errors are created whena numerical method is approximating an exact solution. Truncation errors are thedifference between the approximate solution and the exact solution. Truncationerrors are classified into local truncation errors and global truncation errors [10].

Heath [10] describes that the local truncation error `j is

`j = yj − uj−1(tj),

where j = 1, 2, · · · , n, the numerical solution of an initial value problem of a first-order ODE is yj and uj−1(t) is the solution of the ODE that passes through thepoint before, which is (tj−1, yj−1).

According to Heath [10] the global truncation error en is

en = yn − y(tn),

the difference between the numerical solution yn and the exact solution y(tn) at thefinal time point tn.

The differences between a local truncation error and a global truncation errorare that we compute the local truncation error in the numerical solution of an ODEat a discrete-time point tj. While we compute the global truncation error at thefinal time point tn and we obtain a total error at the final time point tn [28]. In thisthesis, we only compute global truncation errors because in both the convergencestudy and the efficiency study we want to compute global truncation errors at thefinal time point tn. Heath [10] states that global truncation errors are essential tostudy when evaluating the performance of numerical methods.

3.1.2 Numerical properties

Accuracy, stability, and efficiency are three numerical properties that are used todetermine the performance of numerical methods [10].

The following definition of accuracy is from [10].

Definition 3.1.1. The accuracy of a numerical method is said to be of order p if

`j = O(hp+1j ).

The motivation for this definition, with the order of accuracy one less than theexponent of the step size in the local error, is that if the local error is O(hp+1

j ), then

the local error per unit step,`jhj

, is O(hpj), and it can be shown that under reasonable

conditions the global error en is O(hp), where h is the average step size.

Leveque [16] describe that O is a big-oh notation and it is also known as anasymptotic notation which describe a function that is bounded asymptotically byupper bounds. In Definition 3.1.1, j = 1, 2, · · · , n and the global truncation error

16

Page 20: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

O(hp) is independent of h. We expect the following from a numerical method thathas order of accuracy p

eh = Chp + o(hp) when h→ 0,

where eh is an error on a grid of M points and C is in this case an error constant.The little-oh notation o describe a function that is not bounded asymptotically byupper bounds [16].

According to Leveque [16] if h is too small, then

eh ≈ Chp.

A refinement of the grid by a factor of 2, we get

eh2 ≈ C

(h2

)p.

Other factors can certainly be used as well to refine the grid, but refining a grid bya factor of 2 is a standard way.

The ratio of the errors eh and eh2 is

eh

eh2

≈ 2p,

where p is a positive integer and the errors decrease approximately by a factor of2p. Hence,

p ≈ log2

( ehe

h2

).

Furthermore, Leveque [16] describes that we can estimate p with two grid spa-cings h1 and h2 as the following

p ≈log(eh1

eh2

)log(h1

h2)

(3.1)

If the positive integer p > 0, then the global truncation error O(hp) → 0, whenh→ 0, and the numerical method converges with the convergence rate p. The ratep of convergence is known as the order of accuracy of a numerical method. Theconvergence rate p shows how fast the numerical method converges to the exactsolution when the step size h → 0. We can measure the accuracy of a numericalmethod by verifying the convergence rate p of a numerical method [28].

According to Soderlind [26], it is not possible to obtain accuracy in numericalsolutions if a numerical method is not stable. For this reason, a numerical methodmust be stable too. The following definition of stability is from [10]

Definition 3.1.2. A numerical method is said to be stable if small perturbationsdo not cause the resulting numerical solutions to diverge away without bound.

Perturbations are created in numerical computation. Small perturbations aredue to round-off errors or truncation errors in the initial data. A small perturbationis when a small change in the initial value of an ODE leads to often a small change

17

Page 21: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

in the numerical solutions of the ODE. We are perturbing the initial value by doingsmall changes to them. A stable numerical method is not sensitive to small perturb-ations, therefore the numerical solutions do not diverge from the exact solution. Fora stable numerical method, the errors in the numerical solutions decrease to zero.Small perturbations to the numerical solutions of a stable ODE get diminished asthe time increases because the numerical solution curves of the ODE converge tothe exact solution of the ODE. Consequently, the numerical solutions will boundthe exact solution.

Furthermore, an unstable numerical method is sensitive to small perturbations,where a small change in the initial value does result in a major change in thenumerical solution which causes the numerical solutions to diverge from the exactsolution. Small perturbations to a numerical solution of an unstable ODE will growas the time increases because the numerical solution curves of the ODE diverge. Ifthe numerical solutions of an unstable ODE diverge from the exact solution, thenthe numerical solutions are unbounded the exact solution [10].

Sabrowski [25] describes that convergence and error analysis can be used toinvestigate the efficiency of a numerical method. He defines efficiency as

Definition 3.1.3. Numerical efficiency means here the combination of short com-putational time and an acceptable error level.

An important aspect of numerical methods is to verify how much computationaltime is required for numerical methods to compute errors [22]. We can measure theefficiency of numerical methods by computing errors in the numerical solution ofODEs and computational time [25]. Several researchers have improved the efficiencyof Runge-Kutta methods for solving ODEs by decreasing the number of functionevaluations.

3.2 Stability of explicit Runge-Kutta methods

Soderlind [26] emphasizes that the stability property has a crucial role in solvingfirst-order ODEs using numerical methods. We can verify the stability of numericalmethods by analyzing how the numerical methods behave on a linear test equation.In this section, we derive the stability analysis for the five explicit Runge-Kuttamethods for solving a linear test equation from [28] defined as

y′(t) = −λy(t), (3.2)

y(0) = 1, t ∈ [0, T ],

where λ ∈ C.We choose Equation (3.2) to be considered in the stability analysis for the five

explicit Runge-Kutta methods because it is a simple first-order ODE which accordingto Soderlind [26] one choose in the stability analysis to be able to solve it analyticallyby pen and paper.

The known exact solution of Equation (3.2) is

y(t) = e−λt. (3.3)

The exact solution (3.3) is also an exponential solution of Equation (3.2) whichdecrease exponentially to zero when t→∞ and Re(λ) > 0. The notation, Re(λ) > 0

18

Page 22: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

means positive real values of λ on the real coordinate (Re) axis in a complex plane[28]. A complex plane also consists of an imaginary coordinate (Im) axis whichvisualizes complex numbers z = hλ. A numerical method is stable for differentvalues of hλ, where h is the step size. Different values of hλ are bounded in a regioncalled stability region which we can visualize in a complex plane. An numericalmethod is stable if |S(z)|< 1, where S(z) is a stability function. The stabilityfunction S(z) is a series in power of z which approximate the exponential solutionof a linear test equation [20].

Furthermore, in Section 3.2.1 to Section 3.2.5, we analyze each explicit Runge-Kutta method in terms of stability for solving Equation (3.2). We obtain a stabilitycondition for each explicit Runge-Kutta method in the stability analysis. In Section3.2.6, we plot the stability conditions to visualize the stability regions of the fiveexplicit Runge-Kutta methods in a complex plane. Lastly, we compare the stabilityregions to determine which explicit Runge-Kutta method has the smallest and thelargest stability region.

3.2.1 The forward Euler method

Atkinson et al. [4] describe that the forward Euler method is a first-order Runge-Kutta method and it is defined as

yj = yj−1 + hf(tj−1, yj−1).

The forward Euler method is used to compute the numerical solution yj at thediscrete time point tj. The forward Euler method is a one-stage explicit Runge-Kutta method which means that to advance the numerical solution in one timestep, then one function evaluation is needed [28].

We can now derive the stability analysis for the forward Euler method for solvingEquation (3.2) as follows

yj = yj−1 + h(−λyj−1),yj = (1− hλ)yj−1,

where yj, yj−1 are numerical solutions and 1 − hλ is a constant [28]. Furthermore,we have

yj−1 = (1− hλ)yj−2,

yj−2 = (1− hλ)yj−3,

...

y1 = (1− hλ)y0 = (1− hλ).

Consequently, we get the following numerical solution

yj = (1− hλ)j. (3.4)

According to Wang [28] the numerical solution (3.4) of Equation (3.2) obtained bythe forward Euler method is stable because the numerical solution (3.4) convergesto zero. The numerical solution (3.4) decrease in time t as j →∞. To satisfy this,therefore we need to require the following stability condition∣∣∣1− hλ∣∣∣ < 1. (3.5)

19

Page 23: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

The stability condition (3.5) constraints the value of the step size h. The forwardEuler method is stable when it satisfies the stability condition (3.5). The stabilitycondition (3.5) is an inequality less than 1, therefore we can simplify the stabilitycondition by removing the absolute value and we get

−1 < 1− hλ < 1→ 0 < hλ < 2→ 0 < h <2

λ.

The forward Euler method is stable for solving Equation (3.2) if it satisfies thestability condition h < 2

λif λ ∈ R. The inequality 0 < hλ satisfies if λ > 0

according to [28].

We can now investigate how the numerical solution of Equation (3.2) changesfor different values of hλ. Assume that we have h = 2, h = 0.1 and λ in Equation(3.2) is λ = 5. If h = 0.1 and λ = 5, then hλ = 0.5 and hλ < 2 which means thatthe forward Euler method is stable. We can say that the forward Euler method isstable when h < 2

λfor λ ∈ R. If h = 2 and λ = 5, then hλ =10 and hλ > 2, which

means that the forward Euler method is unstable, therefore the numerical solutiondiverges to infinity.

Furthermore, the forward Euler method has the following stability functionS(hλ)

S(hλ) = 1− hλ.

This stability function alternates in sign.

Wang [28] has also obtained the stability condition (3.5) for the forward Eulermethod for solving the Equation (3.2). The stability condition for the forward Eulermethod presented in [2] does not equal the stability condition (3.5) because the testequation solved in [2] is not the same as Equation (3.2). The test equation solvedin [2] is

y′(t) = λy(t), (3.6)

therefore we do not obtain the same stability condition for the forward Euler methodas in [2].

3.2.2 Heun’s method

Witty [29] describes Heun’s method is a second-order Runge-Kutta method and itis defined as

yj = yj−1 +1

2h(k1 + k2

),

k1 = f(tj−1, yj−1),

k2 = f(tj−1 + h, yj−1 + hk1).

Heun’s method is a two-stage explicit Runge-Kutta method which means that toadvance the numerical solution in one time step, then two function evaluations areneeded. Heun’s method consists of one more function evaluation than the forwardEuler method.

20

Page 24: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

We can now derive the stability analysis for Heun’s method for solving Equation(3.2) as the following

k1 = (−λ)yj−1,

k2 =(− λ+

1

2hλ2)yj−1,

yj =(

1− hλ+1

2h2λ2

)yj−1.

Consequently, we get the numerical solution as follows

yj =(

1− hλ+1

2h2λ2

)j. (3.7)

The numerical solution (3.7) decrease in time t as j →∞. To satisfy this, we requirethe following stability condition∣∣∣1− hλ+

1

2h2λ2

∣∣∣ < 1 (3.8)

If we simplify the stability condition (3.8) by removing the absolute value we get,

−1 < 1− hλ+1

2h2λ2 < 1 → 0 < hλ− 1

2h2λ2 < 2→ 0 < hλ < 2 → 0 < h <

2

λ,

where h < 2λ

is the stability condition for Heun’s method when λ ∈ R and h > 0.This stability condition is the same as the stability condition for the forward Eulermethod. The stability function of Heun’s method is

S(hλ) = 1− hλ+1

2(hλ)2.

The stability function of the forward Euler method and Heun’s method are relatedbecause the first two terms of the stability function of Heun’s method are the sameas the terms of the stability function of the forward Euler method. The stabilityfunction of Heun’s method involves three terms, whereas the stability function ofthe forward Euler method involves two terms.

3.2.3 The fourth-order Runge-Kutta method

Roslan [21] describes that the fourth-order Runge-Kutta method (RK4) is alsoknown as the classical Runge-Kutta method defined as

yj = yj−1 +1

6h(k1 + 2k2 + 2k3 + k4

),

k1 = f(tj−1, yj−1),

k2 = f(tj−1 +

1

2h, yj−1 +

1

2hk1

),

k3 = f(tj−1 +

1

2h, yj−1 +

1

2hk2

),

k4 = f(tj−1 + h, yj−1 + hk3).

RK4 is a four-stage explicit Runge-Kutta method which means that to advance thenumerical solution in one time step four function evaluations are needed. For RK4

21

Page 25: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

two more function evaluations are needed than for Heun’s method.We can now derive the stability analysis for RK4 for solving Equation (3.2) and weget

k1 = (−λ)yj−1,

k2 =(− λ+

1

2hλ2)yj−1,

k3 =(− λ+

1

2hλ2 − 1

4hλ3)yj−1,

k4 =(− λ+ hλ2 − 1

2h2λ3 +

1

4h3λ4

)yj−1,

yj =(

1− hλ+1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4

)yj−1.

Consequently, we get the following numerical solution

yj =(

1− hλ+1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4

)j, (3.9)

which decrease in time t as j →∞. To satisfy this, we require the following stabilitycondition ∣∣∣1− hλ+

1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4

∣∣∣ < 1.

The stability function of RK4 is

S(hλ) = 1− hλ+1

2(hλ)2 − 1

6(hλ)3 +

1

24(hλ)4.

This stability function involves five terms which means two more terms than theterms of the stability function of Heun’s method. The first three terms of thisstability function are also involved in the stability function of Heun’s method.

3.2.4 The fifth-order Runge-Kutta method

Gopal et al. [8] define the fifth-order Runge-Kutta method (RK5) as

yj = yj−1 +1

90h(

7k1 + 32k3 + 12k4 + 32k5 + 7k6

),

k1 = f(tj−1, yj−1),

k2 = f(tj−1 +

1

4h, yj−1 +

1

4hk1

),

k3 = f(tj−1 +

1

4h, yj−1 +

1

8hk1 +

1

8hk2

),

k4 = f(tj−1 +

1

2h, yj−1 −

1

2hk2 + hk3

),

k5 = f(tj−1 +

3

4h, yj−1 +

3

16hk1 +

9

16hk4

),

k6 = f(tj−1 + h, yj−1 −

3

7hk1 +

2

7hk2 +

12

7hk3 −

12

7hk4 +

8

7hk5

).

RK5 is a six-stage explicit Runge-Kutta method which means six function evalu-ations are needed to advance the numerical solution in one time step. We see thatRK5 consists of two more function evaluations than RK4.

22

Page 26: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

We can now derive the stability analysis for RK5 for solving the Equation (3.2)and we get

k1 = (−λ)yj−1

k2 =(− λ+

1

4hλ2)yj−1

k3 =(− λ+

1

4hλ2 − 1

32h2λ3

)yj−1

k4 =(− λ+

1

2hλ2 − 1

8h2λ3 +

1

32h3λ4

)yj−1

k5 = (−λ+12

16hλ2 − 9

32h2λ3 +

9

128h3λ4 − 9

514h4λ5

)yj−1

k6 =(− λ+ hλ2 − 1

2h2λ3 +

9

56h3λ4 − 3

112h4λ5 +

72

3584h5λ6

)yj−1.

yj =(

1− hλ+1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4 − 1

120h5λ5 +

1

640h6λ6

)yj−1.

As a consequence, we get the following numerical solution

yj =(

1− hλ+1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4

)j, (3.10)

which decrease in time t as j →∞. To satisfy this, we require the following stabilitycondition ∣∣∣1− hλ+

1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4 − 1

120h5λ5 +

1

640h6λ6

∣∣∣ < 1.

The stability function of RK5 is

S(hλ) = 1− hλ+1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4 − 1

120h5λ5 +

1

640h6λ6.

The stability function of RK5 involves seven terms which means three more termsthan the terms in the stability function of RK4. The first three terms in this stabilityfunction are also involved in the stability function of RK4 and Heun’s method butnot in the stability function of the forward Euler method.

3.2.5 The eighth-order Runge-Kutta method

The eighth-order Runge-Kutta method (RK8) is defined in [5] with Butcher tableau.The RK8 is

yj = yj−1 + h( 34

105k6 +

9

35k7 +

9

35k8 +

9

280k9 +

9

280k10 +

41

840k12 +

41

840k13

),

k1 = f(tj−1, yj−1),

k2 = f(tj−1 +

2

27h, yj−1 +

2

27hk1

),

23

Page 27: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

k3 = f

(tj−1 +

1

9h, yj−1 +

1

36h(k1 + 3k2

)),

k4 = f

(tj−1 +

1

6h, yj−1 +

1

24h(k1 + 3k3

)),

k5 = f

(tj−1 +

5

12h, yj−1 +

1

48h(

20k1 − 75k3 + 75k4

)),

k6 = f

(tj−1 +

1

2h, yn +

1

20h(k1 + 5k4 + 4k5

)),

k7 = f

(tj−1 +

5

6h, yj−1 +

1

108h(− 25k1 + 125k4 − 260k5 + 250k6

)),

k8 = f

(tj−1 +

1

6h, yj−1 + h

( 31

300k1 +

61

225k5 −

2

9k6 +

13

900k7

)),

k9 = f

(tj−1 +

2

3h, yj−1 + h

(2k1 −

53

6k4 +

704

45k5 −

107

9k6 +

67

90k7 + 3k8

)),

k10 = f

(tj−1 +

1

3h, yj−1 + h

(− 91

108k1 +

23

108k4 −

976

135k5 +

311

54k6 −

19

60k7

+17

6k8 −

1

12k9

)),

k11 = f

(tj−1 + h, yj−1 + h

(2383

4100k1 −

341

164k4 +

4496

1025k5 −

301

82k6 +

2133

4100k7,

+45

82k8 +

45

164k9 +

18

41k10

)),

k12 = f

(tj−1, yj−1 + h(

3

205k1 −

6

41k6 −

3

205k7 −

3

41k8 −

3

41k9 +

6

41k10

)),

k13 = f

(tj−1 + h, yj−1 + h

(− 1777

4100k1 −

341

164k4 +

4496

1025k5 −

289

82k6 +

2193

4100k7

+51

82k8 +

33

164k9 +

12

41k10 + k12

)).

RK8 is a thirteen-stage explicit Runge-Kutta method which means that thirteenfunction evaluations are needed to advance the numerical solution in one time step.RK8 consists of nine more function evaluations than RK5.

We can now derive the stability analysis for RK8 for solving Equation (3.2) andwe get

k1 = (−λ)yj−1

24

Page 28: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

k2 =(− λ+

2

27hλ2)yj−1,

k3 =(− λ+

1

9hλ2 − 1

162h2λ3

)yj−1,

k4 =(− λ+

1

6hλ2 − 1

72h2λ3 +

1

296h3λ4

)yj−1

k5 =(− λ+

5

12hλ2 − 25

288h2λ3 +

125

10368h3λ4 − 25

20736h4λ5

)yj−1

k6 =(− λ+

1

2hλ2 − 1

8h2λ3 +

1

48h3λ4 − 1

384h4λ5 +

5

20736h5λ6

)yj−1

k7 =(− λ+

5

6hλ2 − 25

72h2λ3 +

125

1296h3λ4 − 625

31104h4λ5 +

875

279936h5λ6

− 625

1119744h6λ7

)yj−1

k8 =(− λ+

1

6hλ2 − 1

72h2λ3 +

1

1296h3λ4 − 1

31104h4λ5 +

43

1119744h5λ6

+85

10077696h6λ7 +

325

40310784h7λ8

)yj−1,

k9 =(− λ+

2

3hλ2 − 2

9h2λ3 +

4

81h3λ4 − 2

243h4λ5 +

1655

559872h5λ6 +

4279

10077696h6λ7

+7865

20155392h7λ8 − 325

13436928h8λ9

)yj−1,

k10 =(− λ+

1

3hλ2 − 1

18h2λ3 +

1

162h3λ4 − 1

1944h4λ5 − 757

1119744h5λ6 − 439

1679616h6λ7

− 3331

20155392h7λ8 +

65

6718464h8λ9 − 325

161243136h9λ10

)yj−1,

k11 =(− λ+ hλ2 − 1

2h2λ3 +

1

6h3λ4 − 1

24h4λ5 +

14767

1700352h5λ6 − 6511

5101056h6λ7

+53

186624h7λ8 − 7151

183638016h8λ9 +

65

27205632h9λ10 − 325

367276032h10λ11

)yj−1,

k12 =(− λ− 19

566784h6λ7 − 17

45909504h7λ8 − 2081

550914048h8λ9 +

65

183638016h9λ10

− 325

1101828096h10λ11

)yj−1,

k13 =(− λ+ hλ2 − 1

2h2λ3 +

1

6h3λ4 − 1

24h4λ5 +

14767

1700352h5λ6 − 1585

1275264h6λ7

+7297

22954752h7λ8 − 599

17216064h8λ9 +

12809

2203656192h9λ10 +

65

275457024h10λ11

− 325

1101828096h11λ12

)yj−1,

yj =(

1− hλ+1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4 − 1

120h5λ5 +

1

720h6λ6 − 1

5040h7λ7

+1

40320h8λ8 − 491

209018880h9λ9 +

1333

5643509760h10λ10 +

13

501645312h11λ11

− 65

4514807808h12λ12

)yj−1.

25

Page 29: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Consequently, we get the following numerical solution

yj =(

1− hλ+1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4 − 1

120h5λ5 +

1

720h6λ6 − 1

5040h7λ7

(3.11)

+1

40320h8λ8 − 491

209018880h9λ9 +

1333

5643509760h10λ10 +

13

501645312h11λ11

− 65

4514807808h12λ12

)j,

which decrease in time t as j →∞. To satisfy this, we require the following stabilitycondition∣∣∣1− hλ+

1

2h2λ2 − 1

6h3λ3 +

1

24h4λ4 − 1

120h5λ5 +

1

720h6λ6 − 1

5040h7λ7

+1

40320h8λ8 − 491

209018880h9λ9 +

1333

5643509760h10λ10 +

13

501645312h11λ11

− 65

4514807808h12λ12

∣∣∣ < 1.

The stability function of RK8 is

S(hλ) =1− hλ+1

2(hλ)2 − 1

6(hλ)3 +

1

24(hλ)4 − 1

120(hλ)5 +

1

720(hλ)6 − 1

5040(hλ)7)

+1

40320(hλ)8 − 491

209018880(hλ)9 +

1333

5643509760(hλ)10 +

13

501645312(hλ)11

− 65

4514807808(hλ)12.

The stability function of RK8 involves six more terms than the terms of the stabilityfunction of RK5. The stability function of RK5 does also involve the first six termsof the stability function of RK8. The seventh term in this stability function of RK8does not equal the last term of the stability function of RK5 because the fraction isdifferent.

RK8 has a higher number of function evaluations than the forward Euler method,Heun’s method, RK4, RK5, and RK8. The forward Euler method has the lowestnumber of function evaluations. Soderlind [26] discusses that high-order explicitRunge-Kutta methods involving a high number of function evaluations will havehigher computational effort than the low-order explicit-Runge-Kutta methods.

Seka and Assui [24] have also done stability analysis for the forward Eulermethod, Heun’s method, and RK4 considering Equation (3.6). In their stabilityanalysis, they obtained equations that almost equal to Equation (3.4), Equation(3.7), and Equation (3.9). The equations in [24] do not alternate in sign whichEquation (3.4), Equation (3.7), and Equation (3.9) do. The reason for this is thatthe authors have solved Equation (3.6) is a nonnegative test equation which we havenot done in the stability analysis for the five explicit Runge-Kutta methods. How-ever, since the terms of Equation (3.4), Equation (3.7), and Equation (3.9) equalthe terms of the equations in [24], then we can state that Equation (3.4), Equation(3.7), and Equation (3.9) is correct. Furthermore, the first eight terms of Equation(3.11) equal the stability function of RK8 with eleven stages in [24]. The terms ofthis stability function in [24] do not alternate in sign as the terms of Equation (3.11)does. The stability analysis for RK5 and RK8 with thirteen stages is difficult to find

26

Page 30: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

-6 -4 -2 0 2 4 6

Re(h )

-6

-4

-2

0

2

4

6

Im(h

)

Forward Euler

Heun

RK4

RK5

RK8

Figure 3.1: Stability regions of the five explicit Runge-Kutta methods in a complexplane

in the literature. We did the stability analysis for RK5 (see Section 3.2.4) and RK8(see Section 3.2.5) based on the procedure of the stability analysis for the forwardEuler method.

3.2.6 Comparison of stability regions

In Section 3.2.1 to Section 3.2.5, we obtained stability conditions for the five explicitRunge-Kutta methods which are plotted in a complex plane to visualize its stabilityregions in MATLAB (see Script 1 in Appendix A). MATLAB is a software which weuse to perform numerical computation, we visualize the data in figures and tables.We run Script 1 in MATLAB and we get Figure 3.1. We see in Figure 3.1, thatthe stability region of the forward Euler method is a disk of radius 1. The forwardEuler method is centered at the point of hλ = 1. Soderlind [26] has also analyzedstability regions of the forward Euler method for solving Equation (3.6) and obtainedinstead that the forward Euler method is centered at hλ = −1 in a complex plane.The stability region of RK4, RK5, and RK8 have a shape as an ear lobe. The fiveexplicit Runge-Kutta methods are stable inside their regions and unstable outsidetheir regions.

In Figure 3.1, we see that the five explicit Runge-Kutta methods are stable onthe positive real coordinate Re(hλ) of the complex plane. The stability regions areon the right-hand side of the complex plane because the five explicit Runge-Kuttamethods are stable for values of Re(hλ) > 0, which are bounded in its stability

27

Page 31: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

regions. We can see that RK4, RK5, and RK8 have larger stability regions thanthe forward Euler method and Heun’s method. The reason is that as the order ofexplicit Runge-Kutta methods gets higher, then the explicit Runge-Kutta methodsbecome stable for more values of Re(hλ) and its stability regions get larger which isin agreement with [26]. This means that RK4, RK5, and RK8 are stable for morevalues of Re(hλ) than the forward Euler method and Heun’s method. Additionally,according to Atkinson et al. [4] explicit Runge-Kutta methods get larger stabilityregions when they are restricted on too small step sizes. The step size of h needs tobe small for hλ to be in the stability region and to obtain stable numerical methods.The numerical method with the widest stability region has better stability. Thismeans that RK8 is the most stable of all the five explicit Runge-Kutta methodsbecause it has the largest stability region. Also, the forward Euler method with thesmallest stability region is less stable than Heun’s method, RK4, RK5, and RK8.

Moreover, Butcher [6] has also plotted the stability regions of the forward Eulermethod, Heun’s method, and RK4. He obtained a similar figure as Figure 3.1 butwithout the stability regions of RK5 and RK8. He has also plotted the stabilityregions of the forward Euler method, Heun’s method, and RK4 in one complexplane as we have done in Figure 3.1. Therefore, the stability regions of the forwardEuler method, Heun’s method, and RK4 is equal to the stability regions presented in[6]. In Figure 3.1, all the stability regions of the five explicit Runge-Kutta methodsare plotted in one complex plane.

Furthermore, Seka and Assui [24] present the result of their research paper thatRK8 with eleven stages has a smaller stability region than Heun’s method and RK4.This result does not agree with what we see in Figure 3.1, where we see that RK8has the largest stability region of all the five explicit Runge-Kutta methods. Eventhough this result does not agree with what we see in Figure 3.1, this result in [24]is correct because they have investigated the stability of RK8 with eleven stages andnot thirteen stages as we have done in this thesis.

28

Page 32: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Chapter 4

Numerical experiments

This chapter presents two numerical experiments that focus on investigating theaccuracy and efficiency properties of the five explicit Runge-Kutta methods. Thefirst numerical experiment is a convergence study and the second numerical exper-iment is an efficiency study. MATLAB is also used in the convergence study andthe efficiency study to solve initial value problems numerically and to visualize theresults in figures. This is done by running the scripts mentioned in the convergencestudy and the efficiency study in MATLAB, which can be found in Appendix A.

4.1 Convergence study

In this convergence study, we measure the order of accuracy of the five explicitRunge-Kutta methods for solving an initial value problem of a first-order linearODE by verifying the convergence rate p. In this study, the following initial valueproblem of a first-order linear ODE is considered

y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, T ]. (4.1)

A similar first-order linear ODE as Equation (4.1) can be found in [10]. Equation(4.1) is chosen to be considered in this study because it is a simple initial value prob-lem with a known exact solution which enables us to compute the global truncationerror at the final time T .

The exact solution of Equation (4.1) is

y(t) = e−t2

.

In this study, we verify the convergence rate p through three steps. The first step isto compute a numerical solution yhn of Equation (4.1) with the step size h. We choosethe step sizes h, h

2, h

4and h

8, which decrease by a factor of 2 because according to

Leveque [16] it is a common way to make h small to get highly accurate numericalsolutions that we want to obtain. Additionally, other factors can certainly be usedas well. The second step is that we want to compute the following global truncationerror from [28] at the final time T

eh = |yhn − y(tn)|. (4.2)

The global truncation error eh with step size h is the difference between the thenumerical solution yhn and the exact solution y(tn). The third step is that we want

29

Page 33: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

to estimate the convergence rate p by computing three approximations of the con-vergence rate p using Equation (3.1).

We solve Equation (4.1) using the forward Euler method, Heun’s method, RK4and RK5 on the time interval t ∈ [0, T ], where T = 1 and with the step sizesh = 0.1, h

2, h

4and h

8. We solve Equation (4.1) using RK8 on t ∈ [0, T ], where

T = 1.2 and with the step sizes h = 0.4, h2, h

4and h

8. We have chosen other step

sizes h for RK8 because RK8 reaches faster to the machine precision error 10−16

which is the smallest relative error we can get by a computer. Heath [10] describesa relative error as a quotient of the global truncation error and the exact solution.Furthermore, RK8 reaches faster to the machine precision error because it has ahigh accuracy level compared to for instance the forward Euler method. In theconvergence study, we do not want to reach this limit of error because if we reachthis limit, then we will not be able to decrease the errors even more. Therefore, forRK8 we need to use step sizes h that are larger than h = 0.1, h

2, h

4and h

8just to

make sure that the computed errors using RK8 do not decrease too fast. This is whythe step sizes h = 0.4, h

2, h

4and h

8are used instead. Furthermore, the time interval

t ∈ [0, 1.2] was used instead of t ∈ [0, 1] because if we start to solve Equation (4.1)with h = 0.4, then h = 0.4 will not fit in t ∈ [0, 1]. However, it does fit in t ∈ [0, 1.2].

Script 2 is used to define Equation (4.1) which is solved using the forward Eulermethod (see Script 3), Heun’s method (see Script 4), RK4 (see Script 5), RK5 (seeScript 6) and RK8 (see Script 7). The data obtained with Script 2 to Script 7were put in a tabular format in LATEX to get Table 4.1 to Table 4.5. The resultsshown in Table 4.1 to Table 4.5 are also presented in Figure 4.1 which is obtainedby running Script 8.

h Global truncation er-rors

Approximations ofconvergence rate

0.1 1.3827×10−2 —0.05 6.5045×10−3 ≈ 1.08800.025 3.1569×10−3 ≈ 1.04300.0125 1.5554×10−3 ≈ 1.0210

Table 4.1: Global truncation errors and approximations of the convergence rate ofthe forward Euler method for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1].

h Global truncation er-ror

Approximation of con-vergence rate

0.1 1.1739×10−3 —0.05 3.0109×10−4 ≈ 1.96300.025 7.6014×10−5 ≈ 1.98600.0125 1.9085×10−5 ≈ 1.9940

Table 4.2: Global truncation errors and approximations of the convergence rate ofHeun’s method for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1].

30

Page 34: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

h Global truncation er-rors

Approximations ofconvergence rate

0.1 1.6252×10−6 —0.05 1.0253×10−7 ≈ 3.98600.025 6.4067×10−9 ≈ 4.00000.0125 3.9993×10−10 ≈ 4.0020

Table 4.3: Global truncation errors and approximations of convergence rate of RK4for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1].

h Global truncation er-rors

Approximations ofconvergence rate

0.1 2.8055×10−8 —0.05 8.3665×10−10 ≈ 5.06700.025 2.5427×10−11 ≈ 5.04010.0125 7.8292×10−13 ≈ 5.0210

Table 4.4: Global truncation errors and approximations of the convergence rate ofRK5 for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1].

h Global truncation er-rors

Approximations ofconvergence rate

0.4 1.1530×10−7 —0.2 5.2249×10−10 ≈ 7.78600.1 1.9577×10−12 ≈ 8.06000.05 7.3552×10−15 ≈ 8.0560

Table 4.5: Global truncation errors and approximations of the convergence rate ofRK8 for solving y′(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1.2].

31

Page 35: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

10-2

10-1

h

10-15

10-10

10-5

100

glo

ba

l tr

un

ca

tio

n e

rro

rs

Forward Euler

Heun

RK4

RK5

RK8

Figure 4.1: Global truncation errors in the numerical solution of y′(t) = −2ty(t),y(0) = 1, for t ∈ [0, T ] using the five explicit Runge-Kutta methods against the stepsize h on a log-log scale with logarithm of base 10.

32

Page 36: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Table 4.1 to Table 4.5 shows global truncation errors and three approximationsof the convergence rate p of the five explicit Runge-Kutta methods. As the step sizesh in Table 4.1 to Table 4.5 is halved, the global truncation errors of the five explicitRunge-Kutta methods also decrease by a factor of approximately 2p. Accordingto Leveque [16], if global truncation errors decrease by a factor of approximately2p, then for a forward Euler method which is first-order accurate with p = 1 andwe get that the global truncation errors of the forward Euler method decrease byapproximately a factor of 2 because 21 = 2. This means that we should expect thatthe five explicit Runge-Kutta methods decrease by a factor of approximately 2p. Wecan now check if the global truncation errors in Table 4.1 to Table 4.5 do decreaseby a factor of approximately 2p. We check this by investigating how much the firstglobal truncation error in the tables decreases.

In Table 4.1, the first global truncation error decreases by a factor of approxim-ately 2 because

1.3827× 10−2

6.5045× 10−3≈ 2.1260. (4.3)

From Table 4.1, we can see that as the step sizes decrease by half, the global trun-cation errors in Table 4.1 decrease by a factor of approximately 2. We can performthis division (4.3) for all global truncation errors in Table 4.1 to check how muchthey decrease. In Table 4.1, we can see that the forward Euler method has theconvergence rate p ≈ 1, therefore it is first-order accurate. In Figure 4.1, we can seethat the global truncation errors of the forward Euler method are on a line of slope1, which shows that the global truncation errors of the forward Euler method areequal to O(h) which is proportional to h.

In Table 4.2, we can see that the global truncation errors of Heun’s methoddecrease by a factor of approximately 4. The first global truncation error decreaseby a factor of approximately 4 because

1.1739× 10−3

3.0109× 10−4≈ 3.8988.

In Table 4.2, we can see that Heun’s method has the convergence rate p ≈ 2, thereforeit is second-order accurate. Heun’s method is on a line of slope 2, therefore the globaltruncation errors of Heun’s method are equal to O(h2), which is proportional to h2

(see Figure 4.1).For RK4, the global truncation errors decrease by a factor of approximately 16,

which is in agreement with [4]. According to Atkinson et al. [4] we should expectthat RK4 decrease by approximately 24 = 16. The first global truncation errordecrease by a factor of approximately 16 (see Table 4.3) as follows

1.6252× 10−6

1.0253× 10−7≈ 15.851.

From Table 4.3 we can see that RK4 is fourth-order accurate since it has the con-vergence rate p ≈ 4. In Figure 4.1, we can see that the global truncation errors ofRK4 are on a line of slope 4 and equal to O(h4) which is proportional to h4.

RK4 is more accurate than the forward Euler method and Heun’s method becausethe computed global truncation errors of RK4 are smaller than the global truncationerrors of the forward Euler method and Heun’s method. The global truncation errors

33

Page 37: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

of RK4 decrease faster than for the global truncation errors of the forward Eulermethod and Heun’s method. Ascher [2] also have computed the convergence rate ofthe forward Euler method and RK4 using formulas that are similar to the formula(3.1). The convergence rate of the forward Euler method and RK4 obtained in thissection is also obtained in [2].

In Table 4.4, we can see that for RK5 we expect that the global truncation errorsshould decrease by a factor of approximately 32 since 25 = 32. However, we get thatfirst global truncation error decrease by a factor of approximately 34 because

2.8055× 10−8

8.3665× 10−10≈ 33.5329.

The first global truncation error decreased more than what we should expect. RK5has the convergence rate p ≈ 5, therefore it is fifth-order accurate. The globaltruncation errors of RK5 are on a line of slope 5 and are equal to O(h5) which isproportional to h5 (see Figure 4.1). It is difficult to find in the literature about theconvergence rate p of RK5.

In Table 4.5, we can see that for RK8, the global truncation errors should decreaseby a factor of approximately 256 because 28 = 256, but we get that the first globaltruncation error decrease by

1.1530× 10−7

5.2249× 10−10≈ 220.6740,

which is approximately 221. This means that the first global truncation error de-creases less than 256. RK8 has the convergence rate p ≈ 8, therefore it is eighth-orderaccurate (see Table 4.5). In Figure 4.1, the global truncation errors of RK8 are on aline of slope 8 and it is equal to O(h8) which is proportional to h8. It is difficult tofind in the literature about the convergence rate p of RK8. From Table 4.1 to Table4.5 we see that the global truncation errors of the five explicit Runge-Kutta methodsdecrease differently because the order of accuracy of the five explicit Runge-Kuttamethods is different.

Moreover, Figure 4.1 shows that the five explicit Runge-Kutta methods convergeand the global truncation errors are linear with the slope of the convergence rate pof the five explicit Runge-Kutta methods on a log-log scale with the logarithm ofbase 10. A log-log scale with the logarithm of base 10 is used because accordingto Papadopoulos and Simos [19] when we measure accuracy by computing errors atthe final time point we get decimal digits which we can present in the logarithm ofbase 10. In Figure 4.1, we can see that an explicit Runge-Kutta method convergesfaster to the exact solution of Equation (4.1) as the order of accuracy of the explicitRunge-Kutta method gets higher. The forward Euler method and Heun’s methoddo have a low-order of accuracy, therefore they do not converge as fast to the exactsolution as RK4, RK5, and RK8 of high-order of accuracy. The forward Eulermethod has the lowest order of accuracy and RK8 has the highest order of accuracyof all the five explicit Runge-Kutta methods. RK8 converges faster to the exactsolution with the smallest global truncation errors, therefore it is the most accurateexplicit Runge-Kutta method. While the forward Euler method, Heun’s method,RK4, and RK5 have greater global truncation errors than RK8, therefore they areless accurate. The forward Euler method is the least accurate explicit Runge-Kuttamethod. There are smaller global truncation errors in RK5 than the forward Euler

34

Page 38: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

method, Heun’s method, and RK4, therefore RK5 is more accurate. We can see inFigure 4.1 that the slope for the forward Euler method and Heun’s method involvinggreater global truncation errors are shown in the upper part of Figure 4.1. Whilethe slope for RK4, RK5, and RK8 involving smaller global truncation errors areshown in the lower part of Figure 4.1.

4.2 Efficiency study

In this efficiency study, we investigate the efficiency of the five explicit Runge-Kuttamethods for solving an initial value problem for a system of first-order linear ODEs.This study is carried out to find which one of the five explicit Runge-Kutta methodsis most efficient for solving a system of first-order linear ODEs.

The system of first-order linear ODEs is defined as the following,

~y ′(t) = A~y(t) +~b(t), (4.4)

~y(0) = ~y0, t ∈ [0, 1].

The exact solution of Equation (4.4) is a 40401× 1 column vector at the final timepoint t =1. This column vector is the exact solution of the first 40401 elements in theinitial column vector ~y0. Also, ~y0 is a 80802×1 column vector of initial values. In thisefficiency study, we want to find the numerical solution of Equation (4.4) at the finaltime point t =1. The matrix A is a 80802×80802 matrix involving 80802×80802 realnumbers, ~y(t) is a column vector of unknown functions y1(t), · · · , yk(t) dependent

on t. Also, ~b(t) is a 80802 × 1 zero column vector of 80802 zeros and it has lengthzero. The matrix A of Equation (4.4) is already constructed and it is originallyobtained by the finite difference method for the two-dimensional wave equation(2.7) in space. Constructing this matrix A is not part of this thesis but the theoryfor how to construct this matrix A can be found in [17]. Script 9 is used to definethe Equation (4.4).

This efficiency study is carried out through two steps. The first step is to findout the smallest number of time points n such that the five explicit Runge-Kuttamethods are stable. In the first step, we want to obtain the stability limit n forthe five explicit Runge-Kutta methods. In the second step, we solve Equation (4.4)using the stability limit n and we compute global truncation errors at the final timepoint in the numerical solution of Equation (4.4) using the five explicit Runge-Kuttamethods. We are computing the global truncation errors as we increase the stabilitylimit n until the global truncation errors stop decreasing. In the second step, we alsomeasure the time required for the five explicit Runge-Kutta methods to computethe global truncation errors as the stability limit n increases.

We measure the global truncation errors at the final time point using L2 normwhich according to Heath [10] is a vector norm. Vector norms are used to measureerrors and there are different types of vector norms that are useful in differentcontexts. There are m-norms of a vector ~x, where the integer m is m > 0. If m = 1,then we have a L1 norm which is used to measure the magnitude of the distancebetween two points. If m = 2, then we have L2 norm which computes the magnitudeof the difference between the numerical solution and the exact solution at a timepoint. If m =∞, then we have a ∞-norm which is used to compute the maximumerror over a time interval.

35

Page 39: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Assume that we have an global truncation error vector defined by

~en = ~yn − ~y(tn),

where ~yn is the numerical solution vector and ~y(tn) is the exact solution vector atthe final time point tn. Moreover, a L2 norm of ~en at the final time point tn is

‖~en‖2= ‖~yn − ~y(tn)‖2, (4.5)

which we use in Section 4.2.2. A similar global truncation error as Equation (4.5)can be found in [16].

4.2.1 Stability limits

In this section, we want to find when the five explicit Runge-Kutta methods arestable because we do not know at what n we should start to solve Equation (4.4).Therefore, we solve Equation (4.4) using different values of n and solve Equation(4.4) in order to check when the five explicit Runge-Kutta methods become stable.We run the MATLAB codes in Script 10, Script 12, Script 14, Script 16, and Script18 to obtain the stability limit n of the forward Euler method, Heun’s method,RK4, RK5, and RK8, respectively. We run the MATLAB codes several times untilthe five explicit Runge-Kutta methods get started to become stable, which is whenthe numerical solution converges to the exact solution. It is given that the exactsolution of the Equation (4.4) is a magnitude near 5. As a result, we obtain differentvalues of n for which the five explicit Runge-Kutta methods are stable and unstableas presented in Table 4.6. Table 4.6 shows the stability limits of the five explicit

Method Stable UnstableForward Euler n ≥ 22000 n ≤ 21800Heun n ≥ 2100 n ≤ 1980RK4 n ≥ 435 n ≤ 430RK5 n ≥ 580 n ≤ 560RK8 n ≥ 328 n ≤ 300

Table 4.6: Stability limit n of the five explicit Runge-Kutta methods for ~y ′(t) =

A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1].

Runge-Kutta methods for solving Equation (4.4). We can determine that an explicitRunge-Kutta method is stable when the numerical solution move closer to the exactsolution. Also, if the numerical solution does not move closer to the exact solution,then the explicit Runge-Kutta method is unstable. We can see that when n ≥ 22000the forward Euler method is stable but unstable when n ≤ 21800. This means thatfor the forward Euler method it takes n ≥ 22000 number of time points in order tobe stable for solving Equation (4.4). Also, n ≤ 21800 is the number of time pointsfor which the forward Euler method is unstable for solving Equation (4.4). Heun’smethod is stable when n ≥ 2100, but unstable when n ≤ 1980. RK4 is stable whenn ≥ 435, but unstable when n ≤ 430. RK5 is stable when n ≥ 580 but unstablewhen n ≤ 560. RK8 is stable when n ≥ 328 and unstable when n ≤ 300.

36

Page 40: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

When the forward Euler method, Heun’s method, RK4, RK5, and RK8 is stable,then the numerical solution of Equation (4.4) obtained by these five explicit Runge-Kutta methods converge to the exact solution and bound the exact solution. Whenthe five explicit Runge-Kutta methods are unstable, the numerical solution divergesfrom the exact solution and the numerical solution unbound the exact solution. Theforward Euler method and Heun’s method have the highest n number of time pointsin order to be stable. This means that it takes more time steps for the forwardEuler method and Heun’s method to converge to the exact solution than for theRK4, RK5, and RK8. These results are in agreement with what Heath [10] definesa stable and unstable numerical method as discussed in Section 3.1.2.

4.2.2 Time-error efficiency

In this efficiency study, we want to find out the computational time required for thefive explicit Runge-Kutta methods to compute the global truncation errors at thefinal time point tn using the stability limit n in Table 4.6. We also compute the globaltruncation errors in the numerical solution of Equation (4.4) at increasing stabilityn using Equation (4.5) and measure the computational time of the global truncationerrors. We run the MATLAB codes in Script 11, Script 13, Script 15, Script 17,and Script 19 to compute the global truncation errors and the computational time.The data obtained by running these scripts were plotted in tables and then the datain the tables were visualized using figures in MATLAB. This section presents twofigures for each explicit Runge-Kutta method. The first figure visualizes the globaltruncation errors at increasing values of the stability limit n. The second figurevisualizes the computational time in seconds.

Moreover, the results of the global truncation errors and the computational timeare presented in Figure 4.2 to Figure 4.11. We can see from Figure 4.2 to Figure

0 1 2 3 4 5 6

n 105

0

0.02

0.04

0.06

0.08

0.1

0.12

0.14

0.16

0.18

0.2

Glo

ba

l tr

un

ca

tio

n e

rro

r

Figure 4.2: Global truncation errors inthe numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computedat increasing stability n number of timepoints using the forward Euler method.

0 500 1000 1500 2000 2500 3000 3500 4000 4500

Time(second)

0

0.02

0.04

0.06

0.08

0.1

0.12

0.14

0.16

0.18

0.2

Glo

ba

l tr

un

ca

tio

n e

rro

r

Figure 4.3: Time (seconds) required forthe forward Euler method to computeglobal truncation errors in the numericalsolution of the system of ODEs.

4.11 that all the five explicit Runge-Kutta methods are stable because the globaltruncation errors decrease asymptotically. This is because as the stability limit n

37

Page 41: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

0 0.5 1 1.5 2 2.5 3

n 105

0

0.2

0.4

0.6

0.8

1

1.2

1.4

Glo

ba

l tr

un

ca

tio

n e

rro

r

10-3

Figure 4.4: Global truncation errors inthe numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computedat increasing stability n number of timepoints using Heun’s method.

0 500 1000 1500 2000 2500

Time(second)

0

0.2

0.4

0.6

0.8

1

1.2

1.4

Glo

ba

l tr

un

ca

tio

n e

rro

r

10-3

Figure 4.5: Time (seconds) required forthe Heun’s method to compute globaltruncation errors in the numerical solu-tion of the system of ODEs.

400 500 600 700 800 900 1000

n

6.5

7

7.5

8

8.5

9

9.5

10

Glo

ba

l tr

un

ca

tio

n e

rro

r

10-7

Figure 4.6: Global truncation errors inthe numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computedat increasing stability n number of timepoints using RK4.

4 6 8 10 12 14 16 18

Time(second)

6.5

7

7.5

8

8.5

9

9.5

10

Glo

ba

l tr

un

ca

tio

n e

rro

r

10-7

Figure 4.7: Time (seconds) required forRK4 to compute global truncation errorsin the numerical solution of the system ofODEs.

increases, then the global truncation errors in the numerical solution at the final timedecrease. This asymptotic behavior is in agreement with what Heath [10] describeshow stable numerical methods should behave. From Figure 4.2 to Figure 4.11 wealso see that the maximum accuracy of RK4, RK5, and RK8 is 10−7. RK4, RK5,and RK8 produce smaller global truncation errors than the forward Euler methodand Heun’s method. The forward Euler method and Heun’s method produce greaterglobal truncation errors. Both the forward Euler method and Heun’s method havethe maximum accuracy 10−3. Therefore, we can say that Rk4, RK5, and RK8 is moreaccurate than the forward Euler method and Heun’s method. The forward Eulermethod and Heun’s method are the least accurate explicit Runge-Kutta methods

38

Page 42: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

580 590 600 610 620 630 640 650

n

6.55

6.6

6.65

6.7

6.75

6.8

6.85G

lob

al tr

un

ca

tio

n e

rro

r10

-7

Figure 4.8: Global truncation errors inthe numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computedat increasing stability n number of timepoints using RK5.

13.5 14 14.5 15 15.5 16 16.5 17 17.5 18

Time(second)

6.55

6.6

6.65

6.7

6.75

6.8

6.85

Glo

ba

l tr

un

ca

tio

n e

rro

r

10-7

Figure 4.9: Time (seconds) required forRK5 to compute global truncation errorsin the numerical solution of the system ofODEs.

328 329 330 331 332 333 334 335

n

6.545

6.55

6.555

6.56

6.565

6.57

6.575

6.58

6.585

6.59

6.595

Glo

ba

l tr

un

ca

tio

n e

rro

r

10-7

Figure 4.10: Global truncation errors inthe numerical solution of ~y ′(t) = A~y(t) +~b(t), ~y(0) = ~y0, for t ∈ [0, 1] computedat increasing stability n number of timepoints using RK8.

20.5 21 21.5 22 22.5 23

Time(second)

6.545

6.55

6.555

6.56

6.565

6.57

6.575

6.58

6.585

6.59

6.595

Glo

ba

l tr

un

ca

tio

n e

rro

r

10-7

Figure 4.11: Time (seconds) required forRK8 to compute global truncation errorsin the numerical solution of the system ofODEs.

investigated in this thesis. From this accuracy analysis, we can say that the RK4,RK5, and RK8 give more accurate results in the numerical solution which accordingto Soderlind [26] it is because they are higher-order explicit Runge-Kutta methods.

The results that are shown in Figure 4.2 and Figure 4.3 are obtained by runningScript 11 in MATLAB. Figure 4.2 shows that when n ≥ 22000 increases the globaltruncation errors decrease. Also, when n ≥ 22000 increases, it takes more timefor the forward Euler method to compute the global truncation errors as shown inFigure 4.3. Due to this high computational time, we stopped to increase the stabilitylimit n of the forward Euler method at the stability limit n = 600000, which takesapproximately 1 hour and 8 minutes for the forward Euler method to compute.

39

Page 43: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Furthermore, the results that are shown in Figure 4.4, and Figure 4.5 are obtainedby running Script 13 in MATLAB. For Heun’s method, we stopped to increase thestability limit n at the stability limit n =300000 because it takes approximately 40minutes for Heun’s method to compute the global truncation errors which are highcomputational time. It also took too high computational time for the forward Eulermethod, therefore we can state that both the forward Euler method and Heun’smethod is inefficient. In Figure 4.4, Heun’s method in the beginning when weincrease the stability limit n, then the global truncation errors decrease fast. Theglobal truncation errors of Heun’s method start to be a constant at the stabilitylimit n = 7000. As a consequence, we see in Figure 4.4 that the global truncationerrors become flat because the stability limit n = 7000 is large. Consequently, theglobal truncation errors of Heun’s method start to oscillate, which means that as thestability limit n increases, then the global truncation errors can decrease or increasea little bit but it will still be close to the previously computed global truncationerror.

The results that are shown in Figure 4.6 and Figure 4.7 are obtained by runningScript 15 in MATLAB. In Figure 4.6, we can see that at the stability limit n =1000the global truncation errors of RK4 stop decreasing. Also, the results that are shownin Figure 4.8 and Figure 4.9 are obtained by running the Script 17 in MATLAB. InFigure 4.8, we see that at the stability limit n = 700 the global truncation errorsof RK5 stop decreasing. Furthermore, the results that are shown in Figure 4.10and Figure 4.11 are obtained by running the Script 19 in MATLAB. The globaltruncation errors of RK8 stop decreasing at the stability limit n = 700.

In Figure 4.11, we can, for example, take the global truncation error of mag-nitude 6.565 × 10−7, which takes approximately 21.4 seconds for RK8 to computethis global truncation error. If we want to obtain this global truncation error usingRK5 it will take approximately 13.8 seconds (see Figure 4.9), this means that lesscomputational time is required for RK5. Therefore, RK5 is more efficient to com-pute this global truncation error than RK8. In Figure 4.7, we can see that for RK4,it takes approximately 12.5 seconds to compute this global truncation error, there-fore it takes less computational time for RK4 than RK5 and RK8 to compute theglobal truncation error of magnitude 6.565× 10−7. We can easily compare the com-putational time required for RK4, RK5, and RK8 to compute the global truncationerrors because the smallest global truncation errors of RK4, RK5 and RK8 are ofthe same magnitude 10−7 as we see in Figure 4.7, Figure, 4.9 and Figure 4.11. Thesmallest global truncation errors of the forward Euler method and Heun’s methodare of the same magnitude 10−3. This means that the global truncation errors ofthe magnitude of the forward Euler method and Heun’s method are greater thanRK4, RK5, and RK8. This means that to get the same global truncation errors ofmagnitude 10−7 using the forward Euler method and Heun’s method, then we haveto run the Script 11 and Script 13 with much smaller step size. It takes high compu-tational time for the forward Euler method and Heun’s method to obtain the globaltruncation errors of magnitude 10−7, therefore we did not obtain these results.

Moreover, Anidu et al. [1] describe that even though high-order Runge-Kuttamethods such as RK4 require more computational effort than low-order Runge-Kutta methods, it is more efficient than low-order Runge-Kutta methods as theforward Euler method. This efficiency is shown in this section because it tookmore function evaluations to be computed per time step for RK4, RK5, and RK8.

40

Page 44: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

However, RK4, RK5, and RK8 have shown to be more efficient to solve Equation(4.4) than the forward Euler method and Heun’s method, where fewer functionevaluations are required per time step.

41

Page 45: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Chapter 5

Conclusion

In this thesis, we have compared the performance of the forward Euler method,Heun’s method, RK4, RK5, and RK8 in terms of accuracy, stability, and efficiency.We did this through the stability analysis, the convergence study, and the efficiencystudy. We can conclude from the stability analysis that the forward Euler method,Heun’s method, RK4 and RK5 have smaller stability regions than the stability re-gion of RK8. The forward Euler method has the smallest stability region and RK8has the largest stability region. Therefore, RK8 has shown to have better stabilityproperty than the forward Euler method, Heun’s method, RK4, and RK5. There-fore, RK8 is the most stable explicit Runge-Kutta method for solving Equation(3.2). From the convergence study, RK8 has shown to be the most accurate ex-plicit Runge-Kutta method for solving Equation (4.1). This is because RK8 hasa higher convergence rate p, therefore produces more accurate numerical solutionsof Equation (4.1) compared with the forward Euler method, Heun’s method, RK4,and RK5 which are the less accurate explicit Runge-Kutta methods. The forwardEuler method is the least accurate explicit Runge-Kutta method investigated in thisthesis. Furthermore, from the efficiency study, we can state that the forward Eulermethod and Heun’s method are inefficient explicit Runge-Kutta methods. RK5 andRK8 are less efficient compared with RK4 because RK4 requires less computationaltime to compute global truncation errors in the numerical solution of Equation (4.4)than RK5 and RK8. RK5 and RK8 computed the global truncation errors moreefficiently than the forward Euler method and Heun’s method. RK4 has shown tobe the most efficient explicit Runge-Kutta method for solving Equation (4.4).

Some of the results shown in the stability analysis, the convergence study, andthe efficiency study did correspond to previous literature. It was difficult to findprevious literature about the stability analysis and convergence of RK5 and RK8 aswell as the efficiency properties of the five explicit Runge-Kutta methods for solvinga system of ODEs. This means that the results shown in the stability analysis andthe convergence study concerning RK5 and RK8 are new results that are derivedin this thesis. This also means that the efficiency properties of the five explicitRunge-Kutta methods shown in the efficiency study are also new results derived inthis thesis.

In the convergence study and the efficiency study, we did some modifications.One modification we did in the convergence study was to change the step sizes andthe final time point, to be able to run the MATLAB code in Script 7 in Appendix A.Another modification we did, was in the efficiency study, where we for the forward

42

Page 46: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Euler method and Heun’s method stopped increasing the stability limit n becauseit took high computational time for these two explicit Runge-Kutta methods tocompute the global truncation errors. For future work, we want to know whetherit is always the case that one must do modifications for some explicit Runge-Kuttamethods to be able to run MATLAB codes that belong to explicit Runge-Kuttamethods.

43

Page 47: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Bibliography

[1] A. O. Anidu, S. A. Arekete, A. O. Adedayo, and A. O. Adekoya. Dynamiccomputation of Runge-Kutta fourth-order algorithm for first- and second-orderordinary differential equation using java. International Journal of ComputerScience, 12(13):211–218, 2015.

[2] U. M. Ascher and L. R. Petzold. Computer methods for ordinary differentialequations and differential-algebraic equations. Siam, 1998.

[3] R. Ashino, M. Nagase, and R. Vaillancourt. Behind and beyond the matlabode suite. Computers & Mathematics with Applications, 40(4-5):491–512, 2000.

[4] K. Atkinson, W. Han, and D. E. Stewart. Numerical solution of ordinarydifferential equations. John Wiley & Sons, 2011.

[5] S. Bu, W. Jung, and P. Kim. An error embedded Runge-Kutta method forinitial value problems. Kyungpook Mathematical Journal, 56(2):311–327, 2016.

[6] J. C. Butcher. A history of Runge-Kutta methods. Applied numerical math-ematics, 20(3):247–260, 1996.

[7] V. Chauhan and P. K. Srivastava. Computational techniques based on Runge-Kutta method of various order and type for solving differential equations. In-ternational Journal of Mathematical, Engineering and Management Sciences,4(2):375–386, 2019.

[8] D. Gopal, V. Murugesh, and K. Murugesan. Numerical solution of second-orderrobot arm control problem using Runge-Kutta butcher algorithm. InternationalJournal of Computer Mathematics, 83(3):345–356, 2006.

[9] A. Hasan. Numerical computation of initial value problem by various tech-niques. Journal of Science and Arts, 18(1):19–32, 2018.

[10] M. T. Heath. Scientific computing: an introductory survey. McGraw-Hill, 2002.

[11] D. Houcque. Applications of matlab: ordinary differential equations (ode).Robert R. McCormick School of Engineering and Applied Science-NorthwesternUniversity, Evanston, 2008.

[12] Md. A. Islam. A comparative study on numerical solutions of initial valueproblems (ivp) for ordinary differential equations (ode) with euler and Runge-Kutta methods. American Journal of Computational Mathematics, 5(3):393–404, 2015.

44

Page 48: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

[13] S. M. Jung. Hyers-ulam stability of a system of first order linear differentialequations with constant coefficients. Journal of Mathematical Analysis andApplications, 320(2):549–561, 2006.

[14] D. Ketcheson and A. Ahmadia. Optimal stability polynomials for numericalintegration of initial value problems. Communications in Applied Mathematicsand Computational Science, 7(2):247–271, 2013.

[15] S. Larsson and V. Thomee. Partial differential equations with numerical meth-ods. Springer Science & Business Media, 2008.

[16] R. J. Leveque. Finite difference methods for ordinary and partial differentialequations: steady-state and time-dependent problems. Siam, 2007.

[17] K. Mattsson, F. Ham, and G. Iaccarino. Stable boundary treatment for the waveequation on second-order form. Journal of Scientific Computing, 41(3):366,2009.

[18] D. Omale, P. B. Ojih, and M. O. Ogwo. Mathematical analysis of stiff andnon-stiff initial value problems of ordinary differential equation using matlab.International journal of scientific & engineering research, 5(9):49–59, 2014.

[19] D. F. Papadopoulos and T. E Simos. The use of phase lag and amplifica-tion error derivatives for the construction of a modified Runge-Kutta Nystrommethod. In Abstract and Applied Analysis. Hindawi, 2013.

[20] J. S. C. Prentice. Stepsize selection in explicit Runge-Kutta methods for mod-erately stiff problems. Applied Mathematics, 2(6):711–717, 2011.

[21] U. A. M. Roslan, Z. Salleh, and A. Kılıcman. Solving zhou chaotic system usingfourth-order Runge-Kutta method. World Applied Sciences Journal, 21(6):939–944, 2013.

[22] M. Schafer. Computational engineering: introduction to numerical methods.Springer, 2006.

[23] W. E. Schiesser and G. W. Griffiths. A compendium of partial differentialequation models: method of lines analysis with Matlab. Cambridge UniversityPress, 2009.

[24] H. Seka and K. R. Assui. Order of the Runge-Kutta method and evolution ofthe stability region. Ural Mathematical Journal, 5(2):64–71, 2019.

[25] M. M. Stabrowski. An efficient algorithm for solving stiff ordinary differentialequations. Simulation Practice and Theory, 5(4):333–344, 1997.

[26] G. Soderlind. Numerical methods for differential equations. Springer, 2017.

[27] P. J. Van der Houwen. The development of Runge-Kutta methods for partialdifferential equations. Applied Numerical Mathematics, 20(3):261–272, 1996.

[28] S. Wang. Numerical methods for ordinary differential equations. 2020.

45

Page 49: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

[29] W.H. Witty. A new method of numerical integration of differential equations.Mathematics of Computation, 18(87):497–500, 1964.

[30] A. S. Wusu, M. A. Akanbi, and S. A. Okunuga. A three-stage multiderivativeexplicit Runge-Kutta method. American Journal of Computational Mathemat-ics, 3(2):121–126, 2013.

[31] W.Y. Yang, W. Cao, J. Kim, K. W. Park, H. H. Park, J. Joung, J. S. Ro, H. L.Lee, C. H. Hong, and T. Im. Applied numerical methods using MATLAB. JohnWiley & Sons, 2020.

46

Page 50: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Appendix AMATLAB code

Appendix A presents all scripts involving MATLAB code which were constructedto be used in this thesis.

A.1 Script 1

This script is used to plot all stability conditions for the five explicit Runge-Kuttamethods to get the stability regions in a complex plane presented in Section 3.2.6.

1 % This solver generates the stability regions of the

2 % five explicit Runge -Kutta methods in a complex plane

3 % with a real axis Re(h\lambda) and an imaginary axis

4 % Im(h\lambda).

5 % Z=x+1i*y, where Z is a complex number , x is a real

6 % number , i is an imaginary unit and y is an imaginary

7 % number.

8 % R1 is a stability condition for the forward Euler

9 % method.

10 % R2 is a stability condition for Heun 's method.

11 % R3 is a stability condition for RK4.

12 % R4 is a stability condition for RK5.

13 % R5 is a stability condition for RK8.

14 close all

15 clear all

16 clf , hold on

17 % A meshgrid command transforms the domain that are

18 % defined by the vector x and y to X and Y matrices.

19 [X,Y] = meshgrid ( -6:0.01:6 , -6:0.01:6);

20 % [X,Y] = meshgrid (x,y), where x = linspace ( -6:0.01:6)

21 % and y = linspace ( -6:0.01:6).

22 % x = linspace ( -6:0.01:6) , where linspace generate points

23 % from x = -6 to x = 6 with h = 0.1.

24 % y = linspace ( -6:0.01:6) , where linspace generate points

25 % from y = -6 to y = 6 with h = 0.1.

26 Z = X+1i*Y;

27 R1 = abs(1 - Z);

28 R2 = abs(1 - Z + 1/2*Z.^2);

29 R3 = abs(1 - Z + 1/2*Z.^2 - 1/6*Z.^3 + 1/24*Z.^4);

47

Page 51: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

30 R4 = abs(1 - Z + 1/2*Z.^2 - 1/6*Z.^3 + 1/24*Z.^4 - 1/120*

Z.^5 + 1/640*Z.^6);

31 R5 = abs(1 - Z + 1/2*Z.^2 - 1/6*Z.^3 + 1/24*Z.^4 - 1/120*

Z.^5 + 1/720*Z.^6- 1/5040*Z.^7 + 1/40320*Z.^8 -

491/209018880*Z.^9 + 1333/5643509760*Z.^10 +

13/501645312*Z.^11- 65/4514807808*Z.^12);

32 % The command contour is used to plot the line curve of

33 % the stability regions with different colors.

34 % The x-axis and y-axis have a linewidth of 2 points.

35 contour(X,Y,R1 ,[1,1],'k','linewidth ' ,2);36 contour(X,Y,R2 ,[1,1],'r','linewidth ' ,2);37 contour(X,Y,R3 ,[1,1],'g','linewidth ' ,2);38 contour(X,Y,R4 ,[1,1],'m','linewidth ' ,2);39 contour(X,Y,R5 ,[1,1],'c','linewidth ' ,2);40 title('Stability region of forward Euler , Heun , RK4 , RK5 ,

RK8')41 legend('Forward Euler','Heun', 'RK4', 'RK5','RK8')42 xlabel('Re(h\lambda)')43 ylabel('Im(h\lambda)')44 axis square , grid on

A.2 Script 2

Script 2 is used to define the first-order ODE which is presented in Section 4.1.

1 function dy = fun(t,y)

2 dy = -2*t*y;

3 end

4 % t is an independent time variable.

5 % y is a dependent variable.

A.4 Script 3

Script 3 generates the estimated convergence rate of the forward Euler method usedin Section 4.1.

1 % This solver attempts to compute three approximations

2 % of the convergence rate of the forward Euler method

3 % with the step sizes h=0.1, h/2, h/4 and h/8.

4 format long % A format function to get 15 digits.

5 t0 = 0; % Initial time.

6 t(1) = 1; % Final time.

7 h = 0.1; % Step size h.

8 n = (t(1)-t0)/h; % n is the number of time steps.

9 t = t0:h:t(1)-h; % t is the time interval.

10 y0 = 1; % Initial condition.

11 y(1) = y0; % The numerical solution at the final time.

48

Page 52: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

12 % Use the forward Euler method to find the numerical

13 % solution at y(1) which is the next time step after

14 % the initial value y0.

15 % The for loop is repetition of a number amount of times.

16 % for i reads for index i which start from 1 to n.

17 for i = 1:n % Time stepping loop

18 y(i+1) = y(i)+ h*fun(t(i), y(i));

19 end

20 % Exact solution.

21 yExact = exp(-(1) .^2)

22 % Global truncation error at the final time t = 1.

23 Error = abs(y(i+1)-yExact)

24 % Estimated convergence rate.

25 % ((log (0.013827239387109/0.006504577699395))/log (2)) =

1.087985850558898

26 % ((log (0.006504577699395/0.003156961521126))/log (2)) =

1.042918718670773

27 % ((log (0.003156961521126/0.001555416487666))/log (2)) =

1.021235739577236

A.5 Script 4

In this script, the convergence rate of Heun’s method is estimated used in Section4.1.

1 % This solver attempts to compute three approximations

2 % of the convergence rate of Heun 's method

3 % with the step sizes h=0.1, h/2, h/4 and h/8.

4 format long

5 t0 = 0;

6 t(1) = 1;

7 h = 0.1;

8 n = (t(1)-t0)/h;

9 t = t0:h:t(1)-h;

10 y0 = 1;

11 y(1) = y0;

12 % Use Heun 's method.

13 for i = 1:n

14 k1 = fun(t(i),y(i));

15 k2 = fun(t(i)+h,y(i)+h*k1);

16 y(i+1) = y(i)+(1/2)*h*(k1+k2);

17 end

18 % Exact solution.

19 yExact = exp(-(1) .^2)

20 % Global truncation error.

21 Error = abs(y(i+1)-yExact)

22 % Estimated convergence rate.

49

Page 53: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

23 % ((log (0.001173953098629/3.010909612657375e-04))/log(2))

= 1.963103467343805

24 % ((log (3.010909612657375e -04/7.601465913853467e-05))/log

(2)) = 1.985849830323583

25 % ((log (7.601465913853467e -05/1.908536287215101e-05))/log

(2)) = 1.993811046712535

A.6 Script 5

This script is used in Section 4.1 to generate an estimation of the convergence rateof RK4.

1 % This solver attempts to compute three approximations

2 % of the convergence rate of RK4 with the step sizes

3 % h=0.1, h/2, h/4 and h/8.

4 format long

5 t0 = 0;

6 t(1) = 1;

7 h = 0.1;

8 n = (t(1)-t0)/h;

9 t = t0:h:t(1)-h;

10 y0 = 1;

11 y(1) = y0;

12 % Use RK4.

13 for i = 1:n

14 k1 = fun(t(i),y(i));

15 k2 = fun(t(i)+(1/2)*h,y(i)+(1/2)*h*k1);

16 k3 = fun(t(i)+(1/2)*h,y(i)+(1/2)*h*k2);

17 k4 = fun(t(i)+h,y(i)+h*k3);

18 y(i+1) = y(i)+(1/6)*h*(k1+2*k2+2*k3+k4);

19 end

20 % Exact solution.

21 yExact = exp(-(1) .^2)

22 % Global truncation error.

23 Error = abs(y(i+1)-yExact)

24 % Estimated convergence rate.

25 % ((log (1.625254322568104e -06/1.025354282591096e-07))/log

(2)) = 3.986471107147980

26 % ((log (1.025354282591096e -07/6.406794095248358e-09))/log

(2)) = 4.000376043507966

27 % ((log (6.406794095248358e -09/3.999351960715103e-10))/log

(2)) = 4.001764469781143

A.7 Script 6

Script 5 is used in Section 4.1 to estimate the convergence rate of RK5.

50

Page 54: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

1 % This solver attempts to compute three approximations

2 % of the convergence rate of RK5 with the step sizes

3 % h=0.1, h/2, h/4 and h/8.

4 format long

5 t0 = 0;

6 t(1) = 1;

7 h = 0.1;

8 n = (t(1)-t0)/h;

9 t = t0:h:t(1)-h;

10 y0 = 1;

11 y(1) = y0;

12 % Use RK5.

13 for i = 1:n

14 k1 = fun(t(i),y(i));

15 k2 = fun(t(i)+(1/4)*h,y(i)+(1/4)*h*k1);

16 k3 = fun(t(i)+(1/4)*h,y(i)+(1/8)*h*k1 +(1/8)*h*k2);

17 k4 = fun(t(i)+(1/2)*h,y(i) -(1/2)*h*k2+h*k3);

18 k5 = fun(t(i)+(3/4)*h,y(i)+(3/16)*h*k1 +(9/16)*h*k4);

19 k6 = fun(t(i)+h,y(i) -(3/7)*h*k1 +(2/7)*h*k2 +(12/7)*h*

k3 -(12/7)*h*k4 +(8/7)*h*k5);

20 y(i+1) = y(i)+(1/90)*h*(7*k1+32*k3+12*k4+32*k5+7*k6);

21 end

22 % Exact solution.

23 yExact = exp(-(1) .^2)

24 % Global truncation error.

25 Error = abs(y(i+1)-yExact)

26 % Estimated convergence rate.

27 % ((log (2.805542859141141e -08/8.366583537089411e-10))/log

(2)) = 5.067497518285532

28 % ((log (8.366583537089411e -10/2.542765997759489e-11))/log

(2)) = 5.040168017077482

29 % ((log (2.542765997759489e -11/7.829292769656604e-13))/log

(2)) = 5.021372898661645

A.8 Script 7

In this script, the convergence rate of RK8 is estimated used in Section 4.1.

1 % This solver attempts to compute three approximations

2 % of the convergence rate of RK8 with the step sizes

3 % h=0.4, h/2, h/4 and h/8.

4 format long

5 t0 = 0;

6 t(1) = 1.2;

7 h = 0.4;

8 n = round((t(1)-t0)/h);

9 % The round function round n to one digit.

51

Page 55: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

10 t = t0:h:t(1)-h;

11 y0 = 1;

12 y(1) = y0;

13 % Use RK8.

14 for i = 1:n

15 k1 = fun(t(i),y(i));

16 k2 = fun(t(i)+(2/27)*h,y(i)+(2/27)*h*k1);

17 k3 = fun(t(i)+(1/9)*h,y(i)+(1/36)*h*(k1+3*k2));

18 k4 = fun(t(i)+(1/6)*h, y(i)+(1/24)*h*(k1+3*k3));

19 k5 = fun(t(i)+(5/12)*h,y(i)+(1/48)*h*(20*k1 -75*k3+75*

k4));

20 k6 = fun(t(i)+(1/2)*h,y(i)+(1/20)*h*(k1+5*k4+4*k5));

21 k7 = fun(t(i)+(5/6)*h, y(i)+(1/108)*h*(-25*k1 +125*k4

- 260*k5 + 250*k6));

22 k8 = fun(t(i)+(1/6)*h, y(i)+h*((31/300)*k1 + (61/225)

*k5 - (2/9)*k6 + (13/900)*k7));

23 k9 = fun(t(i)+(2/3)*h, y(i)+h*(2*k1 - (53/6)*k4 +

(704/45)*k5 - (107/9)*k6 + (67/90)*k7 + 3*k8));

24 k10 = fun(t(i)+(1/3)*h, y(i)+h*( -(91/108)*k1 +

(23/108)*k4 - (976/135)*k5 + (311/54)*k6 - (19/60)

*k7 + (17/6)*k8 - (1/12)*k9));

25 k11 = fun(t(i)+h,y(i)+h*((2383/4100)*k1 - (341/164)*

k4 + (4496/1025)*k5 - (301/82)*k6 + (2133/4100)*k7

+ (45/82)*k8 + (45/164)*k9 + (18/41)*k10));

26 k12 = fun(t(i),y(i)+h*((3/205)*k1 - (6/41)*k6 -

(3/205)*k7 - (3/41)*k8 + (3/41)*k9 + (6/41)*k10));

27 k13 = fun(t(i)+h,y(i)+h*( -(1777/4100)*k1 - (341/164)*

k4 + (4496/1025)*k5 - (289/82)*k6 + (2193/4100)*k7

+ (51/82)*k8 + (33/164)*k9 + (12/41)*k10 + k12));

28 y(i+1) = y(i)+ h*((34/105)*k6 + (9/35)*k7 + (9/35)*k8

+ (9/280)*k9 + (9/280)*k10 + (41/840)*k12+

(41/840)*k13);

29 end

30 % Exact solution.

31 yExact = exp ( -(1.2) .^2)

32 % Global truncation error at the final time t = 1.2.

33 Error = abs(y(i+1)-yExact)

34 % Estimated convergence rate.

35 %((log (1.153056458369495e -07/5.224903287803784e-10))/log

(2)) = 7.785843106276388

36 %((log (5.224903287803784e -10/1.957739526048385e-12))/log

(2)) = 8.060071693580385

37 %((log (1.957739526048385e -12/7.355227538141662e-15))/log

(2)) = 8.056203141461742

52

Page 56: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

A.9 Script 8

Script 8 is used for Section 4.1 to present how to plot a log-log scale with base 10where global truncation errors at the final time obtained from Script 3 to Script 7is a function of the step size h for the five explicit Runge-Kutta methods.

1 % The step sizes in h1 is used to plot the

2 % global truncation errors of the forward Euler

3 % method , Heun 's method , RK4 and RK5.

4 h1 =[0.1 ,0.05 ,0.025 ,0.0125];

5 % The step sizes in h2 is used to plot the

6 % global truncation error of RK8.

7 h2=[0.4 ,0.2 ,0.1 ,0.05];

8 % Error1 is global truncation errors of the forward

9 % Euler method.

10 error1 = [0.013827239387109 ,0.006504577699395 ,

0.003156961521126 ,0.001555416487666];

11 % Error2 is global truncation errors of Heun 's method.

12 error2 =[0.001173953098629 ,3.010909612657375e

-04 ,7.601465913853467e -05 ,1.908536287215101e -05];

13 % Error3 is global truncation errors of RK4.

14 error3 =[1.625254322568104e -06 ,1.025354282591096e

-07 ,6.406794095248358e -09 ,3.999351960715103e -10];

15 % Error4 is global truncation errors of RK5.

16 error4 =[2.805542859141141e -08 ,8.366583537089411e

-10 ,2.542765997759489e -11 ,7.829292769656604e -13];

17 % Error5 is global truncation errors of RK8.

18 error5 =[1.153056458369495e -07 ,5.224903287803784e

-10 ,1.957739526048385e -12 ,7.355227538141662e -15];

19 % Plot the the step sizes in h1 and h2 and the global

20 % truncation errors in a log -log scale with base 10.

21 loglog(h1,error1)

22 hold on

23 loglog(h1,error2)

24 hold on

25 loglog(h1,error3)

26 hold on

27 loglog(h1,error4)

28 hold on

29 loglog(h2,error5)

30 hold on

31 xlabel('log(h)')32 ylabel('log(error)')33 legend('Forward Euler','Heun','RK4','RK5','RK8')

A.11 Script 9

Script 9 is used to define the system of ODEs described in Section 4.2.

53

Page 57: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

1 function f = fun(t,y0 ,A)

2 % Function f is dependent on t, y0 and A.

3 % y0 is a vector given in y0.mat matfile.

4 % A is a matrix given in A.mat matfile.

5 b = zeros (80802 ,1);

6 % b is a vector.

7 % Function f which define the system of ODEs.

8 f = A*y0+b;

9 end

A.12 Script 10

This script is for Section 4.2 which attempts to find out the the stability limit nsuch that the forward Euler method is stable for solving the system of ODEs.

1 % This solver solves this system of ODEs

2 % y' = A*y + b and

3 % y(0) = y0 is the initial condition ,

4 % in the time interval t = [0,1] using the forward

5 % Euler method and find out at what value of

6 % n the forward Euler method is stable.

7 load ('A') % Load A from the A.mat matfile.

8 t0 = 0; % Initial time.

9 t(1) = 1; % Final time.

10 % The forward Euler method is stable when n = 22000

11 n = 22000;

12 h = (t(1)-t0)/n; % Step size.

13 load ('y0') % load y0 from the y0.mat matfile.

14 for t = t0:h:t(1)-h % t is the time interval.

15 i = 1:n; % Time stepping loop

16 y0 = y0 + h*fun(t,y0,A);

17 end

18 % Plot the figure of y0 at the final time t = 1.

19 plot(y0)

20 hold on;

A.13 Script 11

Script 11 is used in Section 4.2.2 and it attempts to find out the time required forthe forward Euler method to compute the global truncation errors at the final timeas the stability limit n increases.

1 % This solver compute the global truncation errors

2 % at the final time and it verifies the time

3 % required for the forward Euler method to compute

4 % the global truncation errors as the stability

54

Page 58: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

5 % limit n increases.

6 % The errors in the numerical solution of the

7 % system of ODEs is computed using the forward

8 % Euler method.

9 format long

10 load ('A')11 t0 = 0;

12 t(1) = 1;

13 n = 22000;

14 h = (t(1)-t0)/n;

15 load ('y0')16 % Tic toc function measures the computational time

17 % of the global truncation errors.

18 tic

19 for t = t0:h:t(1)-h % Time stepping loop

20 i = 1:n;

21 y0 = y0 + h*fun(t,y0,A);

22 end

23 toc

24 % yn_ex_t1.mat matfile is 40401 x1 vector and this vector

25 % is the exact solution of this system at the final time

26 % t = 1.

27 load('yn_ex_t1 ')28 % Global truncation error at the final time t = 1.

29 norm(y0 (1:40401 ,1) -yn_ex_t1 (1:40401 ,1))

A.14 Script 12

Script 12 is used in Section 4.2 and it attempts to find out the stability limit n suchthat Heun’s method is stable for solving the system of ODEs.

1 % This solver solves the system of ODEs using

2 % Heun 's method and find out at what value

3 % of n Heun 's method is stable.

4 load ('A')5 t0 = 0;

6 t(1) = 1;

7 % Heun 's method is stable when n = 2100.

8 n = 2100;

9 h = (t(1)-t0)/n;

10 load ('y0')11 for t = t0:h:t(1)-h

12 i = 1:n;

13 k1 = fun(t,y0 ,A);

14 k2 = fun(t+h,y0+h*k1,A);

15 y0 = y0 +(1/2)*h*(k1+k2);

16 end

17 % Plot the figure of y0.

55

Page 59: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

18 plot(y0)

19 hold on;

A.15 Script 13

This script is for Section 4.2.2, where we find out the time required for Heun’smethod to compute the global truncation errors at the final time as the stabilitylimit n increases.

1 % This solver compute the global truncation errors

2 % and the time required for Heun 's method to compute

3 % the errors as the stability limit n increases.

4 format long

5 load ('A')6 t0 = 0;

7 t(1) = 1;

8 n = 2100;

9 h = (t(1)-t0)/n;

10 load ('y0')11 tic

12 for t = t0:h:t(1)-h

13 i = 1:n;

14 k1 = fun(t,y0 ,A);

15 k2 = fun(t+h,y0+h*k1,A);

16 y0 = y0 +(1/2)*h*(k1+k2);

17 end

18 toc

19 load('yn_ex_t1 ')20 % Global truncation error.

21 norm(y0 (1:40401 ,1) -yn_ex_t1 (1:40401 ,1))

A.16 Script 14

This script is used in Section 4.2.1, where we want to find out the stability limit nsuch that RK4 is stable for solving the system of ODEs.

1 % This solver will solve the system of ODEs using

2 % RK4 and find out at what value of n RK4 is stable.

3 load ('A')4 t0 = 0;

5 t(1) = 1;

6 n = 435; % RK4 is stable when n = 435.

7 h = (t(1)-t0)/n;

8 load ('y0')9 for t = t0:h:t(1)-h

10 i = 1:n;

11 k1 = fun(t,y0 ,A);

56

Page 60: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

12 k2 = fun(t+(1/2)*h,y0 +(1/2)*h*k1,A);

13 k3 = fun(t+(1/2)*h,y0 +(1/2)*h*k2,A);

14 k4 = fun(t+h,y0+h*k3,A);

15 y0 = y0 +(1/6)*h*(k1+2*k2+2*k3+k4);

16 end

17 % Plot the figure of y0.

18 plot(y0)

19 hold on;

A.17 Script 15

Script 15 is used for Section 4.2.2 and it attempts to find out the time required forRK4 to compute the global truncation errors at the final time as the stability limitn increases.

1 % This solver compute the global truncation errors

2 % and the time required for RK4 to compute

3 % the errors as the stability limit n increases.

4 format long

5 load ('A')6 t0 = 0;

7 t(1) = 1;

8 n = 435;

9 h = (t(1)-t0)/n;

10 load ('y0')11 tic

12 for t = t0:h:t(1)-h

13 i = 1:n;

14 k1 = fun(t,y0 ,A);

15 k2 = fun(t+(1/2)*h,y0 +(1/2)*h*k1,A);

16 k3 = fun(t+(1/2)*h,y0 +(1/2)*h*k2,A);

17 k4 = fun(t+h,y0+h*k3,A);

18 y0 = y0 +(1/6)*h*(k1+2*k2+2*k3+k4);

19 end

20 toc

21 load('yn_ex_t1 ')22 % Global truncation error.

23 norm(y0 (1:40401 ,1) -yn_ex_t1 (1:40401 ,1))

A.18 Script 16

In this script, we want to find out the stability limit n such that RK5 is stable forsolving the system of ODEs used in Section 4.2.1.

1 % This solver will solve the system of ODEs using

2 % RK5 and find out at what value of n RK5 is stable.

3 load ('A')

57

Page 61: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

4 t0 = 0;

5 t(1) = 1;

6 n = 580; % RK5 is stable when n = 580.

7 h = (t(1)-t0)/n;

8 load ('y0')9 for t = t0:h:t(1)-h

10 i = 1:n;

11 k1 = fun(t,y0 ,A);

12 k2 = fun(t+(1/4)*h,y0 +(1/4)*h*k1,A);

13 k3 = fun(t+(1/4)*h,y0 +(1/8)*h*k1 +(1/8)*h*k2,A);

14 k4 = fun(t+(1/2)*h,y0 -(1/2)*h*k2+h*k3 ,A);

15 k5 = fun(t+(3/4)*h,y0 +(3/16)*h*k1 +(9/16)*h*k4 ,A);

16 k6 = fun(t+h,y0 -(3/7)*h*k1 +(2/7)*h*k2 +(12/7)*h*k3

-(12/7)*h*k4 +(8/7)*h*k5,A);

17 y0 = y0 +(1/90)*h*(7*k1+32*k3+12*k4+32*k5+7*k6);

18 end

19 % Plot the figure of y0.

20 plot(y0)

21 hold on;

A.19 Script 17

In this script, we find out the time required for RK5 to compute the global truncationerrors at the final time as the stability limit n increases used in Section 4.2.2.

1 % This solver compute the global truncation errors

2 % and the time required for RK5 to compute

3 % the errors as the stability limit n increases.

4 format long

5 load ('A')6 t0 = 0;

7 t(1) = 1;

8 n = 580;

9 h = (t(1)-t0)/n;

10 load ('y0')11 tic

12 for t = t0:h:t(1)-h

13 i = 1:n;

14 k1 = fun(t,y0 ,A);

15 k2 = fun(t+(1/4)*h,y0 +(1/4)*h*k1,A);

16 k3 = fun(t+(1/4)*h,y0 +(1/8)*h*k1 +(1/8)*h*k2,A);

17 k4 = fun(t+(1/2)*h,y0 -(1/2)*h*k2+h*k3 ,A);

18 k5 = fun(t+(3/4)*h,y0 +(3/16)*h*k1 +(9/16)*h*k4 ,A);

19 k6 = fun(t+h,y0 -(3/7)*h*k1 +(2/7)*h*k2 +(12/7)*h*k3

-(12/7)*h*k4 +(8/7)*h*k5,A);

20 y0 = y0 +(1/90)*h*(7*k1+32*k3+12*k4+32*k5+7*k6);

21 end

22 toc

58

Page 62: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

23 load('yn_ex_t1 ')24 % Global truncation error.

25 norm(y0 (1:40401 ,1) -yn_ex_t1 (1:40401 ,1))

A.20 Script 18

Script 20 is used in Section 4.2.1 and it attempts to find out the stability limit nsuch that RK8 is stable for solving the system of ODEs.

1 % This solver will solve the system of ODEs using

2 % RK8 and find out at what value of n RK8 is stable.

3 load ('A')4 t0 = 0;

5 t(1) = 1;

6 n = 328; % RK8 is stable when n = 328.

7 h = (t(1)-t0)/n;

8 load ('y0')9 for t = t0:h:t(1)-h

10 i = 1:n;

11 k1 = fun(t,y0 ,A);

12 k2 = fun(t+(2/27)*h,y0 +(2/27)*h*k1 ,A);

13 k3 = fun(t+(1/9)*h,y0 +(1/36)*h*(k1+3*k2),A);

14 k4 = fun(t+(1/6)*h, y0 +(1/24)*h*(k1 + 3*k3),A);

15 k5 = fun(t+(5/12)*h,y0 +(1/48)*h*(20*k1 -75*k3+75*k4),A

);

16 k6 = fun(t+(1/2)*h,y0 +(1/20)*h*(k1+5*k4+4*k5),A);

17 k7 = fun(t+(5/6)*h, y0 +(1/108)*h*(-25*k1+125* k4 -

260*k5 + 250*k6),A);

18 k8 = fun(t+(1/6)*h, y0+h*((31/300)*k1 + (61/225)*k5 -

(2/9)*k6 + (13/900)*k7),A);

19 k9 = fun(t+(2/3)*h, y0+h*(2*k1 - (53/6)*k4 + (704/45)

*k5 - (107/9)*k6 + (67/90)*k7 + 3*k8), A);

20 k10 = fun(t+(1/3)*h, y0+h*( -(91/108)*k1 + (23/108)*k4

- (976/135)*k5 + (311/54)*k6 - (19/60)*k7 +

(17/6)*k8 - (1/12)*k9),A);

21 k11 = fun(t+h,y0+h*((2383/4100)*k1 - (341/164)*k4 +

(4496/1025)*k5 - (301/82)*k6 + (2133/4100)*k7 +

(45/82)*k8 + (45/164)*k9 + (18/41)*k10),A);

22 k12 = fun(t,y0+h*((3/205)*k1 - (6/41)*k6 - (3/205)*k7

- (3/41)*k8 + (3/41)*k9 + (6/41)*k10),A);

23 k13 = fun(t+h,y0+h*( -(1777/4100)*k1 - (341/164)*k4 +

(4496/1025)*k5 - (289/82)*k6 + (2193/4100)*k7 +

(51/82)*k8 + (33/164)*k9 + (12/41)*k10 + k12),A);

24 y0 = y0 + h*((34/105)*k6 + (9/35)*k7 + (9/35)*k8 +

(9/280)*k9 + (9/280)*k10 + (41/840)*k12+ (41/840)*

k13);

25 end

26 % Plot the figure of y0.

59

Page 63: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

27 plot(y0)

28 hold on;

A.21 Script 19

Script 19 is used for Section 4.2.2 which generates the time required for RK8 to com-pute the global truncation errors at the final time as the stability limit n increases.

1 % This solver compute the global truncation errors

2 % and the time required for RK8 to compute

3 % the errors as the stability limit n increases.

4 format long

5 load ('A')6 t0 = 0;

7 t(1) = 1;

8 n = 328;

9 h = (t(1)-t0)/n;

10 load ('y0')11 tic

12 for t = t0:h:t(1)-h

13 i = 1:n;

14 k1 = fun(t,y0 ,A);

15 k2 = fun(t+(2/27)*h,y0 +(2/27)*h*k1 ,A);

16 k3 = fun(t+(1/9)*h,y0 +(1/36)*h*(k1+3*k2),A);

17 k4 = fun(t+(1/6)*h, y0 +(1/24)*h*(k1 + 3*k3),A);

18 k5 = fun(t+(5/12)*h,y0 +(1/48)*h*(20*k1 -75*k3+75*k4),A

);

19 k6 = fun(t+(1/2)*h,y0 +(1/20)*h*(k1+5*k4+4*k5),A);

20 k7 = fun(t+(5/6)*h, y0 +(1/108)*h*(-25*k1+125* k4 -

260*k5 + 250*k6),A);

21 k8 = fun(t+(1/6)*h, y0+h*((31/300)*k1 + (61/225)*k5 -

(2/9)*k6 + (13/900)*k7),A);

22 k9 = fun(t+(2/3)*h, y0+h*(2*k1 - (53/6)*k4 + (704/45)

*k5 - (107/9)*k6 + (67/90)*k7 + 3*k8),A);

23 k10 = fun(t+(1/3)*h, y0+h*( -(91/108)*k1 + (23/108)*k4

- (976/135)*k5 + (311/54)*k6 - (19/60)*k7 +

(17/6)*k8 - (1/12)*k9),A);

24 k11 = fun(t+h,y0+h*((2383/4100)*k1 - (341/164)*k4 +

(4496/1025)*k5 - (301/82)*k6 + (2133/4100)*k7 +

(45/82)*k8 + (45/164)*k9 + (18/41)*k10),A);

25 k12 = fun(t,y0+h*((3/205)*k1 - (6/41)*k6 - (3/205)*k7

- (3/41)*k8 + (3/41)*k9 + (6/41)*k10),A);

26 k13 = fun(t+h,y0+h*( -(1777/4100)*k1 - (341/164)*k4 +

(4496/1025)*k5 - (289/82)*k6 + (2193/4100)*k7 +

(51/82)*k8 + (33/164)*k9 + (12/41)*k10 + k12),A);

27 y0 = y0 + h*((34/105)*k6 + (9/35)*k7 + (9/35)*k8 +

(9/280)*k9 + (9/280)*k10 + (41/840)*k12+ (41/840)*

k13);

60

Page 64: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

28 end

29 toc

30 load('yn_ex_t1 ')31 % Global truncation error.

32 norm(y0 (1:40401 ,1) -yn_ex_t1 (1:40401 ,1))

61

Page 65: BACHELOR THESIS IN MATHEMATICS/APPLIED ...mdh.diva-portal.org/smash/get/diva2:1436317/FULLTEXT01.pdfBachelor thesis in mathematics/applied mathematics Current version: 7th June 2020

Appendix BReflection of thesis objectives

In this thesis, we have shown a deep understanding of the mathematics field ofnumerical analysis by implementing several numerical methods of different ordersused in scientific computation. This thesis clearly shows how numerical methodsare connected to our everyday life and in which contexts they are important to use.Furthermore, this thesis describes how various numerical methods, in particular thefive explicit Runge-Kutta methods, perform on different initial value problems of afirst-order linear ODE and an initial value problem of a system of first-order linearODEs. The first-order ODEs and the system of first-order ODEs are defined in detailand solved according to the time frame written in the time plan for the productionof this thesis.

62