numerical methods for solving the heat equation, the wave...

28
Numerical methods for solving the heat equation, the wave equation and Laplace’s equation (Finite difference methods) Mona Rahmani January 2019

Upload: others

Post on 10-Mar-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Numerical methods for solving the heat equation, the wave

equation and Laplace’s equation (Finite difference methods)

Mona RahmaniJanuary 2019

Page 2: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Numerical methods are important tools to simulate different physical phenomena.

Numerical simulation of a rotorCourtesy of NASA’s Ames Research Centre

Numerical simulation of flow around a Porsche 956

Courtesy of NASA’s Ames Research Centre

Page 3: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

• Idea: Use finite difference quotients to approximate the derivatives in the PDE.

• Remember that the definition of the derivative of a function f(x) is:

Finite Difference Methods

• If is sufficiently small:

Page 4: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

First derivative approximationsNow, consider the Taylor series expansion of

From which we can find

The truncation error

A forward difference approximationWith first order accuracy

Page 5: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

First derivative approximationsHow can we improve the accuracy of approximation of ?

Subtract the second line from the first line and solve for

The truncation error

A central difference approximationWith second order accuracy

Page 6: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Second derivative approximationsAgain consider the two Taylor series expansions:

This time add the two lines together and solve for

The truncation error

A central difference approximationWith second order accuracy

Page 7: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving the 1D heat equationConsider the initial-boundary value problem:

Boundary conditions (B. C.’s):

Initial condition (I. C.):

Step 1- Define a discretization in space and time:

time step k,

x0 = 0 xN = 1.0

time step k+1,

t

x

Page 8: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving the 1D heat equationStep 2 - Discretize the PDE. Use a forward difference scheme for the time derivative and a central difference scheme for the space derivative:

Or:

Which in the index notation is:

Page 9: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving the 1D heat equationStep 3 - Write the discrete equations for all nodes in a matrix format and solve the system:

Page 10: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving the 1D heat equationStep 3 - Write the discrete equations for all nodes in a matrix format and solve the system:

The boundary conditions

Page 11: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

The discrete approximation of the 1D heat equation:

Numerical stability - for this scheme to be numerically stable, you have to choose sufficiently small time steps

Numerical accuracy - the numerical accuracy of this scheme is first order in time and second order in space, i.e. the error scales linearly with and quadratically with .

Solving the 1D heat equation

Page 12: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving the 1D wave equationConsider the initial-boundary value problem:

Boundary conditions (B. C.’s):

Initial conditions (I. C.’s):

Step 1- Define a discretization in space and time:

time step k,

x0 = 0 xN = 1.0

time step k+1,

t

x

time step k-1,

Page 13: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Step 2 - Discretize the PDE. Use a central difference scheme for both time and space derivatives:

Solving for gives:

Solving the 1D wave equation

The Courant numer

Page 14: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Step 3 - Write the discrete equations for all nodes in a matrix format and solve the system:

Solving the 1D wave equation

A three-level scheme in time

Page 15: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving the 1D wave equationStep 3 - Write the discrete equations for all nodes in a matrix format and solve the system:

Page 16: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving the 1D wave equationStep 3 - Write the discrete equations for all nodes in a matrix format and solve the system:

The boundary conditions

Page 17: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving the 1D wave equation

Since the numerical scheme involves three levels of time steps, to advance to , you need to know the nodal values at and .

Use the two initial conditions to write a new numerical scheme at :

I.C. 1:

I.C. 2:

Or:

A note on time advancing at t =0:

Discrete wave equation at :

Combine them:

Page 18: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

The discrete approximation of the 1D wave equation:

Numerical stability - for this scheme to be numerically stable, you have to choose sufficiently small time steps

Numerical accuracy - the numerical accuracy of this scheme is second order in time and second order in space, i.e. the error scales quadratically with and .

Solving the 1D wave equation

Page 19: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving Laplace’s equationConsider the boundary value problem:

Boundary conditions (B. C.’s):

Step 1- Define a discretization in x and y:

x

y

0 1

1

The physical domain

x

The numerical meshN+1 points in x direction, M+1 point in y direction

y

Page 20: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving Laplace’s equationStep 2 - Discretize the PDE. Use a central difference scheme for space derivatives in x and y directions:

If :

The node (n,m) is linked to its 4 neighbouring nodes as illustrated in the finite difference stencil:

• This finite difference stencil is valid for the interior of the domain:

• The boundary values are found from the boundary conditions.

Page 21: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

Solving Laplace’s equationStep 3 - Solve the system by Jacobi iteration:

Take successive neighbour averages at each iteration k+1 th:

Until there is small change in the solution (i.e. the solution has converged), as measured by:

Page 22: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

• You can program the methods explained before in Matlab (of course, there are many other options, e.g. Python, C++, Fortran, etc.)

• All the Matlab codes are uploaded on the course webpage.

• For each code, you only need to change the input data and maybe the plotting part. The solver is already there!

• Figures will normally be saved in the same directory as where you saved the code.

Matlab codes for numerical solutions of the heat, the wave and Laplace’s equations:

Page 23: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

The Matlab code for the 1D heat equation

PDE:

B.C.’s:

I.C.:

Set the diffusion coefficient here

Set the domain length here

Tell the code if the B.C.’s prescribe the value of u (Dirichlet type ) or its derivative (Neumann type)

Set the values of the B.C.’s on each side

Specify an initial value as a function of x

Specify the number of grid points, i.e. N+1, remember increasing N increases the accuracy.

Set the initial and final time of the computation. (T_final should be large enough to get a steady state solution)

Set the number of time steps to be taken to go from t_0 to t_final. Remember if this number is too low, you might get a numerical instability.

Page 24: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

The Matlab code for the 1D heat equation

PDE:

B.C.’s:

I.C.:

Results:

0 0.2 0.4 0.6 0.8 1x

0

0.2

0.4

0.6

0.8

1

u(x,t)

The steady-state solution

Time increasing

Page 25: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

The Matlab code for the 1D wave equation

PDE:

B.C.’s:

I.C.’s:

Set the wave speed here

Set the domain length here

Tell the code if the B.C.’s prescribe the value of u (Dirichlet type ) or its derivative (Neumann type)

Set the values of the B.C.’s on each side

Specify the initial value of u and the initial time derivative of u as a function of x

Specify the number of grid points, i.e. N+1.

Set the initial and final time of the computation.

Set the number of time steps to be taken to go from t_0 to t_final.

Page 26: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

The Matlab code for the 1D wave equation

PDE:

B.C.’s:

I.C.’s:0 0.2 0.4 0.6 0.8 1x

-1

-0.5

0

0.5

1u(x,t)

Results:

Standing waves

Page 27: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

The Matlab code for Laplace’s equation

PDE:

B.C.’s:

Specify the domain size here

Set the types of the 4 boundary

Set the B.C.’s on each side of the rectangle

Specify the number of grid points in x and y directions, i.e. N+1 and M+1.

Page 28: Numerical methods for solving the heat equation, the wave ...blogs.ubc.ca/mrahmani/files/2019/01/Numerical_methods.pdf · Step 1- Define a discretization in space and time: time

The Matlab code for Laplace’s equation

PDE:

B.C.’s:

Results:

02

0.5

3

1

1.5

y

1 2

x

2

10 0

0

0.5

1

1.5

0 1 2 3x

0

0.5

1

1.5

2

y

0.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8