Sunteți pe pagina 1din 10

1-9

Finite Difference Method (FDM)


Finite difference methods are numerical processes of solving differential equation
numerically. This method approximates derivatives of functions as the differences between two
points. Thus, finite difference is one of the discretization methods.

Consider a real continuous function f  x  on an interval  m, n  and a   m, n  (i.e. a lies


inside the domain), the exact first derivative of f  x  at x  a is f   a  , which is equal to slope
of the tangential line of f  x  at x  a (figure 1a). If f  x  is a simple function such as
polynomials, we can derive the first derivative of f  x  with ease. However, when the function
goes crazy, finding the analytic derivative might be impossible.

The idea of finite difference is to divide function’s domain into equally spaced blocks (i.e.
discretization) (figure 1b). From here, we have 3 choices to approximate the first derivative of
f  x  at x  a : Forward difference, Backward difference, and Central difference.
1. Forward difference takes the value of the function at the point, which we want to
approximate the derivative  x  , and the point followed it  x  x  . The expression for forward
difference is
f  x  x   f  x  y4  y3
f a  
x x

2. Backward difference, on the other hand, uses the point which comes before the
evaluated point:
f  x   f  x  x  y3  y2
f a  
x x
3. Central difference approximate the derivative from both neighbor points:
f  x  x   f  x  x  y4  y2
f a  
2x 2x

Figure 1 (1a) the derivative of f  x  (1b) discretization of f  x 


2-9

Forward difference Backward difference Central difference

Figure 2 The visualization of forward, backward, and central difference

Example 1 Find the first derivative of f  x  defined on the interval (0,3] at x  2.5 using
forward, backward, and central difference approximation. Let the number of grid be N  6 .
f  x   x 2 sin  e x  ln  x  x  (0,3]

As you can see, directly finding the first derivative of the function would be a nightmare
and therefore, we use finite difference. Firstly, we equally divide our domain into 6 blocks. Then,
x  3 / N  3 / 6  0.5 Table 1 lists the value of the function at each point  y0  y6  .

Table 1: Value of f  x  at each node

x f  x
0 NaN
0.5 -0.1728
1 0
1.5 -0.8881
2 2.4783
2.5 -2.1148
3 9.3385

From the definition, the forward difference of f  x  at x  2.5 is


f  3  f  2.5
f   2.5   22.91
0.5
The approximation for the other cases are

f  2.0   f  2.5 f  3  f  2 
Backward f   2.5   9.182 Central f   2.5   13.72
0.5 0.5
3-9

Figure 3 The approximation of first order derivative of f  x   x 2 sin  e x  ln  x  at x  2.5 from


Forward (red), Backward (Blue), and Central (Green) difference of and their absolute error
comparing to analytic solution as a function of Number of grid  N  .

Using Finite Difference to solve differential equation numerically (first order ODE)

Consider the Ordinary differential equation


du
 x 1 (1.1)
dx
with a boundary condition u  0   0 . The analytic solution for equation (1.1) is
1 2
u  x  x x
2

Figure 4 Graph of the function u  x  and its first derivative u   x  .


4-9

Now, let solve it numerically. The final goal is to solve equation (1.1) on  0,3 using finite
difference. In our case, for simplicity, we shall begin with forward difference.

First, we divide the domain into 3 blocks  x  1 . The problem states that u  0   0 ,
hence, the first point is straightforward, u0  0

To find the next solution  u1  , we discretize equation (1.1) as


du u
  x 1
dx x
u u u u u
Forward difference tells us that  follow initial  1 0 . In addition, u  x   x  1 and, in
x x 0 x x
this case, u  0   u0  0  1  1 , it’s clear that

u1  u0
1
x
u1  u0  x  1
5-9

Repeat the solving steps for u2 and u3 :


u u u3  u2
u1  2 1 u2 
x x
u2  u1  u1x ; u1  1  1  2 and u3  u2  u2 x ; u2   2   1  3
u2  3 u3  6

We can see that the pattern of the solution at node i could be written as
ui  ui 1   x  1 x

N  24
N  12
N 6
N 3

Figure 5 The analytic solution of the problem (red) and the numerical solution (blue) using
forward finite difference method with increasing in the number of grid (smaller grid size) where
N denote the number of grid
6-9

Part of MATLAB code that solve the problem with N  3

% solving equation
Length = 3; % Domain length
N = 3; % Number of grid
dx = Length/N; % Grid spacing (grid size)
node_x = [0:dx:3]; % Position vector of the nodes

u(1) = 0; % initial solution (BD condition)


for i = 2:length(node_x)
u(i) = u(i-1) + dx*(node_x(i-1)+1);
end
figure
plot(node_x,u,'x')

dy
In short, for the ordinary differential equation  f  x  with the boundary condition
dx
y  0   c , the numerical solution of the problem using forward finite difference at note xi is

xi  xi 1  f  x  x

where x is grid size


f  x  is the value of the function at x  xi
i is a node number runs from 0 (left boundary) to N  1 (right boundary) if N is
the number of grid
7-9

Using Finite Difference to solve differential equation numerically (second order ODE)

Consider the second order differential equation


d 2u
 2u  1  0; u   0,5 (1.2)
dx 2
with the boundary conditions u  0   u  5   0 . The analytical solution of equation (1.2) is
 5   x 5  x 
u  x    sec   sin   sin  
 2  2   2

To solve equation (1.2) numerically, we introduce second order finite differences. The
forward scheme is defined by
f  x  2x   2 f  x  x   f  x 
f   x  
 x 
2

Notice that the equation takes 3 points. This means, for 5 points discretization, starting from the
left-most boundary, we have 3 equations for solving 5 independent variables. It is trivial that, at
least 2 boundary conditions have to be given.

3 4 5 node
1 2

Domain boundary

Figure 6 A schematic diagram of the 5 nodes domain. There exists only 3 equations for this
problem.
Discretize the domain into 5 sections (6 nodes;  x  1 ) and rearrange equation (1.2)
d 2u
   2u  1 (1.3)
dx 2
Let’s begin with the first node (left boundary), substitute the second order differential in the
equation (1.3) with forward finite difference approximation
u2  2u1  u0
   2u0  1
 x 
2

u0  2u1  u2    2u0  1
For the latter three nodes, the equation are
u1 2u2 u3    2u1  1
u2 2u3 u4    2u2  1
u3 2u4 u5    2u3  1
8-9

In addition, the boundary condition of this problem are u0  0, u5  0 . Collect these linear
system and rewrite them as
u0 0
u0 2u1 u 2    2u0  1
u1 2u2 u3    2u1  1
u2 2u3 u 4    2u2  1
u3 2u4 u5    2u3  1
u5 0

u0 0
3u0 2u1 u2  1
3u1 2u2 u3  1
3u2 2u3 u4  1
3u3 2u4 u5  1
u5 0

1 0 0 0 0 0   u0   0 
 3 2 1 0 0  
 0  u1   1
0 3 2 1 0 0  u2   1
      (1.4)
0 0 3 2 1 0  u3   1
0 0 0 3 2 1  u4   1
    
0 0 0 0 0 1  u5  0 

Solving matrix multiplication in equation (1.4) require finding the invert of the coefficient
matrix (the big 6x6 matrix). Fortunately, this matrix is nearly diagonal matrix which the
algorithms for inverting is fast and cheap (i.e. require less computational resources). The solution
of equation (1.4) is

 u0   0 
u   
 1  0.2105 
u2   0.5789 
  
u3   0.4737 
u   0.2105 
 4  
u5  0 

Figure 6 Analytic solution (Red) of equation (1.2)


and Numerical solution (Blue)
9-9

Numerically solve Heat equation (PDE)

Consider a metal bar length L is attached to the constant thermal reservoir TLeft and Tright
(Figure 7). The temperature of the bar at position x and time t is T  x, t  . If the system is in
unequilibrium condition Tleft  Tright  T  x, 0   , heat would conduct across the bar and the
changing temperature of the bar is described by

 2T T

t 2
x
where  is heat conductivity (constant).

Figure 7 The bar length L is subjected to a thermal source TLeft and Tright .

First, we discretize the length domain into M sections  x  and time domain into N steps
 t  . Using central finite differences for spatial derivative and forward second-order finite
differences, the temperature at node i and time n is

Ti n 1  1  2  Ti n   Ti n1  Ti n1 


t
where    2 . This could be easily interpreted as
 x 
2

“The temperature at time tn 1 is calculate base on the temperature of node i, i  1 and i  1 from
the previous time step t n ”.

Time

Time

node node node


 x 
2

Because of the discretization, we must choose t  . Otherwise, the solution is unstable.


2
Example 2 Cooling the bar.
10 - 9

A bar length 1.0 m is heated to 100o C ; then, connected to large ice cubes at both end
providing a constant temperature of 0o C . Find the temperature profile of the bar for the first 10 s
. Assume that   0.5 W/m o C .

%1D Heat conductivity


clear
clc
%% Initial parameter
%Spatial domain
L = 1; %Domain Length [m]
Nx = 20; %Number of blocks
dx = L/Nx; %Block size [m]
node_x = [0:dx:L]; %Node Position [m]
%Time domain
Tmax = 10; %Maximum time [s]
Nt = 100000; %Number of time steps
dt = Tmax/Nt; %Time step interval [s]

alpha = 0.5; %Heat conductivity constant [W/m C]

lambda = alpha^2*dt/dx^2;
Temp = 100*ones(Nx+1,Nt); %Set initial Tempurature (Tempurature(space,time))
Temp(1,:) = 0; %Boundary condition [1]
Temp(end,:) = 0; %Boundary condition [2]

%Solving finite difference


for j = 2:size(Temp,2)
for i = 2:(size(Temp,1) - 1)
Temp(i,j) = (1-2*lambda)*Temp(i,j-1) + lambda*(Temp(i+1,j-1) + Temp(i-1,j-1));
end
end

%Plot
figure
subplot(1,3,1)
plot(node_x,Temp(:,1),'rx--');
title('t = 0 s')
xlabel('x [m]')
ylabel('Tempurature [C]')

subplot(1,3,2)
plot(node_x,Temp(:,Nt/2),'rx--');
title('t = 50 s')
xlabel('x [m]')
ylabel('Tempurature [C]')

subplot(1,3,3)
plot(node_x,Temp(:,Nt),'rx--');
title('t = 10 s')
xlabel('x [m]')
ylabel('Tempurature [C]')

*********************

S-ar putea să vă placă și