pdes

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 quantity’s value at some short time interval t in the future by using a finite difference approximation: 2 ( ) () t t f ft t ft t t + +∆ = +∆ . (1) 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, 2 2 f f D t x = , Diffusion Equation (2) 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 2 2 2 2 f f V t x = , Wave Equation (3) 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): 2 2 f f i t x = , Schrodinger Equation (4)

Upload: alenov5

Post on 20-Jul-2016

1 views

Category:

Documents


0 download

TRANSCRIPT

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 quantity’s value at some short time interval t∆ in the future by using a finite difference approximation:

2

( ) ( )tt

ff t t f t tt ∆

+

∂+ ∆ = + ∆

∂. (1)

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,

2

2

f fDt x

∂ ∂=

∂ ∂, Diffusion Equation (2)

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 22

2 2

f fVt x

∂ ∂=

∂ ∂ , Wave Equation (3)

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):

2

2

f fit x

∂ ∂=

∂ ∂, Schrodinger Equation (4)

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 ( )jf 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:

1 1 1 112 2

j j j j j j

j

f f f f f ffx x x x

− + + −− − − ∂= + = ∂ ∆ ∆ ∆

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 2( ) exp( ( 5) )f x x= − − . 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 don’t need the derivative at the points themselves, we can just compute the second derivative from the difference of the derivative between points:

1 12

1 12 2

2j j j j

j j j

j

f f f ff f fx xf

x x x

+ −

+ −

− − − − +∆ ∆∂ = =

∂ ∆ ∆

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). Let’s 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

2( ) ( )t

ff t t f t t Dx

∂+ ∆ = + ∆

∂.

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 0 0( , )j

if f x j x t i t= + ∆ + ∆ , 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.1t0= 0dt= 0.1

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

00.10.20.30.40.50.60.70.80.9

1

11

ijf ==

21

ijf ==

12

ijf ==

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.1t0= 0dt= 0.1

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

initial condition0 0 5 5 5

0.1 0.3090169940.2 0.5877852520.3 0.8090169940.4 0.9510565160.5 10.6 0.9510565160.7 0.8090169940.8 0.5877852520.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

11 1

2

2i i i i ij j j j jf f f f f

Dt x

+− +− − +

=∆ ∆

,

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

( )211 1 1 12 2 2 22 1D t D t D t D ti i i i i i i i

j j j j j j j jx x x xf f f f f f f f∆ ∆ ∆ ∆+

− + − +∆ ∆ ∆ ∆ = + − + = + − +

The excel version of this would be

D12=alpha*C11+(1-2*alpha)*C12+alpha*C13 Where the constant alpha = 2/D t x∆ ∆ 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

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

3. for the initial conditions assume 2( , 0) exp( 4 ( 5) )f x t x= = − × − (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 0.025t∆ = , for D=1. (you really only need the product 2/D t x∆ ∆ 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:

Diffusion Equation

-0.2

0

0.2

0.4

0.6

0.8

1

1.2

0 2 4 6 8 10 12

x

f(x,t)

Series1Series2Series3Series4Series5Series6Series7Series8Series9Series10Series11Series12Series13Series14Series15

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.

1 11 12

2 2

2 2i i i i i ij j j j j jf f f f f f

Vt x

+ −+ −− + − +

=∆ ∆ ,

Derive the equation for advancing each cell value:

( )2 21 11 122 2V ti i i i i i

j j j j j jxf f f f f f∆+ −

+ −∆= − + − +

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,

2. For initial conditions use 2( , 0) exp( 4 ( 2.5) )f x t x= = − × − and ( , 0) 0f x tt

∂= =

∂.

For this, add a time 0.1t t= −∆ = − = 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 0.1t∆ = ( 2 2 2/V t x∆ ∆ =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:

wave equation

00.20.40.60.8

11.2

0 2 4 6

x

f(x,t)

Series1

Series2

Series3

Series4

Series5

Series6

Series7

Series8

Series9

Series10

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

2( , ) exp( 4 ( 2.5) )f x t t x V t= −∆ = − × + ∆ − for the first few time steps, this is what you should get

wave equation

-0.20

0.20.40.60.8

11.2

0 2 4 6

x

f(x,t)

Series1

Series2

Series3

Series4

Series5

Series6

Series7

Series8

Series9

Series10

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 ( 0, ) sin( )f x t 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 ( , 0) sin( / )f x t 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.