0% found this document useful (0 votes)
81 views38 pages

Fac Simile - Only For Aaem Students Fac Simile - Only For Aaem Students

This document is a book chapter about computational partial differential equations for engineering science. It discusses heat conduction and diffusion, including Fourier's law, the heat equation, initial and boundary conditions, and the one-dimensional heat equation. It also covers numerical methods for solving the heat equation, including finite difference methods like the backward Euler method and solving for steady-state diffusion with various boundary conditions. Example MATLAB code is provided to demonstrate the numerical methods.

Uploaded by

Giuseppe Pollio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views38 pages

Fac Simile - Only For Aaem Students Fac Simile - Only For Aaem Students

This document is a book chapter about computational partial differential equations for engineering science. It discusses heat conduction and diffusion, including Fourier's law, the heat equation, initial and boundary conditions, and the one-dimensional heat equation. It also covers numerical methods for solving the heat equation, including finite difference methods like the backward Euler method and solving for steady-state diffusion with various boundary conditions. Example MATLAB code is provided to demonstrate the numerical methods.

Uploaded by

Giuseppe Pollio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

In: Book Title ISBN 0000000000

Editor: Nova Science Publishers, Inc., pp. 1-563 


c 2010 Nova Science Publishers, Inc.

Key Words: Partial differential equations, Numerical methods, Finite-difference methods,


Finite element methods, Mechanics, Scientific computing.
AMS Subject Classification: 35G, 65M, 65N, 74H, 74G, 74E, 74K, 80A.
FA

FA
C

C
SI

SI
M

M
IL

IL
E

E
C OMPUTATIONAL PARTIAL D IFFERENTIAL
-O

-O
E QUATIONS FOR E NGINEERING S CIENCE
N

N
LY

LY
Berardino D’Acunto To my wife Silvana and my daughters Erica and Lucia
FO

FO
University of Naples ”Federico II”
R

R
A

A
A

A
EM

EM
ST

ST
U

U
D

D
EN

EN
TS

TS
vi CONTENTS
2.4. Consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.5. Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.6. Boundary value problems . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.6.1 . Neumann problem . . . . . . . . . . . . . . . . . . . . . . . . . . 36
2.6.2 . Robin problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.6.3 . Heat conduction in multi-layer bodies . . . . . . . . . . . . . . . . 40
Contents 2.7. Backward Euler method . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.7.1 . Neumann and Robin problems . . . . . . . . . . . . . . . . . . . . 44
2.7.2 . Consistency of the backward Euler method . . . . . . . . . . . . . 46
2.7.3 . Non-dimensional heat equation . . . . . . . . . . . . . . . . . . . 47
2.8. Steady-state diffusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Preface iii
2.8.1 . Dirichlet-Robin problem . . . . . . . . . . . . . . . . . . . . . . . 49
FA

FA
1 Heat conduction and diffusion 1 2.8.2 . Neumann problem . . . . . . . . . . . . . . . . . . . . . . . . . . 50
1.1. Fourier’s law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.9. Matlab Interlude 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
C

C
1.2. Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2.9.1 . Function files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.9.2 . Central, Forward and Backward functions . . . . . . . . . . . . . 51
SI

SI
1.3. Heat equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3.1 . Initial-boundary conditions . . . . . . . . . . . . . . . . . . . . . 4 2.9.3 . Function with multiple output variable . . . . . . . . . . . . . . . 56
M

M
1.3.2 . One-dimensional equation . . . . . . . . . . . . . . . . . . . . . . 5 2.9.4 . Flow control structures . . . . . . . . . . . . . . . . . . . . . . . 58
IL

IL
1.3.3 . Heat conduction in moving media . . . . . . . . . . . . . . . . . . 5 2.9.5 . Subfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
1.4. Fick’s law and diffusion . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.9.6 . Useful commands for functions. Anonymous functions . . . . . . 65
E

E
1.4.1 . Diffusion in moving media . . . . . . . . . . . . . . . . . . . . . 7 2.9.7 . Logical operators and logical functions . . . . . . . . . . . . . . . 66
-O

-O
1.5. Porous media and Darcy’s law . . . . . . . . . . . . . . . . . . . . . . . . 7 2.9.8 . Euler functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
1.5.1 . Saturated fluids . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.9.9 . Functions for steady-state problems . . . . . . . . . . . . . . . . . 76
N

N
1.6. Phase-change problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
LY

LY
1.7. Matlab Interlude 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3 Parabolic equations 79
1.7.1 . Creating matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.1. Crank-Nicolson method . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
3.1.1 . Dirichlet problem . . . . . . . . . . . . . . . . . . . . . . . . . . 81
FO

FO
1.7.2 . Sub-matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.7.3 . Matrix manipulations . . . . . . . . . . . . . . . . . . . . . . . . 13 3.1.2 . Robin problem for the Crank-Nicolson method . . . . . . . . . . . 82
3.2. Von Neumann stability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
R

R
1.7.4 . Tridiagonal matrices . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.7.5 . Algebraic operations . . . . . . . . . . . . . . . . . . . . . . . . . 16 3.2.1 . Von Neumann criterion. Examples . . . . . . . . . . . . . . . . . 87
A

A
1.7.6 . Left and right division . . . . . . . . . . . . . . . . . . . . . . . . 16 3.2.2 . Combined method . . . . . . . . . . . . . . . . . . . . . . . . . . 89
A

A
1.7.7 . Flow control structure: for loop . . . . . . . . . . . . . . . . . . . 17 3.3. DuFort-Frankel method . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
EM

EM
1.7.8 . Script files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 3.3.1 . Richardson method . . . . . . . . . . . . . . . . . . . . . . . . . 90
3.3.2 . DuFort-Frankel method . . . . . . . . . . . . . . . . . . . . . . . 92
2 Finite-difference method 21 3.3.3 . Stability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
ST

ST
2.1. Finite differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 3.3.4 . Analysis of consistency . . . . . . . . . . . . . . . . . . . . . . . 93
2.1.1 . Approximation of derivatives . . . . . . . . . . . . . . . . . . . . 23 3.3.5 . Matrix form of DuFort-Frankel method . . . . . . . . . . . . . . . 95
U

U
2.1.2 . Central approximation . . . . . . . . . . . . . . . . . . . . . . . . 24 3.4. Matrix stability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
D

D
2.1.3 . Three-point approximations . . . . . . . . . . . . . . . . . . . . . 25 3.5. Convection-diffusion equation . . . . . . . . . . . . . . . . . . . . . . . . 100
EN

EN
2.1.4 . Functions in two variables . . . . . . . . . . . . . . . . . . . . . . 25 3.5.1 . Explicit methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
2.1.5 . Higher-order derivatives . . . . . . . . . . . . . . . . . . . . . . . 27 3.5.2 . Backward Euler method . . . . . . . . . . . . . . . . . . . . . . . 105
TS

TS
2.1.6 . Approximations to mixed derivatives . . . . . . . . . . . . . . . . 28 3.5.3 . Crank-Nicolson implicit method . . . . . . . . . . . . . . . . . . 109
2.2. Forward Euler method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.5.4 . Diffusion with decay . . . . . . . . . . . . . . . . . . . . . . . . . 111
2.2.1 . Dirichlet problem . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.6. Method of lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
2.3. Introduction to stability . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.6.1 . Dirichlet problem . . . . . . . . . . . . . . . . . . . . . . . . . . 112

v
CONTENTS vii viii CONTENTS
3.6.2 . Neumann problem . . . . . . . . . . . . . . . . . . . . . . . . . . 115 4.6. Matlab Interlude 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
3.6.3 . Convection-diffusion equation . . . . . . . . . . . . . . . . . . . . 117 4.6.1 . Simpson function . . . . . . . . . . . . . . . . . . . . . . . . . . 206
3.7. Variable coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 4.6.2 . Simpson2d function . . . . . . . . . . . . . . . . . . . . . . . . . 210
3.8. Nonlinear equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 4.6.3 . Ode functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
3.8.1 . Explicit method . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 4.6.4 . Bar functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
3.8.2 . Method of lines . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 4.6.5 . Layersfem function . . . . . . . . . . . . . . . . . . . . . . . . . 222
3.9. Matlab interlude 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 4.6.6 . Ball function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
3.9.1 . Crank function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 4.6.7 . Heat functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
3.9.2 . Richardson function . . . . . . . . . . . . . . . . . . . . . . . . . 127 4.6.8 . Wave functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
3.9.3 . Dufort1 function . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
3.9.4 . Upwind function . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 5 Wave motions 239
3.9.5 . Convection1 function . . . . . . . . . . . . . . . . . . . . . . . . 132 5.1. One-dimensional continuum . . . . . . . . . . . . . . . . . . . . . . . . . 240
5.1.1 . Local equations. Concentrated forces . . . . . . . . . . . . . . . . 240
FA

FA
3.9.6 . Mol functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
3.9.7 . Burgers function . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 5.1.2 . Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
C

C
3.9.8 . Nonlinear function . . . . . . . . . . . . . . . . . . . . . . . . . . 142 5.2. Wave equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
5.2.1 . Visco-elastic models . . . . . . . . . . . . . . . . . . . . . . . . . 244
SI

SI
4 Finite element method 145 5.2.2 . Telegraph equation . . . . . . . . . . . . . . . . . . . . . . . . . . 245
M

M
4.1. Essential numerical integration . . . . . . . . . . . . . . . . . . . . . . . . 146 5.3. Courant-Friederichs-Lewy method . . . . . . . . . . . . . . . . . . . . . . 246
4.1.1 . Rectangle rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 5.3.1 . Dirichlet problem . . . . . . . . . . . . . . . . . . . . . . . . . . 247
IL

IL
4.1.2 . Trapezium rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 5.3.2 . Matrix form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
E

E
4.1.3 . Simpson’s rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 5.3.3 . Concentrated forces . . . . . . . . . . . . . . . . . . . . . . . . . 251
-O

-O
4.1.4 . Multiple integrals . . . . . . . . . . . . . . . . . . . . . . . . . . 151 5.3.4 . Neumann and Robin problems . . . . . . . . . . . . . . . . . . . . 253
4.2. Axial motions of bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 5.3.5 . Consistency of CFL method . . . . . . . . . . . . . . . . . . . . . 256
N

N
4.2.1 . Concentrated forces . . . . . . . . . . . . . . . . . . . . . . . . . 155 5.3.6 . Elastic force . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
LY

LY
4.2.2 . Statics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 5.3.7 . Damped wave equation . . . . . . . . . . . . . . . . . . . . . . . 259
4.3. FEM for ordinary differential equations . . . . . . . . . . . . . . . . . . . 156 5.3.8 . Reflection of waves at discontinuities . . . . . . . . . . . . . . . . 260
4.3.1 . Weak form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 5.4. Crank-Nicolson implicit method . . . . . . . . . . . . . . . . . . . . . . . 264
FO

FO
4.3.2 . Method of weighted residuals . . . . . . . . . . . . . . . . . . . . 157 5.4.1 . Initial-boundary problems . . . . . . . . . . . . . . . . . . . . . . 265
4.3.3 . Finite element method. Galerkin method . . . . . . . . . . . . . . 158 5.4.2 . Consistency of Crank-Nicolson method . . . . . . . . . . . . . . . 267
R

R
4.3.4 . Hat-shaped functions . . . . . . . . . . . . . . . . . . . . . . . . 158 5.4.3 . String on visco-elastic base . . . . . . . . . . . . . . . . . . . . . 268
A

A
4.3.5 . Dirichlet problem . . . . . . . . . . . . . . . . . . . . . . . . . . 161 5.4.4 . Weighted implicit method . . . . . . . . . . . . . . . . . . . . . . 268
A

A
4.3.6 . Dirichlet-Neumann problem . . . . . . . . . . . . . . . . . . . . . 166 5.5. Method of lines for the wave equation . . . . . . . . . . . . . . . . . . . . 270
EM

EM
4.3.7 . Problems in Statics of bars . . . . . . . . . . . . . . . . . . . . . . 168 5.5.1 . A nonlinear wave equation . . . . . . . . . . . . . . . . . . . . . 274
4.3.8 . Pure Neumann problem . . . . . . . . . . . . . . . . . . . . . . . 171 5.6. First-order equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
4.3.9 . Steady-state thermal process in multilayered body . . . . . . . . . 172 5.6.1 . Initial-boundary-value problem . . . . . . . . . . . . . . . . . . . 276
ST

ST
4.4. Dirac delta function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 5.6.2 . Variable coefficient . . . . . . . . . . . . . . . . . . . . . . . . . 280
4.4.1 . Dirac function and concentrated forces . . . . . . . . . . . . . . . 175 5.6.3 . Implicit methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
U

U
4.4.2 . Bouncing ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 5.6.4 . The equation Ut + aUx = cU . . . . . . . . . . . . . . . . . . . . 282
D

D
4.4.3 . Heaviside function . . . . . . . . . . . . . . . . . . . . . . . . . . 181 5.6.5 . Lax-Friederichs method . . . . . . . . . . . . . . . . . . . . . . . 284
EN

EN
4.5. Time-dependent problems . . . . . . . . . . . . . . . . . . . . . . . . . . 182 5.6.6 . Lax-Wendroff method . . . . . . . . . . . . . . . . . . . . . . . . 286
4.5.1 . Heat equation. Dirichlet problem . . . . . . . . . . . . . . . . . . 182 5.6.7 . Three-time-level method . . . . . . . . . . . . . . . . . . . . . . . 288
TS

TS
4.5.2 . Neumann problem . . . . . . . . . . . . . . . . . . . . . . . . . . 188 5.6.8 . McCormack method . . . . . . . . . . . . . . . . . . . . . . . . . 289
4.5.3 . Wave equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 5.7. Nonlinear equations. Conservative form . . . . . . . . . . . . . . . . . . . 289
4.5.4 . Dirichlet problem . . . . . . . . . . . . . . . . . . . . . . . . . . 197 5.7.1 . Equation for traffic flow . . . . . . . . . . . . . . . . . . . . . . . 289
4.5.5 . Neumann problem . . . . . . . . . . . . . . . . . . . . . . . . . . 201 5.7.2 . Finite difference methods for nonlinear equations . . . . . . . . . 290
CONTENTS ix x CONTENTS
5.8. Systems of first-order equations . . . . . . . . . . . . . . . . . . . . . . . 294 7 Beam equation 393
5.8.1 . Hyperbolic model of heat conduction . . . . . . . . . . . . . . . . 296 7.1. Euler-Bernoulli beam equation . . . . . . . . . . . . . . . . . . . . . . . . 393
5.8.2 . Lax-Wendroff method for systems . . . . . . . . . . . . . . . . . 297 7.1.1 . Initial-boundary conditions . . . . . . . . . . . . . . . . . . . . . 394
5.8.3 . McCormack method for systems . . . . . . . . . . . . . . . . . . 297 7.2. Finite-difference methods . . . . . . . . . . . . . . . . . . . . . . . . . . 395
5.8.4 . Nonlinear systems . . . . . . . . . . . . . . . . . . . . . . . . . . 299 7.2.1 . Matrix form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
5.9. Matlab interlude 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 7.2.2 . Method of lines for the beam equation . . . . . . . . . . . . . . . 400
7.2.3 . Dynamic response under a pulsing action . . . . . . . . . . . . . . 404
5.9.1 . Cfl functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
7.2.4 . Problems in Statics . . . . . . . . . . . . . . . . . . . . . . . . . . 411
5.9.2 . Nicolson function . . . . . . . . . . . . . . . . . . . . . . . . . . 314
7.3. Solutions to beam equation . . . . . . . . . . . . . . . . . . . . . . . . . . 418
5.9.3 . Molwave functions . . . . . . . . . . . . . . . . . . . . . . . . . . 315
7.4. Finite element methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
5.9.4 . Upwind methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 319 7.4.1 . Shape functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
5.9.5 . Advection function . . . . . . . . . . . . . . . . . . . . . . . . . . 322 7.4.2 . Stiffness matrix. Mass matrix . . . . . . . . . . . . . . . . . . . . 424
5.9.6 . Wendroff function . . . . . . . . . . . . . . . . . . . . . . . . . . 325
FA

FA
7.4.3 . Lumped mass matrix . . . . . . . . . . . . . . . . . . . . . . . . . 426
5.9.7 . Conservative functions . . . . . . . . . . . . . . . . . . . . . . . . 326 7.5. FEM in Statics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
C

C
5.9.8 . Mac function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329 7.6. Concentrated forces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
7.7. Mixed beam element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438
SI

SI
6 Elliptic equations 333 7.8. Matlab interlude 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
M

M
6.1. Historical model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 7.8.1 . Beam functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
IL

IL
6.2. Finite-difference method . . . . . . . . . . . . . . . . . . . . . . . . . . . 334 7.8.2 . Pulsing functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
6.2.1 . Five-point method . . . . . . . . . . . . . . . . . . . . . . . . . . 334 7.8.3 . Functions for Statics . . . . . . . . . . . . . . . . . . . . . . . . . 454
E

E
6.2.2 . Truncation error . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 7.8.4 . Functions for FEM . . . . . . . . . . . . . . . . . . . . . . . . . . 458
-O

-O
6.2.3 . Poisson equation . . . . . . . . . . . . . . . . . . . . . . . . . . . 338 7.8.5 . Mixed functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
6.2.4 . Curved boundary . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
N

N
8 Diffusion in two and three space dimensions 473
6.2.5 . Seven-point method . . . . . . . . . . . . . . . . . . . . . . . . . 343
LY

LY
8.1. Heat equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
6.3. Green’s identities. Uniqueness of solutions . . . . . . . . . . . . . . . . . 344 8.1.1 . 3D explicit method . . . . . . . . . . . . . . . . . . . . . . . . . . 474
6.3.1 . Dirichlet problem . . . . . . . . . . . . . . . . . . . . . . . . . . 344 8.1.2 . Dirichlet problem . . . . . . . . . . . . . . . . . . . . . . . . . . 475
FO

FO
6.3.2 . Neumann problem. Mixed problem . . . . . . . . . . . . . . . . . 345 8.1.3 . Neumann problem . . . . . . . . . . . . . . . . . . . . . . . . . . 478
6.3.3 . Robin problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346 8.1.4 . Implicit methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
R

R
6.4. Neumann problem: five point method . . . . . . . . . . . . . . . . . . . . 347 8.1.5 . Crank-Nicolson method . . . . . . . . . . . . . . . . . . . . . . . 485
A

A
6.4.1 . Curved boundary . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 8.2. Heat equation in curvilinear coordinates . . . . . . . . . . . . . . . . . . . 485
A

A
6.5. FEM in two space dimensions . . . . . . . . . . . . . . . . . . . . . . . . 355 8.2.1 . Polar coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
EM

EM
6.5.1 . Basis functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356 8.2.2 . Cylindrical coordinates . . . . . . . . . . . . . . . . . . . . . . . 486
6.5.2 . Dirichlet problem. Weak form . . . . . . . . . . . . . . . . . . . . 362 8.2.3 . Spherical coordinates . . . . . . . . . . . . . . . . . . . . . . . . 487
6.5.3 . Dirichlet-Neumann problem . . . . . . . . . . . . . . . . . . . . . 367 8.2.4 . Circular symmetry . . . . . . . . . . . . . . . . . . . . . . . . . . 489
ST

ST
8.2.5 . Theis solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
6.6. Matlab interlude 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
8.3. Finite element method . . . . . . . . . . . . . . . . . . . . . . . . . . . . 495
U

U
6.6.1 . Fivepoint function . . . . . . . . . . . . . . . . . . . . . . . . . . 372
8.3.1 . Symmetry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
D

D
6.6.2 . Curved function . . . . . . . . . . . . . . . . . . . . . . . . . . . 374
8.4. 2D DuFort-Frankel method . . . . . . . . . . . . . . . . . . . . . . . . . 501
EN

EN
6.6.3 . Dam1 function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377 8.4.1 . Stability analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 502
6.6.4 . Pyramid function . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 8.5. Method of lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
TS

TS
6.6.5 . Laplace function . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 8.5.1 . 3D MOL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506
6.6.6 . Neumann function . . . . . . . . . . . . . . . . . . . . . . . . . . 384 8.6. Convection-diffusion equation . . . . . . . . . . . . . . . . . . . . . . . . 507
6.6.7 . Pilewall function . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 8.6.1 . Explicit methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 507
6.6.8 . Dam2 function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 8.6.2 . Implicit methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
CONTENTS xi
8.6.3 . 3D methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514
8.6.4 . Convection-diffusion equation with decay . . . . . . . . . . . . . 517
8.7. Matlab Interlude 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518
8.7.1 . Euler2D functions . . . . . . . . . . . . . . . . . . . . . . . . . . 518
8.7.2 . Trench function . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
8.7.3 . Circular function . . . . . . . . . . . . . . . . . . . . . . . . . . . 524
8.7.4 . Theis function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
8.7.5 . Symmetry function . . . . . . . . . . . . . . . . . . . . . . . . . 527
8.7.6 . Theisfem function . . . . . . . . . . . . . . . . . . . . . . . . . . 528
8.7.7 . Dufort2D function . . . . . . . . . . . . . . . . . . . . . . . . . . 529
8.7.8 . Upwind2D function . . . . . . . . . . . . . . . . . . . . . . . . . 531
8.7.9 . Crank2D function . . . . . . . . . . . . . . . . . . . . . . . . . . 532
FA

FA
A Elements of Linear Algebra 535
A.1. Eigenvalues and eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . 535
C

C
A.2. Spectral radius . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
SI

SI
A.3. Block matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538
M

M
A.4. Vector norms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541
A.5. Matrix norms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 542
IL

IL
A.6. Similarity transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . 544
E

E
A.7. Hermitian matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
A.8. Matlab Interlude A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546
-O

-O
A.8.1. Generating eigenvalues . . . . . . . . . . . . . . . . . . . . . . . 546
A.8.2. Generating eigenvectors . . . . . . . . . . . . . . . . . . . . . . . 547
N

N
A.8.3. Commands for eigenvalues . . . . . . . . . . . . . . . . . . . . . 548
LY

LY
A.8.4. Norms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 548
FO

FO
B Classification of PDEs 551
B.1. Second-order PDEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 551
R

R
B.1.1. Equations of second order with n independent variables . . . . . . 553
B.2. Systems of first-order PDEs . . . . . . . . . . . . . . . . . . . . . . . . . . 554
A

A
B.2.1. Classification of systems . . . . . . . . . . . . . . . . . . . . . . . 555
A

A
B.3. Matlab Interlude B . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
EM

EM
B.3.1. Save function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
B.3.2. Load function. Import Wizard . . . . . . . . . . . . . . . . . . . . 558
B.3.3. Saving graphs. Copying and pasting . . . . . . . . . . . . . . . . 559
ST

ST
Bibliography 561
U

U
D

D
EN

EN
TS

TS
80 CHAPTER 3. PARABOLIC EQUATIONS
3.1 . Crank-Nicolson method
Consider the forward and backward Euler methods for the heat equation

Ut − αUxx = F, 0 < x < L, 0 < t ≤ T, (3.1.1)

Chapter 3 discussed in Chapter 2 and rewritten for convenience

uj+1 − uji uj − 2uji + uji−1


i
− α i+1 = fij ,
Δt (Δx)2
Parabolic equations uj+1 − uji uj+1 − 2uj+1 + uj+1
i
− α i+1 i i−1
= fij+1 .
Δt (Δx)2
FA

FA
Summing yields
The chapter presents finite-difference methods for one-dimensional parabolic equations. 2D
C

C
and 3D parabolic equations will be considered in Chapter 8. Transient thermal processes uj+1 − uji α(uj+1 j+1
i+1 − 2ui + uj+1 j j j
i−1 + ui+1 − 2ui + ui−1 ) 1
i
= + (fij + fij+1 ). (3.1.2)
SI

SI
within bodies and diffusion are governed by parabolic equations. In addition, but not less Δt 2(Δx) 2 2
important, in the framework of Terzaghi theory, the equations of soil mechanics, which
M

M
describe the pore pressure evolution in porous media, are of parabolic type. Equation (3.1.2) provides a new method, named Crank-Nicolson method. Algorithm (3.1.2)
IL

IL
is clearly consistent, as so are the schemes from which it was derived, see also the worked
Firstly, the Crank-Nicolson implicit method is discussed and the Dirichlet1 , Neumann
Exercise 3.1.2. It is even more accurate. Indeed, if it is assumed the heat equation to be
E

E
and Robin problems are organized in matrix form, which is very useful for applications and
satisfied at the grid point (i, j + 1/2), then the time approximation at the left side can be
future analysis of stability. Initial-boundary-value problems are explicitly solved.
-O

-O
regarded as the central approximation. Likewise, the space approximation at the right side
The Von Neumann criterion for stability is the most widely used tool for the stability is the central approximation at (i, j +1/2) replaced by the average related to the points (i, j)
analysis. The method is plainly introduced and illustrated with several examples. and (i, j + 1). Thus, it is τij = O((Δx)2 ) + O((Δt)2 ), see Exercise 3.1.2. Additionally,
N

N
j+ 1
LY

LY
Richardson method is presented as example of unstable method. A worked exercise
fi 2 has been replaced by the average (fij + fij+1 )/2. Setting r = αΔt/(Δx)2 , from
illustrates that the approximating solution grows without bounds very rapidly. Also, the
(3.1.2) it follows, fig. 3.1.1,
DuFort-Frankel three-time-level method is introduced. The method is very interesting,
FO

FO
since it is explicit and unconditionally stable although a delicate analysis of consistence
is required. Explicit examples of Dirichlet problems are given to emphasize the different
R

R
situations that can occur for this method.
A

A
The matrix stability is discussed and applied to a number of examples. The role of the
A

A
boundary conditions on the stability is pointed out with examples. Explicit and implicit
EM

EM
methods for the convection-diffusion equation are discussed. The mesh Péclet number is
introduced. Equations with a decay term are also considered.
Furthermore, the method of lines is presented and illustrated with engineering appli-
ST

ST
cations. Some sections are devoted to equations with variable coefficients and nonlinear
equations. Figure 3.1.1. Crank-Nicolson method
U

U
D

D
Matlab interlude 3 presents programs for almost all the methods discussed earlier. The
programs are flexible and can easily be modified for other situations. Exercises encourage
EN

EN
−ruj+1 j+1
i−1 + 2(1 + r)ui − ruj+1 j j j j
i+1 = rui−1 + 2(1 − r)ui + rui+1 + Gi , (3.1.3)
the Reader in this direction.
TS

TS
where, for brevity,
1
Peter Gustav Lejeune Dirichlet, German mathematician, (1805-1859). Determined to study Mathematics, Gji = Δt(fij + fij+1 ).
Dirichlet left Germany for Paris where he attended lectures at the Faculté des Sciences. Returned to Germany he
was Professor at the University of Berlin, and then at the University of Göttingen, where he died. He introduced The Crank-Nicolson method is implicit, since uj+1 i cannot be obtained individually by
the modern concepts of function and integral. He applied analysis to the theory of numbers. (3.1.3) for each grid point. Also, it is unconditionally stable, as proved in Section 3.2.1.

79
3.1.
. CRANK-NICOLSON METHOD 81 82 CHAPTER 3. PARABOLIC EQUATIONS
3.1.1. Dirichlet problem The exact solution is the function



ϕ̄ x−x x2 − x
Consider the Dirichlet problem for equation (3.1.1) U (x, t) = erf √ 1 + erf √ .
2 4αt 4αt
U (x, 0) = ϕ(x), 0 ≤ x ≤ L, (3.1.4)
A Matlab program for the problem above will be presented in Section 3.9.1. For L = 10,
U (0, t) = g1 (t), U (L, t) = g2 (t), t > 0. (3.1.5) T = 1, α = 1, n = 500, x1 = L/4, x2 = 7L/8, r = 4.1667, ϕ̄ = 1, exact and Crank-
Nicolson solutions are shown in fig. 3.1.2 (left). They are visually coincident. The error
The discrete version of initial-boundary conditions of (3.1.4)-(3.1.5) is the following E = U − u is displayed in fig. 3.1.2 (right). The percentage relative error is given by
u0i = ϕi , i = 0, . . . , n, nΔx = L, (3.1.6)

uj0 = g1j , ujn = g2j , j = 1, . . . , m, mΔt = T. (3.1.7)


In the Dirichlet problem formula (3.1.3) can be arranged in the following matrix form,
FA

FA
which incorporates boundary conditions (3.1.7)
C

C
⎡ ⎤⎡ ⎤
2(1 + r) −r uj+1
1
⎢ ⎥ ⎢ j+1 ⎥
SI

SI
⎢ −r 2(1 + r) −r ⎥ ⎢ u2 ⎥
⎢ ⎥⎢ ⎥
⎣ · · · ⎦⎣ · ⎦
M

M
−r 2(1 + r) uj+1
IL

IL
n−1
⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
uj1 r(g1j + g1j+1 ) Gj1
E

E
2(1 − r) r
⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢ r 2(1 − r) r ⎥⎢ uj2 ⎥ ⎢ 0 ⎥ ⎢ Gj2 ⎥
-O

-O
=⎢ ⎥⎢ ⎥+⎢ ⎥+⎢ ⎥.
⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦ Figure 3.1.2. Problem (3.1.9)-(3.1.12): solutions (left) and error (right)
r 2(1 − r) ujn−1 r(g2j + g2j+1 ) Gjn−1
N

N
LY

LY
Hence, with apparent definitions of the symbols, maxi |Uim − umi |
e= × 100 = 0.2894.
maxi |Uim |
Buj+1 = Cuj + bj ,
FO

FO
3.1.2. Robin problem for the Crank-Nicolson method
uj+1 = Auj + aj , (A = B −1 C, aj = B −1 bj ). (3.1.8)
R

R
Consider the Robin problem for the Crank-Nicolson method, where the following boundary
Formula (3.1.8) solves the Dirichlet problem. Indeed, aj is a known vector depending on the
A

A
conditions are prescribed
boundary conditions and matrix B. In addition, the vector u0 is given by initial conditions
A

A
(3.1.6). The computing process can start and equation (3.1.8) provides the solution uj for −Ux (0, t) + h1 U (0, t) = g1 (t), Ux (L, t) + h2 U (L, t) = g2 (t). (3.1.13)
EM

EM
all j.
Let us focus our attention on the end x = 0 and replace the space derivative in (3.1.13)1
with the central approximation
ST

ST
Example 3.1.1 Consider the following Dirichlet problem

Ut − αUxx = 0, 0 < x < L, 0 < t ≤ T, uj1 − uj−1 uj+1 − uj+1


U

U
(3.1.9) − + h1 uj0 = g1j , − 1 −1
+ h1 uj+1
0 = g1j+1 .
2Δx 2Δx
D

D

ϕ̄ = constant if 0 < x1 ≤ x ≤ x2 < L,
EN

EN
U (x, 0) = 0 ≤ x ≤ L, (3.1.10) Summing the formulas above yields
0 otherwise,


uj−1 + uj+1 j j+1
−1 − u1 − u1 + 2Δxh1 (uj0 + uj+1 j j+1
0 ) = 2Δx(g1 + g1 ). (3.1.14)
TS

TS
ϕ̄ −x x
U (0, t) = erf √ 1 + erf √ 2 , t > 0, (3.1.11)
2 4αt 4αt The fictitious term uj−1 + uj+1 can be eliminated by (3.1.3) evaluated for i = 0


−1
ϕ̄ L−x x2 − L
U (L, t) = erf √ 1 + erf √ , t > 0. (3.1.12) r(uj−1 + uj+1 = 2(1 + r)uj+1 − 2(1 − r)uj0 − r(uj1 + uj+1 j
2 4αt 4αt −1 ) 0 1 ) − G0 . (3.1.15)
3.1.
. CRANK-NICOLSON METHOD 83 84 CHAPTER 3. PARABOLIC EQUATIONS
Solving (3.1.14) with respect to uj−1 + uj+1
−1 and the result substituted into (3.1.15) yields Exercise 3.1.1 Discuss the Neumann problem for the heat equation by the Crank-
Nicolson method.
(1 + r + rΔxh1 )uj+1
0 − ruj+1
1
Exercise 3.1.2 Discuss the consistency of the Crank-Nicolson method.
= ruj1 + (1 − r − rΔxh1 )uj0 + rΔx(g1j + g1j+1 ) + Gj0 /2. (3.1.16)
Hint. The truncation error is expressed as
Similar reasoning for i = n leads to

(1 + r + rΔxh2 )uj+1
n − ruj+1
n−1
j+1 j+1 j+1 j j j
Uij+1 − Uij α(Ui+1 − 2Ui + Ui−1 + Ui+1 − 2Ui + Ui−1 ) j+ 1
τij = − − fi 2 . (3.1.23)
Δt 2(Δx)2
= rujn−1 + (1 − r − rΔxh2 )ujn + rΔx(g2j + g2j+1 ) + Gjn /2. (3.1.17)
Consider the Taylor’s series expansions
Setting
Hi = r + rΔxhi , i = 1, 2, (3.1.18) j+ 21 j+ 12 j+ 12 (Δt/2)2
Uij+1 = Ui + (Ut )i Δt/2 + (Utt )i
FA

FA
equations (3.1.16)-(3.1.17) are rewritten as 2
(Δt/2)3 4
C

C
j+ 12 j+ 1 (Δt/2)
(1 + H1 )uj+1
0 − ruj+1
1 = ruj1 + (1 − H1 )uj0 + rΔx(g1j + g1j+1 ) + Gj0 /2, (3.1.19) +(Uttt )i + (Utttt )i 2 + O((Δt)5 ), (3.1.24)
3! 4!
SI

SI
(1 + H2 )uj+1
n − ruj+1 j j j j+1 j
n−1 = run−1 + (1 − H2 )un + rΔx(g2 + g2 ) + Gn /2. (3.1.20) j+ 21 j+ 21 j+ 21 (Δt/2)2
Uij = Ui
M

M
− (Ut )i Δt/2 + (Utt )i
Furthermore, equations (3.1.19), (3.1.20) and (3.1.3), rewritten for convenience, 2
IL

IL
j+ 12 (Δt/2)3 j+ 12 (Δt/2)4
−ruj+1 j+1
− ruj+1 j j j j −(Uttt )i + (Utttt )i + O((Δt)5 ).
i−1 + 2(1 + r)ui i+1 = rui−1 + 2(1 − r)ui + rui+1 + Gi , i = 1, . . . , n − 1,
(3.1.25)
E

E
3! 4!
-O

-O
are arranged in the following matrix form, which incorporates the boundary conditions, Subtracting (3.1.25) from (3.1.24) gives
⎡ ⎤⎡ ⎤ Uij+1 − Uij 2
1 + H1 −r uj+1 j+1/2 j+1/2 (Δt)
N

N
0 = (Ut )i + (Uttt )i + O((Δt)4 ). (3.1.26)
⎢ −r 2(1 + r) −r ⎥ ⎢ j+1 ⎥ Δt 24
LY

LY
⎢ ⎥ ⎢ u1 ⎥
⎢ ⎥⎢ ⎥
⎣ · · · ⎦⎣ · ⎦ Furthermore, from the Taylor’s series expansion
−r 1 + H2 uj+1
n
FO

FO
j
Ui+1 − 2Uij + Ui−1
j
(Δx)2
⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤ = (Uxx )ji + (Uxxxx )ji + O((Δx)4 ),
1 − H1 r uj0 g1j + g1j+1 Gj0 /2 (Δx) 2 12
R

R
⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢ r 2(1 − r) r ⎥⎢ uj1 ⎥ ⎢ 0 ⎥ ⎢ Gj1 ⎥
=⎢ ⎥⎢ ⎥ + rΔx ⎢ ⎥+⎢ ⎥. j+1
− 2Uij+1 + Ui−1
j+1
(Δx)2
A

A
⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦ Ui+1
= (Uxx )j+1 + (Uxxxx )j+1 + O((Δx)4 ).
r 1 − H2 ujn g2j + g2j+1 Gjn /2 i i
A

A
(Δx)2 12
EM

EM
Hence, with apparent definition of matrices and vectors, Summing,
j
Buj+1 = Cuj + bj , Ui+1 − 2Uij + Ui−1
j
U j+1 − 2Uij+1 + Ui−1
j+1
+ i+1 = (Uxx )j+1 + (Uxx )ji
ST

ST
2 2 i
(Δx) (Δx)
uj+1 = Auj + aj , (A = B −1 C, aj = B −1 bj ). (3.1.21)
U

U
(Δx)2
Formula (3.1.21) shows that the Robin problem can be solved. Indeed, aj is a known vector +[(Uxxxx )j+1 + (Uxxxx )ji ] + O((Δx)4 ).
D

D
i (3.1.27)
depending on the boundary conditions and matrix B. In addition, the vector u0 is given by 12
EN

EN
the initial conditions. The computing process can start and equation (3.1.21) provides the In addition,
solution uj for all j.
TS

TS
j+ 21 j+ 12 j+ 21 (Δt/2)2
Finally, notice that boundary conditions (3.1.13) can also be approximated with less (Uxx )j+1
i = (Uxx )i + (Uxxt )i Δt/2 + (Uxxtt )i
2
accuracy by
uj − uj0 ujn − ujn−1 j+ 12 (Δt/2)3
− 1 + h1 uj0 = g1j , + h2 ujn = g2j . (3.1.22) +(Uxxttt )i + O((Δt)4 ),
Δx Δx 3!
3.2.
. VON NEUMANN STABILITY 85 86 CHAPTER 3. PARABOLIC EQUATIONS
j+ 1 j+ 1 j+ 1 (Δt/2)2 where
(Uxx )ji = (Uxx )i 2 − (Uxxt )i 2 Δt/2 + (Uxxtt )i 2 pπΔx
2 ξp = 1 − 4r sin2
. (3.2.5)
j+ 12 (Δt/2)3 2
−(Uxxttt )i + O((Δt)4 ). The unknown coefficients ap , p = 1, . . . , n − 1, in (3.2.4) are determined by the initial
3!
conditions. Indeed, by imposing that (3.2.2) is satisfied, from (3.2.4) we get
Summing yields
n−1

j+ 12 j+ 21 ap sin(pπiΔx) = ϕi , i = 1, . . . , n − 1.
(Uxx )j+1
i + (Uxx )ji = 2 (Uxx )i + (Uxxtt )i (Δt/2)2 /2 + O((Δt)4 ). (3.1.28) (3.2.6)
p=1

Substituting (3.1.28) into (3.1.27) gives Solving system (3.2.6) yields the unknown coefficients. Next, note that when i = 0 and
i = n, from (3.2.4) it follows that uj0 = ujn = 0 and boundary conditions (3.2.3) are
j
Ui+1 −2Uij+ j
Ui−1 j+1
Ui+1 − 2Uij
+ j+1
Ui−1 j+1/2 j+1/2 (Δt)2 satisfied. It remains to show that uji , given by (3.2.4), is solution of (3.2.1). This is easier
+ = (Uxx )i + (Uxxtt )i
2(Δx)2 2(Δx)2 8 done if the expression of uji is suitably modified. From Euler formulas it follows
 √ √  √
FA

FA
(Δx)2 sin(pπiΔx) = e −1pπiΔx − e− −1pπiΔx /(2 −1).
+[(Uxxxx )j+1
i + (Uxxxx )ji ] + O((Δx)4 + (Δt)4 ). (3.1.29) (3.2.7)
24
C

C
j+ 1
Substituting (3.2.7) into (3.2.4) yields
Now, use (3.1.26) and (3.1.29) into (3.1.23) and consider that (Ut − αUxx − f )i 2 = 0:
SI

SI
n−1
√ √ n−1
√ √
2 2 uji = (ap /2 −1)ξpj e −1pπiΔx − (ap /2 −1)ξpj e− −1pπiΔx . (3.2.8)
j+1/2 (Δt) j+1/2 (Δt)
M

M
τij = (Uttt )i − α(Uxxtt )i p=1 p=1
24 8
IL

IL
Hence,
(Δx)2
−α[(Uxxxx )j+1 + (Uxxxx )ji ] + O((Δx)4 + (Δt)4 ),
E

E
i n−1
√ √ −1
√ √
24 uji = (ap /2 −1)ξpj e −1pπiΔx − (a−p /2 −1)ξpj e −1pπiΔx . (3.2.9)
-O

-O
where the principal part of the truncation error is explicitly shown. Thus, the Crank- p=1 p=−(n−1)
Nicolson method is consistent and
By using the notations
N

N
τij = O((Δx)2 ) + O((Δt)2 ). √ √
LY

LY
bp = ap /2 −1 if p > 0, b0 = 0, bp = −a−p /2 −1 if p < 0, (3.2.10)
formula (3.2.9) is rewritten as
3.2 . Von Neumann stability
FO

FO
n−1

The Von Neumann approach to stability is based on the discrete Fourier series of a numeri- uji = bp ξpj e −1pπiΔx
. (3.2.11)
R

R
p=−(n−1)
cal method. To understand the question, consider a simple example: the Dirichlet problem
A

A
for the homogeneous heat equation. If this equation is approximated by the forward Euler In addition, notice that
A

A
method, (see Chapter 2), then pπΔx
ξp = 1 − 4r sin2 = 1 − 2r[1 − cos(pπΔx)]
EM

EM
uj+1 = r(uji+1 + uji−1 ) + (1 − 2r)uji , i = 1, . . . , n − 1, (3.2.1) 2
i  √ √ 
−1pπΔx
= 1 − 2r + 2r cos(pπΔx) = 1 − 2r + r e + e− −1pπΔx
. (3.2.12)
where r = αΔt/(Δx)2 . Assume the following initial data
ST

ST
From (3.2.11) and (3.2.12) it follows that
u0i = ϕ(xi ) = ϕi , i = 0, . . . , n, nΔx = L = 1, (3.2.2)
U

U
n−1

uj+1 = bp ξp ξpj e −1pπiΔx
D

D
and homogeneous boundary conditions i
p=−(n−1)
EN

EN
uj0 = 0, ujn = 0, j = 1, . . . , m. (3.2.3) n−1   
√ √ √
= bp ξpj e −1pπiΔx
1 − 2r + r e −1pπΔx
+ e− −1pπΔx
TS

TS
It can be shown that the solution of problem (3.2.1)-(3.2.3) can be expressed by the discrete
p=−(n−1)
Fourier series
n−1
= (1 − 2r) uji + r(uji+1 + uji−1 ), (3.2.13)
uji = ap (ξp )j sin(pπiΔx), i = 0, . . . , n, (3.2.4)
p=1 which is what we were to prove.
3.2.
. VON NEUMANN STABILITY 87 88 CHAPTER 3. PARABOLIC EQUATIONS

3.2.1. Von Neumann criterion. Examples Substituting uji = ξ j e −1βiΔx into formula (3.2.18) yields
√ √
If each term (or harmonic, or mode) of the Fourier series is estimated by the initial data,
−ξre− −1βΔx
+ ξ(1 + 2r) − ξre −1βΔx
= 1.
then the numerical solution will not grow without bound when t increases, giving stability.
The criterion takes its name from the scientist who first developed and used it. Since it Hence,
applies easily, it is the most widely used tool for the stability analysis of finite-difference ξ[1 + 2r − 2r cos(βΔx)] = 1,
methods.
To simulate an arising error, consider two solutions, say (u1 )ji and (u2 )ji , of a linear ξ = 1/(1 + 4r sin2 (βΔx/2)).
finite-difference method with the same boundary conditions and perturbed initial condi-
The Von Neumann condition |ξ| ≤ 1 is satisfied for any value of r. The implicit method
tions. Clearly, the difference
(3.2.18) is stable for any r, that is, unconditionally stable. However, note that the truncation
uji = (u1 )ji − (u2 )ji ,
error increases as O((Δx)2 ) + O(Δt).
is solution of the homogeneous problem. Following the Von Neumann approach, it is suf-
FA

FA
ficient to consider only a single mode of the Fourier series. In other words, it is enough to Example 3.2.3 Similar reasonings can be used to analyze the stability of Crank-Nicolson
analyze the stability of √ method (3.1.3), rewritten for convenience (F = 0),
uji = ξ j e −1βiΔx ,
C

C
(3.2.14)
−ruj+1 j+1
i−1 + 2(1 + r)ui − ruj+1 j j j
i+1 = rui−1 + 2(1 − r)ui + rui+1 .
SI

SI
where ξ is called the amplification factor. From (3.2.14) it is apparent that the error does

not amplify if Substituting uji = ξ j e −1βiΔx
M

M
into the formula above yields
|ξ| ≤ 1. (3.2.15)
IL

IL
This is the well-known Von Neumann criterion for the numerical stability. It is quite severe. ξ[1 + r − r cos(βΔx)] = 1 − r + r cos(βΔx).
E

E
A less restrictive condition will be presented in Remark 3.2.1.
Hence,
-O

-O
ξ = [1 − 2r sin2 (βΔx/2)]/[1 + 2r sin2 (βΔx/2)],
Example 3.2.1 As first application, consider the initial example
N

N
proving that Von Neumann condition |ξ| ≤ 1 is satisfied for any r. The method is uncondi-
uj+1 = r(uji+1 + uji−1 ) + (1 − 2r)uji .
LY

LY
i (3.2.16) tionally stable.
Substituting (3.2.14) into (3.2.16) yields
Remark 3.2.1 As already mentioned, condition (3.2.15) is quite restrictive. In some situ-
FO

FO
√ √ √ √
j+1 −1βiΔx j −1βiΔx − −1βΔx −1βΔx ations, as in Example 3.2.4, it can become inadequate. A more general condition, under
ξ e =ξ e [1 − 2r + r(e +e )].
which the numerical solution does not grow without bound, is the following
R

R
Hence,
A

A
|ξ| = 1 + O(Δt) ⇔ |ξ| ≤ 1 + CΔt, (3.2.19)
A

A
ξ = 1 − 2r + 2r cos(βΔx) = 1 − 2r(1 − cos βΔx) = 1 − 4r sin2 (βΔx/2).
where the positive constant C is independent of Δt, Δx, and β.
EM

EM
In this case, the Von Neumann condition |ξ| ≤ 1 becomes
Example 3.2.4 Consider the equation
ST

ST
−1 ≤ 1 − 4r sin2 (βΔx/2) ≤ 1,
Ut + λU − αUxx = 0, λ, α = constants. (3.2.20)
U

U
which is clearly satisfied if
D

D
r ≤ 1/2. (3.2.17) Approximate equation (3.2.20) by the forward Euler method
EN

EN
This stability condition is the same as the one found in Chapter 2. uj+1 − uji α
i
+ λuji − (uj − 2uji + uji−1 ) = 0.
Δt (Δx)2 i+1
TS

TS
Example 3.2.2 Consider the backward Euler method discussed in Section 2.7 and rewrit-
ten for convenience, Hence, with the usual position r = αΔt/(Δx)2 ,

−ruj+1 j+1
i−1 + (1 + 2r)ui − ruj+1 j
i+1 = ui . (3.2.18) uj+1
i = (1 − 2r)uji − λΔtuji + r(uji+1 + uji−1 ).
3.2.
. VON NEUMANN STABILITY 89 90 CHAPTER 3. PARABOLIC EQUATIONS

Use the Von Neumann criterion for the stability analysis. Substituting uji = ξ j e −1βiΔx 1 − 4r(1 − p) sin2 (βΔx/2)
ξ= .
into the formula above yields 1 + 4rp sin2 (βΔx/2)

ξ = 1 − 2r + 2r cos(βΔx) − λΔt = 1 − 2r(1 − cos βΔx) − λΔt, Therefore, the Von Neumann condition |ξ| ≤ 1 is satisfied if

ξ = 1 − 4r sin2 (βΔx/2) − λΔt. −1 − 4rp sin2 (βΔx/2) ≤ 1 − 4r(1 − p) sin2 (βΔx/2) ≤ 1 + 4rp sin2 (βΔx/2),

If r ≤ 1/2, then, as proved in Example 3.2.1, |1 − 4r sin2 (βΔx/2)| ≤ 1. Thus, from the where the second inequality is always satisfied and the first leads to
last equation we get
|ξ| ≤ 1 + |λ|Δt, 2r(1 − 2p) sin2 (βΔx/2) ≤ 1.
showing that stability condition (3.2.19) is satisfied. Notice that the change of dependent In conclusion, when
variable 1 − 2p ≤ 0 ⇔ 1/2 ≤ p ≤ 1,
W = U exp(λt) ⇔ U = W exp(−λt),
FA

FA
the combined method is unconditionally stable; otherwise, when p < 1/2, the method is
reduces equation (3.2.20) to the heat equation
stable if
C

C
Wt − αWxx = 0. r ≤ 1/2(1 − 2p). (3.2.22)
SI

SI
Notice that relationship (3.2.22) reduces to r ≤ 1/2 for p = 0.
M

M
Remark 3.2.2 As noted, the influence of boundary conditions on the stability is completely
IL

IL
neglected by the Von Neumann criterion. In Section 3.4 it will be shown that the stability 3.3 . DuFort-Frankel method
E

E
result (3.2.17) must be modified when special boundary conditions are considered. There-
fore it is clear that the Von Neumann criterion only provides a necessary condition for the 3.3.1. Richardson method
-O

-O
stability. In other words, only if the boundary data do not introduce instability, the result
If both derivatives in the heat equation (3.1.1) are approximated by the central finite-diffe-
obtained by this method is also a sufficient condition.
N

N
rence formulas, we obtain the following method
LY

LY
3.2.2. Combined method uj+1 − uj−1 uj − 2uji + uji−1
i i
− α i+1 = 0,
2Δt (Δx)2
FO

FO
Crank-Nicolson method (3.1.3) can be generalized to obtain a more general algorithm for
the heat equation. By approximating the second-order derivative by a weighted average of
where F = 0 for brevity. Hence,
the central approximations related to the points (i, j) and (i, j + 1), we get
R

R
uj+1 − uj−1 = 2r(uji−1 − 2uji + uji+1 ),
A

A
(3.3.1)
uj+1
i − uji α[p(uj+1 j+1
i+1 − 2ui + uj+1 j j j
i−1 ) + (1 − p)(ui+1 − 2ui + ui−1 )]
i i
=
A

A
2
,
Δt (Δx) where r = αΔt/(Δx)2 . Method (3.3.1) is explicit and it is named Richardson method,
EM

EM
or leapfrog method. Since three steps in time are involved, it is a three-time-level method,
(1 + 2pr)uj+1
i − pr(uj+1 j+1
i−1 + ui+1 ) = [1 − 2(1 − p)r]uji + (1 − p)r(uji−1 + uji+1 ), (3.2.21) fig. 3.3.1. Use the Von Neumann criterion to analyze the stability. Substituting uji =
where r = αΔt/(Δx)2 and p is a suitable weighting parameter. For p = 1/2 and p = 1,
ST

ST
(3.2.21) reduces to the Crank-Nicolson method and backward Euler method, respectively.
U

U
In addition, if p = 0, the forward Euler method is recovered. For
D

D
0 < p < 1,
EN

EN
formula (3.2.21) provides a new algorithm named combined method, or variable-weighted
TS

TS
method. The stability
√ of this method can be discussed by Von Neumann criterion. Substi-
tuting uji = ξ j e −1βiΔx into (3.2.21) yields

ξ[1 + 2pr − 2pr cos(βΔx)] = 1 − 2r(1 − p) + 2r(1 − p) cos(βΔx), Figure 3.3.1. Richardson method
3.3.
. DUFORT-FRANKEL METHOD 91 92 CHAPTER 3. PARABOLIC EQUATIONS

ξj e −1βiΔx into (3.3.1) yields The exact solution is the function U (x, t) = sin(πx/L) exp(−(π/L)2 t). A Matlab program
√ √ for the problem above will be presented in Section 3.9.2. For L = 2, T = .5, n = 30,
|ξ|2 − 1 = 2rξ(e −1βΔx
+ e− −1βΔx
− 2), m = 300, the exact and Richardson solutions are shown in fig. 3.3.2 (left). The error
2 E = U − u is displayed in fig. 3.3.2 (right). The percentage relative error is given by
|ξ| − 1 = 4rξ[cos(βΔx) − 1],
maxi |Uim − umi |
|ξ|2 + 8rξ sin2 (βΔx/2) − 1 = 0. e= × 100 = 1.0855 × 10141 .
maxi |Uim |
The roots ξ1 and ξ2 of this equation are such that
3.3.2. DuFort-Frankel method
ξ1 ξ2 = −1, ξ1 + ξ2 = −8r sin2 (βΔx/2). (3.3.2)
Consider again the Richardson method
Therefore, the case |ξ1 | < 1 implies |ξ2 | > 1, giving instability. On the other hand, if
ξ1 = ±1, ξ2 = ∓1, the condition (3.3.2)2 is compatible only with r = 0. The conclusion uj+1 − uj−1 uj − 2uji + uji−1
i i
− α i+1 = 0.
is that such a method is unstable in any case. The numerical results obtained by (3.3.1) 2Δt (Δx)2
FA

FA
can be influenced by rounding errors of increasing amplitude. The Richardson method is
not reliable, see Example 3.3.1. As noted, the Richardson algorithm is a three-time-level If the term uji is replaced by the average
C

C
method. The computing process for these methods requires a special starting procedure,
uji = (uj+1 + uj−1 )/2,
SI

SI
since two lines of values must be known. The first step is provided by the initial conditions i i (3.3.6)
u0i = ϕi = ϕ(xi ). The second step depends on the scheme. For the Richardson method the
M

M
we obtain
second line is the same as the classical explicit method, see Section 3.3.2, uj+1 − uj−1 uj − (uj−1 + uj+1 ) + uji−1
IL

IL
i i
− α i+1 i i
= 0,
2Δt (Δx)2
u1i = r(u0i−1 + u0i+1 ) + (1 − 2r)u0i .
E

E
(1 + 2r)uj+1
i = 2r(uji−1 + uji+1 ) + (1 − 2r)uj−1
i , (3.3.7)
-O

-O
Example 3.3.1 Consider the following Dirichlet problem with r = αΔt/(Δx)2 . Method (3.3.7) is an explicit three-time-level method, named
N

N
DuFort-Frankel method, fig. 3.3.3. As noted in Section 3.3.1, three-time-level methods
Ut − Uxx = 0, 0 < x < L, 0 < t ≤ T, (3.3.3)
LY

LY
U (x, 0) = sin(πx/L), 0 ≤ x ≤ L, (3.3.4)
FO

FO
R

R
A

A
A

A
EM

EM
Figure 3.3.3. DuFort-Frankel method
ST

ST
requires two starting steps. The first line is provided by the initial conditions

u0i = ϕi = ϕ(xi ).
U

U
D

D
To derive the second line, consider equation (3.3.7) for j = 0
EN

EN
(1 + 2r)u1i = 2r(u0i−1 + u0i+1 ) + (1 − 2r)u−1
i , (3.3.8)
TS

TS
Figure 3.3.2. Problem (3.3.3)-(3.3.5): solutions (left) and error (right) Formula (3.3.8) cannot be used because of the fictitious values u−1
i . To eliminate the ghost
values, consider (3.3.6) for j = 0,

U (0, t) = 0, U (L, t) = 0, t > 0. (3.3.5) 2u0i = u1i + u−1


i .
3.3.
. DUFORT-FRANKEL METHOD 93 94 CHAPTER 3. PARABOLIC EQUATIONS
Solve with respect to u−1
i and substitute the result into (3.3.8) to obtain From the Taylor’s series expansion it follows that

u1i = r(u0i−1 + u0i+1 ) + (1 − 2r)u0i , Uij+1 − Uij−1 (Δt)2


(3.3.9) = (Ut )ji + (Uttt )ji + O((Δt)4 ), (3.3.13)
2Δt 6
which is the desired second line. Note that (3.3.9) is the classical explicit method. Uij+1 + Uij−1 = 2Uij + (Utt )ji (Δt)2 + O((Δt) ), 4
(3.3.14)
The DuFort-Frankel method is unconditionally stable, as proved in Section 3.3.3. How-
(Δx) 4
j j
ever, the DuFort-Frankel method should be handled carefully, as illustrated in Section 3.3.4 Ui+1 + Ui−1 = 2Uij + (Uxx )ji (Δx)2 + (Uxxxx )ji 6
+ O((Δx) ). (3.3.15)
where the analysis of the consistency is discussed. 12
Substituting (3.3.13)-(3.3.15) into (3.3.12) yields
3.3.3. Stability (Δt)2 (Δx)2 (Δt)2
τij = (Ut − αUxx )ji + (Uttt )ji − α(Uxxxx )ji + α(Utt )ji
6 12 (Δx)2
The DuFort-Frankel method
+O((Δx)4 ) + O((Δt)4 ) + O((Δt)4 /(Δx)2 ). (3.3.16)
FA

FA
(1 + 2r)uj+1
i = 2r(uji−1 + uji+1 ) + (1 − 2r)uj−1
i , (3.3.10)
The analysis of the consistency leads to consider the limit of truncation error (3.3.16), as
Δx, Δt → 0. Since the ratio O((Δt)2 /(Δx)2 ) is involved, much attention is required. The
C

C
is unconditionally
√ stable, as proved by the Von Neumann analysis of stability. Substituting
uji = ξ j e −1βiΔx into (3.3.10) yields consistency depends on how Δx and Δt tend to zero. Indeed, if Δt → 0 faster than Δx,
SI

SI
for example,
Δt = c(Δx)2 , c = constant,
M

M
(1 + 2r)ξ 2 = 4rξ cos(βΔx) + (1 − 2r),
τij goes to zero and the method DuFort-Frankel is consistent with the heat equation. If Δx
IL

IL

2r cos(βΔx) ± 1 − 4r2 sin2 (βΔx) and Δt tend to zero at the same ratio, say
E

E
ξ= . (3.3.11)
1 + 2r Δt = cΔx, c = constant,
-O

-O
If 1 − 4r sin2 (βΔx) < 0, then (3.3.11) yields
the scheme is inconsistent with the heat equation, although it is compatible with the hyper-

N

N
bolic equation, Appendix B,
2r cos(βΔx) ± I 4r2 sin2 (βΔx) − 1
LY

LY
ξ= , Ut − αUxx + αc2 Utt = 0.
1 + 2r
√ If Δx → 0 faster than Δt, τij goes to infinity and the scheme is inconsistent. This result
FO

FO
where I= −1. Hence,
4r2 − 1 imposes a constraint to the mesh dimensions and a bound to the unconditional freedom
|ξ|2 = < 1.
R

R
1 + 4r + 4r2 allowed by the stability analysis.

Instead, if 1 − 4r sin2 (βΔx) ≥ 0, then it is easy to realize that


A

A
Example 3.3.2 Consider the following Dirichlet problem
A

A

Ut − Uxx = 0, 0 < x < L, 0 < t ≤ T, (3.3.17)
EM

EM
−1 − 2r ≤ 2r cos(βΔx) ± 1 − 4r2 sin2 (βΔx) ≤ 1 + 2r,

which is equivalent to U (x, 0) = sin(πx/L), 0 ≤ x ≤ L, (3.3.18)


ST

ST
|ξ| ≤ 1,
U (0, t) = 0, U (L, t) = 0, t > 0. (3.3.19)
U

U
proving that the DuFort-Frankel method is unconditionally stable. The exact solution is the function U (x, t) = sin(πx/L) exp(−(π/L)2 t). A Matlab program
D

D
for the problem above will be presented in Section 3.9.3. For α = 1, L = 1, T = .25,
EN

EN
3.3.4. Analysis of consistency n = 40, m = 400, it is Δt = (Δx)2 and the DuFort-Frankel method is consistent with the
heat equation. Exact and approximating solutions are shown in fig. 3.3.4 (left). The error
An interesting question of the DuFort-Frankel method is the analysis of the consistency.
TS

TS
E = U − u is displayed in fig. 3.3.4 (right). The percentage relative error is given by
The truncation error is expressed by
maxi |Uim − umi |
U j − (Uij−1 + Uij+1 ) + Ui−1
j e= × 100 = 1.3983.
U j+1 − Uij−1 maxi |Uim |
τij = i − α i+1 2
. (3.3.12)
2Δt (Δx)
3.3.
. DUFORT-FRANKEL METHOD 95 96 CHAPTER 3. PARABOLIC EQUATIONS
From (3.3.20), in matrix notations,
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
uj+1
1 0 1 uj1 uj−1
1 uj0
⎢ j+1 ⎥ ⎢ 1 0 1 ⎥⎢ ⎥ ⎢ j−1 ⎥ ⎢ ⎥
⎢ u2 ⎥ ⎢ ⎥⎢ uj2 ⎥ ⎢ u ⎥ ⎢ 0 ⎥
⎢ ⎥ = p⎢ ⎥⎢ ⎥+q⎢ 2 ⎥ + p⎢ ⎥.
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦
uj+1
n−1
1 0 ujn−1 uj−1
n−1
ujn

Hence, with apparent definitions of vectors and matrices,

uj+1 = pAuj + quj−1 + aj . (3.3.21)

The first starting step for vector equation (3.3.20) is given by the vector form of the initial
conditions
FA

FA
u0 = ϕ.
Figure 3.3.4. Problem (3.3.17)-(3.3.19): solutions (left) and error (right)
The second starting step is derived from the vector version of (3.3.9)
C

C
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
SI

SI
u11 1 − 2r r u01 ru00
⎢ u12 ⎥ ⎢ 1 − 2r r ⎥⎢ u02 ⎥ ⎢ 0 ⎥
⎢ ⎥ ⎢ r ⎥⎢ ⎥ ⎢ ⎥
M

M
⎢ ⎥=⎢ ⎥⎢ ⎥+⎢ ⎥ , u1 = Cu0 +c0 .
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
IL

IL
u1n−1 r 1 − 2r u0n−1 ru0n
E

E
Although the DuFort-Frankel method is a three-time-level method, it can be arranged in
-O

-O
a form typical of two-time-level methods. Indeed, notice that equation (3.3.21) is equivalent
to       
N

N
uj+1 pA qIn−1 uj aj
= + ,
LY

LY
uj In−1 On−1 uj−1 0
where In−1 is the unit matrix or order n − 1 and On−1 is the zero matrix. Hence, the desired
FO

FO
equation
Figure 3.3.5. DuFort-Frankel method: damping waves and inconsistent case (right) vj+1 = Bvj + bj , (3.3.22)
R

R
For α = 1, L = 1, T = 5, n = 20, m = 100, it is Δt = Δx and the DuFort-Frankel where      
A

A
uj pA qIn−1 aj
method is consistent with the hyperbolic equation vj = , B= , bj = .
A

A
uj−1 In−1 On−1 0
EM

EM
Ut − Uxx + Utt = 0.
The numerical solution shows damping waves, fig. 3.3.5 (left). 3.4 . Matrix stability
For α = 1, L = 1, T = 5, n = 100, m = 5, it is (Δt)2 = Δx and the DuFort-Frankel
ST

ST
method is inconsistent, fig. 3.3.5 (right). Reading this section requires elements of vector and matrix norms (see Appendix A).
U

U
Consider an initial-boundary-value problem for a partial differential equation. Assume
D

D
that the corresponding finite-difference problem can be arranged in matrix form as follows
3.3.5. Matrix form of DuFort-Frankel method
EN

EN
The DuFort-Frankel method uj = Auj−1 + aj−1 , (3.4.1)
TS

TS
(1 + 2r)uj+1
i = 2r(uji−1 + uji+1 ) + (1 − 2r)uj−1
i , where aj−1 is a known vector depending on boundary conditions and source term. Some
examples have already been provided. Further examples will be given later on. Denote by
with the positions p = 2r/(1 + 2r), q = (1 − 2r)/(1 + 2r), can be rewritten as
(u1 )ji , (u2 )ji two solutions of equation (3.4.1), which have the same boundary conditions,
uj+1
i = p(uji−1 + uji+1 ) + quj−1
i . (3.3.20) but different data at a prefixed time, say the initial one. If (u1 )ji is regarded as the exact
3.4.
. MATRIX STABILITY 97 98 CHAPTER 3. PARABOLIC EQUATIONS
solution and (u2 )ji as the solution perturbed by a rounding error arising at the initial time, It is easy to realize that
the stability analysis leads to consider ||A||1 = ||A||∞ = 2r + |1 − 2r|,
uji = (u1 )ji − (u2 )ji . showing that, only when
r ≤ 1/2, (3.4.9)
This difference will satisfy the homogeneous problem, that is, equation (3.4.1) with homo-
geneous boundary conditions and vanishing source term: condition (3.4.8) is satisfied with ||A||∞ = ||A||1 = 1. Same result is obtained by consid-
ering the Euclidean norm ||A||2 . Since A is real and symmetric, we have ||A||2 = ρ(A).
uj = Auj−1 . (3.4.2) The eigenvalues of A are evaluated by the general formula of the eigenvalues of tridiagonal
Hence, matrices
u j = Aj u 0 . (3.4.3) λi = 1 − 2r + 2r cos(iπ/n) = 1 − 4r sin2 (iπ/2n).

Next, note that, for every matrix norm ||.||, subordinate to vector norm, from (3.4.3) it Therefore, |λi | ≤ 1 only if (3.4.9) is satisfied.
follows
FA

FA
||uj || = ||Aj u0 ||, (3.4.4) Example 3.4.2 Consider the backward Euler method discussed in Section 2.7. The
Dirichlet problem was arranged in matrix form (3.4.1) with A = B −1 , where B is the
||uj || ≤ ||Aj || ||u0 ||.
C

C
(3.4.5)
(n − 1)-by-(n − 1) tridiagonal matrix
SI

SI
Consequently, a necessary and sufficient condition for the stability of the numerical method ⎡ ⎤
(3.4.2) is the following 1 + 2r −r
M

M
⎢ −r 1 + 2r −r ⎥
||Aj || ≤ K, (3.4.6) ⎢
B=⎢

⎥.
· · ·
IL

IL
⎣ ⎦
where the positive constant K is independent of j, Δx, Δt. Indeed, from the stability
−r 1 + 2r
E

E
definition
||uj || ≤ K||u0 ||, (3.4.7) The eigenvalues A are expressed as
-O

-O
and (3.4.4) it follows
||Aj u
0 ||
λi = 1/[1 + 2r − 2r cos(iπ/n)] = 1/[1 + 4r sin2 (iπ/2n)].
N

N
≤ K, (||u0 || = 0),
||u0 ||
LY

LY
Hence, |λi | ≤ 1 for any r. So, ||A||2 ≤ 1, proving unconditional stability.
which implies (3.4.6). Conversely, from stability condition (3.4.6) and inequality (3.4.5)
we immediately find (3.4.7).
FO

FO
Example 3.4.3 In Sections 2.7 the Robin problem for the heat equation was discussed by
Since ||Aj || ≤ ||A||j , the matrix stability condition ||Aj || ≤ K is clearly satisfied when the forward Euler method. The problem was reduced to vector equation (3.4.1) with the
R

R
||A|| ≤ 1. (3.4.8) (n + 1)-by-(n + 1) matrix A is expressed as
⎡ ⎤
A

A
In addition, it is satisfied under the less restrictive condition H1 r
A

A
⎢ r 1 − 2r r ⎥
⎢ ⎥
||A|| = 1 + O(Δt), ⇔ ||A|| ≤ 1 + CΔt, A=⎢ ⎥.
EM

EM
⎣ · · · ⎦
where C is a constant independent of Δt, Δx. Indeed, for jΔt ≤ T we have r H2
ST

ST
||Aj || ≤ ||A||j ≤ (1 + CΔt)j ≤ (1 + CT /j)j ≤ exp(CT ). where Hi = 1 − 2r(1 + hi Δx), i = 1, 2. It can be proved that the ∞-norm
U

U
Applications of the matrix method are provided in Examples 3.4.1,. . .,3.4.5. ||A||∞ = max{|H1 |, |H2 |, |1 − 2r|} + 2r
D

D
EN

EN
Example 3.4.1 In Section 2.2.1 the Dirichlet problem for the heat equation was arranged is not greater than 1, under the condition
in matrix form (3.4.1) with the (n − 1)-by-(n − 1) tridiagonal matrix A expressed as
r ≤ 1/(2 + hi Δx), i = 1, 2.
TS

TS
⎡ ⎤ (3.4.10)
1 − 2r r
⎢ 1 − 2r r ⎥
⎢ r ⎥ Indeed, if the norm is given by
A=⎢ ⎥.
⎣ · · · ⎦
r 1 − 2r ||A||∞ = |Hi | + 2r = |1 − 2r(1 + hi Δx)| + 2r, for i = 1, or i = 2,
3.4.
. MATRIX STABILITY 99 100 CHAPTER 3. PARABOLIC EQUATIONS
consider (3.4.10) rewritten as In Section 3.2.1, equation (3.4.12) was approximated by the classical explicit method

4r + 2rhi Δx ≤ 2, ⇒ −(1 − 2r − 2rhi Δx) + 2r ≤ 1. uj+1


i = (1 − 2r − λΔt)uji + r(uji+1 + uji−1 ), (3.4.13)

Since it is apparent that where


(1 − 2r − 2rhi Δx) + 2r ≤ 1, r = αΔt/(Δx)2 .
we conclude that |1 − 2r − 2rhi Δx| + 2r ≤ 1 and In matrix notations, equation (3.4.13) is rewritten as
||A||∞ ≤ 1. ⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
(3.4.11) uj+1
1 a r uj1 uj0
⎢ j+1 ⎥ ⎢ r ⎥⎢ ⎥ ⎢ ⎥
⎢ u2 ⎥ ⎢ a r ⎥⎢ uj2 ⎥ ⎢ 0 ⎥
On the other hand, if the norm is given by ⎢ ⎥=⎢ ⎥⎢ ⎥+r⎢ ⎥,
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
||A||∞ = |1 − 2r| + 2r, uj+1
n−1
r a ujn−1 ujn
FA

FA
from (3.4.10) it follows r < 1/2 and ||A||∞ = 1. Thus, estimate (3.4.11) holds in any case, where a = 1 − 2r − λΔt. Hence,
proving that the scheme is stable, under the assumption (3.4.10). Condition (3.4.10) can be
C

C
rewritten as uj+1 = Auj + aj .
r ≤ min{1/(2 + h1 Δx), 1/(2 + h2 Δx)}.
SI

SI
Since uj0 and ujn are provided by the boundary conditions
M

M
This example shows that the boundary data can influence the stability condition and the
matrix analysis is able to emphasize this fact. uj0 = g1 (tj ), ujn = g2 (tj ),
IL

IL
the vector aj is known. Furthermore, note that
E

E
Example 3.4.4 The Crank-Nicolson method was discussed in Section 3.1 and the Dirich-
let problem was expressed in matrix form (3.4.1) with A = B −1 C, where the (n − 1)-by-
-O

-O
||A||∞ = ||A||1 = 2r + |1 − 2r − λΔt|.
(n − 1) symmetric matrices B and C are defined by
⎡ ⎤
N

N
So, under the hypothesis
2(1 + r) −r
LY

LY
⎢ ⎥ r ≤ 1/2,
⎢ −r 2(1 + r) −r ⎥
B=⎢ ⎥,
⎣ · · · ⎦ we get
FO

FO
−r 2(1 + r) ||A||∞ = ||A||1 ≤ 2r + (1 − 2r) + |λ|Δt| ≤ 1 + |λ|Δt|,
⎡ ⎤
2(1 − r) −r proving conditional stability.
R

R
⎢ 2(1 − r) r ⎥
⎢ r ⎥
C=⎢ ⎥.
A

A
⎣ · · · ⎦ 3.5 . Convection-diffusion equation
A

A
r 2(1 − r)
EM

EM
Since CD = DC, also the matrix A = C −1 D is symmetric. Therefore, ||A||2 = ρ(A). The convection-diffusion equation
Moreover, the eigenvalues of A are expressed by
Ut + vUx − αUxx = 0, (3.5.1)
ST

ST
λi = [1 − 2r sin2 (iπ/2n)]/[1 + 4r sin2 (iπ/2n)].
was derived in Chapter 1. To transform equation (3.5.1) to non-dimensional form, consider
U

U
Hence, |λi | ≤ 1 for any r, proving unconditional stability for the Crank-Nicolson method. the following new unknown function and variables
D

D
W = U/U ∗ , ξ = x/L, τ = αt/L2 ,
EN

EN
Example 3.4.5 Consider the equation
where L is a reference length. In addition, U ∗ is a reference quantity with the same dimen-
TS

TS
Ut = αUxx − λU, 0 < x < L, t > 0, (3.4.12)
sion as U . For example, if U denotes temperature, U ∗ is a reference temperature. Since
where λ denotes a decay constant. In addition, assume Dirichlet boundary conditions straightforward calculations imply

U (0, t) = g1 (t), U (L, t) = g2 (t), t > 0. Ux = Wξ U ∗ /L, Uxx = Wξξ U ∗ /L2 , Ut = Wτ U ∗ α/L2 ,
3.5.
. CONVECTION-DIFFUSION EQUATION 101 102 CHAPTER 3. PARABOLIC EQUATIONS
equation (3.5.1) takes the form Condition (3.5.6) is proved in similar manner as (3.5.4). Setting
 
Wτ + Pe Wξ = Wξξ , rα + s|v| if v ≥ 0, rα if v ≥ 0,
p= q= (3.5.7)
rα if v < 0, rα + s|v| if v < 0,
where the Péclet number
Pe = vL/α methods (3.5.3) and (3.5.5) are recollected in the single equation
has been introduced. The Péclet number is the ratio of convective to diffusive times uj+1
i = puji−1 + (1 − p − q)uji + quji+1 , (3.5.8)
|v|/L with the stability condition expressed as p + q = 2rα + s|v| ≤ 1.
|Pe| = = |v|L/α
α/L2
and measures the relative strength of convection compared to diffusion process. Example 3.5.1 Consider the following Dirichlet problem
Ut + vUx − αUxx = 0, 0 < x < L, 0 < t ≤ T, (3.5.9)
3.5.1. Explicit methods 
FA

FA
K if 0 < x1 ≤ x ≤ x2 < L,
U (x, 0) = (3.5.10)
Assume that v is positive in equation (3.5.1). Using the forward approximation for Ut and 0 otherwise,
C

C
the backward approximation for Ux yields U (0, t) = 0, U (L, t) = 0, t > 0. (3.5.11)
SI

SI
Uij+1 − Uij U j − Ui−1
j
U j − 2Uij + Ui−1
j
The problem above describes, for example, the evolution with diffusion of a polluting
+v i = α i+1 + O(Δt + Δx).
M

M
Δt Δx (Δx) 2
IL

IL
Therefore, the following finite-difference method
E

E
uj+1 − uji uj − uji−1 uj − 2uji + uji−1
i
+v i = α i+1 (3.5.2)
-O

-O
Δt Δx (Δx)2
is consistent with partial differential equation (3.5.1). Setting r = Δt/(Δx)2 and s = Figure 3.5.1. Convection with diffusion in a narrow stream
N

N
Δt/Δx, from (3.5.2) it follows that
LY

LY
uj+1
i = (rα + sv)uji−1 + (1 − 2rα − sv)uji + rαuji+1 . (3.5.3)
FO

FO
Method (3.5.2), termed upwind method, is conditionally stable. Indeed, if
R

R
2rα + sv ≤ 1, (3.5.4)
A

A
then
A

A
|uj+1 | ≤ (rα + sv)||uj ||∞ + (1 − 2rα − sv)||uj ||∞ + rα||uj ||∞ ≤ ||uj ||∞ .
EM

EM
i

Hence,
||uj+1 ||∞ ≤ ||uj ||∞ . . . ≤ ||u0 ||∞ ,
ST

ST
proving conditional stability. If v is negative, the forward approximation is used for Ux
U

U
uj+1 − uji uj − uji uj − 2uji + uji−1
D

D
i
+ v i+1 = α i+1 , Figure 3.5.2. Problem (3.5.9)-(3.5.11)
EN

EN
Δt Δx (Δx)2

uj+1 = rαuji−1 + (1 − 2rα + sv)uji + (rα − sv)uji+1 . (3.5.5) substance in a narrow stream. In this context, U denotes the concentration of chemical
TS

TS
i
substances and v the (constant) stream velocity, fig. 3.5.1. A Matlab program for the
The stability condition of (3.5.5) is expressed as problem above will be presented in Section 3.9.4. For α = 0.001, L = 100, T = 2,
2rα − sv ≤ 1. (3.5.6) v = 10, n = 150, 2rα + s|v| = 1, the upwind approximating solution is shown in fig.
3.5.2.
3.5.
. CONVECTION-DIFFUSION EQUATION 103 104 CHAPTER 3. PARABOLIC EQUATIONS
Example 3.5.2 Consider the following Dirichlet problem In matrix notations
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
Ut + vUx − αUxx = 0, 0 < x < L, 0 < t ≤ T, (3.5.12) uj+1
1 1 − (p + q) q uj1 puj0
⎢ j+1 ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ u2 ⎥ ⎢ p 1 − (p + q) q ⎥⎢ uj2 ⎥ ⎢ 0 ⎥
⎧ ⎢ ⎥=⎢ ⎥⎢ ⎥+⎢ ⎥.
⎪ ⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
⎨ K(x − x1 )/(x0 − x1 )if 0 < x1 ≤ x ≤ x0 < L,
U (x, 0) = K(x2 − x)/(x2 − x0 ) if 0 < x0 ≤ x ≤ x2 < L, (3.5.13) uj+1
n−1
p 1 − (p + q) ujn−1 qujn

⎩ 0 otherwise,
Hence,
U (0, t) = 0, U (L, t) = 0, t > 0. (3.5.14) uj+1 = Auj + aj .

For α = 0.001, L = 100, T = 2, v = −10, n = 150, 2rα + s|v| = 1, the upwind For Dirichlet problem, aj is a known vector and the stability can be discussed by the matrix
method. Indeed, consider that

||A||∞ = ||A||1 = |p| + |1 − p − q| + |q|.


FA

FA
Thus, if
C

C
p ≥ 0, q ≥ 0, 1 − p − q ≥ 0, ⇔ s|v| ≤ 2rα, 2rα ≤ 1, (3.5.17)
SI

SI
then
||A||∞ = ||A||1 = 1,
M

M
IL

IL
proving conditional stability. The product s|v| is named Courant number. If the stability
conditions (3.5.17) are fulfilled then
E

E
s|v| ≤ 1.
-O

-O
The Von Neumann stability is discussed in the worked Exercise 3.5.1.
N

N
LY

LY
Figure 3.5.3. Problem (3.5.12)-(3.5.14)
Exercise 3.5.1 Use the Von Neumann criterion to discuss the stability of method (3.5.16),
rewritten for convenience,
approximating solution is displayed in fig. 3.5.3. Fig. 3.5.3 was produced by a program
FO

FO
which will be presented in Section 3.9.4. uj+1
i = (rα + sv/2)uji−1 + (1 − 2rα)uji + (rα − sv/2)uji+1 . (3.5.18)
R

R
Consider equation (3.5.1). Using the forward approximation for Ut and the central

approximations for the space derivatives yields
A

Hint. Substituting uji = ξ j e

A
−1βiΔx
into (3.5.18) yields
A

A
j √
Uij+1− Uij Ui+1 −2Uij + j
Ui−1 j
Ui+1− j
Ui−1 ξ = 1 − 2rα + 2rα cos(βΔx) − vs −1 sin(βΔx).
EM

EM
=α −v + O(Δt + (Δx)2 ).
Δt (Δx)2 2Δx
Hence,
Therefore, the following finite-difference method |ξ|2 = [1 − 4rα sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx).
ST

ST
If stability conditions (3.5.17) are fulfilled then
(uj+1 − uji )/Δt = α(uji+1 − 2uji + uji−1 )/(Δx)2 − v(uji+1 − uji−1 )/(2Δx)
U

U
i (3.5.15)
D

D
v 2 s2 ≤ 2rα, 2rα ≤ 1, (3.5.19)
is consistent with partial differential equation (3.5.1). Setting
EN

EN
and
r = Δt/(Δx)2 , s = Δt/Δx, p = rα + sv/2, q = rα − sv/2, |ξ|2 ≤ [1 − 4rα sin2 (βΔx/2)]2 + 4v 2 s2 sin2 (βΔx/2) cos2 (βΔx/2)
TS

TS
from (3.5.15) it follows that ≤ [1 − 4rα sin2 (βΔx/2)]2 + 8rα sin2 (βΔx/2)[1 − sin2 (βΔx/2)]
≤ 1 − 8rα(1 − 2rα) sin4 (βΔx/2) ≤ 1.
uj+1
i = puji−1 + (1 − p − q)uji + quji+1 . (3.5.16)
3.5.
. CONVECTION-DIFFUSION EQUATION 105 106 CHAPTER 3. PARABOLIC EQUATIONS
Remark 3.5.1 If α = 0, the convection-diffusion equation (3.5.1) reduces to the equation Method (3.5.24) is unconditionally
√ stable, as shown by the Von Neumann criterion. Indeed,
substituting uji = ξ j e −1βiΔx into (3.5.24) yields
Ut + vUx = 0. √
ξ[1 + 2rα − 2rα cos(βΔx) + vs −1 sin(βΔx)] = 1,
This first-order equation, named convection or advection equation, is hyperbolic and will
be discussed in Chapter 5. For the equation above the method (3.5.18) simplifies in |ξ|2 = 1/{[1 + 4rα sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)} < 1.

uj+1 = uji − (sv/2)(uji+1 − uji−1 ), (3.5.20) Setting p = rα + sv/2 and q = rα − sv/2, method (3.5.24) is rewritten as
i

and the equation for ξ reduces to −puj+1 j+1


i−1 + (1 + p + q)ui − quj+1 j
i+1 = ui .

|ξ|2 = 1 + v 2 s2 sin2 (βΔx) > 1. In matrix form


⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
Consequently, the algorithm (3.5.20) is unstable. The diffusivity coefficient is essential to 1+p+q −q uj+1
1 uj1 puj+1
0
⎢ ⎥ ⎢ j+1 ⎥ ⎢ ⎥ ⎢ ⎥
FA

FA
⎢ −p 1 + p + q −q ⎥ ⎢ u2 ⎥ ⎢ uj2 ⎥ ⎢ 0 ⎥
stabilize the scheme. This remark is the starting point of interesting developments for hy- ⎢ ⎥⎢ ⎥=⎢ ⎥+⎢ ⎥.
perbolic partial differential equations. Furthermore, consider the upwind methods (3.5.3) ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦
C

C
and (3.5.5). If α = 0, they reduce to −p 1 + p + q uj+1
n−1 ujn−1 quj+1
n
SI

SI
uj+1 = svuji−1 + (1 − sv)uji , (v > 0), Hence, with apparent definition of the symbols,
M

M
i (3.5.21)
uj+1
i = (1 + sv)uji − svuji+1 , (v < 0). Buj+1 = uj + bj+1 ,
IL

IL
The stability condition for both takes the expression
uj+1 = Auj + aj+1 , (A = B −1 , aj+1 = B −1 bj+1 ).
E

E
-O

-O
s|v| ≤ 1. (3.5.22) For the Dirichlet problem, where the following boundary conditions are prescribed,

Now, it is apparent that the limiting equations (3.5.21) are stable under condition (3.5.22).
N

U (0, t) = g1 (t), U (L, t) = g2 (t), t > 0,

N
This fact suggests that the upwind method should work well for large Péclet numbers, when
LY

LY
the convection is strongly prevailing on the diffusion. A key role is played by the mesh Péclet the vector aj+1 is a known term, since
number
FO

FO
|v|Δx/α, (3.5.23) uj0 = g1j = g1 (tj ), ujn = g2j = g2 (tj ).
R

R
as shown by examples in the following sections.
Example 3.5.3 Consider the Dirichlet problem (3.5.9)-(3.5.11), discussed by the upwind
A

A
3.5.2. Backward Euler method method in Example 3.5.1. For the same values of the parameters (α = 0.001, L = 100,
A

A
T = 2, v = 10, n = 150, m = 500), the approximating solution provided by the backward
EM

EM
Consider the convection-diffusion equation implicit method is displayed in fig. 3.5.4. Notice undesirable oscillations. The oscillatory
behavior of the numerical solution is due to the central approximation of Ux and not to
Ut + vUx − αUxx = 0,
instability. Indeed, the method is stable and the oscillations do not become large. According
ST

ST
and use the backward Euler method introduced in Section 2.7 for the heat equation. The to Remark 3.5.1, the mesh Péclet number is equal to 6.7 × 103 . For small (< 2) mesh Péclet
numbers the method works well, as shown by Example 3.5.4, where the mesh Péclet number
U

U
time derivative is replaced by the backward approximation and the central approximations
is equal to 1.0 × 10−3 .
D

D
are used for the space derivatives:
EN

EN
uj+1 − uji uj+1 − uj+1 uj+1 − 2uj+1 + uj+1 Example 3.5.4 Consider the following Dirichlet problem
i
+ v i+1 i−1
= α i+1 i i−1
.
Δt 2Δx (Δx)2
TS

TS
Ut + vUx − αUxx = 0, 0 < x < L, 0 < t ≤ T, (3.5.25)
Hence, with the positions r = Δt/(Δx)2 and s = Δt/Δx,
U (x, 0) = sin(πx), 0 ≤ x ≤ L, (3.5.26)
−(rα + vs/2)uj+1 j+1
i−1 + (1 + 2rα)ui − (rα − vs/2)uj+1 j
i+1 = ui . (3.5.24)
3.5.
. CONVECTION-DIFFUSION EQUATION 107 108 CHAPTER 3. PARABOLIC EQUATIONS
The philosophy of the upwind method can also be applied to the backward Euler
method, but the situations of positive and negative velocities must be discussed separately.
If v is positive in equation (3.5.1), using the backward approximation for Ux yields
j+1
uj+1 − uji u − uj+1 uj+1 − 2uj+1 + uj+1
i
+v i i−1
= α i+1 i i−1
.
Δt Δx (Δx)2

Hence, with the positions r = Δt/(Δx)2 and s = Δt/Δx,

−(rα + vs)uj+1 j+1


i−1 + (1 + 2rα + vs)ui − rαuj+1 j
i+1 = ui . (3.5.28)

If v is negative, using the forward approximation for Ux yields

uj+1 − uji uj+1 − uj+1 uj+1 − 2uj+1 + uj+1


FA

FA
i
+ v i+1 i
= α i+1 i i−1
,
Figure 3.5.4. Problem (3.5.9)-(3.5.11): Euler implicit method Δt Δx (Δx)2
C

C
−rαuj+1 j+1
i−1 + (1 + 2rα − vs)ui − (rα − sv)uj+1 j
i+1 = ui . (3.5.29)
SI

SI
U (0, t) = sin(−πvt) exp(−απ 2 t), Methods (3.5.28) and (3.5.29) are unconditionally stable, as shown in the worked Exercise
M

M
(3.5.27)
U (L, t) = sin(π(L − vt)) exp(−απ 2 t), t > 0. 3.5.2. The price paid by the upwind version is outlined in Exercise 3.5.3. Setting
IL

IL
 
The exact solution is the function rα + s|v| if v ≥ 0, rα if v ≥ 0,
E

E
p= q= (3.5.30)
rα if v < 0, rα + s|v| if v < 0,
U (x, t) = sin(π(x − vt)) exp(−απ 2 t).
-O

-O
For α = 1, L = 1, T = 1, v = .2, n = 200, m = 6000, exact and approximating solutions equations (3.5.28) and (3.5.29) are arranged in the simple formula
N

N
are shown in fig. 3.5.5 (left). The error E = U − u is displayed in fig. 3.5.5 (right). The
−puj+1 j+1
i−1 + (1 + p + q)ui − quj+1 j
i+1 = ui . (3.5.31)
LY

LY
percentage relative error is given by
FO

FO
Example 3.5.5 Consider the following Dirichlet problem

Ut + vUx − αUxx = 0, 0 < x < L, 0 < t ≤ T,


R

R
(3.5.32)
A

A
A

A
U (x, 0) = 0, 0 ≤ x ≤ L, (3.5.33)

EM

EM
K if t ≤ t0 ,
U (0, t) = U (L, t) = 0, t > 0. (3.5.34)
0 otherwise,
ST

ST
A Malab program for the problem above will be presented in Section 3.9.5. For α = 0.001,
L = 100, T = 5, n = 150, m = 1000, v = 10, the approximating solution provided by the
U

U
upwind version of the backward implicit method is displayed in 3.5.6.
D

D
EN

EN
Figure 3.5.5. Problem (3.5.25)-(3.5.27): solutions (left) and error (right) Exercise 3.5.2 Prove that methods (3.5.28) and (3.5.29) are unconditionally stable.

Hint. Use the Von Neumann criterion. Substituting uji = ξ j e −1βiΔx into (3.5.28) yields
TS

TS
maxi |Uim − umi | √
e= × 100 = 0.8049. ξ{1 + 2rα + vs − 2rα cos(βΔx) − vs exp(− −1βΔx)} = 1,
maxi |Uim |

Figs. 3.5.4 and 3.5.5 were produced by a program which will be presented in Section 3.9.5. ξ{1 + 2rα + vs − 2rα cos(βΔx) − vs[cos(βΔx) − −1 sin(βΔx)]} = 1,
3.5.
. CONVECTION-DIFFUSION EQUATION 109 110 CHAPTER 3. PARABOLIC EQUATIONS
The method is unconditionally stable, as shown by the Von Neumann criterion. Substituting
uji = ξ j eIβiΔx into (3.5.35) yields

ξ{2 + 2rα[1 − cos(βΔx)] + vsI sin(βΔx)}=2 − 2rα[1 − cos(βΔx)] − vsI sin(βΔx),

4[1 − 2rα sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)


|ξ|2 = < 1.
4[1 + 2rα sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)
Furthermore, method (3.5.35) can be arranged in compact matrix form
⎡ ⎤⎡ ⎤
2(1 + rα) −q uj+1
1
⎢ −p 2(1 + rα) −q ⎥ ⎢ j+1 ⎥
⎢ ⎥ ⎢ u2 ⎥
⎢ ⎥⎢ ⎥
FA

FA
⎣ · · · ⎦⎣ · ⎦
Figure 3.5.6. Problem (3.5.32)-(3.5.34) −p 2(1 + rα) uj+1
n−1
C

C
⎡ ⎤⎡ ⎤ ⎡ ⎤
|ξ|2 = 1/{[1 + 4rα sin2 (βΔx/2) + 2vs sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)} < 1,
SI

SI
2(1 − rα) q uj1 p(uj+1
0 + uj0 )
⎢ ⎥⎢ ⎥ ⎢ ⎥
2(1 − rα) q uj2
M

M
proving stability. Method (3.5.29) is discussed similarly. ⎢ p ⎥⎢ ⎥ ⎢ 0 ⎥
=⎢ ⎥⎢ ⎥+⎢ ⎥,
⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
IL

IL
Exercise 3.5.3 Show that the truncation error of the backward implicit method is given p 2(1 − rα) ujn−1 q(uj+1
n + ujn )
E

E
by
τij = O(Δt) + O((Δx)2 ), where p = rα + sv/2 and q = rα − sv/2. Hence, with apparent definition of the symbols,
-O

-O
whereas the truncation error of the upwind version is expressed as Buj+1 = Cuj + bj ,
N

N
τij = O(Δt) + O(Δx).
LY

LY
uj+1 = Auj + aj , (A = B −1 C, aj = B −1 bj ).
3.5.3. Crank-Nicolson implicit method
FO

FO
Consider the convection-diffusion equation Exercise 3.5.4 Show that the truncation error of the Crank-Nicolson implicit method is
R

R
given by
Ut + vUx − αUxx = 0,
A

A
τij = O((Δt)2 ) + O((Δx)2 ).
A

A
and use the Crank-Nicolson method illustrated in Section 3.1 for the heat equation. The
EM

EM
time derivative is replaced by the the central approximation on (i, j + 1/2) and the space
derivatives are approximated by the averages of the central approximations related to the Exercise 3.5.5 Derive the upwind version of the Crank-Nicolson implicit method and
points (i, j) and (i, j + 1): prove the unconditional stability. In addition, show that τij = O((Δt)2 ) + O(Δx).
ST

ST
Hint. For positive v
uj+1 − uji v
i
+ (uj+1 − uj+1 j j
i−1 + ui+1 − ui−1 )
U

U
Δt 4Δx i+1
−(rα + sv)uj+1 j+1
− rαuj+1
D

D
i−1 + (2 + 2rα + sv)ui i+1
α
(uj+1 − 2uj+1 + uj+1 j j j
EN

EN
= i−1 + ui+1 − 2ui + ui−1 ).
2(Δx)2 i+1 i
= (rα + sv)uji−1 + (2 − 2rα − sv)uji + rαuji+1 , (3.5.36)
Hence, with the positions r = Δt/(Δx)2 , s = Δt/Δx,
TS

TS
and
−(rα + sv/2)uj+1 j+1
i−1 + 2(1 + rα)ui − (rα − sv/2)uj+1
i+1 4[1 − (2rα + vs) sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)
|ξ|2 = < 1.
= (rα + sv/2)uji−1 + 2(1 − rα)uji + (rα − sv/2)uji+1 . (3.5.35) 4[1 + (2rα + vs) sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)
3.5.
. CONVECTION-DIFFUSION EQUATION 111 112 CHAPTER 3. PARABOLIC EQUATIONS
3.5.4. Diffusion with decay Hint. Use the change of dependent variable

 t
When the diffusing process of substances is influenced by radioactive decay, a new term W = U exp λ(τ )dτ .
must be inserted into equation (3.5.1). The resulting equation is the following 0

Ut + vUx = αUxx − λU, (3.5.37)


Exercise 3.5.7 Derive the Euler explicit method for equation (3.5.37). Discuss the stabil-
where λ is the decay coefficient. Same equation is obtained if biodegradation (biological ity.
transformation) occurs and first-order kinetic effects are considered. The change of depen- Hint.
dent variable uj+1
i = (rα + sv/2)uji−1 + (1 − 2rα − λΔt)uji + (rα − sv/2)uji+1 .
W = U exp(λt),
reduces equation (3.5.37) to convection-diffusion equation (3.5.1) Exercise 3.5.8 Derive the the Euler implicit method for equation (3.5.37). Prove the
unconditional stability.
Wt + vWx = αWxx .
FA

FA
Hint.
So, it is clear that all the numerical methods derived for (3.5.1) can be generalized to equa-
−(rα + sv/2)uj+1 j+1
− (rα − sv/2)uj+1 j
C

C
tion (3.5.37). Notice that the transformation above still holds if λ is a function of t, as i−1 + (1 + 2rα + λΔt)ui i+1 = ui .
shown in Exercise 3.5.6.
SI

SI
The upwind method for equation (3.5.37) takes the expression Exercise 3.5.9 Derive the the Crank-Nicolson implicit method for equation (3.5.37).
M

M
uj+1 = puji−1 + (1 − p − q − λΔt)uji + quji+1 , (3.5.38) Prove the unconditional stability.
IL

IL
i
Hint.
where
E

E
  −(rα + sv/2)uj+1 j+1
i−1 + (2 + 2rα + λΔt)ui − (rα − sv/2)uj+1
i+1
rα + s|v| if v ≥ 0, if v ≥ 0,
-O

-O
rα = (rα + sv/2)uji−1 + (2 − 2rα − λΔt)uji + (rα − sv/2)uji+1 .
p= q= (3.5.39)
rα if v < 0, rα + s|v| if v < 0.
N

N
In matrix notations, 3.6 . Method of lines
LY

LY
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
uj+1
1 a q uj1 puj0
⎢ ⎥ ⎢ p a q ⎥⎢ ⎥ ⎢ ⎥ 3.6.1. Dirichlet problem
⎢ uj+1 ⎥ ⎢ ⎥⎢ uj2 ⎥ ⎢ 0 ⎥
FO

FO
⎢ 2 =
⎥ ⎢ ⎥⎢ ⎥+⎢ ⎥,
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦ Consider the one-dimensional heat equation
j+1 p a ujn−1 qujn
R

R
un−1 Ut = αUxx , 0 < x < L, t > 0. (3.6.1)
where a = 1 − p − q − λΔt for brevity. Hence, with apparent definition of the symbols,
A

A
When only the space variable is discretized and the x derivative is approximated by finite
A

A
uj+1 = Auj + aj . differences, a system of ordinary differential equations is obtained. Indeed, use the notation
EM

EM
ui = u(xi , t), ⇒ u̇i = ut (xi , t),
In the Dirichlet problem the boundary conditions are given by
where the dot denotes differentiation with respect to t. If the x derivative is replaced by
U (0, t) = g1 (t), U (L, t) = g2 (t),
ST

ST
the central approximation, equation (3.6.1) is approximated by the following system of
ordinary differential equations, fig. 3.6.1,
and the vector aj is a known term, since uj0 = g1 (tj ), ujn = g2 (tj ). Method (3.5.38) is
U

U
conditionally stable. Indeed, if 2rα + s|v| ≤ 1, then α
u̇i = (u − 2ui + ui−1 ), i = 1, . . . , n − 1, nΔx = L.
D

D
(Δx)2 i+1
EN

EN
||A||∞ = ||A||1 = |p| + |1 − 2rα − s|v| − λΔt| + |q| ≤ 1 + |λ|Δt, In matrix notations
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇1 −2 1 u1 u0
TS

TS
which is the desired result.
⎢ · ⎥ ⎢ 1 −2 1 ⎥⎢ · ⎥ ⎢ 0 ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ ⎥ = p⎢ ⎥⎢ ⎥ + p⎢ ⎥,
Exercise 3.5.6 Reduce equation (3.5.37), where λ = λ(t), to the convection-diffusion ⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
equation. u̇n−1 1 −2 un−1 un
3.6.
. METHOD OF LINES 113 114 CHAPTER 3. PARABOLIC EQUATIONS

Figure 3.6.1. Method of lines

where
p = α/(Δx)2 .
FA

FA
Hence, with apparent definition of the symbols, Figure 3.6.3. Numerical solution to problem (3.6.3)-(3.6.5)
C

C
u̇ = Au + a. (3.6.2)
SI

SI
where z is the elevation head and p/γ the pressure head. The function h(z, t) satisfies the
In Dirichlet problem, u0 and un are prescribed functions of time. Vector a is a known term same equation as p
M

M
and system (3.6.2) solves completely the problem with an error of order (Δx)2 .
ht − cν hzz = 0, 0 < z < L, 0 < t ≤ T, (3.6.7)
IL

IL
Example 3.6.1 Fig. 3.6.2 illustrates the consolidation process due to uniform load q. The with initial-boundary conditions derived from (3.6.4)-(3.6.5)
E

E
evolution of the pore pressure p is modelled by the following Dirichlet problem
-O

-O
N

N
LY

LY
FO

FO
R

R
A

A
A

A
Figure 3.6.2. Consolidation
EM

EM
pt − cν pzz = 0, 0 < z < L, 0 < t ≤ T, (3.6.3) Figure 3.6.4. Numerical solution to problem (3.6.7)-(3.6.9)
ST

ST
p(z, 0) = γ(H − z) + q, 0 ≤ z ≤ L, (3.6.4)
h(z, 0) = H + q/γ, 0 ≤ z ≤ L,
U

U
(3.6.8)
p(0, t) = γH, p(L, t) = γ(H − L), t > 0. (3.6.5)
D

D
h(0, t) = H, h(L, t) = H, t > 0. (3.6.9)
In equation (3.6.3), cν = 10−7 m2 /s denotes the consolidation coefficient and γ =
EN

EN
1 ton/m3 is the specific weight of the water. For L = 10 m, H = 12 m, T = 3 years, The numerical solution by MOL, obtained by the same program as p, is shown in fig. 3.6.4.
q = 40 ton/m3 , n = 50, the numerical solution by MOL is displayed in fig. 3.6.3. Fig. The Reader is asked to check that the numerical results provided by problems (3.6.3)-(3.6.5)
TS

TS
3.6.3 was produced by a program which will be presented in Section 3.9.6. and (3.6.7)-(3.6.9) agree with formula (3.6.6).
The piezometric head h is functionally related to p by the relationship
Example 3.6.2 With reference to fig. 3.6.2, consider a steady state characterized by
h = z + p/γ, (3.6.6) piezometric head h = H. Then, h in the upper draining stratum is lowered from H to L
3.6.
. METHOD OF LINES 115 116 CHAPTER 3. PARABOLIC EQUATIONS
according to the functional law H − (H − L)t/T1 . In the lower draining stratum h remains Solving equations (3.6.15) with respect to the ghost terms and the results substituted into
unchanged. The consolidation process is governed by equation (3.6.7) with the following (3.6.16) yields
initial-boundary conditions
u̇0 = 2p(u1 − u0 ) + 2pΔxg1 , u̇n = 2p(un−1 − un ) + 2pΔxg2 . (3.6.17)

Here, it has been tacitly assumed that equation (3.6.12) is satisfied at the end points. Finally,
arrange equations (3.6.14) and (3.6.17) in matrix form
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇0 −2 2 u0 g1
⎢ u̇ ⎥ ⎢ 1 −2 1 ⎥⎢ u ⎥ ⎢ 0 ⎥
⎢ 1 ⎥ ⎢ ⎥⎢ 1 ⎥ ⎢ ⎥
⎢ ⎥ = p⎢ ⎥⎢ ⎥ + 2pΔx ⎢ ⎥,
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
u̇n 2 −2 un g2

u̇ = Au + a.
FA

FA
Boundary conditions (3.6.13) can also be approximated by the forward and backward ap-
C

C
proximations
u 1 − u0 un − un−1
− = g1 , = g2 ,
SI

SI
Δx Δx
M

M
Figure 3.6.5. Numerical solution to problem (3.6.10)-(3.6.11) which avoid fictitious terms. But, the result is less accurate. Formulas, which save the men-
tioned vantage and are as accurate as (3.6.15), are provided by the forward and backward
IL

IL
three-point approximations
h(z, 0) = H, 0 ≤ z ≤ L, (3.6.10)
E

E
 4u1 − 3u0 − u2 −4un−1 + 3un + un−2
− = g1 , = g2 .
-O

-O
H − (H − L)t/T1 , if t ≤ T1 , 2Δx 2Δx
h(0, t) = H, h(L, t) = (3.6.11)
L, if t > T1 .
N

N
For L = 10 m, H = 12 m, T = 3 years, T1 = 1 month, n = 50, the numerical solution by Example 3.6.3 Consider again the consolidation process due the uniform load q, as in
LY

LY
MOL is displayed in fig. 3.6.5. The Matlab program for problem (3.6.10)-(3.6.11) will be Example 3.6.1. Now, another mechanical situation is presented: a clay stratum between a
presented in Section 3.9.6. draining upper stratum and an impervious lower stratum, fig. 3.6.6. The evolution of the
FO

FO
3.6.2. Neumann problem
R

R
Consider the Neumann problem
A

A
A

A
Ut = αUxx , 0 < x < L, t > 0, (3.6.12)
EM

EM
−Ux (0, t) = g1 (t), Ux (L, t) = g2 (t). (3.6.13)
Approximate equation (3.6.12) by the method of lines
ST

ST
u̇i = p(ui+1 − 2ui + ui−1 ), (p = α/(Δx)2 ), (3.6.14)
U

U
Figure 3.6.6. Consolidation
D

D
and use the central approximations for the boundary conditions
EN

EN
u1 − u−1 un+1 − un−1 piezometric head h is modelled by the following Dirichlet-Neumann problem
− = g1 , = g2 . (3.6.15)
2Δx 2Δx
TS

TS
ht − cv hzz = 0, 0 < z < L, 0 < t ≤ T, (3.6.18)
To eliminate the fictitious values, consider the equations which follow from (3.6.14) for
i = 0 and i = n: h(z, 0) = H + q/γ, 0 ≤ z ≤ L, (3.6.19)
u̇0 = p(u1 − 2u0 + u−1 ), u̇n = p(un+1 − 2un + un−1 ). (3.6.16) hz (0, t) = 0, h(L, t) = H, t > 0. (3.6.20)
3.6.
. METHOD OF LINES 117 118 CHAPTER 3. PARABOLIC EQUATIONS
where
α v α v
p= + , q= − .
(Δx)2 2Δx (Δx)2 2Δx
In matrix notations, equation (3.6.23) is rewritten as
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇1 −(p + q) q u1 pu0
⎢ · ⎥ ⎢ −(p + q) q ⎥⎢ · ⎥ ⎢ 0 ⎥
⎢ ⎥ ⎢ p ⎥⎢ ⎥ ⎢ ⎥
⎢ ⎥=⎢ ⎥⎢ ⎥+⎢ ⎥,
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
u̇n−1 p −(p + q) un−1 qun

u̇ = Au + a.
In the Dirichlet problem, u0 and un are assigned functions of time and vector a is a known
term.
FA

FA
Consider the upwind method. The situations v > 0 and v < 0 must be discussed
Figure 3.6.7. Numerical solution to problem (3.6.18)-(3.6.20) separately and the method of lines leads to
C

C
v α
u̇i = − (u − ui−1 ) + (u − 2ui + ui−1 ), i = 1, . . . , n − 1, (v > 0),
SI

SI
The approximating equations for problem (3.6.18)-(3.6.20) are imported partially from the
Δx i (Δx)2 i+1
Dirichlet problem and partially from the Neumann problem:
M

M
v α
u̇0 = 2p(u1 − u0 ) + 2pΔxg1 , u̇i = − (u − ui ) + (u − 2ui + ui−1 ), i = 1, . . . , n − 1, (v < 0).
IL

IL
Δx i+1 (Δx)2 i+1
E

E
u̇i = p(ui+1 − 2ui + ui−1 ), i = 1, . . . , n − 2, Setting
 
-O

-O
u̇n−1 = p(un−2 − 2un−1 ) + pg2 . α/(Δx)2 + |v|/Δx if v ≥ 0, α/(Δx)2 if v ≥ 0,
p= q=
In matrix notations, α/(Δx)2 if v < 0, α/(Δx)2 + |v|/Δx if v < 0,
N

N
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
LY

LY
u̇0 −2 2 u0 2Δxg1 the equations above are arranged in a single equation
⎢ ⎥ ⎢ 1 −2 1 ⎥⎢ ⎥ ⎢ 0 ⎥
⎢ u̇1 ⎥ ⎢ ⎥⎢ u1 ⎥ ⎢ ⎥
⎢ ⎥ = p⎢ ⎥⎢ ⎥ + p⎢ ⎥, u̇i = pui−1 − (p + q)ui + qui+1 , i = 1, . . . , n − 1. (3.6.24)
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
FO

FO
u̇n−1 1 −2 un−1 g2
For future applications, note that equation (3.6.24) is formally the same as equation (3.6.23).
R

R
u̇ = Au + a. (3.6.21)
Consider the following Dirichlet problem
A

A
Example 3.6.4
The Matlab program for system (3.6.21) will be presented in Section 3.9.6. For L = 10 m,
A

A
H = 12 m, T = 3 years, n = 50, the numerical solution by MOL is shown in fig. 3.6.7. Ut + vUx − αUxx = 0, 0 < x < L, 0 < t ≤ T, (3.6.25)
EM

EM
3.6.3. Convection-diffusion equation U (x, 0) = 0, 0 ≤ x ≤ L, (3.6.26)

t/t0 − (t/t0 )2 if t ≤ t0 ,
ST

ST
Consider the convection-diffusion equation U (0, t) = U (L, t) = 0, t > 0. (3.6.27)
0 otherwise,
U

U
Ut + vUx − αUxx = 0, 0 < x < L, t > 0. (3.6.22)
For α = 0.01, L = 100, T = 4, n = 150, v = 10, the approximating solutions provided
D

D
The method of lines for equation (3.6.22) leads to different formulas depending on the by methods (3.6.23) and (3.6.24) are displayed in 3.6.8. Notice unsatisfactory oscillations
EN

EN
approximation chosen for Ux . Using the central approximation yields in the solution by method (3.6.23). Fig 3.6.8 was produced by a program which will be
illustrated in Section 3.9.6.
TS

TS
v α
u̇i = − (u − ui−1 ) + (u − 2ui + ui−1 ), i = 1, . . . , n − 1, nΔx = L.
2Δx i+1 (Δx)2 i+1
Exercise 3.6.1 Discuss the method of lines for the equation with decay
Hence,
u̇i = pui−1 − (p + q)ui + qui+1 , i = 1, . . . , n − 1, (3.6.23) Ut + vUx = αUxx − λU.
3.7.
. VARIABLE COEFFICIENTS 119 120 CHAPTER 3. PARABOLIC EQUATIONS
where αi = α(xi , t) are given functions of t and ui = u(xi , t) unknown functions. System
(3.7.4) can be written in matrix form as follows
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇1 −2p1 p1 u1 p1 u 0
⎢ · ⎥ ⎢ −2p2 p2 ⎥⎢ · ⎥ ⎢ 0 ⎥
⎢ ⎥ ⎢ p2 ⎥⎢ ⎥ ⎢ ⎥
⎢ ⎥=⎢ ⎥⎢ ⎥+⎢ ⎥,
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
u̇n−1 pn−1 −2pn−1 un−1 pn−1 un

where
pi = αi /(Δx)2 , i = 1, . . . , n − 1.
Hence, the vector equation
u̇ = A(p)u + a(p), (3.7.5)
FA

FA
Figure 3.6.8. Problem (3.6.25)-(3.6.27) where the symbols A(p), a(p) emphasize that A and a depend on p.
C

C
Example 3.7.1 Consider the following Dirichlet problem
3.7 . Variable coefficients
SI

SI
Ut − (1 + t)Uxx = 0, 0 < x < L, 0 < t ≤ T, (3.7.6)
M

M
Consider the one-dimensional heat equation
U (x, 0) = sin(πx/L), 0 ≤ x ≤ L, (3.7.7)
IL

IL
Ut − α(x, t)Uxx = 0, (3.7.1)
U (0, t) = 0, U (L, t) = 0, t > 0. (3.7.8)
E

E
where the diffusivity coefficient α is assumed to be a strictly positive function of x and t. The exact solution is the function
-O

-O
The methods introduced for constant coefficient can easily be modified for this situation.
For example, using the explicit Euler method yields U (x, t) = sin(πx/L) exp(−(π/L)2 (t + t2 /2)).
N

N
j j j
uj+1 − uji − 2ui + ui−1 For L = 10, T = 1, n = 60, exact solution and approximating solution by MOL are shown
LY

LY
u
i
= αij i+1 . (3.7.2) in fig. 3.7.1 (left). They are visually the same. The error E = U − u is displayed in fig.
Δt Δx2
3.7.1 (right). The percentage relative error is given by
FO

FO
where αij = α(xi , tj ) = α(iΔx, jΔt). Letting r = Δt/Δx2 , from (3.7.2) it follows that
R

R
uj+1
i = (1 − 2rαij )uji + rαij (uji+1 + uji−1 ), (3.7.3)
A

A
which allows us to integrate numerically equation (3.7.1), when the initial and the bound-
A

A
ary data are prescribed. Method (3.7.3) is conditionally stable. Indeed, if the following
EM

EM
condition
1 − 2rαij ≥ 0, ∀i, j,
ST

ST
is satisfied, from (3.7.3) it follows

|uj+1 | ≤ (1 − 2rαij )||uj ||∞ + 2rαij ||uj ||∞ ≤ ||uj ||∞ ,


U

U
i
D

D
||uj+1 ||∞ ≤ ||uj ||∞ . . . ≤ ||u0 ||∞ ,
EN

EN
proving conditional stability.
TS

TS
In the method of lines, the one-dimensional heat equation (3.7.1) with variable diffusiv- Figure 3.7.1. Problem (3.7.6)-(3.7.8): solutions (left) and error (right)
ity coefficient is approximated by the following system of ordinary differential equations
αi maxi |Ui − ui |
u̇i = (u − 2ui + ui−1 ), i = 1, . . . , n − 1, (3.7.4) e= × 100 = 0.0012.
(Δx)2 i+1 maxi |Ui |
3.7.
. VARIABLE COEFFICIENTS 121 122 CHAPTER 3. PARABOLIC EQUATIONS
Fig. 3.7.1 was produced by a program which will be illustrated in Section 3.9.6. 3.8 . Nonlinear equations
Consider the convection-diffusion equation
3.8.1. Explicit method
Ut + v(x, t)Ux − αUxx = 0, (3.7.9) Consider the equation
Ut = αUxx + Φ(U, Ux ), (3.8.1)
where v depends on x and t. Using again the classic explicit method yields
where Φ may be a nonlinear function of its arguments. For example, if Φ = cU 2 , equation
uj+1 − uji uj − uji−1 uj − 2uji + uji−1 (3.8.1) becomes
i
+ vij i+1 = α i+1 . Ut = αUxx + cU 2 ,
Δt 2Δx Δx2
and when Φ = −U Ux , from (3.8.1) it follows that
Hence, with the positions Δt/Δx2 , s = Δt/Δx,
Ut = αUxx − U Ux .
uj+1 = (rα + svij /2)uji−1 + (1 − 2rα)uji + (rα − svij /2)uji+1 . (3.7.10)
FA

FA
i
Consider the forward approximation for the time derivative and the central approximation
In Exercise 3.7.1 it is asked to show that method (3.7.10) is stable under the hypotheses for the space derivatives to obtain the following finite-difference scheme
C

C
 
uji+1 − uji−1
SI

SI
1 − 2rα ≥ 0, 2rα − s|vi,j | ≥ 0, ∀i, j. (3.7.11) uj+1 = (1 − 2rα)uji + rα(uji+1 + uji−i ) + ΔtΦ uji , , (3.8.2)
i
2Δx
M

M
In the method of lines, equation (3.7.9). may be approximated by the following system
IL

IL
of ordinary differential equations where r = Δt/(Δx)2 . This method is conditionally stable, as shown later on. Preliminary,
some useful formulas are derived. Denote by v and z two solutions related to different
E

E
vi α initial data and let u be the difference
u̇i = − (u − ui−1 ) + (u − 2ui + ui−1 ), i = 1, . . . , n − 1, (3.7.12)
-O

-O
2Δx i+1 (Δx)2 i+1
u = v − z.
N

N
where vi = v(xi , t) are known functions of t and ui unknowns functions. In matrix nota-
From (3.8.2) it follows
LY

LY
tions, system (3.7.12) is written as follows
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤ uj+1
i = (1 − 2rα)uji + rα(uji+1 + uji−i )
u̇1 −2p p − q1 u1 (p + q1 )u0
FO

FO
⎢ · ⎥ ⎢p+q −2p p − q2 ⎥⎢ · ⎥ ⎢ 0 ⎥


⎥ ⎢
⎥=⎢ 2 ⎥⎢
⎥⎢
⎥ ⎢
⎥+⎢

⎥, +ΔtΦ(vij , (vi+1
j j
− vi−1 )/2Δx) − ΔtΦ(zij , (zi+1
j j
− zi−1 )/2Δx).
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
R

R
u̇n−1 p + qn−1 −2p un−1 (p − qn−1 )un Using the mean value theorem yields
A

A
uj+1
i = (1 − 2rα)uji + rα(uji+1 + uji−i ) + Δt(Φju uji + Φjux (uji+1 − uji−1 )/2Δx),
A

A
where
EM

EM
p = α/(Δx)2 , qi = vi /(2Δx), i = 1, . . . , n − 1. where the partial derivatives of Φ are evaluated at a point between v and z. Hence,
Hence, with apparent meaning of the symbols uj+1
i = (1 + Φju Δt − 2rα)uji + (rα − Φjux s/2)uji−1 + (rα + Φjux s/2)uji+1 , (3.8.3)
ST

ST
u̇ = A(p, q)u + a(p, q). (3.7.13) where s = Δt/Δx. Note that, setting
U

U
M = sup |Φu |, M1 = sup |Φux |,
D

D
Matlab application of method (3.7.13) will be illustrated in Section 3.9.6. (x,t) (x,t)
EN

EN
Exercise 3.7.1 Prove the conditional stability of method (3.7.10). the following relations hold
TS

TS
1 + Φu Δt − 2rα ≥ 1 − M Δt − 2rα, rα ± Φux s/2 ≥ rα − M1 s/2.
Exercise 3.7.2 Discuss the convection-diffusion equation with variable coefficients
Therefore, if
Ut + v(x, t)Ux − α(x, t)Uxx = 0. (3.7.14) 1 − M Δt − 2rα ≥ 0, rα − M1 s/2 ≥ 0, (3.8.4)
3.8.
. NONLINEAR EQUATIONS 123 124 CHAPTER 3. PARABOLIC EQUATIONS
from (3.8.3) it follows that As specific application, consider the following initial-boundary conditions for equation
(3.8.6)
||uj+1 ||∞ ≤ (1 + M Δt)||uj ||∞ , U (x, 0) = (2α/v)[1 − tanh(x)], 0 ≤ x ≤ L, (3.8.9)

where ||uj ||∞ = maxi |uji |. Consequently, U (0, t) = (2α/v)[1 − tanh(−2αt)],
t > 0. (3.8.10)
U (L, t) = (2α/v)[1 − tanh(L − 2αt)],
j
||uj ||∞ ≤ (1 + M Δt) ||u0 ||∞ . The exact solution is the function
Furthermore, for jΔt = t ≤ T , U (x, t) = (2α/v)[1 − tanh(x − 2αt)].
j
||uj ||∞ ≤ (1 + M T /j) ||u0 ||∞ ≤ exp(M T )||u0 ||∞ , For α = 1, v = 1, L = 10, T = 1, n = 20, exact solution and approximating solution by
MOL are shown in fig. 3.8.1 (left). They are visually the same. The error E = U − u is
proving the conditional stability of scheme (3.8.2). Note that conditions (3.8.4) are certainly displayed in fig. 3.8.1 (right). The percentage relative error is given by
FA

FA
satisfied when
0 < r ≤ (1 − M Δt)/2α, Δx ≤ 2α/M1 .
C

C
3.8.2. Method of lines
SI

SI
M

M
This section presents the method of lines for nonlinear equation (3.8.1). In this method
only the space derivatives are approximated and the notation ui = u(xi , t) is used. If the x
IL

IL
derivatives are replaced by the central approximations, equation (3.8.1) is approximated by
E

E
the following system of ordinary differential equations
-O

-O
α
u̇i = (u − 2ui + ui−1 ) + Φ(ui , (ui+1 − ui−1 )/(2Δx)), (3.8.5)
(Δx)2 i+1
N

N
where u̇i = ut (xi , t). Special situations are discussed below.
LY

LY
Example 3.8.1 Consider the one-dimensional Burgers’ equation Figure 3.8.1. Problem (3.8.9)-(3.8.10): solutions (left) and error (right)
FO

FO
Ut + vU Ux = αUxx , 0 < x < L, 0 < t ≤ T. (3.8.6)
maxi |Ui − ui |
R

R
e= × 100 = 0.0046.
Equation (3.8.5) occurs in the theory of viscous fluids and in modelling of turbulence. For maxi |Ui |
A

A
Burgers’ equation, system (3.8.5) reduces to
A

A
Fig. 3.8.1 was produced by a program which will be illustrated in Section 3.9.7.
EM

EM
u̇i = −qui (ui+1 − ui−1 ) + p(ui+1 − 2ui + ui−1 ), i = 1, . . . , n − 1, (3.8.7)
Example 3.8.2 Consider the nonlinear equation
where
ST

ST
p = α/(Δx)2 , q = v/(2Δx). Ut = Ux2 /(1 + U ) + Uxx , 0 < x < L, 0 < t ≤ T. (3.8.11)
U

U
For future applications (Section 3.9.7) notice that system (3.8.7) is explicitly written as
For equation above, system (3.8.5) reduces to
D

D
u̇1 = −qu1 (u2 − u0 ) + p(u2 − 2u1 + u0 ),
EN

EN
p(ui+1 − ui−1 )2
u̇i = + p(ui+1 − 2ui + ui−1 ), i = 1, . . . , n − 1, (3.8.12)
u̇i = −qui (ui+1 − ui−1 ) + p(ui+1 − 2ui + ui−1 ), i = 2, . . . , n − 2, (3.8.8) 4(1 + ui )
TS

TS
u̇n−1 = −qun−1 (un − un−2 ) + p(un − 2un−1 + un−2 ). where p = 1/(Δx)2 . System (3.8.12) is explicitly written as
In the Dirichlet problem u0 , un are assigned functions of t, and u1 , . . . , un−1 , unknown p(u2 − u0 )2
functions. u̇1 = + p(u2 − 2u1 + u0 ),
4(1 + u1 )
3.8.
. NONLINEAR EQUATIONS 125 126 CHAPTER 3. PARABOLIC EQUATIONS
p(ui+1 − ui−1 )2 3.9 . Matlab interlude 3
u̇i = + p(ui+1 − 2ui + ui−1 ), i = 2, . . . , n − 2, (3.8.13)
4(1 + ui )
3.9.1. Crank function
p(un − un−2 )2
u̇n−1 = + p(un − 2un−1 + un−2 ). The Crank-Nicolson method was discussed in Section 3.1. The following listing presents a
4(1 + un−1 ) program for the problem in Example 3.1.1.
In the Dirichlet problem u0 , un are assigned functions of t, and u1 , . . . , un−1 , unknown
function u = crank
functions.
% This is the function file crank.m
As special application, consider the following initial-boundary conditions for equation % Crank-Nicholson implicit method is used for solving the following
(3.8.11) % Dirichlet problem:

U (x, 0) = −1 + 1 + 2x2 , 0 ≤ x ≤ L, (3.8.14) % Ut - alpha Uxx = 0,
% U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).
√  % Example:
FA

FA
U (0, t) = −1 + 1 + 4t, U (L, t) = −1 + 1 + 2(L2 + 2t), t > 0. (3.8.15) % if x1 <= x <= x2 then phi(x) = phibar, else phi(x) = 0,
% G1(t) = .5*phibar*(erf(-x1/sqrt(4*alpha*t)) + erf(x2/sqrt(4*alpha*t))),
C

C
The exact solution is the function % G2(t) = .5*phibar*(erf((L-x1)/sqrt(4*alpha*t)) + erf((x2-L)/sqrt(4*alpha*t))),

SI

SI
% Exact solution:
U (x, t) = −1 + 1 + 2(x2 + 2t). % U = .5*phibar*(erf((x-x1)/sqrt(4*alpha*t)) + erf((x2-x)/sqrt(4*alpha*t)));
M

M
% Problem parameters.
For L = 1, T = 1, n = 20, exact solution and approximating solution by MOL are shown
IL

IL
% Modify the problem parameters according to the problem.
in fig. 3.8.2 (left). They are practically the same. Indeed, the percentage relative error is
E

E
% Parameters should be checked, otherwise catastrophic errors could occur.
given by alpha = 1; L = 10; T = 1; nx = 500; nt = 600;
-O

-O
x1 = L/4; x2 = 7*L/8;
if any([alpha L T x2-x1 x1 L- x2 nx-2 nt-2]<=0)
N

N
error(’Check problem parameters’)
LY

LY
end

% Initialization
FO

FO
dx = L/nx; dt = T/nt; r = alpha*dt/dxˆ2;
phibar = 1;
x = linspace(0,L,nx+1);
R

R
t = linspace(0,T,nt+1);
A

A
u = GetPhi(x, nx, x1,x2, phibar);
A

A
b = sparse(nx-1,1);
EM

EM
BB = [-r*ones(nx-1,1) 2*(1+r)*ones(nx-1,1) -r*ones(nx-1,1)];
B = spdiags(BB, -1:1, nx-1, nx-1);
CC = [r*ones(nx-1,1) 2*(1-r)*ones(nx-1,1) r*ones(nx-1,1)];
ST

ST
C = spdiags(CC, -1:1, nx-1,nx-1);
Figure 3.8.2. Problem (3.8.14)-(3.8.15): solutions (left) and error (right)
plot(x,u);
U

U
axis([0 L 0 phibar+.1]);
maxi |Ui − ui | pause; % Press any key to continue
D

D
e= × 100 = 0.0034.
maxi |Ui |
EN

EN
% Finite-difference method
The error E = U − u is displayed in fig. 3.8.2 (right). Fig. 3.8.2 was produced by a for j=2:nt+1
TS

TS
program which will be illustrated in Section 3.9.8. b(1) = r*(GetG1(t(j), alpha, x1, x2, phibar)+u(1) );
b(end) = r*(GetG2(t(j), alpha, L, x1, x2, phibar) + u(nx+1));
u(2:end-1) = B\(C*u(2:end-1)+b);
Matlab applications of methods (3.8.8) and (3.8.13) will be provided in Sections 3.9.7 u(1) = GetG1(t(j), alpha, x1, x2, phibar);
and 3.9.8, respectively. u(end) = GetG2(t(j), alpha, L, x1, x2, phibar);
3.9.
. MATLAB INTERLUDE 3 127 128 CHAPTER 3. PARABOLIC EQUATIONS
% Exact solution. Comment for generic problems. if any([alpha L T nx-2 nt-2]<=0)
U=.5*phibar*(erf((x’-x1)/sqrt(4*alpha*t(j)))+erf((x2-x’)/sqrt(4*alpha*t(j)))); error(’Check problem parameters’)
% plot(x,u); % Uncomment for generic problems. end
plot(x,u,x,U,’r*:’); % Comment for generic problems.
xlabel(’x’); ylabel(’U’); title([’t = ’,num2str(t(j))]); % Initialization
axis([0 L 0 phibar+.1]); % Comment for generic problems. dx = L/nx;
pause(.1); dt = T/nt;
end r = alpha*dt/dxˆ2;
legend(’Crank’,’Exact’,0); x = linspace(0,L,nx+1);
fprintf(’e=%g\n’,max(abs(U-u))/max(abs(U))*100) %Comment for generic problems. t = linspace(0,T,nt+1);
figure(2); plot(x,U-u,’r’); legend(’Error’,0); %Comment for generic problems. u = sin(pi*x’/L); % Initial conditions
pu = u;
%———-Subfunctions——————– % j = 2;
function phi = GetPhi(x, nx, x1, x2, phibar) u(2:nx) = (1-2*r)*u(2:nx) + r*(u(1:nx-1)+u(3:nx+1));
FA

FA
% Subfunction for initial conditions u(1) = 0; u(nx+1) = 0; % Boundary conditions
phi = zeros(nx+1,1); % Example U = sin(pi*x’/L)*exp(-(pi/L)ˆ2*T); % Exact solution
for i=1:nx+1
C

C
phi(i) = phibar* ((x2 - x(i))>0)*((x(i) - x1)>0); % Finite-difference method
SI

SI
% This instruction is equivalent to for j=3:nt+1
% if x(i)<=x2 && x(i)>=x1 ppu = pu;
M

M
% phi(i) = phibar; pu = u;
IL

IL
% end u(2:end-1) = ppu(2:nx) + 2*r*(pu(1:nx-1) - 2*pu(2:nx)+pu(3:nx+1));
end u(1) = 0; u(nx+1) = 0; % Boundary conditions
E

E
%—————————————— plot(x,u,x,U,’r*:’);
xlabel(’x’);
-O

-O
function g1 = GetG1(t, alpha, x1, x2, phibar)
% Subfunction for the first boundary condition. Example ylabel(’U’);
g1 = .5*phibar*(erf(-x1/sqrt(4*alpha*t)) + erf(x2/sqrt(4*alpha*t))); title([’t = ’,num2str(t(j))]);
N

N
%—————————————— pause(.1);
LY

LY
function g2 = GetG2(t, alpha, L, x1, x2, phibar) end
% Subfunction for the second boundary condition. Example legend(’Richardson’,’Exact’,0);
fprintf(’e = %g\n’,max(abs(U-u))/max(abs(U))*100)
FO

FO
g2 = .5*phibar*(erf((L-x1)/sqrt(4*alpha*t))+erf((x2-L)/sqrt(4*alpha*t)));
figure(2);
plot(x,U-u,’r’);
R

R
3.9.2. Richardson function legend(’Error’,0);
A

A
The following listing presents a Matalab program for the Richardson method, which was
A

A
introduced in Section 3.3.1. Since the method is unstable in any case, it cannot be used in 3.9.3. Dufort1 function
EM

EM
practice. The example is useful only to familiarize with unstable results, so that they can be
The analysis of consistency for the Du Fort-Frankel method (Section 3.3.4) emphasized
immediately recognized in other situations.
that the mentioned method can approximate parabolic equations or hyperbolic equations or
ST

ST
become inconsistent, depending on how the truncation error goes to zero. The following
function richardson program illustrates the three situations (Example 3.3.2).
U

U
% This is the function file richardson.m
% Richardson method is used for solving the following Dirichlet problem:
D

D
% Ut - alpha Uxx = 0, function u = dufort1
EN

EN
% U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t). % This is the function file dufort1.m
% Example: % Du Fort-Frankel method is used for solving the following Dirichlet problem:
TS

TS
% phi(x) = sin(pi*x/L), G1(t) = 0, G2(t)=0, % Ut - alpha Uxx = 0,
% Exact solution: U = sin(pi*x/L)*exp(-(pi/L)ˆ2*t). % U() = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).
% Example:
% Problem parameters % phi(x) = sin(pi*x/L), G1(t) = 0, G2(t)=0,
alpha = 1; L = 2; T = .5; nx = 30; nt = 300; % Exact solution: U = sin(pi*x/L)*exp(-(pi/L)ˆ2*t).
3.9.
. MATLAB INTERLUDE 3 129 130 CHAPTER 3. PARABOLIC EQUATIONS
% Problem parameters. Uncomment the desired case Exercise 3.9.1 Although the Du Fort-Frankel method is a three-time-level method it can
alpha = 1; L = 1; T = .25; nx = 40; nt = 400; % Heat (dt = dxˆ2) be arranged in matrix form typical of two-time-level schemes, as shown in Section 3.3.5.
% alpha = 1; L = 1; T = 5; nx = 20; nt = 100; % Wave (dt = dx) Write down a script file using the mentioned matrix form.
% alpha = 1; L = 1; T = 5; nx = 100; nt = 50; % Inconsistent (dtˆ2 = dx)
if any([alpha L T nx-2 nt-2]<=0)
Hint. Modify dufort1 function file as follows.
error(’Check problem parameters’)
end

% Initialization % This is the function file dufort2.m


dx = L/nx;
dt = T/nt; ... (same code as in dufort1)
r = alpha*dt/dxˆ2;
x = linspace(0,L,nx+1); B = [A*p q*eye(nx-1,nx-1); eye(nx-1,nx-1) zeros(nx-1,nx-1)];
t = linspace(0,T,nt+1);
FA

FA
% Anonymous functions for initial-boundary conditions % Finite-difference method
phi = @(x) sin(pi*x’/L);
... (same code as in Dufort1)
C

C
g1 = @(t) 0;
g2 = @(t) 0;
SI

SI
for j=3:nt+1
u = phi(x); a(1) = p*g1(t(j-1));
M

M
a = sparse(nx-1,1); a(nx-1) = p*g2(t(j-1));
CC = [r*ones(nx-1,1) (1-2*r)*ones(nx-1,1) r*ones(nx-1,1)]; z = B*[u(2:nx);v(2:nx)] + [a;zeros(nx-1,1)];
IL

IL
C = spdiags(CC, -1:1, nx-1, nx-1);
u(2:nx) = z(1:nx-1);
AA = [ones(nx-1,1) zeros(nx-1,1) ones(nx-1,1)];
E

E
v(2:nx)=z(nx:end);
A = spdiags(AA, -1:1, nx-1, nx-1);
u(1) = g1(t(j));
-O

-O
p = 2*r/(1+2*r);
u(nx+1) = g2(t(j));
q = (1-2*r)/(1+2*r);
plot(x,u,x,U,’r’);
N

N
U = sin(pi*x’/L)*exp(-(pi/L)ˆ2*T); % Example
LY

LY
... (same code as in Dufort1)
% Finite-difference method
j = 2;
v = u;
FO

FO
3.9.4. Upwind function
a(1) = r*g1(t(j-1)); a(nx-1) = r*g2(t(j-1));
u(2:nx) = C*v(2:nx)+a; The upwind method for the convection-diffusion equation was discussed in Section 3.5.1.
R

R
u(1) = g1(t(j)); The following listing presents a program for the problems in Examples 3.5.1 and 3.5.2. The
A

A
u(nx+1) = g2(t(j)); program works for positive and negative velocities.
for j=3:nt+1
A

A
z = v; function u = upwind
EM

EM
v = u; % This is the function file upwind.m
a(1) = p*g1(t(j-1)); % Upwind method is used for
a(nx-1) = p*g2(t(j-1)); % solving the following initial-boundary-value problem:
ST

ST
u(2:nx) = A*p*v(2:nx) + q*z(2:nx) + a; % Ut + v Ux = alpha Uxx,
u(1) = g1(t(j)); % U(x,0) = phi(x), U(0,t) = g1(t), U(L,t) = g2(t)
U

U
u(nx+1) = g2(t(j)); % Example 1
D

D
plot(x,u,x,U,’r*:’); % phi(x) = k if x >= x1 and x <= x2
EN

EN
xlabel(’x’); ylabel(’U’); % phi(x) = 0, otherwise,
title([’t = ’,num2str(t(j))]); % g1(t) = g2(t) = 0.
TS

TS
pause(.1); % Example 2
end % phi(x) = k*(x -x1)/(x0 - x1) if x1 <= x <= x0,
%legend(’DuFort’,’Exact’,0); % phi(x) = k*(x2 -x)/(x2 - x0) if x0 <= x <= x2,
fprintf(’e=%g\n’,max(abs(U-u))/max(abs(U))*100)% Percentage relative error % phi(x) = 0, otherwise,
figure(2); plot(x,U-u,’r’); legend(’Error’,0); % g1(t) = g2(t) = 0.
3.9.
. MATLAB INTERLUDE 3 131 132 CHAPTER 3. PARABOLIC EQUATIONS
% Problem parameters g1 = 0; % Examples 1,2
alpha = 0.001; L = 100; T = 2; nx = 150; nt = 500; %———————————–
if any([alpha L T nx-2 nt-2]<=0) function g2 = GetG2
error(’Check problem parameters’) g2 = 0; % Examples 1,2
end
v = 10; % Example 1 Exercise 3.9.2 Consider the the convection-diffusion equation
% v = -10; % Example 2

% Stability Ut + vUx − αUxx = 0.


st = 1; dx = L/nx;
while 2*alpha*T/nt/dxˆ2 + T/nt/dx*abs(v) > st In Section 3.5.1 an explicit method was provided for the equation above, where the central
nt=nt+1; approximation was used for Ux . Modify upwind function for the mentioned method.
end Hint. Replace the code lines
dt = T/nt; r = dt/dxˆ2; s = dt/dx;
FA

FA
if v>=0
% Initialization p = alpha*r + v*s; q = alpha*r;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1); else
C

C
u = GetPhi(x, nx, L); U = u; p = alpha*r; q = alpha*r - v*s;
SI

SI
if v>=0 end
p = alpha*r + s*v; q = alpha*r;
M

M
else by the following
IL

IL
p = alpha*r; q = alpha*r - s*v;
p = alpha*r + v*s/2; q = alpha*r - v*s/2;
end
E

E
% Finite difference method
3.9.5. Convection1 function
-O

-O
for j=2:nt+1
u(2:end-1) = p*u(1:end-2) + (1- p - q )*u(2:end-1) + q*u(3:end); The following listing provides an application of the backward implicit method to the
N

N
u(1) = GetG1(t(j)); u(end) = GetG2; convection-diffusion equation, discussed in Section 3.5.2.
plot(x,u,’r*:’,x,U,’k’);
LY

LY
xlabel(’x’); ylabel(’U(x,t)’);
title([’t=’,num2str(t(j))]); function u = convection1
FO

FO
pause(.01); % This is the function file convection1.m
end % Backward Euler implicit method is used for solving the following Dirichlet problem:
R

R
legend([’t = ’,num2str(T)],’t = 0’,0); % Ut + v Ux - alpha Uxx = 0,
% U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).
A

A
%———-Subfunctions————-
% Example 1
function phi = GetPhi(x, nx, L)
A

A
% phi(x) = sin(pi*x),
phi = zeros(nx+1,1); % Examples 1,2
EM

EM
% G1(t) = sin(-pi*v*t)*exp(-piˆ2*t), G2(t) = sin(pi*(L-v*t))*exp(-piˆ2*t),
dx = L/nx; k = 1;
% Exact solution: U = sin(pi*(x - v*t))*exp(-piˆ2*t),
x0 = L/2; x1 = x0 - 4*dx; x2 = x0 + 4*dx;
% Example 2
for i=1:nx+1 % Example 1
ST

ST
% phi(x) = k if x >= x1 and x <= x2
phi(i) = k*(x(i)>=x1)*(x(i)<=x2);
% phi(x) = 0, otherwise,
end
U

U
% g1(t) = g2(t) = 0.
% for i=1:nx+1 % Example 2
D

D
% if x(i)<=x0 % Problem parameters
EN

EN
% phi(i) = k*(x(i) - x1)/(x0 - x1)*(x(i)>=x1); % Modify the problem parameters according to the problem.
% else % Parameters should be checked, otherwise catastrophic errors could occur.
TS

TS
% phi(i) = k*(x2 - x(i))/(x2 - x0)*(x(i)<=x2); alpha = 1; L = 1; T = 1; nx = 200; nt = 6000; v = .2; % Example 1
% end % alpha = 0.001; L = 100; T = 2; nx = 150; nt = 1000; v = 10; % Example 2
% end if any([alpha L T nx-2 nt-2]<=0)
%———————————– error(’Check problem parameters’)
function g1 = GetG1(t) end
3.9.
. MATLAB INTERLUDE 3 133 134 CHAPTER 3. PARABOLIC EQUATIONS
% Initialization Hint. norm(full(C),1), or norm(full(C),2), or norm(full(C),inf).
dx = L/nx; dt = T/nt;
r = dt/dxˆ2; Exercise 3.9.4 Consider the following Dirichlet problem
s = dt/dx;
p = alpha*r + v*s/2; q = alpha*r - v*s/2; Ut + vUx − αUxx = 0, 0 < x < L, 0 < t ≤ T,
x = linspace(0,L,nx+1);
t = linspace(0,T,nt+1); U (x, 0) = 0, 0 ≤ x ≤ L,
b = sparse(nx-1,1); 
BB = [-p*ones(nx-1,1) (1+p+q)*ones(nx-1,1) -q*ones(nx-1,1)]; K if t ≤ t0 ,
U (0, t) = U (L, t) = 0, t > 0.
B = spdiags(BB, -1:1, nx-1, nx-1); 0 otherwise,
u = GetPhi(x, nx, L);
U = sin(pi*(x’-v*T))*exp(-piˆ2*T); % Example 1: exact solution. Write a function file, named convection2, which uses the upwind version of the backward
%U = u; % Example 2: initial condition implicit method for the convection-diffusion equation.
FA

FA
% Finite-difference method Hint. Consider convection1 function. Replace the code line
for j = 2:nt+1 p = alpha*r + v*s/2; q = alpha*r - v*s/2;
C

C
b(1) = p*GetG1(t(j), v);
b(end) = q*GetG2(t(j), L, v); by the following
SI

SI
u(2:end-1) = B\(u(2:end-1) + b); if v>=0
u(1) = GetG1(t(j), v); p = alpha*r + v*s; q = alpha*r;
M

M
u(end) = GetG2(t(j), L, v); else
IL

IL
plot(x,U,’k’,x,u,’r*:’); p = alpha*r; q = alpha*r - v*s;
xlabel(’x’); ylabel(’U’); end
E

E
title([’t = ’,num2str(t(j))]);
In addition, modify the sub-functions definition as follows
-O

-O
pause(.1);
end %———-Subfunctions——————–
legend(’Backward’,’Exact’,0); % Example 1 function phi = GetPhi(nx)
N

N
% legend([’t = ’,num2str(T)],’t = 0’,0); % Example 2 phi = zeros(nx+1,1);
LY

LY
fprintf(’e=%g\n’,max(abs(U-u))/max(abs(U))*100) %Percentage relative error %——————————————
figure(2); plot(x,U-u,’r’); legend(’Error’,0); % Example 1 function g1 = GetG1(t)
t0 = .5; k = 1;
FO

FO
%———-Subfunctions——————– g1 = k*(t<=t0);
function phi = GetPhi(x, nx, L) %——————————————
R

R
phi = sin(pi*x’); % Example 1 function g2 = GetG2
% phi = zeros(nx+1,1); % Example 2
A

A
g2 = 0;
% dx = L/nx; k = 1; x0 = L/2;
A

A
% x1 = x0 - 4*dx; x2 = x0 + 4*dx; Finally, update the commands where the sub-functions are called.
EM

EM
% for i=1:nx+1
% phi(i) = k*(x(i)>=x1)*(x(i)<=x2); Exercise 3.9.5 Write a function file, named convection3, which uses the Crank-Nicol-
% end
son implicit method for the convection-diffusion equation. Compare the results with those
ST

ST
%——————————————
function g1 = GetG1(t, v)
achieved by convection1.
U

U
g1 = sin(-pi*v*t)*exp(-piˆ2*t); % Example 1
D

D
% g1 = 0; % Example 2 3.9.6. Mol functions
%——————————————
EN

EN
function g2 = GetG2(t, L, v) The method of lines was introduced in Section 3.6. The following function is an application
g2 = sin(pi*(L-v*t))*exp(-piˆ2*t); % Example 1 of the mentioned method to the heat equation.
TS

TS
% g2 = 0; % Example 2
function u = mol1
Exercise 3.9.3 Add a code line to convection1 listing to display matrix norms. Execute % This is function file mol1.m
the program and notice decreasing values of any matrix norm for increasing nt . % Method of lines is used for solving the following Dirichlet problem:
3.9.
. MATLAB INTERLUDE 3 135 136 CHAPTER 3. PARABOLIC EQUATIONS
% Ut - alpha Uxx = 0, u(1) = GetG1(gamma, H);
% U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t). u(nx+1) = GetG2(L, gamma, H, T, T1);
% Example 1 plot(u,x,’r*:’);
% phi(x) = gamma*(H - x) + q, G1(t) = gamma*H, G2(t) = gamma*(H - L). xlabel(’u’); ylabel(’z’);
% Example 2 title(’MOL’);
% phi(x) = H + q/gamma, G1(t) = H, G2(t) = H. year = 365*24*3600;
% Example 3 legend(’t = 0 years’,[’t = ’,num2str(T/year),’ years’],0);
% phi(x) = H, G1(t) = H, hold off; % restore the default mode
% G2(t) = H - (H-L)*t/T1, if t<T1,
%———-Subfunctions————-
% G2(t) = L, otherwise.
function phi = GetPhi(q, nx, x, gamma, H)
% Problem parameters phi = gamma*(H - x’) + q*ones(nx+1,1); % Example 1
% Modify the problem parameters according to the problem. % phi = (H + q/gamma)*ones(nx+1,1); % Example 2
% Parameters should be checked, otherwise catastrophic errors could occur. % phi = H*ones(nx+1,1); % Example 3
FA

FA
alpha = 10ˆ(-7); L = 10; T = 3*365*24*3600; nx = 50; %———————————–
if any([alpha L T nx-2]<=0) function g1 = GetG1(gamma, H)
g1 = gamma*H; % Example 1
C

C
error(’Check problem parameters’)
end % g1 = H; % Example 2
SI

SI
% g1 = H; % Example 3
% Initialization %———————————–
M

M
dx = L/nx; function g2 = GetG2(L, gamma, H, t, T1)
p = alpha/dxˆ2;
IL

IL
g2 = gamma*(H - L); % Example 1
x = linspace(0,L,nx+1); % g2 = H; % Example 2
E

E
a = zeros(nx-3,1); % if t<T1 %Example 3
AA = [ones(nx-1,1) -2*ones(nx-1,1) ones(nx-1,1)];
-O

-O
% g2 = H - (H-L)*t/T1;
A = p*spdiags(AA, -1:1, nx-1,nx-1);
% else
gamma = 1;% ton/mˆ3
% g2 = L;
N

N
q = 40;% ton/mˆ2
% end
LY

LY
H = 12;
%———————————–
T1 = 30*24*3600;
function udot=system(t, u, B, b, p, L, gamma, H, T1)
u = GetPhi(q, nx, x, gamma, H); % Initial conditions.
FO

FO
plot(u,x,’k’); udot = B*u + [p*GetG1(gamma, H); b; p*GetG2(L, gamma, H, t, T1)];
% axis([10 12.1 0 10]); % Example 3
R

R
hold on; % This command holds the current plot so that other graphes can be added.
An important feature of the ODE suite is the possibility of specifying the error tolerance
A

A
% Method of lines. The @ operator is used to pass system as a function handle as an optional argument. For example, to set the absolute tolerance to 10 digit accuracy,
A

A
% to ode45. A function handle is a Matlab value that provides a mean of
replace the code line
EM

EM
% calling a function indirectly. The resulting output is a column vector t and
% a solution matrix y. Each row in y corresponds to a time returned in the [t,y] = ode45(@system, [0 T ], u(2:end-1), [ ], A, a, p, L);
% corresponding row of t. The simplest syntax for calling ODE-solver functions
% is the following with the following two lines
ST

ST
% [t,y] = solver(@fun,ti,ic,options,p1,p2,...) options = odeset(’abstol’,1e-10);
% where:
U

U
[t,y] = ode45(@system, [0 T ], u(2:end-1), options, A, a, p, L);
% fun denotes the user written function containing the differential equation to
D

D
% solve (system in this listing);
EN

EN
% ti denotes the time interval, a vector which specifies initial and final time; Exercise 3.9.6 Consider the following Dirichlet problem
% ic is the vector with the initial conditions;
Ut − Uxx = 0, 0 < x < L, 0 < t ≤ T,
TS

TS
% options is a structure that must be created with the function odeset; since in
% this example there are no options, a placeholder [ ] has been used;
% p1, p2,... are optional parameters that are passed to fun (system). U (x, 0) = x2 , 0 ≤ x ≤ L,
[t,y] = ode45(@system, [0 T ], u(2:end-1), [ ], A, a, p, L, gamma, H, T1);
u(2:nx) = y(end,:); % The last row is copied and the boundary conditions are added. U (0, t) = 2t, U (L, t) = 2t + L2 , t > 0.
3.9.
. MATLAB INTERLUDE 3 137 138 CHAPTER 3. PARABOLIC EQUATIONS
The exact solution of the problem above is given by %———-Subfunctions————-
function phi = GetPhi(q, nx, gamma, H)
U (x, t) = x2 + 2t. phi = (H + q/gamma)*ones(nx+1,1); % Example
%———————————–
Modify the function Mol1 for the problem above. Also, evaluate the error. function g1 = GetG1
g1 = 0; % Example
The method of lines was introduced in Section 3.6 and the application to the Neumann %———————————–
function g2 = GetG2(H)
problem for the heat equation was discussed in Section 3.6.2. The following listing presents
g2 = H; % Example
a program for the Dirichlet-Neumann problem in Exemple 3.6.3. %———————————–
function udot = system(t, u, B, b, p, H, dx)
function u = mol2 udot = B*u + [2*p*dx*GetG1; b; p*GetG2(H)];
% This is function file mol2.m
% Method of lines is used for solving the following Dirichlet-Neumann problem: The method of lines for the convection-diffusion equation was discussed in Section
FA

FA
% Ut - alpha Uxx = 0, 3.6.3. The following listing presents a Matlab function for the problem in Example 3.6.4.
% U(x,0) = phi(x), -Ux(0,t) = G1(t), U(L,t) = G2(t).
The program considers wether the central approximation of Ux or the upwind approxima-
C

C
% Example
% phi(x) = H + q/gamma, G1(t) = 0, G2(t) = H. tion.
SI

SI
%Problem parameters
M

M
function u = mol3
% Modify the problem parameters according to the problem. % This is function file mol3.m
IL

IL
% Parameters should be checked, otherwise catastrophic errors could occur. % Method of lines is used for solving the following Dirichlet problem:
alpha = 10ˆ(-7); L = 10; T = 3*365*24*3600; nx = 50; % Ut +v Ux - alpha Uxx = 0,
E

E
if any([alpha L T nx-2]<=0) % U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).
-O

-O
error(’Check problem parameters’) % Example
end % phi(x) = 0, G2(t) = 0
N

N
% G1(t) = t/t0 - tˆ2/t0ˆ2, if t<t0,
% Initilization
% G1(t) = 0, otherwise.
LY

LY
dx = L/nx;
p = alpha/dxˆ2; % Problem parameters. Modify according to the problem.
x = linspace(0,L,nx+1); % Parameters should be checked, otherwise catastrophic errors could occur.
FO

FO
AA = [ones(nx,1) -2*ones(nx,1) [0;2;ones(nx-2,1)]]; alpha = 0.01; v = 10; L = 100; T = 4; nx = 150;
A = p*spdiags(AA, -1:1, nx,nx); if any([alpha L T nx-2]<=0)
R

R
a = zeros(nx-2,1); error(’Check problem parameters’)
gamma = 1;%ton/mˆ3 end
A

A
q = 40;%ton/mˆ2
A

A
H = 12; % Initialization
EM

EM
u = GetPhi(q, nx, gamma, H); % Vector u is initialized with the initial conditions. dx = L/nx;
plot(u,x,’k’); % p = alpha/dxˆ2 + v/dx/2; % Central. Uncomment ...
hold on; % This command holds the current plot so that other graphes can be added. % q = alpha/dxˆ2 - v/dx/2; % ... the desired method
ST

ST
if v>=0 % Upwind. Uncomment the desired method
%Method of lines. p = alpha/dxˆ2 + v/dx; q = alpha/dxˆ2;
[t,y] = ode45(@system, [0 T], u(1:end-1), [ ], A, a, p, H, dx);
U

U
else
u(1:nx) = y(end,:); % The last row is copied and ...
D

D
p = alpha/dxˆ2; q = alpha/dxˆ2 - v/dx;
u(nx+1) = GetG2(H); % ... the boundary conditions are added. end
EN

EN
(u,x,’r*:’); x = linspace(0,L,nx+1);
xlabel(’u’); a = zeros(nx-3,1);
TS

TS
ylabel(’z’); AA = [p*ones(nx-1,1) -(p + q)*ones(nx-1,1) q*ones(nx-1,1)];
title(’MOL’); A = spdiags(AA, -1:1, nx-1,nx-1);
year = 365*24*3600; u = GetPhi(nx); % Initial conditions.
legend(’t = 0 years’,[’t = ’,num2str(T/year),’years’],0);
hold off; % restore the default mode % Method of lines
3.9.
. MATLAB INTERLUDE 3 139 140 CHAPTER 3. PARABOLIC EQUATIONS
% Tic function measures the current time. Also toc function (see below) measures % Example 2
% the current time and compare it with that passed by tic function. The difference, % alpha(x,t) = 1 + t,
% expressed in seconds, is displayed. In this way the user can get the amount of % phi(x) = sin(pi*x/L), G1(t) = 0, G2(t) = 0,
% time that has passed since the computing process started. % Exact solution: U = sin(pi*x/L)*exp(-(pi/L)ˆ2*(t+tˆ2/2)).
tic
[t,y] = ode45(@system, [0 T], u(2:end-1), [ ], A, a, p, q); % Problem parameters
u(2:nx) = y(end,:); % The last row is copied and the boundary conditions are added. % Modify the problem parameters according to the problem.
u(1) = GetG1(T); u(nx+1) = GetG2; % Parameters should be checked, otherwise catastrophic errors could occur.
plot(x,u,’r’); L = 10; T = 1; nx = 60;
toc % see tic. if any([L T nx-2]<=0)
title([’t = ’,num2str(T)]); error(’Check problem parameters’)
legend(’Central’,0); % Central end
% legend(’Upwind’,0); % Upwind
% Initialization
x = linspace(0,L,nx+1);
FA

FA
%———-Subfunctions————-
function phi = GetPhi(nx) nt = 50;
phi = zeros(nx+1,1); % Example tt = linspace(0,T,nt+1);
C

C
%———————————– u = GetPhi(x, L); % Vector u is initialized with the initial conditions.
SI

SI
function g1 = GetG1(t)
% Method of lines. A loop could provide more accurate results
t0 = .5; % Example
M

M
for j=1:nt
g1 = (t/t0 - tˆ2/t0ˆ2)*(t<t0);
[t,y] = ode15s(@system, [tt(j) tt(j+1)], u(2:end-1), [ ], L , nx, x);
IL

IL
%———————————–
u(2:nx) = y(end,:);
function g2 = GetG2
E

E
u(1) = GetG1(tt(j+1)); u(nx+1) = GetG2(tt(j+1), L);
g2 = 0; % Example
U = x’.ˆ2 + 2*tt(j+1) + tt(j+1)ˆ2; % Example 1: exact solution
-O

-O
%———————————–
% U = sin(pi*x/L)*exp(-(pi/L)ˆ2*(tt(j+1) + tt(j+1)ˆ2/2)); % Example 2: exact solution
function udot = system(t, u, A, a, p, q)
plot(x,u,x,U,’r’);
udot = A*u + [p*GetG1(t); a; q*GetG2];
N

N
xlabel(’x’); ylabel(’U’);
LY

LY
title([’t = ’,num2str(tt(j+1))]);
Exercise 3.9.7 Modify mol3 function for the following problem pause(.1);
end
FO

FO
Ut + vUx − αUxx = 0, fprintf(’e=%g\n’,max(abs(U-u))/max(abs(U))*100) % Percentage relative error
legend(’MOL’,’Exact’,0);
U (x, 0) = x2 ,
R

R
figure(2); plot(x,U-u,’r’); legend(’Error’,0);
U (0, t) = v 2 t2 + 2t, U (L, t) = (L − vt)2 + 2t.
A

A
%———-Subfunctions————-
A

A
function phi = GetPhi(x, L)
The exact solution of the problem above is the function U = (x − vt)2 + 2t. phi = x’.ˆ2; % Example 1
EM

EM
% phi = sin(pi*x’/L); % Example 2
The method of lines for the one-dimensional heat equation with variable diffusivity %———————————–
coefficient was introduced in Section 3.7. The following listing presents a Matlab function function g1 = GetG1(t)
ST

ST
for the problem in Example 3.7.1. g1 = 2*t + tˆ2; % Example 1
% g1 = 0; % Example 2
U

U
function u = mol4 %———————————–
D

D
% This is function file mol4.m function g2 = GetG2(t, L)
EN

EN
% Method of lines is used for solving the following Dirichlet problem: g2 = 2*t + tˆ2 + Lˆ2; % Example 1
% Ut - alpha(x,t) Uxx = 0, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t). % g2 = 0; % Example 2
TS

TS
% where alpha(x,t) must be a strictly positive function %———————————–
% Example 1 function p = GetP(x, t, dx, nx)
% alpha(x,t) = 1 + t, p = (1 + t)*ones(nx+1,1)/dxˆ2; % Examples 1 and 2
% phi(x) = xˆ2, G1(t) = 2*t + tˆ2, G2(t) = Lˆ2 + 2*t + tˆ2, %———————————–
% Exact solution: U = xˆ2 + 2*t + tˆ2. %function A = GetA(p)
3.9.
. MATLAB INTERLUDE 3 141 142 CHAPTER 3. PARABOLIC EQUATIONS
n = length(p); u(1) = GetG1(tt(j+1), alpha, v);
AA = [p(3:n) -2*p(2:n-1) p(1:n-2)]; u(nx+1) = GetG2(tt(j+1), alpha, v, L);
A = spdiags(AA, -1:1, n-2, n-2); U = 2*alpha/v - 2*alpha/v*tanh(x’ - 2*alpha*tt(j+1)); % Example 1: exact solution
%———————————– % U = -2*alpha/v - 2*alpha/v*tanh(x’ + 2*alpha*tt(j+1)); % Example 2: exact solution
function udot = system(t, v, L, nx, x) plot(x,u,x,U,’r*:’);
% Notice that v(1) = u(2),...,v(nx-1) = u(nx) title([’t = ’,num2str(tt(j+1))]);
dx = L/nx; xlabel(’x’); ylabel(’U’);
p = GetP(x, t, dx, nx); pause(.1);
udot = GetA(p)*v + [p(2)*GetG1(t); zeros(nx-3,1); p(nx)*GetG2(t, L)]; end
fprintf(’e=%g\n’,max(abs(U-u))/max(abs(U))*100) % Percentage relative error
legend(’MOL’,’Exact’,0);
Exercise 3.9.8 Modify Mol4 function for the convection-diffusion equation with variable
figure(2);
coefficients discussed in Section 3.7. plot(x,U-u,’r’);
legend(’Error’,0);
FA

FA
3.9.7. Burgers function
%———-Subfunctions————-
The following listing presents a Matlab application of the method of lines to the one- function phi = GetPhi(x, alpha, v)
C

C
dimensional Burgers’ equation discussed in Section 3.8.2. phi = 2*alpha/v - 2*alpha/v*tanh(x’); % Example 1
SI

SI
% phi = -2*alpha/v - 2*alpha/v*tanh(x’); % Example 2
%———————————–
M

M
function u = burgers
function g1 = GetG1(t, alpha, v)
% This is function file burgers.m
IL

IL
g1 = 2*alpha/v - 2*alpha/v*tanh(-2*alpha*t); % Example 1
% Method of lines is used for solving the following Dirichlet problem:
% g1 = -2*alpha/v - 2*alpha/v*tanh(2*alpha*t); % Example 2
% Ut + v U Ux = alpha Uxx, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t),
E

E
%———————————–
% Example 1
function g2 = GetG2(t, alpha, v, L)
-O

-O
% phi(x) = 2*alpha/v - 2*alpha/v*tanh(x),
g2 = 2*alpha/v - 2*alpha/v*tanh(L - 2*alpha*t); % Example 1
% G1(t) = 2*alpha/v - 2*alpha/v*tanh(-2*alpha*t),
% g2 = -2*alpha/v - 2*alpha/v*tanh(L + 2*alpha*t); % Example 2
N

N
% G2(t) = 2*alpha/v-2*alpha/v*tanh(L-2*alpha*t),
%———————————–
% Exact solution: U = 2*alpha/v - 2*alpha/v*tanh(x-2*alpha*t).
LY

LY
function udot = system(t, w, L, nx, alpha, v)
% Example 2
% Notice that w(1) = u(2),...,w(nx-1) = u(nx)
% phi(x) = 2*alpha/v-2*alpha/v*tanh(x),
dx = L/nx; p = alpha/dxˆ2; q = v/2/dx;
FO

FO
% G1(t) = -2*alpha/v-2*alpha/v*tanh(2*alpha*t),
udot = zeros(nx-1,1);
% G2(t) =-2*alpha/v-2*alpha/v*tanh(L+2*alpha*t),
udot(1) = -q*w(1)*(w(2) - GetG1(t, alpha, v)) + p*(w(2)-2*w(1))...
% Exact solution: U = -2*alpha/v-2*alpha/v*tanh(x+2*alpha*t).
R

R
+ p*GetG1(t, alpha, v);
udot(2:nx-2) = -q*w(2:nx-2).*(w(3:nx-1) - w(1:nx-3)) + p*(w(3:nx-1)...
A

A
% Problem parameters
% Modify the problem parameters according to the problem. - 2*w(2:nx-2) + w(1:nx-3));
A

A
% Parameters should be checked, otherwise catastrophic errors could occur. udot(nx-1) = -q*w(nx-1)*(GetG2(t, alpha, v, L) - w(nx-2)) + p*(w(nx-2)...
EM

EM
L = 1; alpha = 1; v = 1; T = 1; nx = 20; - 2*w(nx-1)) + p*GetG2(t, alpha, v, L);
if any([alpha L T nx-2]<=0)
error(’Check problem parameters’) 3.9.8. Nonlinear function
ST

ST
end
The following listing presents a Matlab application of the method of lines to the nonlinear
U

U
% Initialization
equation discussed in Example 3.8.2.
D

D
x = linspace(0,L,nx+1);
nt = 50;
EN

EN
tt = linspace(0,T,nt+1); function u = nonlinear
u = GetPhi(x, alpha, v);% % Vector u is initialized with the initial conditions. % This is function file nonlinear.m
TS

TS
% Method of lines is used for solving the following Dirichlet problem:
% Method of lines. A loop could provide more accurate results % Ut = Uxˆ2/(1+U) + Uxx, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t),
for j = 1:nt % Example 1
[t,y] = ode45(@system, [tt(j) tt(j+1)], u(2:end-1), [ ], L, nx, alpha, v); % phi(x) = -1 + sqrt(1 + 2*x.ˆ2),
u(2:nx) = y(end,:); % G1(t) = -1 + sqrt(1 + 4*t), G2(t) = -1 + sqrt(1 + 2*(Lˆ2 + 2*t)),
3.9.
. MATLAB INTERLUDE 3 143 144 CHAPTER 3. PARABOLIC EQUATIONS
% Exact solution: U = -1 + sqrt(1+ 2*(Lˆ2 + 2*t)). udot(2:nx-2) = p/4*(w(3:nx-1) - w(1:nx-3)).ˆ2./(1 + w(2:nx-2)) + p*(w(3:nx-1)...
% Example 2 - 2*w(2:nx-2) + w(1:nx-3));
% phi(x) = -1 - sqrt(1 + 2*x.ˆ2), udot(nx-1) = p/4*(GetG2(t, L) - w(nx-2))ˆ2/(1 + w(nx-1)) + p*(w(nx-2)...
% G1(t) = -1 + sqrt(1 + 4*t), G2(t) = -1 + sqrt(1 + 2*(x.ˆ2 + 2*t)), - 2*w(nx-1)) + p*GetG2(t,L);
% Exact solution: U = -1 - sqrt(1 + 2*(x.ˆ2 + 2*t)).

% Problem parameters
% Modify the problem parameters according to the problem. Further Reading: [Bellomo and Preziosi (1995)], [Bellomo, Lods, Revelli and Ridolfi
% Parameters should be checked, otherwise catastrophic errors could occur. (2008)], [Crank (1957)], [Forsythe and Wasov (1960)], [Hundsdorfer and Verwer (2003)],
L = 1; T = 1; nx = 20; [Kays and Crawford (1993)], [Lapidus and Pinter (1982)], [Larsson and Thomée (2003)],
if any([L T nx-2]<=0) [LeVeque (2007)], [Mitchell and Griffiths (1995)], [Morton (1996)], [Morton and May-
error(’Check problem parameters’) ers (2002)], [Necati Ozisik (1994)], [Parter (1979)], [Schiesser (2009)], [Smith (1985)],
end
[Strikwerda (2004)], [Thomée (2001)].
% Initialization
FA

FA
x = linspace(0,L,nx+1);
nt = 10;
C

C
tt = linspace(0,T,nt+1);
SI

SI
u = GetPhi(x);% % Vector u is initialized with the initial conditions.
M

M
% Method of lines. A loop provides more accurate results.
for j = 1:nt
IL

IL
[t,y] = ode15s(@system, [tt(j) tt(j+1) ], u(2:end-1), [ ], L, nx);
u(2:nx) = y(end,:);
E

E
u(1) = GetG1(tt(j+1)); u(nx+1) = GetG2(tt(j+1), L);
-O

-O
U = -1 + sqrt(1 + 2*(x’.ˆ2 + 2*tt(j+1))); % Example 1: exact solution
% U = -1 - sqrt(1 + 2*(x’.ˆ2 + 2*tt(j+1))); % Example 2: exact solution
N

N
plot(x,u,x,U,’r*:’);
title([’t = ’,num2str(tt(j+1))]); xlabel(’x’); ylabel(’U’);
LY

LY
pause(.1);
end
FO

FO
fprintf(’e=%g\n’,max(abs(U-u))/max(abs(U))*100) % Percentage relative error
legend(’MOL’,’Exact’,0);
figure(2); plot(x,U-u,’r’); legend(’Error’,0);
R

R
%———-Subfunctions————-
A

A
function phi = GetPhi(x)
A

A
phi = -1 + sqrt(1 + 2*x’.ˆ2); % Example 1
EM

EM
% phi = -1 - sqrt(1 + 2*x’.ˆ2); % Example 2
%———————————–
function g1 = GetG1(t)
g1 = -1 + sqrt(1 + 4*t); % Example 1
ST

ST
% g1 = -1 - sqrt(1 + 4*t); % Example 2
%———————————–
U

U
function g2 = GetG2(t, L)
D

D
g2 = -1 + sqrt(1 + 2*(Lˆ2 + 2*t)); % Example 1
EN

EN
% g2 = -1 - sqrt(1 + 2*(Lˆ2 + 2*t)); % Example 2
%———————————–
TS

TS
function udot = system(t, w, L, nx)
% Notice that w(1) = u(2),...,w(nx-1) = u(nx)
dx = L/nx; p = 1/dxˆ2;
udot = zeros(nx-1,1);
udot(1) = p/4*(w(2) - GetG1(t))ˆ2/(1 + w(1)) + p*(w(2) - 2*w(1)) + p*GetG1(t);

You might also like