Professional Documents
Culture Documents
Computational Science:
Computational Methods in Engineering
One‐Dimensional
Finite‐Difference Method
1
Outline
• Introduction & Problem Setup
• Conventional Finite‐Difference Method
• Improved Finite‐Difference Method
• Matrix Operators
• Incorporating Boundary Conditions
• Solving ODE’s
1
9/22/2019
Introduction & Problem Setup
Slide 3
The Finite‐Difference Method
The finite‐difference method is a way of obtaining a numerical solution to differential
equations. It does not give a symbolic solution.
Governing Equation
d 2 y dy
y0 0 x 10
dx 2 dx
y 0 1, y 10 5
Matrix Equation
A y b
Numerical Solution
y A b
1
Slide 4
2
9/22/2019
Functions are Discrete
To obtain a numerical solution using the finite‐difference method, functions are stored as
arrays of discrete points.
The function can be resolved more accurately using more points, but the solution will be
more computationally intensive to obtain. This is a fundamental tradeoff.
Slide 5
Discrete Functions are Stored as Column Vectors
Discrete functions are stored as a 1D array of numbers in a column vector.
d 2 y dy
y0
dx 2 dx
d2 d
2 1 y 0
dx dx
A y 0 y
Slide 6
3
9/22/2019
Conventional
Finite‐Difference Method
Slide 7
Step 1 – Identify Governing Equation & Boundary
Values
Governing Equation
d 2 y dy
y0 0 x 10
dx 2 dx
Boundary Values
y 0 1
y 10 5
Slide 8
4
9/22/2019
Step 2 – Approximate Derivatives with Finite‐
Differences (1 of 3)
First, let the function be discrete.
This allows us to approximate the derivatives with finite‐differences.
d 2 y dy
y0
dx 2 dx
y x x 2 y x y x x y x y x x
y x 0
x 2 x
Slide 9
Step 2 – Approximate Derivatives with Finite‐
Differences (2 of 3)
It is critical to ensure that each term in the finite‐difference equation exists at the same point.
y x x 2 y x y x x y x y x x
y x 0
x 2 x
Exists at x Exists at x - x/2
Exists at x
This is not a healthy or stable formulation because
not all of the terms exist at the same point.
Exists at x
Slide 10
10
5
9/22/2019
Step 2 – Approximate Derivatives with Finite‐
Differences (3 of 3)
This is the correct finite‐difference equation.
d 2 y dy
y0
dx 2 dx
y x x 2 y x y x x y x x y x x
y x 0
x 2 2x
All terms exist at x.
Slide 11
11
Step 3 – Write Finite‐Difference Equation Using Array
Indices
d 2 y dy
y0
dx 2 dx
y x x 2 y x y x x y x x y x x
y x 0
x 2 2x
yi 1 2 yi yi 1 yi 1 yi 1
yi 0
x 2 2x
Slide 12
12
6
9/22/2019
Step 4 – Rearrange Finite‐Difference Equation
The finite‐difference equation is rearranged so as to collect the y terms.
yi 1 2 yi yi 1 yi 1 yi 1
yi 0
x 2 2x
1 2 1 1 1
y 2 yi 2 yi 1
2 i 1
yi 1 yi 1 yi 0
x x x 2x 2x
1 1 2 1 1
2 yi 1 1 2 yi 2 yi 1 0
x 2x x x 2x
Slide 13
13
Step 5 – Setup Grid
Let’s solve this problem using 21 points.
x
x = 10
x=0
x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
The grid spacing x is then
xb xa 10 0
x 0.5
N 1 21 1
Slide 14
14
7
9/22/2019
Step 6 – Revise Finite‐Difference Equation
Substituting x = 0.5 into our finite‐difference equation gives
1 1 2 1 1
2 yi 1 1 2 yi 2 yi 1 0
x 2x x x 2x
1 1 2 1 1
2 yi 1 1 2 yi 2 yi 1 0
0.5 2 0.5 0.5 0.5 2 0.5
5 yi 1 7 yi 3 yi 1 0
Slide 15
15
Step 7 – Write Finite‐Difference Equation at Each
Point on Grid
5 yi 1 7 yi 3 yi 1 0
x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
These terms exist
outside of the grid.
Slide 16
16
8
9/22/2019
Step 8 – Incorporate Boundary Values
5 yi 1 7 yi 3 yi 1 0
y21 = 5
y1 = 1
x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Slide 17
17
Step 8 – Write Set of Equations as a Single Matrix
Equation
y1 1
y 0
2
y3 0
y4 0
y5 0
y6 0
y 0
7
y8 0
y 0
9
y10 0
y11 0
y12 0
y13 0
y 0
14
y15 0
y 0
16
y17 0
y18 0
y19 0
y20 0
y21 5
A
y b Slide 18
18
9
9/22/2019
Step 9 – Solve Matrix Equation
1
y1 1 1.0000
y 0
2 2.0497
y3 0 3.1160
y4 0 3.8545
y5 0 3.8005
y6 0 2.4436
y 0 -0.6324
7
y8 0 -5.5482
y 0 -11.8919
9
y10 0 -18.5007
y11 0 -23.3484
y12 0 -23.6453
y13 0 -16.2582
y 0 1.4730
14
y15 0 30.5339
y 0 68.7909
16
y17 0 109.6222
y18 0 141.1336
y19 0 146.6082
y20 0 106.8630
y21 5 5.0000
Slide 19
19
Step 10 – Plot the Result
y1 1.0000
y 2.0497
2
y3 3.1160
y4 3.8545
y5 3.8005
y6 2.4436
y -0.6324
7
y8 -5.5482
y -11.8919
9
y10 -18.5007
y11 -23.3484
y12 -23.6453
y13 -16.2582
y 1.4730
14
y15 30.5339
y 68.7909
16
y17 109.6222
y18 141.1336
y19 146.6082
y20 106.8630
y21 5.0000
Slide 20
20
10
9/22/2019
Improved
Finite‐Difference Method
Slide 21
21
Step 1 – Identify Governing Equation & Boundary
Values
Governing Equation
d 2 y dy
y0 0 x 10
dx 2 dx
Boundary Values
y 0 1
y 10 5
Slide 22
22
11
9/22/2019
Step 2 – Write Equation in Matrix Form Going Term‐
by‐Term
d2 d
2
y x y x y x 0
dx dx
D 2x y D x y y 0
Slide 23
23
D 2x y D x y y 0
A y 0 standard form
A D2x D x I A = DX2 - DX + I;
Slide 24
24
12
9/22/2019
Step 4 – Incorporate Boundary Values (1 of 4)
y1 0
y 0
2
y3 0
y4 0
y5 0
y6 0
y 0
7
Start with direct
y8 0
y 0
9
matrix equation. y10 0
y11 0
A y 0
y12 0
y13 0
y 0
14
y15 0
y 0
16
y17 0
y
18 0
y19 0
y20 0
y21 0
Slide 25
25
Step 4 – Incorporate Boundary Values (2 of 4)
y1 0
y 0
2
y3 0
y4 0
y5 0
y6 0
y 0
7
Zero out rows in [A]
y8 0
y 0
9
that correspond to y10 0
the boundary y11 0
y12 0
points. For this
y13 0
y 0
case, these are the 14
y15 0
rows for y1 and y21.
y 0
16
y17 0
y
18 0
y19 0
y20 0
y21 0
Slide 26
26
13
9/22/2019
Step 4 – Incorporate Boundary Values (3 of 4)
y1 0
y 0
2
y3 0
y4 0
y5 0
y6 0
y 0
7
Insert 1’s in the
y8 0
y 0
9
diagonal position of y10 0
the rows in [A] y11 0
y12 0
where zeros were
y13 0
y 0
inserted. 14
y15 0
y 0
16
y17 0
y
18 0
y19 0
y20 0
y21 0
Slide 27
27
Step 4 – Incorporate Boundary Values (4 of 4)
y1 1
y 0
2
y3 0
y4 0
y5 0
y6 0
y 0
7
Insert boundary
y8 0
y 0
9
values in column y10 0
vector [b]. y11 0
y12 0
y13 0
y 0
14
y15 0
y 0
16
y17 0
y
18 0
y19 0
y20 0
y21 5
Slide 28
28
14
9/22/2019
Step 5 – Solve Matrix Equation
1
y1 1 1.0000
y 0
2 2.0497
y3 0 3.1160
y4 0 3.8545
y5 0 3.8005
y6 0 2.4436
y 0 -0.6324
7
y8 0 -5.5482
y 0 -11.8919
9
y10 0 -18.5007
y11 0 -23.3484
y12 0 -23.6453
y13 0 -16.2582
y 0 1.4730
14
y15 0 30.5339
y 0 68.7909
16
y17 0 109.6222
y18 0 141.1336
y19 0 146.6082
y20 0 106.8630
y21 5 5.0000
Slide 29
29
Step 6 – Plot the Result
y1 1.0000
y 2.0497
2
y3 3.1160
y4 3.8545
y5 3.8005
y6 2.4436
y -0.6324
7
y8 -5.5482
y -11.8919
9
y10 -18.5007
y11 -23.3484
y12 -23.6453
y13 -16.2582
y 1.4730
14
y15 30.5339
y 68.7909
16
y17 109.6222
y18 141.1336
y19 146.6082
y20 106.8630
y21 5.0000
Slide 30
30
15
9/22/2019
Matrix Operators
Slide 31
31
Functions Vs. Operations (1 of 2)
2
a x f x b x f x c x f x g x
x 2
x
Operations Functions
Everything else in a differential Functions only appear in a
equation is something that differential equation as the
operates on a function. unknown or as the
excitation.
a x , b x , c x point-by-point
multiplication on f x f x unknown
2
, calculates derivatives of f x g x excitation
x x 2
scales entire f x
Slide 32
32
16
9/22/2019
Functions Vs. Operations (2 of 2)
A Dx2 f B Dx f C f g
Operations Functions
Operations are always stored Functions are stored as
in square matrices. Any column vectors.
linear operation can be put
into matrix form. f1 g1
f g
l11 l12 l1M 2
f 2
g
l l l2 M
L 21 22
fM gM
lM 1 lM 2 lMM
Slide 33
33
Point‐by‐Point Multiplication (1 of 2)
b3 b4 b5 b6
b2 Since we are storing our “functions” in vector form,
b1 how do we perform a point‐by‐point multiplication
using a square matrix?
f1
f5 f6
f2
b x f x Bf
f3 f4
x1 x2 x3 x4 x5 x6
b1 0 0 0 0 0 f1 b1 f1
0 b 0 0 0 0 f b f
2 2 2 2
?
0 0 b3 0 0 0 f 3 b3 f 3
0 0 0 b4 0 0 f 4 b4 f 4
0 0 0 0 b5 0 f 5 b5 f 5
f 6 b6 f 6
0 0 0 0 0 b6
B f B f
Slide 34
34
17
9/22/2019
Point‐by‐Point Multiplication (2 of 2)
b3 b4 b5 b6
b2 Since we are storing our “functions” in vector form,
b1 how do we perform a point‐by‐point multiplication
using a square matrix?
f1
f5 f6
f2
b x f x Bf
f3 f4
x1 x2 x3 x4 x5 x6
b1 0 0 0 0 0 f1 b1 f1
0 b 0 0 0 0 f b f
2 2 2 2
0 0 b3 0 0 0 f 3 b3 f 3
0 0 0 b4 0 0 f 4 b4 f 4
0 0 0 0 b5 0 f 5 b5 f 5
f 6 b6 f 6
0 0 0 0 0 b6
B f B f
Slide 35
35
First‐Order Partial Derivative (1 of 2)
How can a square matrix be constructed so that when it premultiplies a vector it gives a vector containing the
first‐order partial derivative?
f x Dx f
f1
f2 f3 f4
f5 f6
x
x1 x2 x3 x4 x5 x6
0 1 0 0 0 0 f1 f 2 f 0 2 x
1 0 1 0 0 0 f f f 2
2 3 1 x
?
1 0 1 0 1 0 0 f 3
f 4 f 2 2 x
2 x 0 0 1 0 1 0 f 4 f 5 f 3 2 x
0 0 0 1 0 1 f 5 f 6 f 4 2 x
0 0 1 0 f 6 f 7 f 5 2 x
0 0
Dx f Dx f
Slide 36
36
18
9/22/2019
First‐Order Partial Derivative (2 of 2)
How can a square matrix be constructed so that when it premultiplies a vector it gives a vector containing the
first‐order partial derivative?
f x Dx f
f1
f2 f3 f4
f5 f6
x
x1 x2 x3 x4 x5 x6
0 1 0 0 0 0 f1 f 2 f 0 2 x
1 0 1 0 0 0 f f f 2
2 3 1 x
1 0 1 0 1 0 0 f 3
4 2 x
f f 2
2 x 0 0 1 0 1 0 f 4 f 5 f 3 2 x
0 0 0 1 0 1 f 5 f 6 f 4 2 x
0 0 1 0 f 6 f 7 f 5 2 x
0 0
Dx f Dx f
Slide 37
37
Second‐Order Partial Derivative (1 of 2)
How can a square matrix be constructed so that when it premultiplies a vector it gives a vector containing the
second‐order partial derivative?
2
f x Dx2 f
f1
f2 f3 f4
f5 f6
x 2
x1 x2 x3 x4 x5 x6
2 1 0 0 0 0 f1 f 2 2 f1 f 0 x
2
1 2 1 0 0 0 f f 2 f f 2
2 3 2 1 x
?
1 0 1 2 1 0 0 f 3 f 4 2 f3 f 2 x
2
2x 0 0 1 2 1 0 f 4 f 5 2 f 4 f 3 2x
0 0 0 1 2 1 f 5 f 6 2 f 5 f 4 2x
0 0 0 0 1 2 f 6 f 7 2 f 6 f 5 2x
Dx2 f D2 f
x
Slide 38
38
19
9/22/2019
Second‐Order Partial Derivative (2 of 2)
How can a square matrix be constructed so that when it premultiplies a vector it gives a vector containing the
second‐order partial derivative?
2
f x Dx2 f
f1
f2 f3 f4
f5 f6
x 2
x1 x2 x3 x4 x5 x6
2 1 0 0 0 0 f1 f 2 2 f1 f 0 x
2
1 2 1 0 0 0 f f 2 f f 2
2 3 2 1 x
1 0 1 2 1 0 0 f 3 f 4 2 f3 f 2 x
2
2 2
x 0 0 1 2 1 0 f 4 f 5 2 f 4 f 3 x
0 0 0 1 2 1 f 5 f 6 2 f 5 f 4 2x
0 1 2 f 6 f 7 2 f 6 f 5 2x
0 0 0
D2 f D2 f
x x
Slide 39
39
Why Do We Need Separate Derivative Matrices for First‐ and Second‐
Order Derivatives?
It is known that,
2
x 2
x x
?
Dx Dx Dx
2 1 1
Can we just calculate D(2) from D(1)?
Yes, but this does not make efficient use of the grid. For a 5‐point, 1D grid, we have
1 0 1 0 0 2 1 0 0 0
0 2 0 1 0 1 2 1 0 0
1 1
Dx Dx 1 0 2 0 1 Dx 2 0 1 2 1 0
1 1 2
2 x x
2
0 1 0 2 0 0 0 1 2 1
0 0 1 0 1 0 0 0 1 2
This is not as accurate because it calculates the This derivate matrix makes optimal use of the
derivative with poorer grid resolution than is available. available grid resolution.
Slide 40
40
20
9/22/2019
USE SPARSE MATRICES!!!!!!!
WARNING !!
The derivative operators will be EXTREMELY large matrices.
For a small grid that is just 100200 points:
NEVER AT ANY POINT should you use FULL MATRICES in the
finite‐difference method. Not even for intermediate steps. NEVER!
Slide 41
41
Placing Diagonals into Sparse Matrices in MATLAB
A =
[ 1 0 0 0 0 0 ]
M = 6; [ 0 1 0 0 0 0 ]
Z = sparse(M,M);
d = ones(M,1);
[ 0 0 1 0 0 0 ]
A = spdiags(d,0,Z); [ 0 0 0 1 0 0 ]
[ 0 0 0 0 1 0 ]
[ 0 0 0 0 0 1 ]
A =
M = 6;
[ 0 1 0 0 0 0 ]
Z = sparse(M,M); [ -1 0 1 0 0 0 ]
d = ones(M,1); [ 0 -1 0 1 0 0 ]
A = spdiags(-d,-1,Z); [ 0 0 -1 0 1 0 ]
A = spdiags(+d,+1,A);
[ 0 0 0 -1 0 1 ]
[ 0 0 0 0 -1 0 ]
42
42
21
9/22/2019
Incorporating Boundary Conditions
Slide 43
43
Dirichlet Boundary Conditions
(1 of 2)
The simplest boundary condition is to assume all function values outside of the grid are zero.
d 2 fi fi 1 2 f i fi 1
x
2
dx 2
f1 f2 f3 f4 f5 f6 f7
x
x1 x2 x3 x4 x5 x6 x7
d 2 f1 0 2 f1 f 2 d 2 f7 f6 2 f7 0
x x
2 2
dx 2 dx 2
44
44
22
9/22/2019
Dirichlet Boundary Conditions
(2 of 2)
x1
d 2 f1 0 2 f1 f 2
dx 2
x
2 2 1 0 0 0 0 0 f1
x2
d 2 f 2 f1 2 f 2 f 3
1 2 1 0 0 0 0 f
dx 2 x
2
2
x3
d 2 f3 f 2 2 f 3 f 4
dx 2
x
2 0 1 2 1 0 0 0 f3
x 1
d f 4 f3 2 f 4 f 5
1 2 1 f4
2
x4
2
0 0 0 0
x
x 0
2
dx 2
x5
d f5 f 4 2 f 5 f 6
2
0 0 1 2 1 0 f5
x
2
dx 2
x6
d 2 f6 f5 2 f6 f7
dx 2
x
2
0 0 0 0 1 2 1 f6
d 2 f7 f6 2 f7 0 0 0 0 0 0 1 2 f
x7 dx 2
x
2
7
x
Dx2
45
45
Periodic Boundary Conditions
(1 of 2)
If the problem is periodic (i.e. keeps repeating), then the value outside of the grid is the
same as the value at the opposite side of the grid.
d 2 fi fi 1 2 f i fi 1
x
2
dx 2
f1 f2 f3 f4 f5 f6 f7
x
x1 x2 x3 x4 x5 x6 x7
d 2 f1 f 7 2 f1 f 2 d 2 f 7 f 6 2 f 7 f1
x x
2 2
dx 2 dx 2
46
46
23
9/22/2019
Periodic Boundary Conditions
(2 of 2)
x1
d 2 f1 f 7 2 f1 f 2
dx 2
x
2 2 1 0 0 0 0 1 f1
x2
d 2 f 2 f1 2 f 2 f 3
1 2 1 0 0 0 0 f
dx 2 x
2
2
x3
d 2 f3 f 2 2 f 3 f 4
dx 2
x
2 0 1 2 1 0 0 0 f3
x 1
d f 4 f3 2 f 4 f 5
1 2 1 f4
2
x4
2
0 0 0 0
x
x 0
2
dx 2
x5
d f5 f 4 2 f 5 f 6
2
0 0 1 2 1 0 f5
x
2
dx 2
x6
d 2 f6 f5 2 f6 f7
dx 2
x
2
0 0 0 0 1 2 1 f6
d 2 f 7 f 6 2 f 7 f1 1 0 0 0 0 1 2 f
x7 dx 2
x
2
7
x
Dx2
47
47
Neuman Boundary Conditions
(1 of 3)
The Neuman boundary condition allows functions to continue linearly off of the grid as if to
infinity.
df i f i 1 fi 1 d 2 fi fi 1 2 fi f i 1
2x x
2
dx dx 2
f1 f2 f3 f4 f5 f6 f7
x
x1 x2 x3 x4 x5 x6 x7
df1 f 2 f1 d 2 f1 df 7 f 7 f 6 d 2 f7
0 0
dx x dx 2 dx x dx 2
48
48
24
9/22/2019
Neuman Boundary Conditions
(2 of 3)
x1 df1 2 f 2 2 f1
dx
2x 2 2 0 0
0 0 0 f1
x2 df 2 f 3 f1
1 0 1 0 0 0 0 f
dx 2x 2
x3 df3 f 4 f 2
dx
2x
0 1 0 1 0 0 0 f3
x 1
x4 df 4 f5 f 3
0 0 1 0 1 0 0 f4
2x
dx 2x
x5 df5 f 6 f 4
0 0 0 1 0 1 0 f5
dx 2x
x6 df 6 f 7 f5
dx
2x 0 0 0 0 1 0 1 f6
df 7 2 f 7 2 f 6 0 0 0 2 2 f
x7
dx
2x 0 0 7
x
Dx 49
49
Neuman Boundary Conditions
(3 of 3)
x1 d 2 f1
dx 2
0 0 0 0 0 0 0 0 f1
x2
d 2 f 2 f1 2 f 2 f 3
dx 2
x
2
1
2 1 0 0 0 0 f2
d f3 f 2 2 f 3 f 4
0 1 2 1 0 0 0 f3
2
x3
x
2
dx 2
x 1
d f 4 f3 2 f 4 f 5
0 1 2 1 0 0 f4
2
x4
2
0
x
x 0
2
dx 2
x5
d 2 f5 f 4 2 f 5 f 6
x
2 0 0 1 2 1 0 f5
dx 2
x6
d 2 f6 f5 2 f6 f7
dx 2
x
2
0 0 0 0 1 2 1 f6
x7 d 2 f7
dx 2
0
0
0 0 0 0 0 0 f7
x
Dx2
50
50
25
9/22/2019
High‐Order Boundary Conditions (1 of 2)
Here we estimate the derivative at the boundaries using special finite‐difference equations
derived specifically for these points.
d 2 fi fi 1 2 f i fi 1
dx 2 h2
f1 f2 f3 f4 f5 f6 f7
x
x1 x2 x3 x4 x5 x6 x7
d 2 f1 2 f1 5 f 2 4 f3 f 4 d 2 f 7 f 4 4 f5 5 f6 2 f7
dx 2 h2 dx 2 h2
51
51
High‐Order Boundary Conditions (2 of 2)
x1
d 2 f1 2 f1 5 f 2 4 f 3 f 4
dx 2
x
2 2 5 4 1 0 0 0 f1
x2
d 2 f 2 f1 2 f 2 f 3
dx 2
x
2
1
2 1 0 0 0 0 f2
d f3 f 2 2 f 3 f 4
0 1 2 1 0 0 f3
2
x3 dx 2
x
2 0
x 1
x4
d 2 f 4 f3 2 f 4 f 5
2
0 0 1 2 1 0 0 f4
x
x 0
2
dx 2
x5
d 2 f5 f 4 2 f 5 f 6
x
2 0 0 1 2 1 0 f5
dx 2
x6
d 2 f6 f5 2 f6 f7
dx 2
x
2
0 0 0 0 1 2 1 f6
d f 7 f 4 4 f5 5 f 6 2 f 7 0 0 1 4 5 2 f7
2
x7 dx 2
x
2
0
x
Dx2 52
52
26
9/22/2019
Solving Ordinary
Differential Equations
Slide 53
53
Boundary Values
Many differential equations give boundary values, or initial conditions, that must be
incorporated into the matrix equation in order to obtain a numerical solution.
For example, a problem may be stated as…
d 2 f x df x
5 6 f x 0 0 x2
dx 2 dx
f 0 2 f 2 0.2
Boundary values
Slide 54
54
27
9/22/2019
Why are Boundary Values Needed?
To solve the differential equation, it is first converted to matrix form.
d2 d
2
f x 5 f x 6 f x 0
dx dx
D x f 5 D x f 6 f 0
2
Next, this is rearranged into standard form 𝐀𝐱 𝐛.
A f 0 A D x2 5 D x 6 I
This is not solvable.
The boundary values have to be incorproated in order to obtain a
f A 0 0
1 nontrivial solution. When this is done, the matrix equation will becomes
A f b
Slide 55
55
Incorporating Boundary Values
It is possible to incorporate the boundary values into the matrix operators that we used
to build [A]. However, it is usually easier to incorporate these directly into the final
matrix equation.
The steps are:
1. Build direct matrix equation [A][f]=[0].
2. For each row in [A] that corresponds to a boundary point:
1. Replace entire row with 0’s.
2. Insert a ‘1’ into the diagonal, or pivot, position.
3. Place the boundary value in the same row of [b].
Slide 56
56
28
9/22/2019
Solve Matrix Equation
This matrix equation is now in the form
A f b
This is solved for [f] as follows.
f A b
1
For small to moderate size problems, this can be solved directly using LU decomposition, or just backward
division in MATLAB.
f = A\b; DO NOT USE f = inv(A)*b
For large problems, iterative methods are preferred, but the conditioning of [A] becomes important and a
solution is not guaranteed.
Slide 57
57
Example (1 of 10)
Solve the following ordinary differential equation.
d 2 f x df x
2
5 6 f x 0 0 x2
dx dx
f 0 2 f 2 0.2
Slide 58
58
29
9/22/2019
Example (2 of 10)
Step 1 – Formulate the matrix equation [A][f] = [0].
d2 d
f x 5 f x 6 f x 0
dx 2 dx
D x f 5 D x f 6 f 0
2
D 5 D 6 I f 0
2
x x
A f 0
A D x2 5 D x 6 I
Slide 59
59
Example (3 of 10)
Step 2 – Calculate grid parameters N and x.
Choose N = 11. This is just a first guess!
Calculate x.
ba 20
x 0.2
N 1 11 1
% GRID PARAMETERS
Nx = 11;
dx = (xb - xa)/(Nx - 1);
Slide 60
60
30
9/22/2019
Example (4 of 10)
D x2 D x I
Step 3 – Build Matrix Operators , , .
50 25 0 0 0 0 0 0 0 0 0
25 50 25 0 0 0 0 0 0 0 0
0 25 50 25 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0 0
0 0 25 50 25 0 0 0 0 0 0
0 0 0 25 50 25 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
D x2 0 0 0 0 25 50 25 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 25 50 25 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 25 50 25 0 0 I 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 25 50 25 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0 25 50 25 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 25 50
0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 1
0 2.5 0 0 0 0 0 0 0 0 0
2.5 0 2.5 0 0 0 0 0 0 0 0
0 2.5 0 2.5 0 0 0 0 0 0 0
0 0 2.5 0 2.5 0 0 0 0 0 0
0 0 0 2.5 0 2.5 0 0 0 0 0
Dx 0 0 0 0 2.5 0 2.5 0 0 0 0
% BUILD MATRIX OPERATORS
0 2.5 0
0
0
0
0
0
0
0
0
0 0
0
2.5
2.5
0
0
2.5
0
0 0
I = speye(Nx,Nx);
0
0 0 0 0 0 0 2.5 0 2.5 0
[DX,DX2] = fdder1d(Nx,dx);
0 0 0 0 0 0 0 0 2.5 0 2.5
0 0 0 0 0 0 0 0 0 2.5 0
Slide 61
61
Example (5 of 10)
Step 4 – Calculate [A] and [b].
44 37.5 0 0 0 0 0 0 0 0 0 0
12.5 44 37.5 0 0 0 0 0 0 0 0 0
0 12.5 44 37.5 0 0 0 0 0 0 0 0
0 0 12.5 44 37.5 0 0 0 0 0 0 0
0 0 0 12.5 44 37.5 0 0 0 0 0 0
A D x2 5 D x 6 I 0 0 0 0 12.5 44 37.5 0 0 0 0 b 0
0 0 0 0 0 12.5 44 37.5 0 0 0 0
0 0 0 0 0 0 12.5 44 37.5 0 0 0
0 0 0 0 0 0 0 12.5 44 37.5 0 0
0 0 0 0 0 0 0 0 12.5 44 37.5 0
0 0 0 0 0 0 0 0 0 12.5 44 0
Slide 62
62
31
9/22/2019
Example (6 of 10)
Step 5 – Incorporate Boundary Values into [A] and [b].
1 0 0 0 0 0 0 0 0 0 0 2
12.5 44 37.5 0 0 0 0 0 0 0 0 0
0 12.5 44 37.5 0 0 0 0 0 0 0 0
0 0 12.5 44 37.5 0 0 0 0 0 0 0
0 0 0 12.5 44 37.5 0 0 0 0 0 0
A D x2 5 D x 6 I 0 0 0 0 12.5 44 37.5 0 0 0 0 b 0
0 0 0 0 0 12.5 44 37.5 0 0 0 0
0 0 0 0 0 0 12.5 44 37.5 0 0 0
0 0 0 0 0 0 0 12.5 44 37.5 0 0
0 0 0 0 0 0 0 0 12.5 44 37.5 0
0 0 0 0 0 0 0 0 0 0 1 0 .2
63
Example (7 of 10)
Step 6 – Solve for [f].
2.0000
2.6842
2.4828
2.0184
1.5407
f A b 1.1349
1
0.8181
0.5816
0.4097
0.2868
0.2000
64
32
9/22/2019
Example (8 of 10)
Step 7 – Check for Convergence
A solution was obtained, but it was all based on a guess for how many points N to use on the grid.
It is critical to check for convergence by increasing the value of N until the changes in the solution
are neglibable.
Converged around
N ≈ 20.
Slide 65
65
Example (9 of 10)
Step 8 – Obtain the final answer at convergence (i.e. N = 20).
% PLOT RESULT
h = plot(x,f,'-ob','LineWidth',2);
h2 = get(h,'Parent');
set(h2,'LineWidth',2,'FontSize',18);
xlabel('$x$','Interpreter','LaTex');
ylabel('$f(x)$','Interpreter','LaTex');
T = [0 0.5 1 1.5 2];
L = {'0' '0.5' '1.0' '1.5' '2.0'};
set(gca,'XTick',T,'XTickLabel',L);
T = [0.5 1 1.5 2 2.5 3];
L = {'0.5' '1.0' '1.5' '2.0' '2.5' '3.0'};
set(gca,'YTick',T,'YTickLabel',L);
Slide 66
66
33
9/22/2019
Example (10 of 10)
Step 9 – Post process the data.
The finite‐difference method is complete.
Usually, after obtaining a solution, the results are analyzed further.
Slide 67
67
Code Altogether
d 2 f x df x % DEFINE BOUNDARY VALUES
5 6 f x 0 xa = 0;
xb = 2;
dx 2 dx fa = 2;
0 x2 fb = 0.2;
f 0 2 f 2 0.2
% GRID PARAMETERS
Nx = 20;
dx = (xb - xa)/(Nx - 1);
% SOLVE PROBLEM
f = A\b;
Slide 68
68
34