PDEs - Finite - Difference - Diffusion - Wave PDF

You might also like

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

Finite Differences and Partial Differential Equations: applications to the Diffusion

Equation and the Wave Equation

Goal

In this lab you will study properties of partial differential equations using numerical
techniques. Finite difference solutions to the diffusion and wave equations demonstrate
the role of boundary conditions and initial conditions.

Background

Last semester we learned a numerical technique for calculating the solutions of equations
of motion (leap frog integration). Basically, the technique was: given an equation for the
time derivative of quantity that can be evaluated a at time t and its value at that instant,
one can compute the quantitys value at some short time interval t in the future by
using a finite difference approximation:
f
f (t + t ) = f (t ) + t . (1)
t t + t
2

In practice, we used this equation to advance a solution to the equations of motion given
non-linear forcing terms that would have otherwise been difficult at best to solve
analytically. In this exercise you will further develop your techniques for using finite
differences to approximate differential equations, and discover that these techniques can
be applied for solving partial differential equations. This technique will be then be used
in an excel spreadsheet program to solve two important equations in physics, namely the
diffusion equation and the wave equation. The diffusion equation,
f 2 f
=D 2 , Diffusion Equation (2)
t x
is used to describe a number of transport phenomena such as heat transport through
matter. As an example, f might represent the temperature in the ground as a function of
depth. Here, D is a constant (known as a diffusivity) and might represent the insulation
value of the material being described. The real value to us at this point, is not that it
helps us understand heat transport (we hope to get to this later in the semester), but that it
resembles the wave equation we are studying in class:
2 f 2 f
2
= V , Wave Equation (3)
t 2 x 2
This wave equation differs in that the second derivative w.r.t. time is used rather than the
first derivative w.r.t. time. The diffusion equation also has a passing resemblance to the
Schrodinger wave equation for a particle moving in free space (which we will be
studying in several weeks):
f 2 f
i= = 2 , Schrodinger Equation (4)
t x
the main difference from the diffusion equation is that the time derivative term is
imaginary. Amazingly, this complex modification changes the behavior from diffusive to
wave-like.

Finite Differences

We are going to need a technique for computing the derivatives from a set of values that
representative a continuous function. Suppose you are given values of a function at a
series of discrete points representing distance, say f j = f ( j x ) and want to compute the
derivative of the function at the point j. The way this is done is to compute the derivative
using finite differences and to average the derivative of the function for the interval
before and after the point in question:

f 1 f f j 1 f j +1 f j f j +1 f j 1
= j + =
x j 2 x x 2x

Exercise 1. Set up a uniform spatial grid in Excel with x ranging from 0 to 10 and
x =0.1. Compute the derivative for f(x)=cos(pi x) and the Gaussian
f ( x) = exp(( x 5) 2 ) . What should you do for the end points for computing the
derivative?

Before we are done, we will also need the expressions for the second derivative of a
function. In some ways, this is easier than doing the derivative because the second
derivative is simply the derivative of the first derivative. We dont need the derivative at
the points themselves, we can just compute the second derivative from the difference of
the derivative between points:

f j +1 f j f j f j 1
x x f 2 f + f
f
2
= = j +1 j j 1

x 2 j
x x 2

Exercise 2. Using the spatial grid and functions set up for Exercise 1, compute the
second derivatives of the three functions. Do these agree with the analytic formula? How
does the accuracy of the finite difference scheme vary with your grid spacing x ?

Solving PDEs

OK, how do we proceed from here? Previously, we were content with finding a solution
that was parameterized by time, that is we found solutions like f(t). Now, we are going to
have to find solutions which are not only functions of time, but also functions of space
f(x,t). Lets start by considering the diffusion equation. Basically, this looks a lot like
the equations you have already studied. To determine the value of some function at some
future instant in time, all you need is information about the spatial derivative at the
present time. Using Eqs. (1) and (2), show yourself that

2 f
f (t + t ) = f (t ) + t D .
x 2 t

The equation for the time derivative is given in terms of spatial derivatives at the present
time. Clearly, what needed is some technique and framework for computing that spatial
derivatives at a given time. Here is how it works.

First, you need to define a grid on which you will determine the value f(x,t). We will
approximate the continuous function f(x,t) by a set of discrete values separated both in
time and space. These values are usually written as f i j = f ( x0 + j x, t0 + it ) , so the
indices i and j represent the temporal and spatial parts of a grid. In Excel, then, the
solution to the differential equation will be represented by a two-dimensional table.

dx= 0.1
t0= 0
dt= 0.1

x (down) t (across) 0 0.1 0.2 0.3 0.4 0.5 0.6

0 f ji==11 f ji==12
0.1 f ji==21
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1

The goal here is to completely fill in this table with values. The solution will satisfy
the differential equation in all interior points, and it will satisfy initial conditions and
boundary conditions on the edges of this table.

The boundary values and initial conditions are quantities that are inputs to the problem
and represent physical conditions. For example, suppose your diffusion problem
represents temperature along a rod of length 1 m and you want to determine the
temperature in the range 0 m< x < 1 m, and the temperature at each end is fixed by being
in contact with a thermal resevoir such that the temperature T(x=2,t) =2 degrees C and
T(x=1,t)=5 degrees C. In addition, you need to know the initial conditions of the
temperature. Suppose we choose an initial condition of T(x,t=0)=sin( x). The initial
conditions and boundary conditions then let you immediately fill in the boundaries of the
grid with values:

x0= 0
dx= 0.1
t0= 0
dt= 0.1

x (down) t (across) 0 0.1 0.2 0.3

initial condition
0 0 5 5 5
0.1 0.309016994
0.2 0.587785252
0.3 0.809016994
0.4 0.951056516
0.5 1
0.6 0.951056516
0.7 0.809016994
0.8 0.587785252
0.9 0.309016994
1 0 2 2 2

To fill in the interior points, we need the finite difference equations approximating the
differential equation. For the diffusion Equation, this becomes

f ji +1 f ji f ji1 2 f ji + f ji+1
=D ,
t x 2

where the time derivative can be compute from the spatial derivative at the present time.
After a little rearranging, this equation becomes

f ji +1 = f ji + D2t f ji1 2 f ji + f ji+1 =


x
Dt
x 2
( )
f ji1 + f ji 1 2 D2 t + D2t f ji+1
x x

The excel version of this would be

D12=alpha*C11+(1-2*alpha)*C12+alpha*C13

Where the constant alpha = Dt / x 2 is determined by your grid spacings and the
magnitude of of the diffusion D.
Exercise 3. Solving the Diffusion Equation.

1. Set up a grid ranging from 0 < x < 10 with a grid spacing of x =0.25;

2. for boundary conditions assume the endpoints are fixed to


f ( x = 0, t ) = f ( x = 10, t ) = 0.0 (fill these in now);

3. for the initial conditions assume f ( x, t = 0) = exp(4 ( x 5) 2 ) (fill these values


in now);

4. Fill in the finite difference equations to advancing the solution in time to fill out
the rest of the grid. Simulate the diffusion equation for 0<t<10, with a time step
of t = 0.025 , for D=1. (you really only need the product Dt / x 2 which is
alpha=1.0).

If you plot a number of different time slices on top of each other, you should get a
solution that looks something like this, showing how an initial disturbance decays away
with time:

Series1
Diffusion Equation
Series2
Series3
1.2
Series4
1 Series5
Series6
0.8
Series7
0.6 Series8
f(x,t)

0.4 Series9
Series10
0.2
Series11
0 Series12
0 2 4 6 8 10 12 Series13
-0.2
Series14
x
Series15

5. How does your solution depend upon t ? You should observe that the time step
needs to be smaller than a critical value for this numerical technique to work.

6. Next, change the initial condition to f(x,t=0)=0, but change the boundary
conditions to f(x=0.0,t)=5.0 and f(x=10.,t)=0.0.
Solving The Wave Equation

As the last exercise, we will solve the wave equation (Equation 3). The main difference
from the diffusion equation is that the time derivative is second order. No problem, as we
already have a formula for the second derivative that we can now apply to the time part
of the equation.

Starting from the finite difference representation of both the time and space derivatives,
ie.

f ji +1 2 f ji + f ji 1 f ji+1 2 f ji + f ji1
=V2
t 2 x 2 ,
Derive the equation for advancing each cell value:

(f 2 f ji + f ji1 )
2 t 2
f ji +1 = 2 f ji f ji 1 + V i
j +1
x 2

How much initial information is required to start this problem? You can see that
information from two previous time steps is required to advance this solution forward.

Exercise 4. Solving the Wave Equation. Solve for 0<t<10, 0<x<5, V=0.5

1. Set up a grid ranging from 0 < x < 5 with a grid spacing of x =0.1,
f
2. For initial conditions use f ( x, t = 0) = exp(4 ( x 2.5) 2 ) and ( x, t = 0) = 0 .
t
For this, add a time t = t = 0.1 = column before the t=0 column, and set the
values equal to the t=0 column. What does this do?
3. Using a time step of t = 0.1 ( V 2 t 2 / x 2 =0.25 ) fill in the finite difference
equations on the interior points of the grid. If you plot all the different times you
get something like this:
w ave equation Series1
Series2
1.2 Series3
1 Series4
0.8 Series5
f(x,t)

0.6 Series6
0.4
Series7
0.2
Series8
0
Series9
0 2 4 6
Series10
x
Series11

4. You can get rid of the two counter propagating modes if you change the time
derivative boundary condition. You can use a clever trick to do this, by
propagating the solution back by a time step, setting
f ( x, t = t ) = exp(4 ( x + V t 2.5) 2 )
for the first few time steps, this is what you should get
w ave equation Series1
Series2
1.2 Series3
1 Series4
0.8 Series5
0.6 Series6
f(x,t)

0.4
Series7
0.2
Series8
0
Series9
-0.2 0 2 4 6
Series10
x
Series11

From this data, you can measure the velocity of the pulse propagation. What is the
value?

5. Observe the reflection of the waves from the two boundaries. What happens to
the wave?

6. Change the boundary condition at x=5 so that the spatial derivative rather than the
value equals 0 (set f(j=b)=f(j=b-1), where b is the cell number of the last grid
point). How does the reflection change.

7. Change the boundary condition at x=5 back to 0. Now, change the boundary
condition at x=0 so that f ( x = 0, t ) = sin( t ) to simulate harmonic drive.
Describe the behavior. Use several frequencies corresponding to = , 2 , 4 .

8. Standing waves. If you initialize the wave to have a fractional set of half
wavelengths within the simulation, ie f ( x, t = 0) = sin(n / L) where L is the size
of the box, standing waves result. Measure the frequency of oscillations for the
first two harmonics of oscillations.

You might also like