Problem Sheet 2 Question 2a - 3rd Order Taylor#
The general form of the population growth differential equation
with the initial condition
For N=4 with the analytic (exact) solution
Apply 3rd Order Taylor Method to approximate the solution of the given initial value problems using the indicated number of time steps. Compare the approximate solution with the given exact solution, and compare the actual error with the theoretical error.
## Library
import numpy as np
import math
%matplotlib inline
import matplotlib.pyplot as plt # side-stepping mpl backend
import matplotlib.gridspec as gridspec # subplots
import warnings
import pandas as pd
warnings.filterwarnings("ignore")
General Discrete Interval#
The continuous time \(a\leq t \leq b \) is discretised into \(N\) points seperated by a constant stepsize
Specific Discrete Interval#
Here the interval is \(0\leq t \leq 4\) with \(N=4\)
This gives the 5 discrete points with stepsize h=1:
This is generalised to
The plot below illustrates the discrete time steps from 0 to 4.
### Setting up time
t_end=4
t_start=0
N=4
h=(t_end-t_start)/(N)
t=np.arange(t_start,t_end+0.01,h)
fig = plt.figure(figsize=(10,4))
plt.plot(t,0*t,'o:',color='red')
plt.plot(t[0],0*t[0],'o',color='green')
plt.title('Illustration of discrete time points for h=%s'%(h))
plt.plot();

3rd Order Taylor Solution#
The 3rd Order Taylor difference equation is given by
where
and
which gives
for \(i=0,1,2,3\) with the initial condition,
IC=1
INTITIAL_CONDITION=IC
w=np.zeros(N+1)
w[0]=INTITIAL_CONDITION
for i in range (0,N):
w[i+1]=w[i]+h*(t[i]-w[i])+h*h/2*(1-t[i]+w[i])+h*h*h/6*(-1+t[i]-w[i])
y=2*np.exp(-t)+t-1 # Exact Solution
Global Error#
The upper bound of the global error is:
where \(|y''''(t)|\leq M \) for \(t \in (0,4)\) and \(L\) is the Lipschitz constant. Generally we do not have access to \(M\) or \(L\), but as we have the exact solution we can find \(M\) and \(L\).
Local Error#
The global error consistent of an exponential term and the local error. The local error for the 3rd order Taylor method is
by differentiation the exact solution
four times
and
Then we choose the largest possible value of \(y'''\) on the interval of interest \((0,4)\), which gives,
therefore
This gives the local error
Lipschitz constant#
The Lipschitz constant \(L\) is from the Lipschitz condition,
The constant can be found by taking partical derivative of \(f(t,y)=t-y\) with respect to \(y\)
Upper_bound=h*h*h/(12)*(np.exp(t)-1) # Upper Bound
Results#
fig = plt.figure(figsize=(12,5))
# --- left hand plot
ax = fig.add_subplot(1,4,1)
plt.plot(t,w,'o:',color='k')
plt.plot(t[0],w[0],'o',color='green')
#ax.legend(loc='best')
plt.title('Numerical Solution (w) h=%s'%(h))
# --- right hand plot
ax = fig.add_subplot(1,4,2)
plt.plot(t,y,color='blue')
plt.title('Analytic Solution (y)')
#ax.legend(loc='best')
ax = fig.add_subplot(1,4,3)
plt.plot(t,np.abs(w-y),'o:',color='red')
plt.title('Exact Error |w-y|')
# --- title, explanatory text and save
ax = fig.add_subplot(1,4,4)
plt.plot(t,np.abs(w-y),'o:',color='red')
plt.plot(t,Upper_bound,color='purple')
plt.title('Upper Bound')
# --- title, explanatory text and save
fig.suptitle(r"$y'=t-y$", fontsize=20)
plt.tight_layout()
plt.subplots_adjust(top=0.85)

d = {'time t_i': t, ' 2nd Order Taylor (w_i) ':w, 'Exact (y_i) ':y, 'Exact Error( |y_i-w_i|) ':np.abs(y-w),'Upper Bound':Upper_bound}
df = pd.DataFrame(data=d)
df
time t_i | 2nd Order Taylor (w_i) | Exact (y_i) | Exact Error( |y_i-w_i|) | Upper Bound | |
---|---|---|---|---|---|
0 | 0.0 | 1.000000 | 1.000000 | 0.000000 | 0.000000 |
1 | 1.0 | 0.666667 | 0.735759 | 0.069092 | 0.143190 |
2 | 2.0 | 1.222222 | 1.270671 | 0.048448 | 0.532421 |
3 | 3.0 | 2.074074 | 2.099574 | 0.025500 | 1.590461 |
4 | 4.0 | 3.024691 | 3.036631 | 0.011940 | 4.466513 |