Introduction To Partial Differential Equation - III. Numerical Methods
Introduction To Partial Differential Equation - III. Numerical Methods
Introduction To Partial Differential Equation - III. Numerical Methods
Numerical methods
For an ODE for u(x) defined on the interval, x ∈ [a, b], and consider a uniform grid with ∆x = (b−a)/N,
discretization of x, u, and the derivative(s) of u leads to N equations for ui, i = 0, 1, 2, ..., N, where ui ≡
u(i∆x) and xi ≡ i∆x. (See illustration below.)
The idea for PDE is similar. The diagram in next page shows a typical grid for a PDE with two variables (x
and y). Two indices, i and j, are used for the discretization in x and y. We will adopt the convention, u i, j ≡
u(i∆x, j∆y), xi ≡ i∆x, yj ≡ j∆y, and consider ∆x and ∆y constants (but allow ∆x to differ from ∆y).
For a boundary value problem with a 2nd order ODE, the two b.c. would reduce the degree of freedom
from N to N−2; We obtain a system of N−2 linear equations for the interior points that can be solved with
typical matrix manipulations. For an initial value problem with a 1st order ODE, the value of u0 is given.
Then, u1, u2, u3, ..., are determined successively using a finite difference scheme for du/dx. We will discuss
the extension of these two types of problems to PDE in two dimensions.
Case 1: PDE + closed boundary
2 2
∂ u ∂ u
Example 1: Solve Laplace equation, 2
2
= 0 , defined on x ∈ [0,1], y ∈ [0, 1] ,
∂x ∂y
with the boundary conditions
The domain for the PDE is a square with 4 "walls" as illustrated in the following figure. The four boundary
conditions are imposed to each of the four walls.
(Ex. 1 continued)
Physically, one can think of u as (for example) temperature and the solution of this PDE as the "equilibrium
state" of the diffusion process (transport of heat from the area with higher temperature to the area with
lower temperature) described by the heat equation (see Part I),
2 2
∂u ∂ u ∂ u
2 ,
= 2
∂t ∂x ∂y
in which t is time. As t → ∞, u ceases to evolve further so ∂u/∂t → 0. The heat equation is then reduced to
Laplace equation. In the diagram in the preceding page, one can think of the domain for the PDE as a
square slab of metal with its four sides attached to thermal reservoirs to maintain constant temperatures.
The right and top sides are slightly warmer at 2°C, while the left and lower sides are cooler at 1°C. With
these constraints, we expect the equilibrium state to be non-uniform within the domain; Temperature should
decrease approximately from the top right corner to the lower left corner. The solution of the Laplace
equation will give us the answer. Let us consider the solution on a grid with ∆x = ∆y = 1/3, as illustrated
in the figure in next page
(Ex. 1 continued)
In the preceding diagram, the values of the variables in green are already given by the boundary conditions.
The only unknowns are the red u i, j at the interior points. We have 4 unknowns, need 4 equations to
determine their values. Let us first approximate the second partial derivatives in the PDE by central
difference scheme (5th equation for second derivative in Table 6-1 of G&S textbook),
2
∂ u ui−1 , j −2 u i , j ui1 , j
2
≈ 2 , (1)
∂x i, j x
2
∂ u u i , j −1 −2 u i , j ui , j 1
2
≈ 2 . (2)
∂y i, j y
(Ex. 1 continued)
Equations (1) and (2) are the same as those for the ordinary 2nd derivatives, d 2u/dx2 and d 2u/dy2, only that
in Eq. (1) y is held constant (all terms in Eq. (1) have the same j) and in Eq. (2) x is held constant (all terms
have the same i). For those who are not yet familiar with the index notation, Eqs. (1) and (2) are
equivalent to
2
∂ u u x− x , y −2 u x , y u x x , y
2
≈ 2 , (1a)
∂x x
2
∂ u u x , y− y −2 u x , y u x , y y
2
≈ 2 . (2a)
∂y y
The correspondence between the two set of notations is illustrated in the following.
(Ex. 1 continued)
Plugging Eqs. (1) and (2) into the original Laplace equation, we obtain
The distribution of the variables in Eq. (3) on the grid is illustrated in the diagrams in the preceding page.
The partial derivatives, ∂2u/∂x2+∂2u/∂y2, in the PDE can be evaluated by (3) at the grid point (i, j) using the
discrete values of u at (i, j) itself (with weight of − 4) and those at its 4 neighboring points - at left, right,
top, and bottom. The diagram in next page illustrates how this fits into the grid system of our problem. For
example, at the grid point, (i, j) = (2,2), the terms in Eq. (3) are u2,2 at center and u2,3, u2,1, u1,2, and u3,2 at top,
bottom, left, and right of the grid point. The relevant grid points form a "cross" pattern.
(Ex. 1 continued)
(Ex. 1 continued)
Using Eq. (3), we can now write the equations for ui, j at the four interior points,
A quick reference for the relationships among the red and green symbols can be found in the preceding
diagram. The red symbols correspond to the unknown ui,j at the interior points. The green ones are known
values of ui,j given by the boundary conditions (see the original boundary conditions in p.2),
Moving the green symbols in Eq. (4) to the right hand side and replacing them with the known values given
by the b.c. in Eq. (5), we obtain
−4 1 0 1 u1,1 −2
1 −4 1 0 u1,2
= −3 , (6)
0 1 −4 1 u2,2 −4
1 0 1 −4 u2,1 −3
which can be readily solved to obtain the final solution, (u1,1, u1,2 , u2,2 , u2,1) = (1.25, 1.5, 1.75, 1.5).
(Ex. 1 continued)
The solution is illustrated in the following figure. The behavior of the solution is as we expected:
Constrained by the boundary conditions, The "temperature", u, decreases from the top right corner to lower
left corner of the domain. Note that, unlike the matrix for the boundary value problems for ODE, the
matrix in Eq. (6) is not strictly tridiagonal. It is however only slightly more complicated; We can discretize
the system on a finer grid with even more grid points and yet there will always be just 3 non-zero elements
in each row and each column of the matrix.
Case 2: Evolution equation defined on t ∈[0, ∞)
∂u ∂u
=c ,
∂t ∂x
Like in Example 1, we should discretize the system on a two-dimensional grid for x and t using the
notation, ui,j ≡ u(i∆x, j∆t), xi ≡ i∆x, and tj ≡ j∆t. Unlike Example 1, here the domain for the PDE is
unbounded in x, and semi-infinite in t (analogous to an initial value problem for ODE). There is only one
"wall" at t = 0 along which the values of u are given by the boundary condition. See the following diagram.
(Ex. 2 continued)
The following diagram provides further detail for the grid. The values of ui, 0 at the grid points at the
bottom (t = 0 or j = 0, marked by circles) are given by the boundary condition, ui, 0 = Fi (the discretized
form of the boundary condition (I), where Fi ≡ F(i∆x) ). From the bottom row with known values of u, we
integrate the PDE forward in t ("upward" in the following diagram) to t = ∆t (j = 1) to obtain ui, 1, i.e.,
u(x,∆t), marked by green triangles. The process then continues to the next level at j = 2 (t = 2∆t), and so on.
In the diagram, the arrows that connect a circle to a triangle are only symbolic. Usually, the value of u at a
triangle with j = 1 is determined by not only the value of u at the circle below it but also those at several
neighboring circles. This will be clarified shortly.
(Ex. 2 continued)
To perform the integration in t, we discretize the PDE by approximating ∂u/∂t with the forward difference
scheme, and ∂u/∂x with the central difference scheme (this combination is but one of many possible
choices),
u i , j1 −ui , j
∂u
∂t i,j
≈
t
, (7)
ui1 , j −ui −1 , j
∂u
∂x i, j
≈
2 x
. (8)
The partial differentiation in Eq. (7) is performed with x held constant (the index i is the same for all terms
in that equation). Likewise, t (or index j) is held constant in Eq. (8). Combining (7) and (8) we have
u i , j 1−ui , j u −u
= c i1 , j i−1 , j ,
t 2 x
or
u i , j 1 = − c t
2 x
ui −1, j ui , j
c t
u
2 x i 1, j
. (9)
The u in the left hand side of Eq. (10) is located at level j+1 (or t = (j+1)∆t), while those in right hand side
are all located at level j (or t = j∆t). The situation is illustrated in the following diagram for j = 0. Here,
ui,1 at the level of j =1 (t = ∆t), marked by a triangle, is determined by ui−1,0 , ui,0 , and ui+1,0 (marked by
circles), located one level below it at j = 0 (t = 0). Since the values of u at the level of j = 0 are given by the
boundary condition, ui,1 is readily obtained from Eq. (10). Once all ui,1 at the level of j = 1 are calculated,
they can be used to determine ui,2 at the next level (going one step upward in the diagram), and so on.
(Ex. 2 continued)
Let's try an example with c = 1, ∆x = 0.2, ∆t = 0.1 (⇒ α = 0.25 in Eq. (10)), and with the boundary
condition,
u4, 0 = 1 ,
ui, 0 = 0 for all i ≠ 4. (11)
See the plot in p.17 for the "initial state" given by Eq. (11). With the given c, ∆x, and ∆t, Eq. (10) becomes
The equation that's needed to integrate Eq. (12) forward from t = 0 to t = ∆t (i.e., from j = 0 to j = 1) is
The results in (13) are our solution of the PDE at t = ∆t = 0.1. They can be used to integrate the PDE
forward to t = 2∆t = 0.2 (j = 2), and so on. Just as a further demonstration, at j = 2 we have
(Ex. 2 continued)
All other ui, 2 with i > 6 or i < 2 are zero. The plot in next page shows the "initial state" and the solutions at
t = ∆t and t = 2∆t (i.e., ui,j at j = 0, 1, and 2).
Lastly, note that the finite difference scheme used in this example is far from the best. It is chosen for its
simplicity. In the analytic solution (see Part I), the shape of the initial state should remain the same at t = 0,
0.1, and 0.2. Moreover, u would not become negative at any location. As it turns out, it is not trivial to fix
these problems even with more sophisticated numerical schemes. This is beyond the scope of our
discussion.
(Ex. 2 continued)
The number on the abscissa is the index "i". The three panels
from top to bottom are j = 0, 1, and 2 (or t = 0, 0.1, and 0.2).
A Matlab program that was used to solve the PDE and produce
the plot is listed in next page.
(Ex. 2 continued)
% Matlab code for Example 2 for PDE
% Written by HPH
% ----------------
% Define and initialize the variables
for i = 1:10
xi(i) = i;
for j = 1:3
uij(i,j) = 0;
end
end
%
% The boundary condition (which essentially defines the "initial state")
%
uij(4,1) = 1;
%
% integration to t = 0.1 and 0.2
%
for j = 1:2
for i = 2:9
uij(i,j+1) = -0.25*uij(i-1,j)+uij(i,j)+0.25*uij(i+1,j);
end
end
%
% making the plots for t = 0, 0.1, and 0.2
%
for i = 1:10
u0(i) = uij(i,1);
u1(i) = uij(i,2);
u2(i) = uij(i,3);
z(i) = 0;
end
subplot(3,1,1);plot(xi,z,'k--',xi,u0,'r-o');axis([1 10 -0.5 1])
subplot(3,1,2);plot(xi,z,'k--',xi,u1,'r-o');axis([1 10 -0.5 1])
subplot(3,1,3);plot(xi,z,'k--',xi,u2,'r-o');axis([1 10 -0.5 1])