I'm trying to solve exercises from Steven Strogatz's Non-Linear Dynamics and Chaos. In exercise 2.8.3, 2.8.4, and 2.8.5, one is expected to implement the Euler method, improved Euler method, and Runge-Kutta (4th order) method respectively for the initial value problem dx/dt = -x; x(0) = 1 to find x(1).
Analytically, the answer is 1/e. And I was finding the error obtained in each method. Much to my surprise, I was getting lesser error in Euler than in Improved Euler and Runge-Kutta!
My code looks like this. Sorry for the shabbiness.
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
to = 0
xo = 1
tf = 1
deltaT = np.zeros([5])
errorE = np.zeros([5])
errorIE = np.zeros([5])
errorRK = np.zeros([5])
for j in range(0,5):
n = pow(10,j)
deltat = (tf - to)/(n)
print ("delta t is",deltat)
deltaT[j] = deltat
t = np.linspace(to,tf,n)
xE = np.zeros([n])
xIE = np.zeros([n])
xRK = np.zeros([n])
xE[0] = xo
xIE[0] = xo
xRK[0] = xo
for i in range (1,n):
#Regular Euler
xE[i] = deltat*(-xE[i-1]) + xE[i-1]
#Improved Euler
IEintermediate = deltat*(-xIE[i-1]) + xIE[i-1]
xIE[i] = xIE[i-1] - deltat*(xIE[i-1] + IEintermediate)/2
#Runge-Kutta fourth order
k1 = -deltat*xRK[i-1]
k2 = -deltat*(xRK[i-1] + k1/2)
k3 = -deltat*(xRK[i-1] + k2/2)
k4 = -deltat*(xRK[i-1] + k3)
xRK[i] = xRK[i-1] + (k1 + 2*k2 + 2*k3 + k4)/6
print (deltat,xE[i],xIE[i],xRK[i])
errorE[j] = np.exp(-1) - xE[n-1]
errorIE[j] = np.exp(-1) - xIE[n-1]
errorRK[j] = np.exp(-1) - xRK[n-1]
The errors :
For delT = 1.0
For delT = 0.1
For delT = 0.01
For delT = 0.001
For delT = 0.0001
Is this legit? If not, why is this happening?
You are doing only n-1
integration steps of step size h=1/n
, thus you compute
exp(-(n-1)/n)=1/e*exp(1/n)
which has the approximate value
1/e + 1/e*1/n
The reported error values are exactly that, -h/e
, which is first order and thus gets visibly distorted by the order 1 Euler method. The Euler value is, more exactly
(1-1/n)^(n-1) = exp((n-1)*(-1/n-1/(2n^2)+O(1/n^3))
= 1/e*exp(1/(2n)+..)
= 1/e + h/(2e) + ...
If you adapt the code to make the extra step to reach time 1
, you will get a correct error picture.
delta t is 1.0
Euler 0.0 0.367879441171
imp. Euler 0.5 -0.132120558829
Runge-Kutta 4 0.375 -0.00712055882856
delta t is 0.1
Euler 0.3486784401 0.0192010010714
imp. Euler 0.368540984834 -0.00066154366211
Runge-Kutta 4 0.367879774412 -3.33241056083e-07
delta t is 0.01
Euler 0.366032341273 0.00184709989821
imp. Euler 0.367885618716 -6.17754474969e-06
Runge-Kutta 4 0.367879441202 -3.09130498977e-11
delta t is 0.001
Euler 0.367695424771 0.000184016400479
imp. Euler 0.367879502531 -6.13592486265e-08
Runge-Kutta 4 0.367879441171 -4.05231403988e-15
delta t is 0.0001
Euler 0.367861046433 1.83947385133e-05
imp. Euler 0.367879441785 -6.13176398545e-10
Runge-Kutta 4 0.367879441171 -2.6645352591e-15