You are on page 1of 11

Boundary Value Problems for Ordinary Differential Equations 31

1.4 Finite Difference Method for BVP ODEs

1.4.1 Derivation of finite difference approximations to derivatives

Assume that the dependent variable is the polynomial in such as

(1.103)

where is the specified value of and are constants to be determined.

To determine constants :

Setting in Eq. (1.103) will result in .

The differentiation of Eq. (1.103) with respect to gives

(1.104)

Setting in Eq. (1.104) gives in .

Taking the second derivative of by results in

(1.105)

Setting in Eq. (1.105) yields in .

The equation for coefficient is .

Substitution of coefficients in Eq. (1.103) results in a Taylor series

(1.106)

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 32

The Taylor series by Eq. (1.106) are

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 .

The forward difference approximation to the first derivative is given as

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

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 33

where .

The backward difference approximation to the first derivative is given as

(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)

where means that the truncation error of approximation is proportional to .

To derive an approximation for at , Taylor series by Eq. (1.106) is used around the point for

and

(1.114)

(1.115)

Multiplying by 4 and subtracting results in

or

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 34

Then,

(1.116)

This is the second-order approximation to the first derivative at the left boundary.

Similarly, Taylor series is used around point for and

(1.117)

(1.118)

Multiplying by 4 and subtracting results in

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)

Multiplying Eq. (1.120) by 2 and subtracting from Eq. (1.121) result in

Thus, the first-order forward approximation to the second derivative is given by

(1.122)

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 35

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)

Adding Eq. (1.124) to Eq. (1.125) results in

Thus, the second-order central approximation to the second derivative is given by

(1.126)

The second-order forward approximation to the second derivative is derived as

(1.127)

and the second-order backward approximation to the second derivative as

(1.128)

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 36

Table. Summary of finite difference approximations to first and second derivatives

First derivative Second derivative


Internal points Internal points
1st order 1st order
forward difference forward difference

backward difference backward difference

2nd order 2nd order


central difference central difference

Left boundary Left boundary


1st order 2nd order

2nd order

Right boundary Right boundary


1st order 2nd order

2nd order

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 37

1.4.2 Finite difference method for second-order BVP ODE


First, we will solve the single linear second-order equation of the form:

(1.129)

subject to the Dirichlet boundary conditions

(1.130)

We introduce a grid of uniformly-spaced internal points, , and two end points, and ,

as
, or

and the mesh size is defined as

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)

Multiplying Eq. (1.131) by and collecting terms results in

Equation (1.131) can be rearranged as

, (1.134)

where

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 38

Consider Eq. (1.134) for

Substituting the boundary condition by Eq. (1.132) results in

and

(1.135)

Consider Eq. (1.134) for

Substituting the boundary condition by Eq. (1.133) results in

and

(1.136)

Thus, the following system of N linear algebraic equations is obtained.

(1.137)

The system of linear algebraic equations (1.134) in matrix notation is given as

(1.138)

where

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 39

The matrix A is a tridiagonal matrix.

Example: Pressure driven flow of fluid between two stationary plates


Consider the flow of viscous fluid between two stationary parallel plates separated by distance . The flow

is driven by a uniform pressure gradient in the x-direction, . Here, is a longitudinal coordinate

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-

direction with the velocity varying only in the y-direction):

with boundary conditions and , i.e. the no-slip conditions at the edges of the

plate. Here, is the fluid viscosity.

The BVP ODE in the standard form by Eq. (1.129) is given as

Figure 1.6: Illustration of viscous fluid flow between two stationary parallel plates

The analytical solution to this problem is as follows

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 40

We will solve the problem for , and .

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)

Dr. Boris Golman MSC 602 Advanced Applied Mathematics


Boundary Value Problems for Ordinary Differential Equations 41

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()

The simulation results are illustrated in Fig. 1.7.

Figure 1.7: Fluid flow between parallel plates. Finite difference method for BVP ODE

Dr. Boris Golman MSC 602 Advanced Applied Mathematics

You might also like