1.4 Finite Difference Method For BVP Odes
1.4 Finite Difference Method For BVP Odes
1.4 Finite Difference Method For BVP Odes
(1.103)
To determine constants :
(1.104)
(1.105)
(1.106)
for :
(1.107)
where .
for :
(1.108)
where .
Let us truncate the forward Taylor series by Eq. (1.107) after the first derivative term as
, (1.109)
where .
or
(1.110)
Thus, Eq. (1.110) specifies the first-order forward difference approximation to the first derivative.
We will derive the backward difference approximation to the first derivative using the backward Taylor
series by Eq. (1.108) as
where .
(1.111)
Thus, Eq. (1.111) specifies the first-order backward difference approximation to the first derivative.
To derive the second-order approximation to the first derivative, we subtract Eq. (1.108) from Eq. (1.107)
resulting in
(1.112)
Then, the second-order central finite difference approximation to the first derivative could be
written as
(1.113)
To derive an approximation for at , Taylor series by Eq. (1.106) is used around the point for
and
(1.114)
(1.115)
or
Then,
(1.116)
This is the second-order approximation to the first derivative at the left boundary.
(1.117)
(1.118)
or
(1.119)
This is the second-order approximation to the first derivate at the right boundary.
To derive the forward finite difference approximation to the second derivative we use the forward Taylor
series by Eq. (1.107) around point for and as
(1.120)
(1.121)
(1.122)
By analogy, we can derive the first-order backward approximation to the second derivative as
(1.123)
To derive the forward finite difference approximation to the second derivative we use the forward Taylor
series by Eq. (1.107) around point for and as
(1.124)
(1.125)
(1.126)
(1.127)
(1.128)
2nd order
2nd order
(1.129)
(1.130)
We introduce a grid of uniformly-spaced internal points, , and two end points, and ,
as
, or
Replacing the first and second derivatives in Eq. (1.129) by their second-order difference approximations
in each internal points results in
(1.131)
and
(1.132)
(1.133)
, (1.134)
where
and
(1.135)
and
(1.136)
(1.137)
(1.138)
where
measuring distance along the plates, and is a transverse coordinate such that the plates are located at
and . The governing equations on the velocity of the fluid are (assuming flow in the x-
with boundary conditions and , i.e. the no-slip conditions at the edges of the
Figure 1.6: Illustration of viscous fluid flow between two stationary parallel plates
import numpy as np
import scipy.sparse
import scipy.sparse.linalg
import matplotlib.pyplot as plt
#
# define p,q and r
def p(x): return 0
def q(x): return 0
def r(x): return 100
#
# set boundary points
a = 0
b = 0.1
#
# specify boundary conditions
alpha = 0.0 # at x = a
beta = 0.0 # at x = b
#
# specify the number of internal points N
N = 99
#
# compute mesh size
h = (b-a)/(N+1)
# specify uniform grid of internal points
x = np.linspace(a+h, b-h, N)
#
# allocate arrays
diagonals = np.zeros((3, N)) # 3 diagonals
R = np.zeros(N) # right-hand side
# calculate diagonals of tridiogonal matrix
diagonals[0,:] = -(1 + p(x[:])*h/2)/2 # subdiagonal, a_i
diagonals[1,:] = 1 + (q(x[:])*h**2)/2 # main diagonal, b_i
diagonals[2,:] = -(1 - p(x[:])*h/2)/2 # superdiagonal, c_i
# calculate vector of right-hand side
R[:] = (h**2)*r(x[:])/2.
R[0] = R[0] - diagonals[0,0]*alpha
R[N-1] = R[N-1] - diagonals[2,N-1]*beta
#
# construct tridiagonal matrix
A = scipy.sparse.spdiags(diagonals, [-1,0,1], N, N, format='csc')
#
# solve tridiagonal system of linear equations
y = scipy.sparse.linalg.spsolve(A, R)
# add boundary points
X = np.hstack([a, x, b])
Y = np.hstack([alpha, y, beta])
#
# plot results
plt.plot(X, Y)
# calculate analytical results
mu = 1
d = 0.1
x = np.linspace(0,0.1)
Pdrop = -100 # pressure drop
u = -(Pdrop) * (d**2) / 2.0 / mu * (x / d - (x / d)**2)
plt.plot(x,u,'r--')
#
plt.xlabel('distance between plates')
plt.ylabel('fluid velocity')
plt.legend(('finite difference', 'analytical'))
plt.savefig('Finite difference linear second-order BVP ODE.png', dpi=600)
plt.show()
Figure 1.7: Fluid flow between parallel plates. Finite difference method for BVP ODE