BACHELOR THESIS IN 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¨ampadmatematik

DIVISION OF APPLIED MATHEMATICS MALARDALEN¨ UNIVERISTY SE-721 23 VASTER¨ AS,˚ SWEDEN 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 Westerb¨ack

Examiner: Doghonay Arjmand

Comprising: 15 ECTS credits 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 forward , Heun’s method, RK4, RK5, and RK8. This thesis aims to compare the accuracy, stability, and efficiency properties of the five explicit Runge-Kutta methods. For accuracy, we carry out a convergence study to verify the convergence rate 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 methods for solving a linear test equation. For efficiency, we carry out an efficiency study to compare the efficiency of the five explicit Runge-Kutta methods for solving a system of first-order linear ODEs, which is the main focus of this thesis. This system of first-order linear ODEs is a semi- of a two-dimensional wave equation. Acknowledgments

l would like to express my sincere gratitude to my supervisor, Siyang Wang, who has provided me with a deep understanding of MATLAB features. This understanding allowed me to be able to solve initial value problems of first-order linear ODEs in MATLAB. l would also like to thank Siyang Wang and my reviewer Thomas Westerb¨ack for their input and guidance on how to develop this thesis. Contents

1 Introduction 6 1.1 Background ...... 6 1.2 Literature review ...... 7 1.3 Research question ...... 9 1.4 Outline ...... 9

2 Differential equations 10 2.1 Partial differential equations ...... 10 2.2 Ordinary differential equations ...... 13

3 Numerical methods for ODEs 15 3.1 Finite difference methods ...... 15 3.1.1 Errors ...... 16 3.1.2 Numerical properties ...... 16 3.2 Stability of explicit Runge-Kutta methods ...... 18 3.2.1 The forward Euler method ...... 19 3.2.2 Heun’s method ...... 20 3.2.3 The fourth-order Runge-Kutta method ...... 21 3.2.4 The fifth-order Runge-Kutta method ...... 22 3.2.5 The eighth-order Runge-Kutta method ...... 23 3.2.6 Comparison of stability regions ...... 27

4 Numerical experiments 29 4.1 Convergence study ...... 29 4.2 Efficiency study ...... 35 4.2.1 Stability limits ...... 36 4.2.2 Time-error efficiency ...... 37

5 Conclusion 42

Bibliography 46

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

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

B Reflection of thesis objectives 46

3 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 y0(t) = −2ty(t), y(0) = 1, for t ∈ [0,T ] using the five explicit Runge-Kutta methods against 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 0(t) = A~y(t) + ~ b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability n number of time points using the forward Euler method...... 37 4.3 Time (seconds) required for the forward Euler method to compute global truncation errors in the numerical solution of the system of ODEs...... 37 4.4 Global truncation errors in the numerical solution of ~y 0(t) = A~y(t) + ~ b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability n number of time points using Heun’s method...... 38 4.5 Time (seconds) required for the Heun’s method to compute global truncation errors in the numerical solution of the system of ODEs. . . 38 4.6 Global truncation errors in the numerical solution of ~y 0(t) = A~y(t) + ~ b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability n number of time points using RK4...... 38 4.7 Time (seconds) required for RK4 to compute global truncation errors in the numerical solution of the system of ODEs...... 38 4.8 Global truncation errors in the numerical solution of ~y 0(t) = A~y(t) + ~ b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability n number of time points using RK5...... 39 4.9 Time (seconds) required for RK5 to compute global truncation errors in the numerical solution of the system of ODEs...... 39 4.10 Global truncation errors in the numerical solution of ~y 0(t) = A~y(t) + ~ b(t), ~y(0) = ~y0, for t ∈ [0, 1] computed at increasing stability n number of time points using RK8...... 39 4.11 Time (seconds) required for RK8 to compute global truncation errors in the numerical solution of the system of ODEs...... 39

4 List of Tables

4.1 Global truncation errors and approximations of the convergence rate of the forward Euler method for solving y0(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1]...... 30 4.2 Global truncation errors and approximations of the convergence rate of Heun’s method for solving y0(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1]. . 30 4.3 Global truncation errors and approximations of convergence rate of RK4 for solving y0(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1]...... 31 4.4 Global truncation errors and approximations of the convergence rate of RK5 for solving y0(t) = −2ty(t), y(0) = 1, for t ∈ [0, 1]...... 31 4.5 Global truncation errors and approximations of the convergence rate of RK8 for solving y0(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 0(t) = ~ A~y(t) + b(t), ~y(0) = ~y0, for t ∈ [0, 1]...... 36

5 Chapter 1

Introduction

1.1 Background

A differential equation is an equation that relates one or several functions and its [10]. Differential equations are used to model advanced systems, for in- stance, mechanical and electrical systems from fields such as biology, social sciences, , and . Mechanical systems and electrical systems are two ex- amples of physical systems that change over time [18]. Differential equations are used to describe a continuous change of physical systems mathematically. Partial differential equations (PDEs) and ordinary differential equations (ODEs) are two classes 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 heat conduction problem for solid bodies. Heat equations describe how the heat tem- perature distributes in, for instance, a rode as the time changes. Wave equations exist to be used to model physical problems such as physical systems, where wave motions 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 to solve those differential equations whose exact