Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

9/6/2020

Computational Science:
Computational Methods in Engineering

Introduction to One‐Dimensional
Finite‐Difference Method

Outline

• Introduction & Problem Setup
• Conventional Finite‐Difference Method
• Improved Finite‐Difference Method

1
9/6/2020

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
 y0 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/6/2020

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
  y0
dx 2 dx

 d2 d   
 2   1 y  0  
 dx dx   
 
 
 
 A y    0  y   
 
 
 
 
 
 
 

Slide 6

3
9/6/2020

Conventional
Finite‐Difference Method

Slide 7

Step 1 – Identify Governing Equation & Boundary 
Values
Governing Equation
d 2 y dy
  y0 0  x  10
dx 2 dx

Boundary Values
y  0  1
y 10   5

Slide 8

4
9/6/2020

Step 2 – Approximate Derivatives with Finite‐
Differences (1 of 3)
First, let the function be discrete.  

This allows the derivatives to be approximated with finite‐differences. 

d 2 y dy
  y0
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 𝑥 Δ𝑥⁄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

5
9/6/2020

Step 2 – Approximate Derivatives with Finite‐
Differences (3 of 3)
This is the correct finite‐difference equation.

d 2 y dy
 y0
dx 2 dx

y  x  x   2 y  x   y  x  x  y  x  x   y  x  x 
  y  x  0
x 2 2x

All terms exist at x.

Slide 11

Step 3 – Write Finite‐Difference Equation Using Array 
Indices

d 2 y dy
 y0
dx 2 dx

y  x  x   2 y  x   y  x  x  y  x  x   y  x  x 
  y  x  0
x 2 2x

yi 1  2 yi  yi 1 yi 1  yi 1
  yi  0
x 2 2x

Slide 12

6
9/6/2020

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 2x

1 2 1 1 1
y  2 yi  2 yi 1 
2 i 1
yi 1  yi 1  yi  0
x x x 2x 2x

 1 1   2   1 1 
 2  yi 1  1  2  yi   2   yi 1  0
 x 2x   x   x 2x 

Slide 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

7
9/6/2020

Step 6 – Revise Finite‐Difference Equation
Substituting x = 0.5 into the finite‐difference equation gives

 1 1   2   1 1 
 2  yi 1  1  2  yi   2   yi 1  0
 x 2x   x   x 2x 

 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

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

8
9/6/2020

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

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

9
9/6/2020

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

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

10
9/6/2020

Improved
Finite‐Difference Method

Slide 21

Step 1 – Identify Governing Equation & Boundary 
Values
Governing Equation
d 2 y dy
  y0 0  x  10
dx 2 dx

Boundary Values
y  0  1
y 10   5

Slide 22

11
9/6/2020

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

Step 3 – Factor Out [y] To Put in Standard Form

 D 2x   y    D x  y    y    0

 D   D    I  y  0


2
x x


 A y    0 standard form
 A   D2x    D x    I  A = DX2 - DX + I;

Slide 24

12
9/6/2020

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

Step 4 – Incorporate Boundary Values (2 of 4)
   y1  0 
   y  0
  2   
   y3  0 
    
   y4   0 
   y5  0 
    
   y6  0 
   y  0
Zero out rows in [A] that  

 7   
  y8  0 
   y  0
correspond to the    9   
   y10  0 
boundary points.  For this  

   
  y11   0 
case, these are the rows  

  y12  0 
   
   y13  0 
for y1 and y21.    y  0
   14   
   y15  0 
   y  0
   16   
   y17  0 
 y   
   18  0 
   y19  0 
    
   y20  0 
   y21  0 

Slide 26

13
9/6/2020

Step 4 – Incorporate Boundary Values (3 of 4)
   y1  0 
   y  0
  2   
   y3  0 
    
   y4   0 
   y5  0 
    
   y6  0 
   y  0
  7   
   y8  0 
Insert 1’s in the diagonal  

  y  0
 9   
   y10  0 
position of the rows in [A]     
   y11   0 
where zeros were inserted.    y12  0 
    
   y13  0 
   y  0
   14   
   y15  0 
   y  0
   16   
   y17  0 
 y   
   18  0 
   y19  0 
    
   y20  0 
   y21  0 

Slide 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   
   y8  0 
   y  0
Insert boundary values in    9   
   y10  0 
column 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

14
9/6/2020

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

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

15
9/6/2020

Computational Science:
Computational Methods in Engineering

Matrix Operators

Outline

• One‐Dimensional Matrix Operators
• Incorporating Boundary Conditions

1
9/6/2020

One‐Dimensional Matrix Operators

Slide 3

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 4

2
9/6/2020

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 5

Point‐by‐Point Multiplication (1 of 2)
b3 b4 b5 b6 Since the functions are stored in in column‐vector 
b2
b1 form, how are point‐by‐point multiplications 
performed using a square matrix?
f1
f5 f6
f2
b x f  x Bf 
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 6

3
9/6/2020

Point‐by‐Point Multiplication (2 of 2)
b3 b4 b5 b6 Since the functions are stored in in column‐vector 
b2
b1 form, how are point‐by‐point multiplications 
performed using a square matrix?
f1
f5 f6
f2
b x f  x Bf 
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 7

First‐Order Partial Derivative (1 of 2)
How can a square matrix be constructed so that when it premultiplies a vector it calcualtes 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  f0 
 1 0 1 0 0 0   f  f f 
  2  3 1


?
1 0 1 0 1 0 0 f 3  
  
 1  f4  f2 

2 x  0 0 1 0 1 0   f 4  2 x  f5  f 3 
 0 0 0 1 0 1   f 5 
  
0 0 1 0   f 6 
 f6  f4 


f 7  f5 
 0 0 
   
  
 Dx  f  Dx   f 

Slide 8

4
9/6/2020

First‐Order Partial Derivative (2 of 2)
How can a square matrix be constructed so that when it premultiplies a vector it calcualtes 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  f0 
 1 0 1 0 0 0   f  f f 
  2  3 1
1 

0  1 0 1 0 0   f
  
3
 1  f4  f2 

2 x  0 0 1 0 1 0   f 4  2 x  f5  f 3 
 0 0 0 1 0 1   f 5 
  
 f6  f4 


?
0 0 1 0   f 6 
 0 0  f 7  f5 

   
  
 Dx  f  Dx   f 

Slide 9

Second‐Order Partial Derivative (1 of 2)
How can a square matrix be constructed so that when it premultiplies a vector it calculates 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 
 1 2 1 0 0 0   f   f 2f  f 
  2  3 2 1


?
1  0 1 2 1 0 0   f3  1  f 4  2 f 3  f 2 
  
 0 0 0 1 2 1   f 5 
  
0 0 0 0 1 2   f 6 

 2x  0 0 1 2 1 0   f 4   2x  f 5  2 f 4  f 3 
 f6  2 f5  f 4 


f 7  2 f 6  f 5 
 

   
  
 Dx2  f  D2   f 
  x  
  Slide 10

5
9/6/2020

Second‐Order Partial Derivative (2 of 2)
How can a square matrix be constructed so that when it premultiplies a vector it calculates 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 
 1 2 1 0 0 0   f   f 2f  f 
  2  3 2 1

1  0 1 2 1 0 0   f3  1  f 4  2 f 3  f 2 
    
 2x  0 0 1 2 1 0   f 4   2x  f 5  2 f 4  f 3 
 0 0 0 1 2 1   f 5 
  
 f6  2 f5  f 4 


?
0 1 2   f 6 
 0 0 0  f 7  2 f 6  f 5 
     
D2  f  D2   f 
 x x  
  Slide 11

Why Are Separate Derivative Matrices Needed for 1st and 2nd‐Order 
Derivative Matrices?
It is known that,
2  

x 2
x x
So why can can’t 𝐃 simply be calculated as 𝐃 =𝐃 𝐃 ?

It turns out this does not make efficient use of the grid.  For a 5‐point, 1D grid, this would be

 1 0 1 0 0   2 1 0 0 0 
 0 2 0 1 0   1 2 1 0 0 
1   1  
Dx1 Dx1   1 0 2 0 1  Dx2  2  0 1 2 1 0 
 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 12

6
9/6/2020

USE SPARSE MATRICES!!!!!!!

WARNING !!
The derivative operators will be EXTREMELY large matrices.
For a small grid that is just 100200 points:

Total Number of Points: 20,000


Size of Derivate Operators: 20,000  20,000
Total Elements in Matrices: 400,000,000
Memory to Store One Full Matrix: 6 Gb
Memory to Store One Sparse Matrix: 1 Mb

NEVER AT ANY POINT should you use FULL MATRICES in the 
finite‐difference method.  Not even for intermediate steps.  NEVER!
Slide 13

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 ]

14

7
9/6/2020

Incorporating Boundary Conditions

Slide 15

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

16

8
9/6/2020

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 
17

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

18

9
9/6/2020

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 
19

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
 
2x  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

20

10
9/6/2020

Neuman Boundary Conditions
(2 of 3)
x1 df1 2 f 2  2 f1
dx

2x 2 2 0 0
0 0 0  f1 
x2 df 2 f 3  f1

 1 0 1 0 0 0 0 f 
dx 2x   2
x3 df3 f 4  f 2
dx

2x
 0 1 0 1 0 0 0  f3 
x 1    
x4 df 4 f5  f 3
0 0 1 0 1 0 0  f4 
2x 

dx 2x

x5 df5 f 6  f 4
 0 0 0 1 0 1 0  f5 
   
dx 2x

x6 df 6 f 7  f5
dx

2x 0 0 0 0 1 0 1  f6 
df 7 2 f 7  2 f 6  0 0 0 2 2 f 
x7
dx

2x 0 0  7
x
 Dx  21

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 
22

11
9/6/2020

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

23

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  24

12
9/6/2020

Computational Science:
Computational Methods in Engineering

Solving One‐Dimensional 
Ordinary Differential Equations

Outline

• Solving one‐dimensional ordinary differential 
equations
• Example

1
9/6/2020

Solving One‐Dimensional
Ordinary Differential Equations

Slide 3

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 x2
dx 2 dx

f  0  2 f  2   0.2

Boundary values

Slide 4

2
9/6/2020

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 5

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 the direct matrix equation  𝐴 𝑓 0.
2. For each row in 𝐴 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 𝑏 .

Slide 6

3
9/6/2020

Solve the Matrix Equation
This matrix equation is now in the form
 A f   b

This is solved for  𝑓 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 7

Example

Slide 8

4
9/6/2020

Problem Setup
Solve the following ordinary differential equation. 

d 2 f  x df  x 
2
5  6 f  x  0 0 x2
dx dx

f  0  2 f  2   0.2

% DEFINE BOUNDARY VALUES


xa = 0;
xb = 2;
fa = 2;
fb = 0.2;

Slide 9

Formulation of the Matrix  𝐴
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 10

5
9/6/2020

Calculate the Grid Parameters
Step 2 – Calculate grid parameters N and x.

Choose N = 11.  This is just a first guess!

Calculate x.
ba 20
x    0.2
N  1 11  1

% GRID PARAMETERS
Nx = 11;
dx = (xb - xa)/(Nx - 1);

Slide 11

Build Matrix Operators
 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    0
I  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 12

6
9/6/2020

Build Initial Matrix Equation  𝐴 𝑓 0
Step 4 – Calculate Initial  𝐴 and  𝑏 .
  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 

% CALCULATE [A] AND [b]


A = DX2 + 5*DX + 6*I;
b = sparse(Nx,1);

Slide 13

Incorporate Boundary Values Into  𝐴 and  𝑏
Step 5 – Incorporate Boundary Values into  𝐴 and  𝑏 .
 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 

% INCORPORATE BOUNDARY VALUES


A([1 Nx],:) = 0;
A(1,1) = 1;
A(Nx,Nx) = 1;
b([1 Nx]) = [ fa fb ];
Slide 14

7
9/6/2020

Solve for Unknown Function  𝑓
Step 6 – Solve for  𝑓 .

 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 

% SOLVE FOR [f]


f = A\b;
Slide 15

Check for Convergence
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 16

8
9/6/2020

Obtain Final Converged Answer
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 17

Analyze the Answer
Step 9 – Post process the data.

The finite‐difference method is complete.

Usually, after obtaining a solution, the results are analyzed further.

Slide 18

9
9/6/2020

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 x2 fb = 0.2;

f  0  2 f  2   0.2
% GRID PARAMETERS
Nx = 20;
dx = (xb - xa)/(Nx - 1);

% BUILD MATRIX OPERATORS


I = speye(Nx,Nx);
[DX,DX2] = fdder1d(Nx,dx);

% CALCULATE [A] AND [b]


A = DX2 + 5*DX + 6*I;
b = sparse(Nx,1);

% INCORPORATE BOUNDARY VALUES


A([1 Nx],:) = 0;
A(1,1) = 1;
A(Nx,Nx) = 1;
b([1 Nx]) = [ fa fb ];

% SOLVE PROBLEM
f = A\b;
Slide 19

10

You might also like