Euler’s Method and Runge Kutta 4th Order Method in Python

Pushkar Marathe
4 min readSep 9, 2022

--

Introduction

Euler’s method, also known as Forward Euler’s Method, can used for solving ordinary differential equations (ODEs) which is named after Leonhard Euler. In numerical analysis, Runge Kutta Methods are a family of implicit and explicit iterative methods, which include the Eulers’s method, used in temporal discretization for the approximate solutions of simultaneous nonlinear equations. These methods were developed around 1900 by the German mathematicians Carl Runge and Wilhelm Kutta.

The scope of this writing is limited to the implementation of Euler’s method and Runge Kutta 4th order method in python comparing their performance as a function of step size with an example. The detailed explanation of mechanics working behind these both methods, their advantages and limitations can be found elsewhere. Interested readers can tap in here [1].

General Definition

A first order ODE with an initial value can be written as,

Euler’s Method

Mathematical formulation of Euler’s method is as follows,

Runge Kutta 4th order method

And that of Runge Kutta 4th order method is as follows,

Python Implementation

As usual, first, let’s import necessary libraries,

import numpy as np
import matplotlib.pyplot as plt

In order to compare these methods numerically, let’s look at an example. Assume that the ODE which we wish to integrate and its initial value is as follows,

Equation 1

Of which the analytical solution is,

Equation 2

The above ODE can be coded in a function as,

##### ODE in a function + Initial Condition #####y0 = 1
def f(y,t):
return 2*(1-y) - np.exp(-4*t)

Next, lets code both Euler’s method and Runge Kutta 4th order method in two separate functions

##### Euler's Method #####def Euler_method(f,y0,t):
y = np.zeros(len(t))
y[0] = y0
for i in range(0,len(t)-1):
y[i+1] = y[i] + f(y[i],t[i])*(t[i+1]-t[i])
return y
y_Euler = Euler_method(f,y0,t)
##### Runge Kutta 4th Order Method #####def RK_method(f,y0,t):
y = np.zeros(len(t))
y[0] = y0
for i in range(0,len(t)-1):
h = t[i+1]-t[i]
F1 = h*f(y[i],t[i])
F2 = h*f((y[i]+F1/2),(t[i]+h/2))
F3 = h*f((y[i]+F2/2),(t[i]+h/2))
F4 = h*f((y[i]+F3),(t[i]+h))
y[i+1] = y[i] + 1/6*(F1 + 2*F2 + 2*F3 + F4)
return y
y_RK = RK_method(f,y0,t)

Now, discretizing independent variable (t) and calculating the step size (h)

##### Independent Variable Discretization #####points = 21
t = np.linspace(0,2,points)
##### Step Size Calculation #####h = t[2]-t[1]

Computing analytical solution (Eq. 2) for comparison purpose,

##### Analytical solution #####t_ana = np.linspace(0,2,51)
y_ana = 1/(2*np.exp(4*t_ana)) + 1 - 1/(2*np.exp(2*t_ana))

Last but not least, lets plot everything together and compare,

##### Plotting #####plt.figure(num=1, dpi=150, figsize=(7, 5))
plt.rcParams["font.family"] = "serif"
plt.rcParams['figure.facecolor'] = 'white'
plt.rcParams['savefig.facecolor']='white'
plt.plot(t_ana,y_ana,'ro',label = "Analytical Solution",markersize=2.5)
plt.plot(t,Euler_method(f,y0,t),'b-', label = "Euler")
plt.plot(t,RK_method(f,y0,t),'k-', label = "Runge Kutta")
plt.legend(loc=4)
plt.grid(True, color='lightgray')
plt.xlabel("$t$ (-)")
plt.ylabel("$y$ (-)")
plt.ylim([0.75,1])
plt.text(s='Euler\'s Method Vs. Runge Kutta 4th Order Method', x=1, y=1.03, fontsize=15, ha='center', va='center')
plt.text(s=f'n = {points}, h = {h:.3f}', x=1, y=1.01, fontsize=11, ha='center', va='center')
plt.savefig('euler_rk_output.png', transparent=False, bbox_inches="tight")
plt.show()

Output:

Effect of step size on solution

It can be seen in the gif below that, as step size decreases or number of integration points increase, solutions computed by Euler’s Method and Runge Kutta 4th Order converge.

Last Remarks

Above code can be easily expanded to solver simultaneous ODES. This is just a starting point. This article is written only for educational purposes and to express my love for programming. Lastly, I am not a professional programmer so (I know that) this code is far from optimum but most importantly it works! :) Any constructive feedback is welcome.

Source

  1. Numerical Methods for Ordinary Differential Equations: Initial Value Problems; David F. Griffiths, Desmond J. Higham (2010)

Contact

If you wish to get in touch with me, feel free to reach out at https://pushkarsmarathe.com/

--

--

Pushkar Marathe

Research Scientist | PhD in Chemical Engineering | Programming Enthusiast