0% found this document useful (0 votes)
32 views

D'Acunto - Matlab For Engineering [2021]

The document is a publication by World Scientific titled 'MATLAB for Engineering' authored by Bernardino D’Acunto, aimed at engineering students and professionals. It introduces MATLAB commands, focusing on matrix manipulation and solving partial differential equations using methods like Finite Element and Finite Difference. The book includes numerous examples, exercises, and a comprehensive index to assist readers in applying MATLAB to various scientific problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

D'Acunto - Matlab For Engineering [2021]

The document is a publication by World Scientific titled 'MATLAB for Engineering' authored by Bernardino D’Acunto, aimed at engineering students and professionals. It introduces MATLAB commands, focusing on matrix manipulation and solving partial differential equations using methods like Finite Element and Finite Difference. The book includes numerous examples, exercises, and a comprehensive index to assist readers in applying MATLAB to various scientific problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 326

12380_9789811240669_TP.

indd 1 9/7/21 9:15 AM


B1948 Governing Asia

This page intentionally left blank

B1948_1-Aoki.indd 6 9/22/2014 4:24:57 PM


World Scientific

12380_9789811240669_TP.indd 2 9/7/21 9:15 AM


Published by
World Scientific Publishing Co. Pte. Ltd.
5 Toh Tuck Link, Singapore 596224
USA office: 27 Warren Street, Suite 401-402, Hackensack, NJ 07601
UK office: 57 Shelton Street, Covent Garden, London WC2H 9HE

Library of Congress Cataloging-in-Publication Data


Names: D’Acunto, Bernardino, author.
Title: MATLAB for engineering / Berardino D’Acunto, University of Naples Federico II, Italy.
Description: New Jersey : World Scientific Publishing Co. Pte. Ltd., [2022] |
Includes bibliographical references and index.
Identifiers: LCCN 2021033978 | ISBN 9789811240669 (hardcover) |
ISBN 9789811240676 (ebook for institutions) | ISBN 9789811240683 (ebook for individuals)
Subjects: LCSH: Engineering--Data processing. | MATLAB.
Classification: LCC TA345 .D24 2022 | DDC 620.001/51--dc23
LC record available at https://lccn.loc.gov/2021033978

British Library Cataloguing-in-Publication Data


A catalogue record for this book is available from the British Library.

Copyright © 2022 by World Scientific Publishing Co. Pte. Ltd.


All rights reserved. This book, or parts thereof, may not be reproduced in any form or by any means,
electronic or mechanical, including photocopying, recording or any information storage and retrieval
system now known or to be invented, without written permission from the publisher.

For photocopying of material in this volume, please pay a copying fee through the Copyright Clearance
Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, USA. In this case permission to photocopy
is not required from the publisher.

For any available supplementary material, please visit


https://www.worldscientific.com/worldscibooks/10.1142/12380#t=suppl

Desk Editor: Amanda Yun

Typeset by Stallion Press


Email: [email protected]

Printed in Singapore

Amanda - 12380 - Matlab for Engineering.indd 1 14/9/2021 9:43:41 am


September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-FM page v

Preface

This book provides an introduction to Matlab for engineering students and


engineers. However, the book can also be useful for students, technicians
and researchers in all scientific areas whenever they need to apply Matlab
to solve problems arising from their areas of studies, work and research.
It is assumed that they do not have any initial Matlab knowledge and the
reader is very interested to learn this powerful tool of scientific computing.
After introducing the essential Matlab commands, mainly devoted to
matrix manipulation, most of the other commands are presented in action,
within programs aimed at solving specific problems. In the first part of
the book, the function file is presented. This concept plays a basic role in
Matlab programming and, therefore, is illustrated with many examples.
Since physical processes occur very often in space and time, the
related governing equations are partial differential equations. Therefore,
most Matlab programs in this book are devoted to solve these types of
equations. Both the Finite Element Method and Finite Difference Method
are introduced and applied. Generally, a problem is discussed globally:
the mathematical model from the physical phenomena is derived and the
equations are solved with Matlab.
The book presents more than a hundred listings and number of exercises
at the end of each chapter.
I wish to thank World Scientific Publishing for inviting me to write this
book. Special thanks are due to Shaun Tan Yi Jie, who helped me while I
was writing the book.

Berardino D’Acunto Naples, March 2021

v
B1948 Governing Asia

This page intentionally left blank

B1948_1-Aoki.indd 6 9/22/2014 4:24:57 PM


September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-FM page vii

Contents

Preface v
Chapter 1. Function Files 1
1.1 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.1 Creating Matrices . . . . . . . . . . . . . . . . . . 1
1.1.2 Matrix Indexing . . . . . . . . . . . . . . . . . . . 2
1.1.3 Matrix Manipulation . . . . . . . . . . . . . . . . 4
1.1.4 Tridiagonal Matrices . . . . . . . . . . . . . . . . . 6
1.1.5 Matrix Operations . . . . . . . . . . . . . . . . . . 8
1.1.6 Right and Left Divisions . . . . . . . . . . . . . . 9
1.2 Script Files . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.1 For Loop . . . . . . . . . . . . . . . . . . . . . . . 10
1.2.2 Examples of Script Files . . . . . . . . . . . . . . 11
1.3 Introduction to Function Files . . . . . . . . . . . . . . . . 15
1.3.1 Structure of Function Files . . . . . . . . . . . . . 15
1.3.2 Function with a Multiple Output Variable . . . . 17
1.3.3 Flow Control Structures . . . . . . . . . . . . . . . 19
1.3.4 Local Functions, Anonymous Functions . . . . . . 25
1.3.5 Logical Operators and Logical Functions . . . . . 27
1.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

Chapter 2. The Finite Difference Method 39


2.1 Finite Difference Approximations of Derivatives . . . . . . 39
2.1.1 Forward, Backward and Central Approximations . 39

vii
September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-FM page viii

viii Matlab for Engineering

2.1.2 Approximation of Functions Depending on Two


Variables . . . . . . . . . . . . . . . . . . . . . . . 46
2.1.3 Approximation of Higher Order Derivatives . . . . 47
2.2 Diffusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.2.1 Fourier’s Law and Heat Equation . . . . . . . . . 49
2.2.2 Fick’s Law and Diffusion . . . . . . . . . . . . . . 55
2.2.3 Free Boundary Value Problems . . . . . . . . . . . 56
2.3 Finite Difference Method . . . . . . . . . . . . . . . . . . . 58
2.3.1 Explicit Euler Method . . . . . . . . . . . . . . . . 58
2.3.2 Stability, Convergence, Consistence . . . . . . . . 64
2.3.3 Boundary Value Problems . . . . . . . . . . . . . 68
2.3.4 Diffusion in a Multi-layer Medium . . . . . . . . . 76
2.3.5 Implicit Euler Method . . . . . . . . . . . . . . . . 80
2.3.6 Crank–Nicolson Method . . . . . . . . . . . . . . . 84
2.3.7 Von Neumann Stability Criterium . . . . . . . . . 89
2.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

Chapter 3. Diffusion and Convection 99


3.1 Convection-diffusion Equation . . . . . . . . . . . . . . . . 99
3.1.1 Upwind Method . . . . . . . . . . . . . . . . . . . 99
3.1.2 Other Finite Difference Methods for the
Convection-Diffusion Equation . . . . . . . . . . . 107
3.1.3 Advection Equation . . . . . . . . . . . . . . . . . 111
3.2 Method of Lines . . . . . . . . . . . . . . . . . . . . . . . 118
3.2.1 Heat Equation . . . . . . . . . . . . . . . . . . . . 118
3.2.2 Nonlinear Equations . . . . . . . . . . . . . . . . . 127
3.2.3 Variable Diffusivity Coefficient . . . . . . . . . . . 133
3.2.4 Convection-Diffusion Equation . . . . . . . . . . . 136
3.3 Saving Data and Figures . . . . . . . . . . . . . . . . . . . 140
3.3.1 Save Function . . . . . . . . . . . . . . . . . . . . 140
3.3.2 Load Function . . . . . . . . . . . . . . . . . . . . 142
3.3.3 Saving Figures . . . . . . . . . . . . . . . . . . . . 143
3.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Chapter 4. Introduction to the Finite Element Method 153


4.1 Numerical Integration . . . . . . . . . . . . . . . . . . . . 153
4.2 Finite Element Method . . . . . . . . . . . . . . . . . . . 164
4.2.1 Axial Motion of a Bar . . . . . . . . . . . . . . . . 164
4.2.2 Weak Solution . . . . . . . . . . . . . . . . . . . . 167
September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-FM page ix

Contents ix

4.2.3 Shape Functions . . . . . . . . . . . . . . . . . . . 169


4.2.4 Boundary Value Problems . . . . . . . . . . . . . 171
4.2.5 Axial Displacement and Stress in a Bar . . . . . . 180
4.2.6 Concentrated Force and Dirac Function . . . . . . 184
4.3 Partial Differential Equations . . . . . . . . . . . . . . . . 189
4.3.1 Diffusion Equation . . . . . . . . . . . . . . . . . . 189
4.3.2 Wave Equation . . . . . . . . . . . . . . . . . . . . 197
4.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

Chapter 5. Introduction to the Finite Element Method


in Two Spatial Dimensions 213
5.1 Elliptic Partial Differential Equations . . . . . . . . . . . . 213
5.1.1 Green’s Identities . . . . . . . . . . . . . . . . . . 213
5.1.2 Boundary Value Problems . . . . . . . . . . . . . 214
5.2 Finite Element Method in Two Spatial Dimensions . . . . 217
5.2.1 Shape Functions . . . . . . . . . . . . . . . . . . . 217
5.2.2 Weak Form of the Poisson Equation . . . . . . . . 225
5.2.3 Dirichlet–Neumann Problem . . . . . . . . . . . . 230
5.2.4 Applications to the Dam and Sheet Pile Wall . . . 234
5.3 Finite Difference Method . . . . . . . . . . . . . . . . . . . 240
5.3.1 Five-Point Method . . . . . . . . . . . . . . . . . . 240
5.3.2 Model of a Dam . . . . . . . . . . . . . . . . . . . 247
5.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257

Chapter 6. The Euler–Bernoulli Beam 267


6.1 Finite Element Method . . . . . . . . . . . . . . . . . . . 267
6.1.1 Euler–Bernoulli Beam Equation . . . . . . . . . . 267
6.1.2 Shape Functions . . . . . . . . . . . . . . . . . . . 270
6.1.3 Weak Form . . . . . . . . . . . . . . . . . . . . . . 274
6.2 Statics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
6.3 Beam Subjected to Concentrated Forces . . . . . . . . . . 296
6.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

Bibliography 311
Index 313
B1948 Governing Asia

This page intentionally left blank

B1948_1-Aoki.indd 6 9/22/2014 4:24:57 PM


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 1

Chapter 1

Function Files

Matlab1 derives its power from its extensive capability of manipulating


matrices. Therefore, this topic is discussed in the first section of this chapter.
The name “Matlab” cames from Mat(rix) lab(oratory). Matlab programs
are written in files with an .m extension. There are two kinds of m-files:
script files and function files. The latter are much more interesting than
the former. Script files are illustrated in Sec. 1.2 while function files are
discussed in Sec. 1.3. See Moler (2011), which could be useful for the topics
in Sec. 1.1.

1.1 Matrices
1.1.1 Creating Matrices
The command
A = [1 2 3; 4 -5 6; 7 8 -9]
creates the matrix
⎡ ⎤
1 2 3

A= 4 −5 6 ⎦.
7 8 −9

The command A’ generates the transpose matrix of A. Therefore, the


command
B = A’

1 Matlab is a registered trademark of The MathWorks, Inc.

1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 2

2 Matlab for Engineering

produces the matrix


⎡ ⎤
1 4 7
B = ⎣2 −5 8 ⎦.
3 6 −9
A matrix with one row is a row vector and a matrix with one column is a
column vector. For example, the command
rv = [10 11 12 13]
produces

rv = [10 11 12 13]

and the command


cv = [pi; cos(pi)]
produces
 
3.1416
cv = .
−1.000
As noted, π is introduced in Matlab with the notation pi.

1.1.2 Matrix Indexing


To access specific elements of a matrix, use indexing. To refer to a single
element, use the A(i,j) command. For example, if A is the matrix introduced
beforehand, then the command
A(2,3)
returns 6 and the command
A(2,3) = 16
replaces 6 with 16 in A
⎡ ⎤
1 2 3
A = ⎣4 −5 16 ⎦.
7 8 −9
To access a submatrix of a matrix, use the colon operator. The A(i,:)
command returns the i-th row of A. For example, the command
A(2,:)
returns

[4 − 5 16],
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 3

Function Files 3

and the command


A(2,:) = 2*A(2,:)
replaces the preceding row in A with the row 8 −10 32
⎡ ⎤
1 2 3
A = ⎣8 −10 32 ⎦.
7 8 −9

The A(i:h,:) command, where i ≤ h, produces the submatrix formed by the


rows: i, i + 1, . . . , h. For example, the command
A(2:3,:)
produces
 
8 −10 32
,
7 8 −9
and the command
A(2:3,2:3)
produces
 
−10 32
.
8 −9
The previous command can also be used to extract submatrices with non-
consecutive rows (or columns). See Exercises 1.4.1 and 1.4.2.
The row vectors can be created with specific commands too. The
x = linspace(x1, x2, n) command creates a row vector of n equally spaced
elements from x1 to x2 with step (x2 − x1)/(n − 1). For example, the
command
x = linspace(0, 10, 6)
generates the vector

x = [0 2 4 6 8 10].

The same vector is generated with the x = 0:2:10 command, where the
initial value, step and final value are specified. Step 1 can be omitted. For
example, the command
y = 0:10
produces

y = [0 1 2 3 4 5 6 7 8 9 10].
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 4

4 Matlab for Engineering

The v(end) command returns the last element of the vector v. For example,
the command
y(end)
returns 10. This command can be useful when a vector is crested dynam-
ically during the program execution and the vector length is not known a
priori.
The A(i,:) = [ ] command deletes the i-th row of the matrix A.
For example, the A(3,:) = [ ] command deletes the third row and the
A([1 3],:) = [ ] command deletes the first and third rows. More generally,
the A(i:h,:) = [ ] command, where i ≤ h, deletes the rows from i to h.
This is similar for columns. The previous command can be used in other
situations. See Exercise 1.4.3.

1.1.3 Matrix Manipulation


A matrix can be appended to another, provided the dimensions match.
After creating the matrices C and D, and the vector x
C = [1 2 3; 4 5 6]; D = [7 8; 9 10]; x = [11; 12; 13];
the commands
[C D]
[C; x’]
produce the following matrices
⎡ ⎤
  1 2 3
1 2 3 7 8
, ⎣ 4 5 6 ⎦.
4 5 6 9 10
11 12 13

The [D x] and [C x] commands generate error messages. The reader is


asked to do Exercise 1.4.4.
Matlab provides commands to quickly generate specific matrices. The
ones(m,n) and zeros(m,n) commands produce m-by-n matrices of ones
and zeros, respectively. The eye(m,n) command generates the m-by-n unit
matrix. For example, the commands
eye(2,3)
eye(2)
return
   
1 0 0 1 0
, .
0 1 0 0 1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 5

Function Files 5

If x is a vector of n elements, the diag(x) command creates a square matrix


and places the elements of x on the main diagonal. For example, if
x = [1 2 3];
the command
diag(x)
produces
⎡ ⎤
1 0 0
⎣0 2 0⎦.
0 0 3
The diag(x,h) command creates a square matrix and places the elements
of x along the diagonal specified by h, where h = 0 indicates the main
diagonal, while h > 0 specifies a diagonal in the upper triangular part of
the matrix and h < 0 specifies a diagonal in the lower triangular part.
Therefore, the diag(x,0) command generates the same matrix as before and
the commands
diag(x,-2)
diag(x,1)
produce the following matrices
⎡ ⎤
0 0 0 0 0 ⎡ ⎤
⎢0 0 0 0 0⎥ 0 1 0 0
⎢ ⎥ ⎢ ⎥
⎢1 0 0 0 0⎥, ⎢0 0 2 0 ⎥.
⎢ ⎥ ⎣0 0 0 3 ⎦
⎣0 2 0 0 0⎦
0 0 0 0
0 0 3 0 0
If u is a vector, the A = reshape(u,n,m) command reshapes u into the n-by-
m matrix A. The number of elements in u must be n × m, otherwise the
command will generate an error message. For example, after creating the
vector u with the command
u = [1; 2; 3; 4; 5; 6];
the command
A = reshape(u,2,3)
generates the 2-by-3 matrix
 
1 3 5
A= .
2 4 6
Of course, the command works with matrices too. Indeed, the command
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 6

6 Matlab for Engineering

B = reshape(A,3,2)
produces the 3-by-2 matrix


1 4
B = ⎣2 5⎦.
3 6

The A(:) command reshapes the matrix A into a column vector. For
example, the command
v = A(:)
returns the original vector u. The reader is asked to do Exercise 1.4.5.

1.1.4 Tridiagonal Matrices


Tridiagonal matrices can be created with the diag(x,h) command. For
example, after creating the vectors
x1 = [1 1 1]; u =[2 2 2 2]; x2 = [3 3 3];
the command
A = diag(x1,-1) + diag(u) + diag(x2,1)
generates the following tridiagonal matrix
⎡ ⎤
2 3 0 0
⎢1 2 3 0⎥
A=⎢
⎣0
⎥.
1 2 3⎦
0 0 1 2

Large tridiagonal matrices require a large amount of memory. The A =


spdiags(B,d,m,n) command helps to save memory. This command generates
an m-by-n matrix and places the columns of B along the diagonals specified
by d. For example, if B was created with the B = [-ones(5,1) (1:5)’
ones(5,1)] command, then the A = spdiags(B, -1:1, 5, 5) command produces
the following tridiagonal matrix
⎡ ⎤
1 1 0 0 0
⎢−1 2 1 0 0⎥
⎢ ⎥
A=⎢
⎢ 0 −1 3 1 0⎥
⎥.
⎣0 0 −1 4 1⎦
0 0 0 −1 5
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 7

Function Files 7

The sparse matrix A is displayed by Matlab as follows


(1, 1) 1
(2, 1) −1
(1, 2) 1
(2, 2) 2
(3, 2) −1
(2, 3) 1
(3, 3) 3.
(4, 3) −1
(3, 4) 1
(4, 4) 4
(5, 4) −1
(4, 5) 1
(5, 5) 5
Only the nonzero elements are shown a column at a time. The C = full(A)
command displays A in its usual form.
Try to recreate the matrix A by replacing its size, 5, with 100. Inspect
A with the whos A command. Note that 3,980 bytes were used to store A.
Next, use the C = full(A) command to save the full form of A in C. While
typing the whos C command, note that 80,000 bytes were used to store C.
The spy(A) command generates a picture of the matrix A. Using the spdiags
command requires attention. See Exercises 1.4.6 and 1.4.7.
The A = repmat(B,m,n) command replicates the p-by-q matrix B as
specified by m and n, and generates the mp-by-nq matrix A. For example,
the command
A = repmat(eye(2),2,3)
generates
⎡ ⎤
1 0 1 0 1 0
⎢0 1 0 1 0 1⎥
A=⎢
⎣1
⎥.
0 1 0 1 0⎦
0 1 0 1 0 1
If B is a scalar, say B = 5, the commands
A = repmat(B,2,3)
u = repmat(B,1,3)
v = repmat(B,2,1)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 8

8 Matlab for Engineering

produce

   
5 5 5 5
A= ,u = 5 5 5 , v = ,
5 5 5 5

respectively. Exercise 1.4.8 is useful.

1.1.5 Matrix Operations


Matlab performs all the algebraic operations on matrices. If A and B are
m-by-n matrices, the addition and subtraction are produced with the A +
B and A − B commands. Moreover, a matrix A can be added to a scalar a
with the Matlab command B = A + a. This operation generates the matrix
Bij = Aij +a. If A and B are m-by-n and n-by-p matrices, respectively, then
the matrix product is generated with the A * B command. For example,
after creating the matrices
A = [1 2 3; 4 5 6]; B = [7 8; 9 0; -1 -2];
the commands
A*B
B’ * A’
generate the following matrices

   
22 2 22 67
, .
67 20 2 20

Of course, it results in

(A ∗ B) = B  ∗ A ,

as it is known. If u is a row vector and v is a column vector, the


u * v command yields the scalar product. For example, after creating the
vectors
u = [1 2 −3]; v = [4; 5; 6];
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 9

Function Files 9

the command
u*v
produces −4. A matrix A can be multiplied with a scalar a with the B =
A * a command. This operation produces the matrix Bij = Aij a = aAij .
Therefore, it is A * a = a * A. Moreover, in Matlab, an original element-by-
element product between two same size matrices can be generated. The C =
A.*B command creates the matrix C given by Cij = Aij Bij . For example,
if A and B are the matrices defined beforehand, then the commands
A’.*B
A.*B’
produce the following matrices

⎡ ⎤
7 32  
⎣ 18 7 18 −3
0 ⎦, .
32 0 −12
−3 −12

The A.*B command generates an error. The element-by-element product


between a scalar a and a matrix A is meaningful. It is indicated with a.*A
and the result is the same as a*A. The element-by-element product is very
useful in some situations. See Exercise 1.4.9.

1.1.6 Right and Left Divisions


The A/B command is named right division. The command returns the
product of A times the inverse of B. It is the same as A*B^-1, but faster,
since a specific program was devoted to this command by Matlab, whereas
the A*B^-1 command uses the general program for A*B^-n. For example,
after creating the matrices
A = [1 2; 3 4]; B = [5 6; 7 8];
the command
A/B
generates the following matrix

 
3.0000 −2.0000
.
2.0000 −1.0000
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 10

10 Matlab for Engineering

Of course, the command works with scalar variables. For example, if


a = 4; b = 2;
the command
a/b
returns 2.
The A\B command is named left division. It returns the product of the
inverse of A times B. It is the same as A^-1*B, but faster. For example, if
A and B are the matrices defined beforehand, then the command
A\B
generates the following matrix
 
−3.0000 −4.0000
.
4.0000 5.0000

The command works with scalar variables too. For example, if a and b are
the variables defined beforehand, then the command
a\b
returns 0.5000. The left division is used to solve the algebraic linear systems
Ax = b. The unknown vector x is found by the simple A\b command. See
Exercise 1.4.10. Moreover, Matlab allows element-by-element left and right
divisions: A./B and A.\B. See Exercise 1.4.11.

1.2 Script Files


A script file is a file containing a set of Matlab commands. To execute
this type of file, it is equivalent to writing and executing the commands in
sequence at the Command Window. A new script file is created by the New
Script button and can be saved into any directory. To execute a script file,
press the Run button or type its name at the command line. Examples of
script files will be illustrated in Sec. 1.2.2, after introducing the for loop in
Sec. 1.2.1.

1.2.1 For Loop


This section presents a first flow control structure provided by Matla: the for
loop. Other structures, e.g., the while loop, will be introduced in Sec. 1.3.3.
The syntax of the for loop is outlined below.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 11

Function Files 11

For loop
for variable = expression
code lines
end

For example, in the loop


for j = 1:9
j
end
the variable j goes from 1 to 9 with step 1: 1, 2, 3, . . . , 9. In addition, in the
loop
for i=2:-.2:1
i
end
the variable i goes from 2 to 1 with step −0.2: 2, 1.8, 1.6, . . . , 1.

1.2.2 Examples of Script Files

Example 1.2.1 The following script file plots the function

u(x, t) = sin x cos t, 0 ≤ x ≤ π, 0 ≤ t ≤ 10, (1.2.1)

at several times. The function u describes the small oscillations of a thin


bar with fixed ends (Fig. 1.2.1). For future applications, let us note that

t=5
1
sin x cos t
0.8
0.6
0.4
0.2
u 0
-0.2
-0.4
-0.6
-0.8
-1
0 0.5 1 1.5 x 2 2.5 3

Figure 1.2.1. u = sin x cos t.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 12

12 Matlab for Engineering

the function u satisfies the partial differential equation

∂ 2u ∂ 2 u
− 2 = 0, (1.2.2)
∂t2 ∂x
and verifies the initial-boundary conditions

u(x, 0) = sin x, ut (x, 0) = 0, u(0, t) = u(π, t) = 0. (1.2.3)

See Exercise 1.4.12.

% This is the script file script 1.m. %......[1]


% It plots the function u = sinxcost and prints the matrix u(i,j).
clc; %......[2]
nx = 10; %......[3]
x = linspace(0, pi, nx+1); %......[4]
time = 10; nt = 60; t = linspace(0, time, nt+1);
u = zeros(nx+1, nt+1); %......[5]
for j = 1:nt+1
u(:,j) = sin(x’)*cos(t(j)); %......[6]
plot(x,u(:,j),’r’); %......[7]
axis([0 pi -1 1]); %......[8]
xlabel(’x’); ylabel(’u’); %......[9]
legend(’sin x cos t’,1); %....[10]
title([’t = ’, num2str(t(j))]); %....[11]
pause(.1); %....[12]
end
disp(u’); %....[13]
—————— Notes ——————————–

[1] Any word following the % sign is a note, a comment, and is ignored
by Matlab.
[2] The clc command cleans the Command Window. Information on a
command is obtained by writing help name of command. The reader
is invited to take a look at See also where further commands are
suggested.
[3] Note the final semi-colon. The value of this variable is saved, but not
printed in the Command Window.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 13

Function Files 13

[4] The x = linspace(x1, x2, n) command creates a row vector of n elements


equally spaced from x1 to x2 with step (x2 − x1)/(n − 1). Therefore,
x = linspace(0, pi, nx+1) generates a vector of nx + 1 elements with
step dx = π/nx, i.e., the vector x = [0 dx 2 ∗ dx...π].
[5] The matrix u is initialized. Initializing a matrix is not requested by
Matlab, but is strongly recommended. Indeed, matrix initialization
allows Matlab to allocate the matrix entries in contiguous areas of
memory, which results in the script running faster.
[6] u(:,j) indicates the j-th column of the matrix u. Since x is a row vector,
the transpose sign ’ converts it to a column vector.
[7] The plot command plots (in red) the function (of x) u(x, t(j)) at any
time t(j).
[8] The axis([x1 x2 y1 y2]) command sets the axis limits. If this command
is not introduced, Matlab sets the axis limits automatically. A partial
axis control is also possible. For example, the axis([-inf x2 y1 inf])
command sets the maximum limit for the x-axis and the minimum
limit for the y-axis.
[9] The xlabel and ylabel commands are optional. The previous commands
place the labels beside the corresponding axes. Since the labels are text
strings, they must be introduced between two ’ signs.
[10] The legend command creates the legend. The user can specify its
position. For example, the legend(’sin x cos t’,’Location’,’northeast’)
command creates the legend in the upper right corner and the
legend(’sin x cos t’,’Location’,’best’) command creates the legend in
the best position.
[11] The title command adds text at the top of the graph. In Fig. 1.2.1, it
is used to show the time corresponding to the current position of the
bar. The text is composed of two strings. The first, t =, is statical.
The second, related to the current time, is dynamic and changes every
time a new plot is made. The num2str(t(j)) command converts the real
number t(j) to a text string.
[12] The pause(s) command, where s is a real number, stops the execution
for s seconds.
[13] The disp command displays the matrix u . The transpose matrix is
more interesting than u, since its first row is composed by the values
corresponding to the initial condition of the bar, the second row
contains the values related to the second time, and the last row shows
the last values.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 14

14 Matlab for Engineering

1
0.8
0.6
u
0.4
0.2
0
0.4
t 0.3 0.8 1
0.2 0.6 x
0.1 0.4
0.2
0 0

Figure 1.2.2. 3D plot of the function u = sin(πx) exp(−π 2 t).

Example 1.2.2 The following script file produces 2D and 3D plots of the
function (Fig. 1.2.2),

u = sin(πx) exp(−π 2 t), 0 ≤ x ≤ 1, t ≤ 0.4. (1.2.4)

The function u describes the temperature evolution in a solid where one


dimension is prevailing over the others. For future applications, note that
u satisfies the partial differential equation
∂ 2u ∂ 2 u
− 2 = 0, (1.2.5)
∂t ∂x
and verifies the initial-boundary conditions

u(x, 0) = sin(πx), u(0, t) = u(1, t) = 0. (1.2.6)

See Exercise 1.4.13.

% This is the script file script 2.m.


% It produces 2D and 3D plots of the function u = sin(πx) exp(−π 2 t).
clc;

% Initialization
L = 1; nx = 20; x = linspace(0, L, nx+1);
time = .4; nt = 40; t = linspace(0, time, nt+1);
u = zeros(nx+1, nt+1);
% 2D Plot
for j = 1:nt+1
u(:,j)= sin(pi*x’)*exp(-pi^2*t(j));
plot(x,u(:,j));
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 15

Function Files 15

axis([0 L 0 1]);
xlabel(’x’); ylabel(’u’);
legend(’sin(pi x) *exp(-pi^2 t)’);
title([’t = ’, num2str(t(j))]);
pause(.1);
end
% 3D Plot
pause;
% The pause command stops the execution. Press any key to continue.
figure(2);
surf(x,t,u’)
xlabel(’x’); ylabel(’t’); zlabel(’u’);
% Print
disp(u’);

1.3 Introduction to Function Files


1.3.1 Structure of Function Files
The function file is an m-file that starts with a line of function definition,
where the function name, the input variables passed to the function, and
the output variables returned by the function are specified. The first line
is followed by comment lines and code lines that form the function body.
The last code line is the statement end that ends the function. The syntax
is outlined below.

Function
function [output] = name of function (input)
% comments
code lines
end

Example 1.3.1 As the first example, consider the following simple


function.

function y = sqr(x) %......[1]


% This is the function file sqr.m %......[2]
% The sqr function returns x squared. If x is a matrix, %......[3]
% sqr(x) returns the element-by-element product of matrices. %......[4]
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 16

16 Matlab for Engineering

y = x.*x; %......[5]
end
————————– Notes ——————————–

[1] The square brackets are optional when the output consists of one
variable, as in this example, or when there is no output variable.
The square brackets are necessary for a multiple output variable. The
function can be called with a different variable name and the result can
be assigned to variables that have different names. In addition, the file
name where the function is saved must be the same as the function
name.
[2] First line of comment. The comment lines are printed when the user
types “help sqr” at the command line. See Exercise 1.4.14.
[3] Second line of comment.
[4] Third line of comment.
[5] Function body. All variables used here, as well as in the function
definition, are local and private. See Remark 1.3.1.

The simplest way to call the sqr function is without any output. For
example, the sqr(2) and sqr([1 2]) commands return

4 and 1 4,

respectively. The sqr(1,2) command generates an error since the sqr function
must be called with one input variable. If the function output has to be used,
then the complete syntax must be considered. For example, the command
z = sqr(2);
assigns the value 4 to the variable z that can be used in other statements.

Remark 1.3.1 Initialize the variable a = 1 in the Command Window.


Check that a was saved. Consider the sqr function and add two new code
lines
a = 0;
b = 10;
just before end. Matlab outlines the warning that the values of the last
variables might be unused. Do not care about that. The new variables
will be deleted very soon. Click on the — sign to the left of b = 10;. A
small gray disk appears that changes to red after saving the sqr.m file.
Execute the function by using, for example, the sqr(9) command. Note that
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 17

Function Files 17

the function execution will stop at the code line b = 10;, where there is
the disk. The Prompt changes to K>> since we are in the Debug phase.
Inspect the variable a by typing a followed by Enter. You may see a = 0.
Continue the function execution by pressing the Continue button. You will
obtain the result ans = 81. Now, inspect the variable a again and note that
a = 1. All this emphasizes the local and private character of the variables
defined in the function body. Before executing the function, the value of
a was 1. During the execution, it was a = 0. After the execution, it was
a = 1. The variables defined in the function body cannot interfere with the
variables defined in the external world and vice versa. Moreover, we also
learned how to inspect some variables during the Debug phase. Finally,
delete the new variables added in the function.

1.3.2 Function with a Multiple Output Variable

Example 1.3.2 The following listing presents an example of a function


with a multiple output variable. The heat flux function returns the heat
flux vector q in a thin solid, according to Fourier’s law

q(x, t) = −k∇u(x, t), (1.3.1)

where k is the thermal conductivity of the material, u is the temperature


and ∇u is its space gradient. See Sec. 2.2.1 for details.

function [qx, qy] = heat flux(u,dx,dy,k)


% This is the function file heat flux.m.
% The heat flux in a thin solid is computed according to Fourier’s law. The
% input variable u is the matrix with the temperature values. The input
% variables dx and dy are the spaces among the points along the x- and y-
% direction, respectively. The thermal conductivity k is a positive real
% number, for example, 62.3 (iron), 387.6 (copper), 418.7 (silver), 0.173
% (rubber), 1.177(glass), 2.215 (ice).

[ux, uy] = gradient(u,dx,dy);


% The gradient(u,dx,dy) function returns the numerical values of the two
% components of the gradient vector by using dx and dy.
qx = -k*ux; qy = -k*uy;
end
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 18

18 Matlab for Engineering

0.4

0.3

0.2

0.1
y
0

-0.1

-0.2

-0.3
0 0.2 0.4 x 0.6 0.8 1

Figure 1.3.1. Heat flux.

Example 1.3.3 A way to call the heat flux function is illustrated in the
following listing. The heat flux vector is plotted (Fig. 1.3.1) and printed.

% This is the script file heat flux ex.m.

k = 62.3; Lx = 1; nx = 20; Ly = .1; ny = 6;


x = linspace(0,Lx,nx+1); dx = Lx/nx;
y = linspace(0,Ly,ny+1); dy = Ly/ny;
u = zeros(ny+1,nx+1);
for j=1:ny+1,
u(j,:)=x.^2;
end
[qx, qy] = heat flux(u,dx,dy,k);
quiver(x(2:end-1),y(2:end-1),qx(2:end-1,2:end-1),qy(2:end-1,2:end-1));
% The quiver function plots vectors as arrows with components X and
% Y at the points with coordinates x and y. See Exercise 1.4.15.
rectangle(’position’,[0,0,Lx,Ly]);
% Rectangle with bottom left corner in (0,0), base Lx, and height Ly.
axis(’equal’); % Same scale for both axes.
xlabel(’x’); ylabel(’y’);
disp(qx);

Remark 1.3.2 The coefficient k is a strictly positive real number. If, by


mistake, a negative k is passed to the heat flux function, then the function
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 19

Function Files 19

output would be wrong. Therefore, data analysis and flow control of the
code are indispensable. This topic is discussed in the next section. In the
same section, we will provide a modified version of the heat flux function
that is capable of eliminating the problem outlined above.

1.3.3 Flow Control Structures


This section presents the flow control structures if, switch and while. The
general syntax of the if-elseif-else structure is provided below.

If-elseif-else
if logical condition
code lines
elseif logical condition
code lines
else
code lines
end

Special cases include the following: if, if-else, and if-elseif, see below. In
addition, all cases can be nested.

If-elseif-else: special cases


if logical condition if logical condition if logical condition
code lines code lines code lines
end else elseif logical condition
code lines code lines
end end

As the first application, the heat flux function is suitably modified and
the problem outlined in Remark 1.3.2 is eliminated.

function [qx, qy] = heat flux(u,dx,dy,k)


% comment lines
if k > 0 % If the coefficient k is positive, the function is executed.
[ux, uy] = gradient(u,dx,dy); qx = -k*ux; qy = -k*uy;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 20

20 Matlab for Engineering

else % Otherwise, a message is sent to the user.


disp(’k must be a positive real number.’)
end

Example 1.3.4 When the function has no output, the function definition
can be simplified, as in the following listing.

function if 1(i)
% This is the function file if 1.m. It is an application on if-elseif-else.
% Note the simplified function definition. It could also be written:
% function [ ] = if 1(i). Call the function by passing 0 or 1 as an argument.

x1 = -pi; x2 = pi; nx = 20; x = linspace(x1,x2,nx+1)


if i == 0 % The = = sign is a relational operator. It should not be confused
% with the = sign that is an assignment operator. The code x = y
% assigns the value of y to x. Instead, the code x == y compares
% the values of the variables that retain their values.
% The Matlab relational operators are provided at the end
% of the listing.
plot(x,sin(x));
elseif i == 1
plot(x,cos(x));
else
disp(’Please call the function by passing 0 or 1 as argument.’)
% This message is sent if the function was called with an
% argument different from 0 or 1. Beside disp, errordlg(’...’)
% can be used too. In this case, the message is shown in a frame.
% In both cases, the function is executed. A stronger command
% is error(’...’) that stops the function execution.
end
end

Relational operators
== equal to
˜= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 21

Function Files 21

Another example is provided in Exercise 1.4.16.

Example 1.3.5 The following listing considers a function with a multiple


output variable.

function [max, min] = maxmin vector(b)


% This is the function file maxmin vector.m.
% The function returns the maximum and minimum elements of a vector.
% For example, if b = [1 2 43 56 1 3], the command
% [M m] = maxmin vector(b)
% produces
% M = 56
% m=1

min = b(1); max = b(1);


for i=2:length(b)
% The length(u) command, where u is a vector, returns the number of
% elements in u.
if b(i) < min
min = b(i);
end
if b(i) > max
max = b(i);
end
end
end

Remark 1.3.3 The maxmin vector function requires a vector as an input.


Since the input is not checked, the function is executed even when a matrix
is passed. As a result, the function will return a wrong result. For example,
the commands
b = [3 2; 3 4]; [M m] = maxmin vettore(b)
produce the following wrong result
M=3
m=3
To avoid such undesirable situations, the function will be modified in
Example 1.3.10, Sec. 1.3.5, after introducing a number of logical functions.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 22

22 Matlab for Engineering

If the code flow has a choice of many possibilities, using if may make
the program slower. Matlab provides a more efficient command for these
situations: the switch structure. Its syntax is outlined below.

Switch
switch expression
case value 1
code group 1
case value 2
code group 2
...
case value n
code group n
otherwise
last code group
end

The role of expression can be played by a number as well as a text


string, see Examples 1.3.6 and 1.3.7. Code group i is executed when value i
matches expression. The last code group is executed when no code group
was executed. This code group is optional, but strongly recommended.
Example 1.3.6 The following listing presents a function that returns the
graph of sin x in the color specified by the input variable. For example,
calling switch 1(’red’) returns the graph of sin x in red. If the passed color
is unavailable, a blue color is used and a message is sent about the change.

function switch 1(color name)


% This is the function file switch 1.m.
% It is an application on the switch structure.
x1 = -pi; x2 = pi; nx = 20; x = linspace(x1,x2,nx+1);
switch color name
case ’green’
c = ’g’;
case ’red’
c = ’r’;
case ’yellow’
c = ’y’;
case ’black’
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 23

Function Files 23

c = ’k’;
case ’blue’
c = ’b’;
otherwise
c = ’b’;
str = upper(color name);
% The upper function converts color name passed by the user
% to capital letters.
disp(strcat(str,’ color unavailable. Replaced with blue.’));
% The strcat function concatenates the dynamic string str and the
% static string ’color ... blue’. The disp function shows the
% complete message to the user.
end
plot(x,sin(x),c);
end

Example 1.3.7 Another example on switch is the following function where


the input argument is a number.
function switch 2(i)
% This is the function file switch 2.m.
% For example, use switch 2(3) to call the function.
x1 = -pi; x2 = pi; nx = 20; x = linspace(x1,x2,nx+1);
switch i
case 1
c = ’g’;
case 2
c = ’r’;
case 3
c = ’c’;
case 4
c = ’y’;
case 5
c = ’k’;
case 6
c = ’b’;
otherwise
c = ’b’; disp(’ Unavailable. Replaced with blue.’)
end
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 24

24 Matlab for Engineering

plot(x,sin(x),c);
end

Matlab provides two commands for loops: for and while. The for loop
was introduced in Sec. 1.2.1. Now, the while loop is presented. Its syntax is
outlined below.

While loop
while condition
code lines
end

First, condition is evaluated. If it is true, the code lines are executed and
condition is evaluated again. The process is repeated infinitely until condition
becomes false. If condition is initially false, code lines are never executed.
Applications on a while loop are provided in Examples 1.3.8 and 1.3.9, and
Exercises 1.4.17 and 1.4.18.

Example 1.3.8 Consider the following listing.

% This is the script file while 1.m. It is an application on while loop.

i = 0; a = 10;
while i < a
i = i + 1;
disp(i);
end
Executing the file yields
1
2
...
10.
The break command forces the program to exit from the while loop, even
if condition is true. For example, insert the following code just before end
and guess what happens. However, using a break is not recommended.
if i == 6
break;
end
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 25

Function Files 25

Example 1.3.9 Another example is illustrated in the following listing.


function y = while 2(str)
% This is the function file while 2.m.
% The function returns the number of spaces in the input string.
% For example, the command
% spaces = while 2(’I am from Naples’)
% produces
% spaces = 3.

y = 0; i = 1;
c = isspace(str); %......[1]
while i<= length(c)
if c(i)>0
y = y + 1;
end
i = i + 1;
end
end

—————– Notes —————————–


[1] The c = isspace(str) command, where str is a text string, returns a row
vector the same size as str containing ones and zeros, where 1 corresponds
to a space character and 0 to any other character. For example, if
str = ’I am from Naples’,
then
c = [0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0].

1.3.4 Local Functions, Anonymous Functions


Local functions are functions defined within a function file. They are visible
only to the main function and other local functions. Therefore, they cannot
be called by other functions. All variables defined in local functions are
private. The local functions are known as subfunctions too. A simple
example is illustrated in the following listing.
function y = local function(str)
% This is the function file local function.m.
% It is an application on local functions. The function returns the numbers of
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 26

26 Matlab for Engineering

% characters in the input string different from spaces. For example, the
% command
% ns = local function(’I am from Naples’)
% produces
% ns = 13.

c = isspace(str);
s = GetSpaces(c);
y = length(c) - s;
end
———- Local function ——————–
% The local function GetSpaces returns the number of spaces.
function s = GetSpaces(c)
s = 0;
for i=1:length(c)
if c(i) > 0
s = s + 1;
end
end
end
The anonymous functions are a powerful tool provided by Matlab to
define simple functions. The syntax is outlined below.

Anonymous function
function name = @(arg1, arg2,...) function expression

As noted, the function name is followed from the = sign, the @ sign
that characterizes the anonymous functions, and the input variables in
parentheses. Next, after some spaces, the function expression. A simple
example of an anonymous function is the following
f = @(x) x + 2;
that defines the function f (x) = x + 2, where x can be an array. After
defining f , the command
feval(f,3)
evaluates f for x = 3 and produces
5.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 27

Function Files 27

Equivalently, the command f(3) can be used. In addition, the command


fplot(f,[0 1])
returns the graph of f (x) = x + 2 on the interval (0, 1). A further example
is suggested in Exercise 1.4.20 after introducing the logical functions in
Sec. 1.3.5. An anonymous function has an important limitation: it must be
defined in one line. However, it can be used in many situations.

1.3.5 Logical Operators and Logical Functions


Matlab provides three logical operators that are outlined below.

Logical operators
& Logical AND
| Logical OR
˜ Logical NOT

The first two operators work with at least two operands. The third operator
needs one operand. In logical expressions related to scalar variables, the
following symbols must be used: && and ||, instead of & and |.
Logical AND evaluates the truth or falseness of the operands and
returns true if all operands are true; otherwise it returns false. For example,
the expression
a > 0 && b > 0 && c > 0
returns 1 (true) if a, b and c are strictly positive scalar variables and 0
(false) if at least one is less than or equal to zero. For example, if a, b and
c were initialized as
a = 1; b = -1; c = pi;
then the expressions
a > 0 && b > 0
c > 0 && b > 0
a > 0 && b > 0 && c > 0
return
0
and the expression
a > 0 && c > 0
returns
1.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 28

28 Matlab for Engineering

Note that in Matlab, false is expressed by “0” and true is expressed by “1”
or, more generally, any nonzero value. Therefore, the expression
a && b && c
returns
1.
The expression
a-b>0
returns
1
since it is true and the expression
a-c>0
returns
0
since it is false. See Exercises 1.4.1 to 1.4.21. If the operands are vectors
of the same length, each element of a vector is evaluated with the
corresponding elements of the other vectors and a same length vector of
zeros and ones is returned. For example, after creating the vectors
u = [0 1 3]; v = [-1 0 1]; z = [-3 -1 0];
the expression
u&v&z
returns
0 0 0.
If a is a scalar, for example,
a = 1;
then the expression
a&u
returns
0 1 1
since Matlab evaluates the scalar with each element of u. If the operands are
matrices of the same size, a same size matrix of zeros and ones is returned.
For example, after creating the matrices
A = [0 1 3; 4 5 6]; B = [-1 0 1; -3 -2 0];
the expression
A&B
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 29

Function Files 29

returns
0 0 1
1 1 0
The logical operator & is frequently used in if-else and while structures. For
example,
if a >= 0 && b < 0
code lines
end

The logical operator | evaluates the operand truth or falseness and


returns false if all operands are false; if at least one operand is true, it
returns true. For example, the expression
a > 0 || b > 0 || c > 0
returns “1” if at least one variable assumes a positive value and “0” if all
scalars are less than or equal to zero. If the operands are matrices of the
same size, then each element of a matrix is evaluated with the corresponding
elements of the other matrices and a same size matrix of zeros and ones
is produced. In addition, the expression a | A, where a is a scalar and A
is a matrix, is a compatible statement. In this case, any element of A is
evaluated with a. The OR operator | is often used in flow control code. See
Exercise 1.4.22.

Example 1.3.10 As noted in Remark 1.3.3, the maxmin vector function


should be modified. The corrected version is shown below.

function [max, min] = maxmin vector(b)


...
if size(b,1) == 1 || size(b,2) == 1
% The size(A) command, where A is a matrix, returns a vector of two
% elements that specify the number of rows and columns in A, respectively.
Place the old code here.
else
error(’The input variable must be a vector.’);
end
end

Logical NOT ~ operator works with one operand. It evaluates the


operand’s truth or falseness and returns “false” if the operand is “true”
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 30

30 Matlab for Engineering

and “true” if the operand is “false”. For example, after creating the
vector v
v = [-1 0 1];
the expression
~v
returns
0 1 0.

Matlab provides logical functions too. Some of them — all, any, find
and ismember — will be presented in this section. If u is a vector, the logical
function
all(u)
returns “1” if all the elements of u are different from zero, otherwise it
returns “0”. For example, after creating the vectors
u = [0 1 2]; v = [1 2 3];
the command
all(u)
returns
0
and the command
all(v)
returns
1.
If A is a matrix, the command
all(A)
evaluates the column vectors of A and returns a row vector of zeros and
ones with a length equal to the number of columns of A. For example, after
creating the matrix
A = [0 1 2; 1 2 3];
calling
all(A)
returns
0 1 1.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 31

Function Files 31

The function can also be called with an optional argument: all(A,n). In this
case, the function evaluates according to the dimension specified by n. For
example, the command
all(A,1)
evaluates according to the first dimension (row) and considers the columns
as vectors. The result is the same as all(A). The command
all(A,2)
evaluates according to the second dimension (column) and considers the
rows as vectors. It returns
0
1.

The logical function


any(u)
returns “1” if at least one element of vector u is different from zero,
otherwise it returns “0”. For example, after creating the vector
u = [0 1 2];
the any(u) command returns “1”. If a matrix A is passed as an argument,
any(A) works exactly the same as all, including the possibility of the optional
argument: any(A,n).

If v is a vector and p is a real number, the logical function


find(v > p)
finds the elements of v greater than p and returns their indices. For example,
the command
find(-2:3 > 1)
returns
5 6
that are the indices of the two elements, 2 and 3, greater than 1. If A is a
matrix, the function
[ri ci] = find(A == p).
finds the elements of A equal to p and returns the two vectors ri and ci
containing the row and column indices of the element of A equal to p. For
example, after creating the matrix
A = [0 3; -4 0];
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 32

32 Matlab for Engineering

the command
[ri ci] = find(A == 0)
produces
ri =
1
2
ci =
1
2
since A(1,1) = 0 and A(2,2) = 0. The command
[ri ci] = find(A)
returns the indices of the nonzero elements of A. In addition, the command
[ri ci vs] = find(A)
also returns the vector vs containing the values of nonzero elements of A. For
example, if A is the matrix created beforehand, then the previous command
returns
ri =
2
1
ci =
1
2
vs =
-4
3.

If A and B are matrices, the logical function


ismember(A,B)
evaluates if an element of A belongs to B and returns “1” in the positive
case, otherwise “0”. Therefore, a matrix with the same size as A is generated
containing zeros and ones. For example, if A is the matrix created with the
command
A = [0 3; -4 0];
then the command
ismember(A,0)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 33

Function Files 33

produces
1 0
0 1
and the command
ismember(A,A)
returns
1 1
1 1.

1.4 Exercises

Exercise 1.4.1 After creating the matrix


A = [1 2 3; 4 5 6; 7 8 9; 1 1 1; 2 2 2];
extract the submatrix formed by the first and third rows.

Answer. A([1 3],:).

Exercise 1.4.2 Extract the submatrix formed by the first, second, third
and fifth rows from the matrix A, created in Exercise 1.4.1.

Hint. Clearly, the A([1 2 3 5],:) command works. However, it is not the most
efficient when we consider a matrix with 4,000 rows and want to extract
the submatrix formed by the first 2,000 rows and the last one. The reader
is asked to find a more efficient command.

Exercise 1.4.3 Delete the first, second, third and fifth rows of matrix A.

Hint. Use a command more efficient than A([1 2 3 5],:)=[ ].

Exercise 1.4.4 Let


   
123 7 8
C= , D= .
456 9 10

The [C’; D] command works to produce the matrix


⎡ ⎤
1 4
⎢2 5 ⎥
⎢ ⎥
⎢3 6 ⎥.
⎢ ⎥
⎣7 8 ⎦
9 10
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 34

34 Matlab for Engineering

What is the result of the [C’; D’] command?

Exercise 1.4.5 Convert the matrix


⎡ ⎤
1 4
B = ⎣2 5⎦
3 6
to a vector, say z.

Exercise 1.4.6 Use the B = [[1; 2; 3] ones(3,1) -[1; 2; 3]] command to


create the matrix
⎡ ⎤
1 1 −1
B = ⎣2 1 −2⎦.
3 1 −3

Then, create the matrix A = spdiags(B, -1:1, 3, 3).

Exercise 1.4.7 Create the matrix A = spdiags(B, -1:1, 3, 3), where B is


the matrix defined by
⎡ ⎤
1 1 0
B = ⎣2 1 −2⎦.
0 1 −3
Compare the matrix A with the one obtained in the previous exercise.

Exercise 1.4.8 Create the matrix A = spdiags(B, [-4 -1:1 4], 9, 9)


by using the matrix B = repmat([ones(3,1) 4*[ones(2,1);0] 2*ones(3,1)
3*[0;ones(2,1)] ones(3,1)], 3, 1). Try to guess the result.

Exercise 1.4.9 Consider the simple function f (x) = x, x ∈ [1, 20].


Suppose that f (x) was discretized with the vector x = [1 2 3 · · · 20]. Write
the Matlab command that discretizes the function f 2 (x) and produces the
vector [1 4 9 · · · 400].

Answer. x.*x, or x.2 .

Exercise 1.4.10 Consider the three-hinged arch in Fig. 1.4.1 (left). Use
the left division to calculate the constraint reactions. Assume: F = 4N , q =
2N/m and L = 4m. Use the free body diagram (or the Lagrangian model),
e.g., D’Acunto and Massarotti (2016), illustrated in Fig. 1.4.1 (right).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 35

Function Files 35

q qL X3 X3 qL
F F
B X4 X4
L
A C X1 X5
X2 X6

L/2 L/2 L/2 L/2

Figure 1.4.1. Three-hinged arch (left) and free body diagram (right).

Hint.
⎡ ⎤⎡ ⎤ ⎡ ⎤
F + X1 − X3 = 0 1 0 −1 0 0 0 X1 −4
X2 + X4 − qL = 0 ⎢0 1 0 1 0 0⎥ ⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ X2 ⎥ ⎢ 8 ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
qL2 /2 + LX1 − LX2 = 0 ⎢4 −4 0 0 0 0⎥ ⎢ X3 ⎥ ⎢ −32 ⎥
⇔⎢ ⎥⎢ ⎥=⎢ ⎥.
X3 − X5 = 0 ⎢0 0 1 0 −1 0⎥ ⎢ X4 ⎥ ⎢ 0 ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
X6 − X4 − qL = 0 ⎣0 0 0 −1 0 1⎦ ⎣ X5 ⎦ ⎣ 8 ⎦
LX6 − LX5 − qL2 /2 = 0 0 0 0 0 −4 4 X6 16

Exercise 1.4.11 Execute the element-by-element right division A./B,


where
   
1 2 1 2
A= , B= .
6 8 3 4

Exercise 1.4.12 Verify that the function u, defined in (1.2.1), satisfies


Eq. (1.2.2) and initial-boundary Conditions (1.2.3).

Exercise 1.4.13 Verify that the function u, defined in (1.2.4), satisfies


Eq. (1.2.5) and initial-boundary Conditions (1.2.6).

Exercise 1.4.14 Type help sqr at the command line and press Enter.

Exercise 1.4.15 Replace end with the specific values.

Hint. Consider the matrix u.

Exercise 1.4.16 Call the following function. The function output is shown
in Fig. 1.4.2.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 36

36 Matlab for Engineering

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
-4 -3 -2 -1 0 1 2 3 4

Figure 1.4.2. Function output.

function if 2(i)
% This is the function file if 2.m.
x1 = -pi; x2 = pi; nx = 20; x = linspace(x1,x2,nx+1);
c = ’b’;
if i == 0
c = ’g’;
elseif i == 1
c = ’r’;
end
plot(x,sin(x),c);
end

Exercise 1.4.17 Try to guess the value of y after executing the following
file.

% This is the script file while 3.m. It is an exercise on the while loop.
y = 2;
while y > 2
y = y - 1;
disp(y);
end

Exercise 1.4.18 Replace while with for in the function while 2, see
Example 1.3.9.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 37

Function Files 37

Exercise 1.4.19 Consider the function u on the interval [0, L]


0 if x ∈ [0, x1 ] ∪ ]x2 , L],
u(x) = (1.4.1)
1 if x ∈]x1 , x2 [.
Write a script file where Function (1.4.1) is defined by using logical
operators. Next, plot the function.

Answer.
% This is the script file logical 1.m. It is an exercise on logical operators.
L = 2; n =101; i1 = 31; i2 = 61;
x = linspace(0,L,n); x1 = x(i1); x2 = x(i2);
u(1:n) = (x(1:n) - x1 > 0).*(x2 - x(1:n) > 0);
plot(x,u);
axis(’equal’);

Exercise 1.4.20 Write a script file where Function (1.4.1) is defined by


using an anonymous function. Next, plot the function.

Exercise 1.4.21 Write a script file where Function (1.4.1) is defined by


using a for loop. Next, plot the function.

Exercise 1.4.22 Consider the following code.

a = 1; L = 2; b = 0; T = 3; n = 10;
if a <= 0 || L <= 0 || T <= 0 || n <= 2
b = 1;
end
disp(b);
What is the value of b after executing the listing?
B1948 Governing Asia

This page intentionally left blank

B1948_1-Aoki.indd 6 9/22/2014 4:24:57 PM


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 39

Chapter 2

The Finite Difference Method

The chapter presents the Finite Difference Method (FDM). This method
dates back to Euler1 who introduced it in Institutiones calculi Differentialis
(1755). The modern researches on the FDM started after the paper by
Courant, et al. (1928), where the method was used to obtain approximated
solutions to Partial Differential Equations (PDEs). In this field, the method
was improved mainly after the Second World War when powerful computers
were available. The books by Collatz (1966); Forsythe and Wasov (1960),
and Richtmyer and Morton (1967) had a great role in stimulating research
on the FDM. Other books by Cooper (1998); Kharab and Guenther
(2002) considered Matlab applications too. Today, the FDM is considered
a consolidated tool that is able to provide reliable solutions of PDEs and is
used by scientists and technicians in many scientific areas, e.g., D’Acunto
(2004); de Vahl Davis (1986). In this chapter, FDM will be applied to
the heat equation by introducing these noteworthy methods: Explicit Euler
Method Implicit Euler Method and Crank–Nicolson Method.
A section is devoted to the equation governing heat propagation and
diffusion.

2.1 Finite Difference Approximations of Derivatives


2.1.1 Forward, Backward and Central Approximations
Let f (x) be a function defined on the interval [0, L]. A finite set of points
xi , i = 0, ..., n, xi ∈ [0, L], forms a grid or mesh. Of special importance
are the grids with a constant step indicated with h or Δx (Fig. 2.1.1). If

1 Leonard Euler, a Swiss scientist, 1707–1783. He formulated the laws of solid and fluid

dynamics. He published Institutiones Calculi Differentialis and introduced the Euler’s


angles for rotating bodies.

39
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 40

40 Matlab for Engineering

Figure 2.1.1. Grids with a constant step.

we restrict f (x) to x = xi , we obtain the discrete counterpart, or discrete


version, of f . The value of f for x = xi will be indicated with fi

fi = f (xi ) = f (ih), i = 0, . . . , n. (2.1.1)

Consider the Taylor2 series

(h2 h3
f (x + Δx) = f (x) + f  (x)h + f  (x) + f  (x) + · · · , (2.1.2)
2 6
evaluated at x = xi

h2 h3
fi+1 = fi + fi h + fi + fi + ··· , (2.1.3)
2 6
where Notation (2.1.1) was used. The previous formula can be written in a
more concise way by using the symbol O (capital o)

fi+1 = fi + fi h + O(h2 ). (2.1.4)

The symbol O(hn ) indicates a quantity going to zero as hn , i.e., a quantity


bounded by a positive constant times hn . Solving (2.1.4) with respect to fi
yields
fi+1 − fi
fi = + O(h). (2.1.5)
h
The ratio (fi+1 −fi )/h approximates the derivative fi with an error of order
h
fi+1 − fi
fi ≈ (2.1.6)
h

2 Brook Taylor, a British scientist, 1685–1731. He published Methodus Incrementorum

Directa et Inversa (1715). He stated Taylor’s theorem, which was valorized only many
years later by Lagrange.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 41

The Finite Difference Method 41

and defines the forward approximation of the derivative fi . The backward
approximation
fi − fi−1
fi ≈ , (2.1.7)
h
is inferred similarly. See Exercise 2.4.2. From Formula (2.1.6), we realize
that the forward approximation cannot be applied in the last point of the
interval where f is defined. Similarly, from Formula (2.1.7), it follows that
the backward approximation cannot be applied in the first point of the
interval.

Example 2.1.1 The forward function is presented. It returns the forward


approximation of the derivative. Two arguments are passed to forward when
it is called: the vector u, containing the values of the function to be derived,
and the step h.
function y = forward(u,h)
% This is the function file forward.m.
% It returns the forward approximation of derivatives. Since the forward
% approximation cannot be applied in the last point, the vector length
% returned by the forward function is equal to that of vector u minus 1.
% Example
% a = 0; b = 1; nx = 20; x = linspace(a,b,nx+1); dx = (b - a)/nx;
% u = x.^2;
% dfu = forward(u,dx)

n = length(u) - 1;
y = (u(2:n+1) - u(1:n))/h;
% This vector equality is equivalent to
% y(1) = (u(2)-u(1))/h, ..., y(n) = (u(n+1)-u(n))/h.
% Note that length(y) = n.
end
A way to call forward is suggested in the function comments. Another
way is illustrated in the next example.

Example 2.1.2 The forward function is applied to calculate the forward


approximation of the derivative of sin x. The exact and approximating
derivatives are plotted. See Fig. 2.1.2. The error is evaluated.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 42

42 Matlab for Engineering

% This is the script file forward ex1.m.


% The forward function is called and applied.
a = -pi; b = pi; nx = 32; x = linspace(a,b,nx+1); dx = (b-a)/nx;
Du = cos(x); u = sin(x);
dfu = forward(u,dx);
plot(x,Du,’r’,x(1:nx),dfu,’k-*’);
legend(’Exact’,’Forward’);
xlabel(’x’); ylabel(’du/dx’);
error = max(abs(Du(1:nx)-dfu));
% If v is a vector, max(v) returns the greatest element of v, and abs(v)
% returns the vector containing the absolute values of the elements of v.
fprintf(’Maximum error = %g\n’, error)
% fprintf formats data and displays the results on the screen.
% g converts numerical data to a compact format.
% \n starts a new line.

See Exercise 2.4.1, which is related to the error. A function similar to


forward can be written for the backward approximation too. See Exercises
2.4.2–2.4.4.
Consider the Taylor series for f (xi + Δx) and f (xi − Δx)
h2 h3
fi+1 = fi + fi h + fi + fi + ··· ,
2 6
h2 h3
fi−1 = fi − fi h + fi − fi + ··· .
2 6

1
Exact
Forward
0.6

0.2
du/dx

-0.2

-0.6

-1
-4 -2 0 x 2 4

Figure 2.1.2. Forward and exact derivatives of sin x.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 43

The Finite Difference Method 43

Subtract the second from the first

fi+1 − fi−1 = 2fi h + O(h3 ),

and solve the result with respect to fi


fi+1 − fi−1
fi = + O(h2 ).
2h
Hence, the formula for the central approximation of fi is
fi+1 − fi−1
fi ≈ , (2.1.8)
2h
with an error of order h2 . The central approximation is more accurate than
forward and backward approximations. Clearly, it cannot be applied in
the first and last points of the interval where the function is defined. The
following example provides the listing of the central function that returns
the central approximation of derivatives. Two arguments must be passed to
central when it is called: the vector u, containing the values of the function
to differentiate, and the step h.

Example 2.1.3

function y = central(u,h)
% This is the function file central.m.
% It returns the central approximation of derivatives. Since the central
% approximation cannot be applied in the first and last points, the vector
% length returned by the central function is equal to that of vector u minus 2.
% Example
% a = 0; b = 1; nx = 20; x = linspace(a,b,nx+1); dx = (b - a)/nx;
% u = x.^2;
% dcu = central(u,dx)
n = length(u) - 2;
y = (u(3:n+2) - u(1:n))/h/2;
end
A way to call central is suggested in the function comments. Another
way is illustrated in the following example.

Example 2.1.4 Forward, backward, central and exact derivatives of sin x


are calculated and compared. The exact and approximating derivatives are
plotted. See Fig. 2.1.3. The related errors are evaluated.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 44

44 Matlab for Engineering

1
Exact
Forward
0.6 Backward
Central

0.2
du/dx

-0.2

-0.6

-1
-4 -2 0 x 2 4

Figure 2.1.3. Forward, backward, central and exact derivatives of sin x

% This is the script file central ex1.m.


% The central function is called and applied. The exact and approximating
% forward, backward and central derivatives of u = sin x are plotted. The
% errors are evaluated.
a = -pi; b = pi; nx = 32; x = linspace(a,b,nx+1); dx = (b-a)/nx;
u = sin(x); Du = cos(x);
dfu = forward(u,dx); dbu = backward(u,dx); dcu = central(u,dx);
plot(x,Du,’r’,x(1:nx),dfu,’k-o’,x(2:nx+1),dbu,’k-*’,x(2:nx),dcu,’k’);
legend(’Exact’,’Forward’,’Backward’,’Central’);
xlabel(’x’); ylabel(’du/dx’);
errorf = max(abs(Du(1:nx)-dfu));
errorb = max(abs(Du(2:nx+1)-dbu));
errorc = max(abs(Du(2:nx)-dcu));
fprintf(’Maximum forward error = %g\n’,errorf)
fprintf(’Maximum backward error = %g\n’,errorb)
fprintf(’Maximum central error = %g\n’,errorc)
See Exercise 2.4.5, which is related to the error. As noted, in the
previous example, the points where the approximating derivatives cannot
be applied were not plotted. For example, in the case of the central
approximation, the first and last points were excluded. When the derivatives
in such points are necessary, the forward approximation can be applied
in the first point and the backward in the last. The error will increase,
since the forward and backward approximations are less accurate than
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 45

The Finite Difference Method 45

the central one. In these situations the three-point forward and backward
approximations can be applied. They are accurate to the order 2, like the
central approximation, and the error does not grow. The formulas for the
three-point forward and backward approximations are the following
4fi+1 − 3fi − fi+2
fi ≈ , (2.1.9)
2h
−4fi−1 + 3fi + fi−2
fi ≈ , (2.1.10)
2h
respectively, with an error of order h2 . See Exercise 2.4.6.

Example 2.1.5 The following listing provides the derivative of u = x2


by using the central approximation and Formulas (2.1.9) and (2.1.10) for
the first and last points. The result is compared with the exact derivative
and the gradient function by Matlab. Indeed, the gradient is the same
as the derivative for a function of one variable. Central + three-point
approximation, gradient and exact derivatives are plotted. See Fig. 2.1.4.
% This is the script file central ex2.m
% The derivative of u = x2 is calculated by using the central approximation
% and three-point forward and backward approximations for first and last
% points, respectively. Also, the gradient function by Matlab is applied.
a = 0; b = 1; nx = 20; x = linspace(a,b,nx+1); dx = (b-a)/nx;
u = x.^2; Du = 2*x;

2
Exact
Central
1.6 Gradient

1.2
Du
0.8

0.4

0
0 0.2 0.4 x 0.6 0.8 1

Figure 2.1.4. Central (+ three-point) approximation, Matlab gradient and exact


derivatives.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 46

46 Matlab for Engineering

g = gradient(u,dx);
dcu = zeros(nx+1,1);
dcu(2:nx) = (u(3:nx+1)-u(1:nx-1))/dx/2;
% Central approximation
dcu(1) = (4*u(2)-3*u(1)-u(3))/2/dx;
% Three-point forward approximation
dcu(nx+1) = (-4*u(nx)+3*u(nx+1)+u(nx-1))/2/dx;
% Three-point backward approximation
plot(x,Du,’r’,x,dcu,’k’,x,g,’bo:’);
xlabel(’x’); ylabel(’Du’); axis([a b min(Du) max(Du)]);
legend(’Exact’,’Central’,’Gradient’,’Location’,’NorthWest’);
As it results immediately from Fig. 2.1.4, the derivative provided by gradient
presents a greater error in the first and last points. The error tends to vanish
for increasing nx. However, it is worth investigating. See Exercise 2.4.8.

2.1.2 Approximation of Functions Depending on Two


Variables
Consider a function depending on two variables x ∈ [0, L] and t ∈ [0, T ],
and introduce the notation ui,j = u(xi , tj ). If t denotes time, the notation
uji is used too. For a mesh with constant steps Δx and Δt, shown in
Fig. 2.1.5, it is

uji = u(xi , tj ) = u(iΔx, jΔt), i = 0, . . . , n, j = 0, . . . , m,

Figure 2.1.5. Space-time mesh.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 47

The Finite Difference Method 47

where Δx = L/n and Δt = T /m. The partial derivative will also be


indicated with the following notations
∂u ∂u
ux = , ut = .
∂x ∂t
Their approximations follow easily from the approximation formulas of a
function in one variable. The forward, backward and central approximations
of ux are expressed as

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


(ux )ji ≈ , (ux )ji ≈ i , (ux )ji ≈ i+1 , (2.1.11)
Δx Δx 2Δx
respectively. The forward, backward and central approximations of ut are
expressed as

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


(ut )ji ≈ i
, (ut )ji ≈ i i
, (ut )ji ≈ i i
, (2.1.12)
Δt Δx 2Δx
respectively. The reader is asked to derive the previous formulas. See
Exercise 2.4.9. Exercise 2.4.10 is devoted to three-point approximations.

2.1.3 Approximation of Higher Order Derivatives


This section considers the approximations for the second derivatives.
Firstly, the forward approximation for the partial derivative utt is presented.
Consider the Taylor series

uj+1
i = uji + (ut )ji Δt + (utt )ji Δt2 /2 + O(Δt3 ), (2.1.13)

uj+2
i = uji + (ut )ji 2Δt + (utt )ji 2Δt2 + O(Δt3 ). (2.1.14)

Subtract (2.1.13)×2 from (2.1.14) and solve the result with respect to (utt )ji

(utt )ji = (uj+2


i − 2uj+1
i + uji )/(Δt)2 + O(Δt).

Hence, the desired approximation with an error of order Δt

uj+2 − 2uj+1 + uji


(utt )ji ≈ i i
. (2.1.15)
(Δt)2
An analogous result holds for the derivative uxx , with an error of order Δx,

uji+2 − 2uji+1 + uji


(uxx )ji ≈ . (2.1.16)
(Δx)2
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 48

48 Matlab for Engineering

The backward approximations are obtained with similar reasoning

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


(utt )ji ≈ i i
2
, (uxx )ji ≈ , (2.1.17)
(Δt) (Δx)2
with an error of order Δt and Δx, respectively. Moreover, consider the
Taylor series
(Δt)2 (Δt)3
uj+1
i = uji + (ut )ji Δt + (utt )ji + (uttt )ji + O((Δt)4 ),
2! 3!
(Δt)2 (Δt)3
uj−1
i = uji − (ut )ji Δt + (utt )ji − (uttt )ji + O((Δt)4 ).
2! 3!
Let us sum the two previous formulas and solve the result with respect to
(utt )ji

uj+1 − 2uji + uj−1


(utt )ji = i i
+ O((Δt)2 ).
(Δt)2
Hence, the central approximation of the partial derivative utt is
uj+1 − 2uji + uj−1
(utt )ji ≈ i i
, (2.1.18)
(Δt)2
with an error of order (Δt)2 . Formula (2.1.18) is more accurate than
(2.1.15)-(2.1.17). Of course, an analogous formula holds for uxx

(uxx )ji ≈ (uji+1 − 2uji + uji−1 )/(Δx)2 . (2.1.19)

See Exercise 2.4.11. For functions depending on two variables, the approx-
imations for the mixed derivative should be discussed too. Consider the
forward approximation of uxt . First, calculate the forward approximation
for the time derivative (ux )t by using the forward approximation (2.1.12)1

(ux )j+1 − (ux )ji


(uxt )ji = i
+ O(Δt).
Δt
Next, calculate the forward approximations for the space derivatives by
using Formula (2.1.11)1

uj+1 j+1
i+1 − ui − uji+1 + uji
(uxt )ji = + O(Δx) + O(Δt).
ΔxΔt
Hence, the desired formula with an error of order O(Δx) + O(Δt) is

uj+1 j+1
i+1 − ui − uji+1 + uji
(uxt )ji ≈ . (2.1.20)
ΔxΔt
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 49

The Finite Difference Method 49

Similar reasonings lead to the backward approximation


uj−1 j−1
i−1 − ui − uji−1 + uji
(uxt )ji ≈ , (2.1.21)
ΔxΔt
with an error of order O(Δx) + O(Δt) and to the central approximation

uj+1 j+1 j−1 j−1


i+1 − ui−1 − ui+1 + ui−1
(uxt )ji ≈ , (2.1.22)
4ΔxΔt
with an error of order O((Δx)2 ) + O((Δt)2 ). See Exercise 2.4.12.

2.2 Diffusion
This section presents the equation governing heat propagation and diffusion
Cannon (1984); Carslaw and Jager (1959); Crank (1979). The Matlab
programs for the mentioned equation will be illustrated in Sec. 2.3. The
heat equation, introduced by Fourier3 , is the basic tool for solving problems
of heat propagation in solids. The heat equation is a parabolic partial
differential equation. Its solution depends on initial-boundary conditions,
as illustrated in the next section. Fourier’s methodology stimulated other
scientists to use the mathematical formulation for different physical phe-
nomena. Indeed, some years later, Fick4 and Darcy5 introduced similar laws
for diffusion and fluid flow in porous media.

2.2.1 Fourier’s Law and Heat Equation


Fourier’s law (1822) follows from observations and experiences that outline
that heat flux in homogeneous and isotropous solids is proportional to the
thermal gradient and flows from hotter to colder regions

q(x, t) = −k∇u(x, t). (2.2.1)

In Eq. (2.2.1), the vector q(x, t) indicates the heat (or thermal) flux, the
heat flux per unit time per unit isothermal surface, u(x, t) the temperature

3 Jean Baptiste Fourier, a French scientist, 1768–1830. He was taught by Lagrange at

the Ecole Normale of Paris. He published Théorie Analytique de la Chaleur (1822). He


participated in the military expedition to Egypt with Napoleon.
4 Adolf Eugen Fick, a German scientist, 1829–1901. He was Professor of Physiology at

the University of Würburg. He introduced Fick’s law of diffusion in 1855.


5 Henry Philibert Gaspard Darcy, a French scientist, 1803–1858. He was Chief Engineer in

Dijon. He published Darcy’s law on fluid flow in porous media in Les Fontaines publiques
de la Ville de Dijon (1856).
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 50

50 Matlab for Engineering

and k the thermal conductivity of the material. Note that ∇u denotes the
gradient of u with respect to the only space variables
 
∂u ∂u ∂u
∇u = , , . (2.2.2)
∂x1 ∂x2 ∂x3
It is essential to know the gradient properties to better understand
Fourier’s law (2.2.1). See Exercises 2.4.13 and 2.4.14.
In the thermal process in a solid B, both functions q and u are
unknown. Therefore, Fourier’s law is unable to determine both heat flux
and temperature. We need a second equation, which can be provided by
the principle of conservation of energy: rate of energy in V = heat flow
entering and leaving V through boundary ∂V + energy production in V ,
where V is any control volume included in B. See Fig. 2.2.1. The energy
balance is formalized as follows
  
ρet (x, t)dx = − q · n dS + F (x, t)dx, (2.2.3)
V ∂V V

where n is the outward unit normal vector to the surface ∂V at the


integration point, ρ(x) is the density of the solid at rest, e(x, t) is the
internal energy per unit mass and et is the partial derivative of e with
respect to time. In addition, F (x, t) indicates the heat quantity produced
per unit time per unit volume by internal heat generators. The internal
energy depends on the temperature e = e(u). For most materials and a
wide temperature interval, the dependence is linear
e = cp u, (2.2.4)

Figure 2.2.1. Control volume


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 51

The Finite Difference Method 51

where cp indicates the specific heat at constant pressure. Now, consider the
Gauss6 divergence theorem,
 
q · n dS = ∇ · q dx, (2.2.5)
∂V V

where
∇ · q = ∂q1 /∂x1 + ∂q2 /∂x2 + ∂q3 /∂x3
indicates the divergence of q with respect to the only space variables.
Substituting (2.2.4)–(2.2.5) into (2.2.3) yields

[cp ρut + ∇ · q − F ] dx = 0, (2.2.6)
V

that holds for any control volume V . If the integrand function is continuous,
then it follows from (2.2.6)
cp ρut + ∇ · q − F = 0. (2.2.7)
We used the following theorem: If f (x) is a continuous function on B, then

f (x) dx = 0, ∀V ⊆ B ⇒ f (x) = 0, ∀x ∈ B. (2.2.8)
V

Statement (2.2.8) can be proved as follows. Suppose there exists x̄ ∈ B such


that f (x̄) > 0. The continuity hypothesis implies f (x) > 0, ∀x belonging
to a suitable neighbor of x̄, say I. Of course, the integral of f (x) on I is
positive. This is a contradiction since I is a special V . The contradiction is
removed only if f (x̄) = 0. The reasoning is similar if it is supposed there
exists x̄ ∈ B such that f (x̄) < 0. See Exercise 2.4.15.
Consider Fourier’s law, q = −k∇u, in energy balance Eq. (2.2.7) and
obtain
cp ρut − ∇ · (k∇u) = F, x ∈ B, 0 < t ≤ T. (2.2.9)
Partial differential Eq. (2.2.9) is named a heat equation. For constant k,
(2.2.9) simplifies to
cp ρut − kΔu = F, x ∈ B, 0 < t ≤ T, (2.2.10)

6 Johann Friedrich Carl Gauss, a German scientist, 1777–1855. He was the greatest

mathematician since antiquity. He made significant contributions in Mathematics and


Physics.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 52

52 Matlab for Engineering

ut − αΔu = f, x ∈ B, 0 < t ≤ T, (2.2.11)

where f = F/cp ρ, α = k/cp ρ denotes the thermal diffusivity, and Δ the


Laplace7 operator
∂2 ∂2 ∂2
Δ = ∇2 = 2 + 2 + 2.
∂x1 ∂x2 ∂x3
Solving the heat equation yields the unknown function u(x, t). Next, the
heat flux q is derived from Fourier’s Law. However, solving the heat
equation requires initial conditions and boundary conditions. Indeed, the
time evolution of the temperature in a solid depends on the initial thermal
state and the thermal conditions on the boundary of the solid.
The initial condition is formalized by assigning the function u(x, t) for
t=0

u(x, 0) = ϕ(x), x ∈ B. (2.2.12)

Let us illustrate the main types of linear boundary conditions. The


boundary condition of the first type is related to the situation where the solid
surface is kept to a fixed temperature. Therefore, this condition specifies
the value of the function u(x, t) on the boundary

u(x, t) = g(x, t), x ∈ ∂B. (2.2.13)

Condition (2.2.13) is also named the Dirichlet8 boundary condition.


The boundary condition of the second type considers a known heat flux
on the solid boundary. Since the heat flux is related to the temperature
gradient, this condition is expressed as
∂u
k (x, t) = g(x, t), x ∈ ∂B, (2.2.14)
∂n
where ∂/∂n indicates the outward normal derivative to the boundary
surface. See Exercise 2.4.16. The special case g = 0 corresponds to an
adiabatic boundary surface. Condition (2.2.14) is also named the Neumann9
boundary condition.

7 Pierre Simon Laplace, a French scientist, 1749–1827. He worked on Celestial Mechanics

and Probability. He introduced the Laplace equation and Laplace transform.


8 Pietro Gustavo Dirichlet, a German scientist, 1805–1859. He was Professor at the

University of Göttingen. He made deep contributions in Mechanics and Analysis.


9 Carl Gottfried Neumann, a German scientist, 1832–1925. He was Professor at the

University of Leipzig. He worked on Mathematical Physics and Electrodynamics.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 53

The Finite Difference Method 53

The boundary condition of the third type is a linear combination of


temperature and heat flux
∂u
k (x, t) + hu(x, t) = g(x, t), x ∈ ∂B. (2.2.15)
∂n
Condition (2.2.15) is also named the Robin10 boundary condition. An
example of Condition (2.2.15) is provided by Newton’s11 law of cooling,
which states that the rate of heat loss of a body is directly proportional to
the temperature difference between a body and the environment
∂u
−k (x, t) = h[u(x, t) − uenv (x, t)], x ∈ ∂B, (2.2.16)
∂n
where uenv indicates the known environment temperature and h is the heat
transfer coefficient. Setting huenv = g, Eq. (2.2.16) is reduced to boundary
Condition (2.2.15).
When g = 0, the corresponding boundary condition of the first, second,
or third kind is named homogeneous. The heat equation is named homoge-
neous if f = 0. An initial-boundary value problem is named homogeneous
if both the equation and boundary condition are homogeneous.
One-dimensional heat conduction modeling is adopted when the main
thermal variables change predominantly in one defined direction, say x. In
this situation, all functions in the heat equation depend on x and t only,
and Eq. (2.2.10) is reduced to the one-dimensional heat equation

cp ρut − (kux )x = F (x, t), 0 < x < L, 0 < t ≤ T.

For constant k, the previous equation simplifies to

ut (x, t) − αuxx (x, t) = f (x, t), 0 < x < L, 0 < t ≤ T,

that is used in many applications. The initial condition simplifies to

u(x, 0) = ϕ(x), 0 ≤ x ≤ L.

The Dirichlet boundary conditions are reduced to

u(0, t) = g1 (t), u(L, t) = g2 (t), t > 0.

10 Victor Gustave Robin, a French scientist, 1855–1897. He was Professor of Mathematical


Physics at the Sorbonne in Paris. He worked mainly on Thermodynamics.
11 Sir Isaac Newton, an English scientist, 1642–1727. He was one of the most important

scientists of all time. He formulated the laws of Dynamics, published in Philosophiae


Naturalis Principia Mathematica (1687).
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 54

54 Matlab for Engineering

The Neumann boundary conditions assume the following expression

−kux (0, t) = g1 (t), kux (L, t) = g2 (t), t > 0.

The - sign for x = 0 depends on the fact that the outward normal direction
is opposed to that of x. For the same reason, the Robin boundary conditions
are written as follows

−kux (0, t) + h1 u(0, t) = g1 (t), kux (L, t) + h2 u(L, t) = g2 (t), t > 0.

When the thermal process occurs in a moving medium, Fourier’s law


is modified to take account of the convective term due to the motion

q = −k∇u + ρcp uv, (2.2.17)

where v is the velocity of x. Substituting (2.2.17) into the energy equation


cp ρut + ∇ · q − F = 0 yields

cp ρut − ∇ · (k∇u) + ∇ · (cp ρuv) = F. (2.2.18)

If ρcp is constant, from (2.2.18), it follows that (see Exercise 2.4.17)

cp ρut − ∇ · (k∇u) + cp ρ(v · ∇u + u∇ · v) = F, (2.2.19)

that is reduced to

cp ρut − ∇ · (k∇u) + cp ρv · ∇u = F, (2.2.20)

when ∇ · v = 0 (incompressible flow). Equation (2.2.20) is named


a convection-diffusion equation, or advection-diffusion equation, since it
governs thermal processes depending on heat diffusion influenced by
convection, or advection. Equation (2.2.20) is very important since it
governs a number of physical phenomena. For constant k, the convection-
diffusion equation (2.2.20) simplifies to

ut − αΔu + v · ∇u = f, (2.2.21)

where α = k/cp ρ and f = F/cp ρ. The one-dimensional case equations


(2.2.19)–(2.2.21) are expressed as

cp ρut − (kux )x + cp ρ(vux + uvx ) = F, (2.2.22)

cp ρut − (kux )x + cp ρvux = F, (2.2.23)

ut − αuxx + vux = f, (2.2.24)


respectively, where v indicates the only nonzero velocity component.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 55

The Finite Difference Method 55

2.2.2 Fick’s Law and Diffusion


Diffusion is the physical process of mass transfer from one region to another
in a system, usually fluid or gas. It is modeled by equations very similar to
those of heat propagation. The basic law for diffusion is Fick’s Law, which
follows from physical experiences. It states that a diffusing mass moves
from regions of higher concentration to regions of lower concentration. The
concentration C is the dissolving mass per unit volume. Therefore, if the
vector J indicates diffusive flux per unit area per unit time, the direction of
J is opposed to the concentration gradient. For isotropous medium at rest,
Fick’s law is formalized as

J = −D∇C, (2.2.25)

where D is the diffusion coefficient. A second equation involving the


concentration can be derived from the principle of mass conservation: rate
of mass in the control volume V = mass entering and leaving V through
boundary ∂V , in absence of internal mass production. This principle leads
to the following mass balance equation
 
Ct dx = − J · n dS, ∀ V, (2.2.26)
V ∂V

where n is the outward unit normal vector to the surface ∂V at the


integration point. Using the Gauss divergence Theorem (2.2.5) in Formula
(2.2.26) yields

[Ct + ∇ · J] dx = 0, ∀ V.
V

This equation holds ∀ V . Therefore, it implies

Ct + ∇ · J = 0, ∀ x. (2.2.27)

Substituting (2.2.25) into Formula (2.2.27), one arrives at the diffusion


equation

Ct = ∇ · (D∇C), (2.2.28)

completely similar to the heat equation. Equation (2.2.28) is also named a


diffusive equation. For constant D, Eq. (2.2.28) simplifies to

Ct = DΔC.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 56

56 Matlab for Engineering

For the diffusion process in a moving medium, Fick’s law is replaced by

J = −D∇C + Cv, (2.2.29)

where v is the velocity of x. Considering (2.2.29) in (2.2.28) yields

Ct = DΔC − ∇ · (Cv),

Ct − DΔC + v · ∇C + C∇ · v = 0, (2.2.30)

where D was assumed to be constant. Equation (2.2.30) is similar to


Eq. (2.2.19). Therefore, from (2.2.30), equations similar to (2.2.20) to
(2.2.24) are immediately derived with the temperature replaced by the
concentration.

2.2.3 Free Boundary Value Problems


In some problems of noteworthy physical interest, the boundary can move
with time during the thermal process. These problems are named free
boundary value problems Crank (1979, 1984); Rubinstein (1971). Phase
transition, e.g., ice turning to water, is a typical example of these problems.
Indeed, consider the one-dimensional melting process where the liquid phase
is separated from the solid one by a sharp interphase of the equation (see
Fig. 2.2.2)

x = s(t).

As outlined earlier, this function is unknown, since it depends on the


thermal process. It cannot be assigned a priori and is a further unknown
of the problem to be determined together with the temperature field. If the
heat transfer occurs by conduction in both phases and there is no internal

t
a
x = s(t)

x = s(t)
x
LIQUID SOLID x

Figure 2.2.2. Phase change.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 57

The Finite Difference Method 57

heat generation, then the temperature evolution is governed by the following


equations

ut = αuxx , 0 < x < s(t), t > 0, (2.2.31)

ust = αs usxx , s(t) < x < a, t > 0, (2.2.32)

where the index s is related to the solid phase. At the liquid-solid interphase,
the temperature assumes the melting temperature value um ,

u(s(t), t) = us (s(t), t) = um , t > 0. (2.2.33)

Moreover, the initial and boundary conditions must be assigned. For


example, reasonable conditions in a melting process are the following

u(x, 0) = ϕ(x)(≥ um ), 0 ≤ x ≤ s(0), (2.2.34)

u(0, t) = g(t)(≥ um ), t > 0, (2.2.35)

for the liquid phase and

us (x, 0) = ϕs (x)(≤ um ), s(0) ≤ x ≤ a, (2.2.36)

us (a, t) = gs (t)(≤ um ), t > 0, (2.2.37)

for the solid phase. Since the function s(t) is unknown, a further equation
is needed for solving Problems (2.2.31) to (2.2.37). This is provided by
the energy balance at the interphase, where the heat flux must equate the
absorbed heat given by the product of the latent heat L times the mass of
liquid converted from the solid phase

Aq(s(t), t) − Aqs (s(t), t) = AρLṡ(t), t > 0, (2.2.38)

where ρ is the liquid density and A the sectional area perpendicular to the
x-axis. Considering Fourier’s law, q = −kux, in Eq. (2.2.38) yields

ks usx (s(t), t) − kux (s(t), t) = ρLṡ(t), t > 0, (2.2.39)

which is named the Stefan12 condition. Equation (2.2.39) allows us to


determine the unknown free boundary x = s(t) and the interphase position.
The other equations give the temperature time evolution in both phases.

12 Jozef Stefan, an Austrian scientist, 1835–1893. He was Professor at the University of

Vienna. He worked on Thermodynamics and the Electromagnetic Theory.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 58

58 Matlab for Engineering

The difficulty is due to the fact that both problems must be solved
simultaneously.
A particular case of the previous problem occurs when the solid phase
temperature remains constant and equal to um during the whole melt-
ing process. In mathematical formulation, this happens when conditions
(2.2.36) and (2.2.37) are replaced by

us (x, 0) = um , s(0) ≤ x ≤ a, (2.2.40)

us (a, t) = um , t > 0, (2.2.41)

respectively. Indeed, the Problems (2.2.32), (2.2.40) and (2.2.41) have the
solution

us (x, t) = um , s(t) ≤ x ≤ a, t > 0. (2.2.42)

In this situation, the phase change process is completely determined by


the only liquid phase (one-phase Stefan problem) and only the equations
related to this phase must be solved

ut = αuxx , 0 < x < s(t), t > 0,

u(x, 0) = ϕ(x) ≥ um , 0 ≤ x ≤ s(0),

u(0, t) = g(t) ≥ um , t > 0,

u(s(t), t) = um , t ≥ 0,

−kux(s(t), t) = ρLṡ(t), t > 0.

2.3 Finite Difference Method


2.3.1 Explicit Euler Method
Consider the one-dimensional heat equation

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

As already outlined, Eq. (2.3.1) can be uniquely solved only when the initial-
boundary conditions are assigned

U (x, 0) = ϕ(x), 0 ≤ x ≤ L, (2.3.2)


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 59

The Finite Difference Method 59

U (0, t) = g1 (t), U (L, t) = g2 (t), 0 < t ≤ T. (2.3.3)

Conditions (2.3.3) are Dirichlet boundary conditions. Other boundary


conditions will be discussed in Sec. 2.3.3. Using the forward approximation
for the derivative Ut in (2.3.1) and the central approximation for Uxx yields
the following finite difference equation

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


i
− α i+1 = fij , (fij = F (xi , tj )). (2.3.4)
Δt (Δx)2

Note that a solution for Eq (2.3.4) was indicated with u, whereas U is a


solution of partial differential Equation (2.3.1). These different notations
will always be used in the following. Solving Eq. (2.3.4) with respect to
uj+1
i yields (Fig. 2.3.1)

uj+1
i = r(uji+1 + uji−1 ) + (1 − 2r)uji + Δtfij , (2.3.5)

where
r = αΔt/Δx2 . (2.3.6)

Equation (2.3.5) is named the Explicit Euler Method. The adjective explicit
emphasizes that when the values uji are known for some j, then Eq. (2.3.5)
explicitly provides the unknown values uj+1i . The method is characterized
by the forward approximation of the time derivative. Let us show that
Method (2.3.5) can be successfully applied to get the solution at any time
when initial-boundary Conditions (2.3.2) and (2.3.3) are given. From initial
Condition (2.3.2), it follows that the values

u0i = ϕ(xi ) = ϕi , i = 0, . . . , n, nΔx = L, (2.3.7)

0 x L

Figure 2.3.1. Explicit Euler Method.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 60

60 Matlab for Engineering

are known. Using them in Eq. (2.3.5) yields the values u1i , i = 1, . . . , n − 1.
In addition, the first and last values are provided by boundary Conditions
(2.3.3): u10 = g11 = g1 (t1 ) and u1n = g21 = g2 (t1 ). Next, the process is
repeated. Lastly, note that Formula (2.3.5) can be arranged in the following
matrix form
⎡ j+1 ⎤ ⎡ ⎤⎡ j ⎤ ⎡ ⎤
u1 1 − 2r r u1 rg1j + Δtf1j
⎢ j+1 ⎥ ⎢ ⎥⎢ j ⎥ ⎢ ⎥
⎢ u2 ⎥ ⎢ r 1 − 2r r ⎥ ⎢ u2 ⎥ ⎢ Δtf2,j ⎥,
⎢ ⎥=⎢ ⎥⎢ ⎥+⎣ ⎦
⎣ · ⎦ ⎣ . . . ⎦⎣ · ⎦ ···
j+1 j j j
un−1 r 1 − 2r un−1 rg2 + Δtfn−1

that incorporates the boundary conditions. Hence, with the apparent


meaning of the symbols,

uj+1 = Auj + aj , (2.3.8)

where the vector aj is known as given by boundary conditions and the


source term.

Example 2.3.1 The following function applies the Explicit Euler Method
(2.3.5) to solve the Dirichlet problem (2.3.1)–(2.3.3).
% function u = euler e(alpha, L, T, nx, phi, g1, g2, f)
% This is the function file euler e.m.
% Explicit Euler Method is applied to solve the Dirichlet problem:
% Ut - alpha Uxx = F, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).
% The input arguments are: thermal diffusivity, length of the solid, final time,
% number of points on the space grid, initial-boundary conditions, source term.
% The function returns a vector with the solution at the final time.
% Check data
if alpha <= 0 || L <= 0 || T <= 0 || nx <= 2
error(’Check alpha, L, T, nx’)
end
% Stability
dx = L/nx; nt = 10; st =.5;
while alpha*T/nt/dx2 > st
% The stability condition is checked. If it is not satisfied, nt is increased
% until alpha*T/nt/dx2 (= r) is less than or equal to 0.5.
nt = nt+1;
end
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 61

The Finite Difference Method 61

% Initialization
dt = T/nt; r = alpha*dt/dx2 ;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = phi(x);
% The vector u is initialized with the initial data. The command is
% equivalent to: u = feval(phi,x);
% Explicit Euler Method
for j=2:nt+1
u(2:nx)=(1-2r)*u(2:nx)+r*(u(1:nx-1)+u(3:nx+1))+dt*f(x(2:nx),t(j-1));
% The computed solution at the next time is saved in the same vector u.
% At the end of the process, u contains the solution at the final time.
u(1) = g1(t(j)); u(nx+1) = g2(t(j));
% Boundary conditions
end
end
A way to call euler e is illustrated in the following example.

Example 2.3.2 Euler e function is called to solve the Dirichlet problem

Ut = Uxx , 0 < x < 1, 0 < t ≤ T, (2.3.9)

U (x, 0) = sin(πx), 0 ≤ x ≤ 1, (2.3.10)

U (0, t) = U (1, t) = 0, 0 < t ≤ T. (2.3.11)


The solution is plotted in Fig. 2.3.2.
function u = euler e ex1
% This is the function file euler e ex1.m.
% Euler e function is called to solve the special Dirichlet problem:
% Ut - Uxx = 0, U(x,0) = sin(pi*x), U(0,t) = U(L,t) = 0.
% The approximating solution is plotted together with the exact solution:
% U = sin(pi*x)*exp(-pi2 *T). The error is evaluated.
alpha = 1; L = 1; T = 0.4; nx = 40;
phi = @(x) sin(pi*x);
g1 = @(t) 0;
g2 = @(t) 0;
f = @(x,t) 0;
u = euler e(alpha, L, T, nx, phi, g1, g2, f);
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 62

62 Matlab for Engineering

t = 0.4
0.02
Euler
Exact
0.016

0.012
U
0.008

0.004

0
0 0.2 0.4 x 0.6 0.8 1

Figure 2.3.2. Graph of the solution to Problem (2.3.9)–(2.3.11).

x = linspace(0,L,nx+1); U = sin(pi*x)*exp(-pi2 *T);


plot(x,u,’k’,x,U,’r*:’);
xlabel(’x’); ylabel(’U’);
title([’t = ’,num2str(T)]);
legend(’Euler’,’Exact’);
fprintf( ’Maximum error = %g\n’,max(abs(U-u)))
end
Another application is suggested in Exercise 2.4.19. The euler e function
returns a vector with the solution at the final time. If we are interested to
know the approximating solution during the whole computational process
for 0 < t ≤ T , then the approximating solution must be saved in a matrix,
as illustrated in the following example where the matrix form (2.3.8) is
applied.

Example 2.3.3
function [u, nt] = euler em(alpha, L, T, nx, phi, g1, g2)
% This is the function file euler em.m.
% Explicit Euler Method in matrix form is applied to solve the Dirichlet
% problem: Ut - alpha Uxx = 0, U(x,0)=phi(x), U(0,t)=G1(t), U(L,t)=G2(t).
% The input arguments are: thermal diffusivity, length of the solid, final time,
% number of points on the space grid, initial-boundary conditions. The function
% returns a matrix with the approximating solutions at tj , j = 1, ..., nt + 1
% and the number of points on the time grid.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 63

The Finite Difference Method 63

% Check data
if any([alpha L T nx-2]<= 0)
error(’Check alpha, L, T, nx’)
end
% Stability
dx = L/nx; nt = 10; st=.5;
while alpha*T/nt/dx2 > st
% The stability condition is checked. If it is not satisfied, nt is increased
% until alpha*T/nt/dx2 (= r) is less than or equal to 0.5.
nt = nt+1;
end
% Initialization
dt = T/nt; r = alpha*dt/dx2 ;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = zeros(nx+1,nt+1);
u(:,1) = feval(phi, x);
B = [r*ones(nx-1,1) (1-2*r)*ones(nx-1,1) r*ones(nx-1,1)];
A = spdiags(B, -1:1, nx-1, nx-1);
b = zeros(nx-1,1);
% Explicit Euler Method in matrix form
for j=2:nt+1
b(1) = r*u(1,j-1); b(nx-1) = r*u(nx+1,j-1);
u(2:nx,j) = A*u(2:nx,j-1)+b;
u(1,j) = feval(g1,t(j));
u(nx+1,j) = feval(g2,t(j));
end
end
A way to call and apply euler em is shown in the following example.

Example 2.3.4
function u = euler em ex1
% This is the function file euler em ex1.m
% Euler em function is called to solve the special Dirichlet problem:
% Ut - Uxx = 0, U(x,0) = x2 , U(0,t) = 2*t, U(L,t) = L2 + 2*t.
% The approximating solution is plotted together with the exact solution:
% U = x2 + 2*t. The error is evaluated.
alpha = 1; L = 1.5; T = 1; nx = 10;
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 64

64 Matlab for Engineering

phi = @(x) x.2 ;


g1 = @(t) 2*t;
g2 = @(t) L2 +2*t;
[u, nt] = euler em(alpha, L, T, nx, phi, g1, g2);
x = linspace(0,L,nx+1);
U = x’.2 + 2*T;
for j = 1:nt+1
plot(x,u(:,j),’k’,x,U,’r*:’);
xlabel(’x’); ylabel(’u’);
time=(j-1)*T/nt; title([’t = ’,num2str( time )]);
legend(’Euler’,’Exact’,’Location’,’NorthWest’);
pause(.01);
end
u = u(:,nt+1); fprintf( ’Maximum error = %g\n’,max(abs(U - u)))
end
Some code lines of the euler e and euler em functions were never
discussed: the lines related to the while loop that imposes the constraint
r ≤ 1/2. Using the right value of the parameter r is crucial, as explained
in the next section.

2.3.2 Stability, Convergence, Consistence


The solutions of difference equations, such as (2.3.5), are the result of a
computational process with thousands of operations and inevitable round-
off errors. A stable algorithm is able to control this kind of error. To better
understand the question, let us refer to the Explicit Euler Method (2.3.5)
and suppose that a round-off error, say e, arises when the solution related
to the point (h, k) of the space-time mesh is computed. Evidently, the error
propagates to the next rows during the computational process. Investigate
its influence on the solution. Denote with uji the solution without error, and
with ūji , the solution perturbed by the error e. This second solution is the
same as uji when j < k and it is affected by the error when j = k:

ūji = uji for j < k; ūki = uki for i = h; ūkh = ukh + e.

Let us evaluate the error, defined by

eji = uji − ūji


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 65

The Finite Difference Method 65

e/8 3e/8 3e/8 e/8


e/4 e/2 e/4
e/2 e/2
e j=k
i=h

Figure 2.3.3. Error propagation for r = 1/2.

for j ≥ k. Since Eq. (2.3.5) is linear, the error eji satisfies the homogeneous
equation

ej+1
i = (1 − 2r)eji + r(eji+1 + eji−1 ). (2.3.12)

Use this equation to investigate the error propagation for some values of
the parameter r = αΔt/Δx2 . For r = 1/2, Formula (2.3.12) simplifies to

ej+1
i = (eji+1 + eji−1 )/2,

and the error, computed for some values of j greater than k, is shown
in Fig. 2.3.3. Note the decreasing error behavior. This fact suggests that
Method (2.3.5) is stable for r = 1/2. Next, consider r = 2. Formula (2.3.12)
is reduced to

ej+1
i = 2(eji+1 + eji−1 ) − 3eji ,

and the error propagation is shown in Fig. 2.3.4. Note the increasing error
and oscillating behavior. Solutions obtained with this last value of r are
unreliable, since they are affected by out-of-control errors. Method (2.3.5)
is unstable when the second value of parameter r is considered. To prevent
the error from growing without limits, restrictive actions must be adopted
on the mesh size. The stability analysis investigates this question.
Consider a finite difference equation that can be expressed in matrix
form as

uj = Auj−1 + aj−1 , (2.3.13)

where the known term aj−1 depends on the boundary conditions and source
term. Indicate with (u1 )ji and (u2 )ji , two solutions of (2.3.13) that have
same boundary conditions but different initial conditions. If (u1 )ji represents
the solution without round-off errors and (u2 )ji represents the solution
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 66

66 Matlab for Engineering

4e -12e 17e -12e 4e

2e -3e 2e

e j=k
i=h

Figure 2.3.4. Error propagation for r = 2.

perturbed by round-off errors at the initial time, the stability analysis leads
to a discussion of the behavior of

uji = (u1 )ji − (u2 )ji .

This difference satisfies the homogeneous equation

uj = Auj−1 , (2.3.14)

with homogeneous boundary conditions. Method (2.3.14) is said to be


unconditionally stable if there exists a positive constant K, independent
of Δx, and Δt, such that for Δx, and Δt sufficiently small and jΔt ≤ T ,
it is

||uj || ≤ K||u0 ||. (2.3.15)

Formula (2.3.15) means that the error at time tj is controlled by the initial
error. If (2.3.15) holds, but Δt is functionally related to Δx, the method
is named conditionally stable, or stable. This situation occurs in the case
discussed in Example 2.3.5, where the maximum norm of a vector is used.
The maximum norm of the vector v = (vi ) is the maximum of the absolute
values of its elements:

||v||∞ = max |vi |. (2.3.16)


i

See Exercise 2.4.20 for Matlab commands on norms.

Example 2.3.5 Consider the homogeneous Explicit Euler Method

uj+1
i = r(uji+1 + uji−1 ) + (1 − 2r)uji , i = 1, . . . , n − 1, (2.3.17)
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 67

The Finite Difference Method 67

with Dirichlet homogeneous boundary conditions

uj0 = ujn = 0. (2.3.18)

Prove that the method is stable under the hypothesis

0 < r = αΔt/(Δx)2 ≤ 1/2. (2.3.19)

From (2.3.17)–(2.3.19), it follows that

|uj+1
i | ≤ r(|uji+1 | + |uji−1 |) + |1 − 2r| |uji |

≤ r(max |uji | + max |uji |) + (1 − 2r) max |uji |.


i i i

Using Definition (2.3.16) yields

|uj+1
i | ≤ ||uj ||∞ , i = 1, . . . , n − 1,

max |uj+1
i | ≤ ||uj ||∞ ,
i

||uj+1 ||∞ ≤ ||uj ||∞ · · · ≤ ||u0 ||∞ .

Therefore, the Explicit Euler Method is conditionally stable, as condition


(2.3.15) is satisfied (with K = 1) under Assumption (2.3.19).
The convergence considers the error that arises when the analytical
solution of a partial differential equation is compared with the correspond-
ing solution of the finite difference equation. Denote with U the solution
of an initial-boundary value problem for a partial differential equation
and suppose that this is well posed, i.e., the solution exists and depends
continuously on data. In addition, denote with uji the corresponding
approximating solution. The difference of the two solutions, evaluated in
the mesh point (i, j), is the discretization error in that point

Eij = Uij − uji .

The convergence depends on the behavior of Eij . A finite difference method


is named convergent at time t if ||Ej || = ||Uj − uj || goes to zero when Δx,
Δt → 0 and jΔt → t. It can be proved that the Explicit Euler Method
converges under Hypothesis (2.3.19).
The consistency considers the error that arises when the partial
differential equation is approximated by the finite difference equation. If
the analytical solution U of a partial differential equation is substituted in
the corresponding finite difference equation, it returns a residual, since the
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 68

68 Matlab for Engineering

analytical solution generally does not satisfy the finite difference equation.
The residual is named a local truncation error. For example, for the Explicit
Euler Method, it is

Uij+1 − Uij U j − 2Uij + Ui−1


j
− α i+1 − fij = τij , (2.3.20)
Δt (Δx)2

where τij is the mentioned error. A finite difference method is named


consistent or compatible with the partial differential equation that it
approximates if τij goes to zero when the mesh is refined, i.e., Δx, Δt → 0.
Explicit Euler Method is consistent with the heat equation. Indeed, consider
the formulas of the forward and central approximations

Uij+1 − Uij U j − 2Uij + Ui−1


j
(Ut )ji = + O(Δt), (Uxx )ji = i+1 + O((Δx)2 ),
Δt (Δx)2
and substitute them in (2.3.20)

τij = (Ut − αUxx )ji − fij + O(Δt) + O((Δx)2 ). (2.3.21)

Since (Ut − αUxx )ji = fij , from (2.3.21), it follows the desired result.

2.3.3 Boundary Value Problems


Consider the one-dimensional heat equation

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

with the initial condition

U (x, 0) = ϕ(x), 0 ≤ x ≤ L, (2.3.23)

and Neumann boundary conditions

−Ux (0, t) = g1 (t), Ux (L, t) = g2 (t), 0 < t ≤ T. (2.3.24)

Different from the Dirichlet problem, the boundary values are now
unknowns to be determined, like the other values. Consequently, the
unknowns are two more. The Explicit Euler Method

uj+1
i = r(uji+1 + uji−1 ) + (1 − 2r)uji + Δtfij , i = 1, . . . , n − 1, (2.3.25)

provides n − 1 equations, which are insufficient to determine all n + 1


unknowns. The simplest way to get two more equations is to use the forward
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 69

The Finite Difference Method 69

and backward approximations for the two boundary Conditions (2.3.24)

uj1 − uj0 ujn − ujn−1


− = g1j , = g2j , (2.3.26)
Δx Δx
where g1j = g1 (jΔt) and g2j = g2 (jΔt). Now, the Neumann problem can be
solved. For any j, the unknowns uj+1 j+1
1 , . . . , un−1 are obtained from (2.3.25)
and the unknowns uj+10 , un
j+1
from (2.3.26)

uj+1
0 = uj+1
1 + g1j+1 Δx, uj+1
n = uj+1 j+1
n−1 + g2 Δx. (2.3.27)

Formulas (2.3.27) are accurate of order Δx. More accurate formulas, of


order (Δx)2 , can be obtained if the central approximations are used for
boundary Conditions (2.3.24):

uj1 − uj−1 ujn+1 − ujn−1


− = g1j , = g2j . (2.3.28)
2Δx 2Δx
These formulas present the ghost values uj−1 , ujn+1 and cannot be directly
applied. The ghost values can be eliminated. Assume that Eq. (2.3.25) also
holds on the boundaries, for i = 0 and i = n,

uj+1
0 = (1 − 2r)uj0 + r(uj1 + uj−1 ) + Δtf0j , (2.3.29)

uj+1
n = (1 − 2r)ujn + r(ujn+1 + ujn−1 ) + Δtfnj . (2.3.30)

Solving (2.3.28)1 with respect to uj−1 and substituting the result in (2.3.29)
yields

uj+1
0 = (1 − 2r)uj0 + 2r(uj1 + g1j Δx) + Δtf0j . (2.3.31)

Similarly, from (2.3.28)2 and (2.3.30), it follows

uj+1
n = (1 − 2r)ujn + 2r(ujn−1 + g2j Δx) + Δtfnj . (2.3.32)

Equations (2.3.31)–(2.3.32) are the two new equations we need, together


with Eqs. (2.3.25), to solve the Neumann problem with greater accuracy.
Lastly, all equations can be arranged in compact matrix form as follows
⎡ j+1 ⎤ ⎡ ⎤⎡ j ⎤ ⎡ ⎤
u0 1 − 2r 2r u0 2rg1j Δx + Δtf0j
⎢ uj+1 ⎥ ⎢ r 1 − 2r r ⎥ ⎢ uj ⎥ ⎢ Δtf1j

⎢ 1 ⎥=⎢ ⎥ ⎢ 1 ⎥ +⎢⎢

⎥.
⎣ · ⎦ ⎣ . . . ⎦ ⎣ · ⎦ ⎣ ··· ⎦
uj+1
n 2r 1 − 2r ujn 2rg2j Δx + Δtfnj
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 70

70 Matlab for Engineering

Equivalently, with the apparent definition of the symbols,


uj+1 = Auj + aj . (2.3.33)

A function that solves the Neumann Problem (2.3.22)–(2.3.24) in the


simplest case (2.3.27) can be obtained from the euler e function in Example
2.3.1 with few modifications.

Example 2.3.6 A function is presented that applies Explicit Euler


Method (2.3.5) to solve the Neumann Problem (2.3.22)–(2.3.24).
% function u = euler en(alpha, L, T, nx, phi, g1, g2)
% This is the function file euler en.m.
% Explicit Euler Method is applied to solve the Neumann problem:
% Ut - alpha Uxx = 0, U(x,0) = phi(x), -Ux(0,t) = G1(t), Ux(L,t) = G2(t).
% The input arguments are: thermal diffusivity, length of the solid, final time,
% number of points on the space grid, initial-boundary conditions.
% The function returns a vector with the solution at the final time.
% Check data
if alpha <= 0 || L <= 0 || T <= 0 || nx <= 2
error(’Check alpha, L, T, nx’)
end
% Stability
dx = L/nx; nt = 10;
st=.5;
while alpha*T/nt/dx2 > st
nt = nt+1;
end
% Initialization
dt = T/nt; r = alpha*dt/dx2 ;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = feval(phi,x);
% Explicit Euler Method
for j=2:nt+1
u(2:nx) = (1-2*r)*u(2:nx) + r*(u(1:nx-1)+u(3:nx+1));
u(1) = u(2) + dx*g1(t(j)); u(nx+1) = u(nx) + dx*g2(t(j));
% Neumann boundary conditions
end
end
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 71

The Finite Difference Method 71

Some modifications to the euler en function are suggested in Exercises


2.4.21 and 2.4.22. A way to call euler en is illustrated in the following
example.

Example 2.3.7
function u = euler en ex1
% This is the function file euler en ex1.m.
% Euler en function is called to solve the special Neumann problem:
% Ut - Uxx = 0, U(x,0) = sin(pi*x), G1(t) = G2(t) = -pi/L*exp(-(pi/L)2*t).
% The approximating solution is plotted together with the exact solution:
% U = sin(pi*x)*exp(-pi2 *T). The error is evaluated.
alpha = 1; L = 1.5; T = .3; nx = 30;
phi = @(x) sin(pi*x/L);
g1 = @(t) -pi/L*exp(-(pi/L)2*t);
g2 = @(t) -pi/L*exp(-(pi/L)2*t);
u = euler en(alpha, L, T, nx, phi, g1, g2);
x = linspace(0,L,nx+1);
U = sin(pi*x/L)*exp(-(pi/L)2 *T);
fprintf( ’Maximum error = %g\n’,max(abs(U-u)))
plot(x,u,’k’,x,U,’r*:’);
xlabel(’x’); ylabel(’u’); title([’t = ’,num2str( T )]);
legend(’Euler-N,’Exact’,’Location’,’NorthWest’);
end

Example 2.3.8 A function is presented that applies the matrix form


(2.3.33) of the Explicit Euler Method and solves the Neumann Problem
(2.3.22)–(2.3.24).
% function u = euler enm(alpha, L, T, nx, phi, g1, g2)
% This is the function file euler enm.m.
% Explicit Euler Method in matrix form is applied to solve the
% Neumann problem:
% Ut - alpha Uxx = 0, U(x,0) = phi(x), -Ux(0,t) = G1(t), Ux(L,t) = G2(t).
% The input arguments are: thermal diffusivity, length of the solid, final time,
% number of points on the space grid, initial-boundary conditions.
% The function returns a vector with the solution at the final time.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 72

72 Matlab for Engineering

% Check data
if alpha <= 0 || L <= 0 || T <= 0 || nx <= 2
error(’Check alpha, L, T, nx’)
end
% Stability
dx = L/nx; nt = 10; st=.5;
while alpha*T/nt/dx2 > st
nt = nt+1;
end
% Initialization
dt = T/nt; r = alpha*dt/dx2 ;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = feval(phi,x’);
B = [[r*ones(nx-1,1);2*r;0] (1-2*r)*ones(nx+1,1) [0;2*r;r*ones(nx-1,1)]];
A = spdiags(B, -1:1, nx+1, nx+1);
b = zeros(nx+1,1);
% Explicit Euler Method in matrix form
for j=2:nt+1
b(1) = 2*r*dx*g1(t(j-1)); b(nx+1) = 2*r*dx*g2(t(j-1));
u = A*u + b;
end
u = u’;
end
A way to call euler enm is suggested in Exercise 2.4.23.
Consider the Dirchlet–Neumann problem

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

U (x, 0) = ϕ(x), 0 ≤ x ≤ L, (2.3.35)

U (0, t) = g1 (t), Ux (L, t) = g2 (t), 0 < t ≤ T. (2.3.36)

A function for Problem (2.3.34)–(2.3.36) is provided in the following


example and a way to call it is illustrated in Example 2.3.10.

Example 2.3.9
function u = euler edn(alpha, L, T, nx, phi, g1, g2)
% This is the function file euler edn.m.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 73

The Finite Difference Method 73

% Explicit Euler Method is applied to solve the Dirichlet–Neumann


% problem:
% Ut - alpha Uxx = 0, U(x,0) = phi(x), U(0,t) = G1(t), Ux(L,t) = G2(t).
% The input arguments are: thermal diffusivity, length of the solid, final time,
% number of points on the space grid, initial-boundary conditions.
% The function returns a vector with the solution at the final time.
% Check data
if alpha <= 0 || L <= 0 || T <= 0 || nx <= 2
error(’Check alpha, L, T, nx’)
end
% Stability
dx = L/nx; nt = 10;
st=.5;
while alpha*T/nt/dx2 > st
nt = nt+1;
end
% Initialization
dt = T/nt; r = alpha*dt/dx2 ;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = feval(phi,x);
% Explicit Euler Method
for j=2:nt+1
u(2:nx) = (1-2*r)*u(2:nx) + r*(u(1:nx-1)+u(3:nx+1));
u(1) = g1(t(j)); % Dirichlet boundary condition
u(nx+1) = u(nx) + dx*g2(t(j)); % Neumann boundary condition
end
end

Example 2.3.10 Consider Problem (2.3.34)–(2.3.36) where

U (x, 0) = ϕ(x) = ϕ̄ = 0.5, 0 ≤ x ≤ L,


 
ϕ̄ L2
U (0, t) = g1 (t) = 0, Ux (L, t) = √ exp − , 0 < t ≤ T.
παt 4αt
The following listing calls euler edn, solves the the previous problem and
plots the solution (Fig. 2.3.5).
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 74

74 Matlab for Engineering

t=2
0.5

0.4

0.3
u

0.2

0.1
Euler-D-N
Exact
0
0 2 4 x 6 8 10

Figure 2.3.5. Graph of the solution.

function u = euler edn ex1


% This is the function file euler edn ex1.m.
% Euler edn function is called to solve the special Dirichlet-Neumann problem:
% Ut - Uxx = 0, U(x,0) = phibar, G1(t) = 0,
% G2(t) = phibar*exp(-L2 /(4*alpha*t))/sqrt(pi*alpha*t).
% The approximating solution is plotted together with the exact solution:
% U = phibar*erf(x/sqrt(4*alpha*T)). The error is evaluated.
alpha = 1; L = 10; T = 1; nx = 60;
phibar = .5;
phi = @(x) phibar*ones(1,nx+1);
g1 = @(t) 0;
g2 = @(t) phibar*exp(-L2 /(4*alpha*t))/sqrt(pi*alpha*t);
u = euler edn(alpha, L, T, nx, phi, g1, g2);
x = linspace(0,L,nx+1);
U = phibar*erf(x/sqrt(4*alpha*T));
fprintf( ’Maximum error = %g\n’,max(abs(U-u)))
plot(x,u,’k*’,x,U,’r:’);
xlabel(’x’); ylabel(’u’); title([’t = ’,num2str( T )]);
legend(’Euler-D-N’,’Exact’,’Location’,’SouthEast’);
end
Other applications are suggested in Exercises 2.4.24–2.4.26.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 75

The Finite Difference Method 75

In the previous example, the error function erf(y) was used. It is defined
by the formula
 y  ∞
2 2
erf(y) = √ exp(−η 2 ) dη ⇒ erf(∞) = √ exp(−η 2 ) dη = 1.
π 0 π 0
In addition, the complimentary error function erfc(y) is expressed as
 ∞
2
erfc(y) = √ exp(−η 2 ) dη ⇒ erfc(y) = 1 − erf(y).
π y
Consider the Robin problem

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

U (x, 0) = ϕ(x), 0 ≤ x ≤ L, (2.3.38)

−Ux (0, t) + h1 U (0, t) = g1 (t), Ux (L, t) + h2 U (L, t) = g2 (t), 0 < t ≤ T.


(2.3.39)
The Robin boundary conditions can be approximated exactly as the
Neumann conditions. Indeed, we can apply forward and backward approx-
imations to (2.3.39) and get the two following equations

uj1 − uj0 ujn − ujn−1


− + h1 uj0 = g1j , + h2 ujn = g2j . (2.3.40)
Δx Δx
Solving Formulas (2.3.40) with respect to uj+1
0 and uj+1
n , respectively, yields

uj+1 + g1j+1 Δx uj+1


n−1 + g2
j+1
Δx
uj+1
0 = 1
, uj+1
n = . (2.3.41)
1 + h1 Δx 1 + h2 Δx
Formulas more accurate than (2.3.41) are obtained by considering the
central approximations for boundary Conditions (2.3.39):

uj1 − uj−1 ujn+1 − ujn−1


− + h1 uj0 = g1j , + h2 ujn = g2j . (2.3.42)
2Δx 2Δx
The ghost values uj−1 and ujn+1 are eliminated as in the Neumann problem
and we get

uj+1
0 = [1 − 2r(1 + h1 Δx)]uj0 + 2r(uj1 + Δxg1j ) + Δtf0j , (2.3.43)

uj+1
n = [1 − 2r(1 + h2 Δx)]ujn + 2r(ujn−1 + Δxg2j ) + Δtfnj . (2.3.44)

The previous equations and the Explicit Euler Method, rewritten below,

uj+1
i = r(uji+1 + uji−1 ) + (1 − 2r)uji + Δtfij , i = 1, ..., n − 1, (2.3.45)
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 76

76 Matlab for Engineering

provide a system of n + 1 equations for the n + 1 unknowns uj+1 j+1


0 , . . . , un .
System (2.3.43)–(2.3.45) solves the Robin problem. It can be arranged in a
compact matrix form that incorporates the boundary conditions,
⎡ j+1 ⎤ ⎡ ⎤⎡ j ⎤ ⎡ ⎤
u0 H1 2r u0 2rg1j Δx + Δtf0j
⎢ uj+1 ⎥ ⎢ r 1 − 2r r ⎥ ⎢ uj ⎥ ⎢ Δtf1j ⎥
⎢ 1 ⎥ ⎢ ⎥⎢ 1 ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ · ⎥=⎢ . . . ⎥⎢ · ⎥+⎢ ··· ⎥,
⎢ j+1 ⎥ ⎢ ⎥⎢ j ⎥ ⎢ j ⎥
⎣ un−1 ⎦ ⎣ r 1 − 2r r ⎦ ⎣ un−1 ⎦ ⎣ Δtfn−1 ⎦
j
uj+1
n 2r H2 u j
n 2rg 2 Δx + Δtf j
n

where H1 = 1 − 2r(1 + h1 Δx) and H2 = 1 − 2r(1 + h2 Δx). Equivalently,


with the apparent definition of the symbols,
uj+1 = Auj + aj . (2.3.46)

2.3.4 Diffusion in a Multi-layer Medium


Diffusion, or heat conduction, in a multi-layer medium is involved in
many engineering problems. Moreover, the situation of several more or
less permeable layers in aquifers is frequent and the analysis of the pore
pressure in multi-layer aquifers leads to consider similar mathematical
models. A two-layer system is discussed in the following (Fig. 2.3.6). The
generalization to more layers is straightforward. In the mentioned case, the
physical process is governed by the following equations

Ut − α1 Uxx = F, 0 < x < xh , 0 < t ≤ T, (2.3.47)

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

U (x− +
h , t) = U (xh , t), 0 < t ≤ T, (2.3.49)

k1 Ux (x− +
h , t) = k2 Ux (xh , t), 0 < t ≤ T, (2.3.50)
where x = xh indicates the interphase position. Equations (2.3.49)
and (2.3.50) express the continuity of temperature and heat flux at

xh x

Figure 2.3.6. Multi-layer medium.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 77

The Finite Difference Method 77

the interphase. Of course, the initial-boundary conditions, depending on


special problem, must be associated to Eqs. (2.3.47)–(2.3.50). Since Ux is
discontinuous for x = xh (Fig. 2.3.6), the function U cannot be a solution
to the heat equation (in the classical sense).
Consider the Explicit Euler Method. If a mesh is used where xh is a
node xh = hΔx, then we get the following discrete version of Problem
(2.3.47)–(2.3.50)

uj+1
i = r1 (uji+1 + uji−1 ) + (1 − 2r1 )uji + Δtfij , 0 < i < h, (2.3.51)

uj+1
i = r2 (uji+1 + uji−1 ) + (1 − 2r2 )uji + Δtfij , h < i < n, (2.3.52)

k1 (−4uj+1 j+1
h−1 + 3uh + uj+1 j+1 j+1
h−2 ) = k2 (4uh+1 − 3uh − uj+1
h+2 ), (2.3.53)

where ujh = U (x− + 2


h , tj ) = U (xh , tj ) and ri = αi Δt/(Δx) , i = 1, 2. Equation
(2.3.53) was obtained from (2.3.50) by applying three-point approximations.
Equations (2.3.51) and (2.3.52) explicitly provide the values uj+1 j+1
2 , . . . , uh−1
and uj+1 j+1
h+1 , . . . , un−1 , respectively. Considering these results in Eq. (2.3.53),
we find

uj+1
h = (4k1 uj+1 j+1 j+1 j+1
h−1 − k1 uh−2 − k2 uh+2 + 4k2 uh+1 )/3/(k1 + k2 ). (2.3.54)

Lastly, the values uj+1


0 and uj+1
n follow from the boundary conditions.

Example 2.3.11 A function is presented that applies Formulas (2.3.51)–


(2.3.54) to solve Problem (2.3.47) to (2.3.50).
function u = layers(alpha1, alpha2, k1, k2, T, L, h, nx, phi1, phi2, g1, g2)
% This is the function file layers.m.
% Explicit Euler Method is applied to solve the problem:
% Ut = alpha1 Uxx = 0, x < xh, U(x,0) = phi1(x), U(0,t) = G1(t),
% Ut = alpha2 Uxx = 0, x > xh, U(L,0) = phi2(x), U(L,t) = G2(t),
% U(xh-,t) = U(xh+,t), k1*Ux(xh-,t) = k2*Ux(xh+,t),
% The input arguments are: thermal diffusivity coefficients, thermal
% conductivity coefficients,, final time, length of the solid, interphase
% position, number of points on the space grid, initial-boundary conditions.
% The function returns a vector with the solution at the final time.
% Check data
if any ([alpha1 alpha2 k1 k2 L T nx-2]<= 0)
error(’Check alpha1, alpha2, k1, k2, L, T, nx’)
end
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 78

78 Matlab for Engineering

% Stability
dx = L/nx; nt = 10; st = .5;
while max([alpha1 alpha2])*T/nt/dx2 > st
nt = nt+1;
end
% Initialization
dt = T/nt; r1 = alpha1*dt/dx2; r2 = alpha2*dt/dx2 ;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = [phi1(x(1:h)) phi2(x(h+1:nx+1))];
% Explicit Euler Method
for j=2:nt+1
u(2:h-1) = (1-2*r1)*u(2:h-1) + r1*(u(1:h-2) + u(3:h));
u(h+1:nx) = (1-2*r2)*u(h+1:nx) + r2*(u(h:end-2) + u(h+2:nx+1));
u(h) = (4*k1*u(h-1) - k1*u(h-2) + 4*k2*u(h+1)...
- k2*u(h+2))/(k1+k2)/3;
u(1) = g1(t(j)); u(nx+1) = g2(t(j));
end
end

Example 2.3.12 A way to call layers is illustrated below by considering


the simple initial-boundary conditions

a1 x2 + c1 x + V, x < xh ,
U (x, 0) = (2.3.55)
a2 (x − L)2 + c2 (x − L), x > xh ,

U (0, t) = 2t + V, U (0, t) = 2t, t > 0, (2.3.56)

where a1 , a2 , c1 , c2 and V , are constants specified in the listing. The solution


is plotted in Fig. 2.3.7. The error is evaluated.
function u = layers ex1
% This is the function file layers ex1.m.
% Layers function is called to solve the special problem:
% Ut = alpha1 Uxx = 0, x < xh, U(x,0) = a1*x2 + c1*x + V,
% Ut = alpha2 Uxx = 0, x > xh, U(x,0) = a2*(x - L)2 + c2*(x - L),
% U(xh-,t) = U(xh+,t), k1*Ux(xh-,t) = k2*Ux(xh+,t),
% U(0,t) = 2*t + V, U(L,t) = 2*t.
% The approximating solution is plotted together with the exact solution:
% U = 2*t + a1*x2 + c1*x + V, x < xh,
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 79

The Finite Difference Method 79

t = 1000
10000

8000

U
6000

4000

Euler
Exact
2000
0 0.05 0.1 0.15 x 0.2 0.25 0.3 0.35

Figure 2.3.7. Graph of the solution to Problem (2.3.51)–(2.3.56).

% U = 2*T + a2*(x - L)2 + c2*(x - L), x > xh.


% The error is evaluated.
alpha1 = 85.9*10( − 6); alpha2=12.4*10( − 6); k1 = 202.4; k2 = 45;
T = 103 ; L = .35; nx = 18; h = 10;
x = linspace(0,L,nx+1); xh = x(h);
V = 8000; a1 = 1/alpha1; a2 = 1/alpha2;
c1 = (-a1*k2*xh2-a2*k2*(xh-L)2+2*k1*a1*xh*(xh-L)-k2*V )/...
(k2*xh-k1*(xh-L));
c2 = (a2*k1*(xh-L)2+a1*k1*xh2-2*a2*k2*xh*(xh-L)-k1*V )/...
(k2*xh-k1*(xh-L));
phi1 = @(x) a1*x.2 + c1*x + V;
phi2 = @(x) a2*(x - L).2 + c2*(x - L);
g1 = @(t) 2*t + V;
g2 = @(t) 2*t;
u = strati(alpha1, alpha2, k1, k2, T, L, h, nx, phi1, phi2, g1, g2);
U(1:h) = 2*T + a1*x(1:h).2 + c1*x(1:h) + V;
U(h+1:nx+1) = 2*T + a2*(x(h+1:nx+1) - L).2 + c2*(x(h+1:nx+1) - L);
% Exact solution
fprintf( ’Maximum error = %g\n’,max( abs(U-u) ) )
plot(x,u,’k*’,x,U,’r’);
title([’t = ’,num2str(T)]);
xlabel(’x’); ylabel(’U’);
legend(’Euler’,’Exact’,’Location’,’SouthWest’);
end
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 80

80 Matlab for Engineering

0 x L

Figure 2.3.8. Implicit Euler Method.

2.3.5 Implicit Euler Method


This section presents another numerical method for the heat equation. Let
us begin to discuss the Dirichlet problem
Ut − αUxx = F, 0 < x < L, 0 < t ≤ T, (2.3.57)
U (x, 0) = ϕ(x), 0 ≤ x ≤ L, (2.3.58)
U (0, t) = g1 (t), U (L, t) = g2 (t), 0 < t ≤ T. (2.3.59)
Consider the point (xi , tj+1 ) of the space-time grid (see Fig. 2.3.8), and
discretize Eq. (2.3.57) by using the backward approximation for the time
derivative and the central approximation for the space derivative,

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


i
− α i+1 i i−1
= fij+1 . (2.3.60)
Δt (Δx)2
Hence,
−ruj+1 j+1
i−1 + (1 + 2r)ui − ruj+1 j j+1
i+1 = ui + fi Δt, i = 1, . . . , n − 1, (2.3.61)
where the usual parameter r = αΔt/(Δx)2 was introduced. Formula
(2.3.61) is named the Implicit Euler Method. The method is characterized by
the backward approximation of the time derivative. The adjective implicit
outlines that equation (2.3.61) alone is unable to determine the three
unknowns on the left-hand side when the right-hand side is known. However,
when all values uji , i = 0, ..., n, are known, Formula (2.3.61) provides a
system of n−1 equations in the n−1 unknowns uj+1 i , i = 1, . . . , n−1. Solving
j+1 j+1
such a system yields the unknowns u1 ,. . . ,un−1 . The method works, as the
unknowns are obtained implicitly from Formula (2.3.61). In addition, the
values of the first and last points are obtained from the boundary conditions
uj0 = g1j = g1 (tj ), ujn = g2j = g2 (tj ), j = 1, . . . , m, mΔt = T. (2.3.62)
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 81

The Finite Difference Method 81

In conclusion, the method provides the following system for the unknowns
⎡ ⎤ ⎡ j+1 ⎤ ⎡ j ⎤ ⎡ j+1 ⎤
1 + 2r −r u1 u1 rg1 + f1j+1 Δt
⎢ ⎥ ⎢ j+1 ⎥ ⎢ j ⎥ ⎢ ⎥
⎢ −r 1 + 2r −r ⎥ ⎢ u2 ⎥ ⎢ u2 ⎥ ⎢ f2j+1 Δt ⎥
⎢ ⎥⎢ ⎥=⎢ ⎥+⎢ ⎥.
⎣ . . . ⎦ ⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦
j+1 j j+1 j+1
−r 1 + 2r un−1 un−1 rg2 + fn−1 Δt

Hence, in compact matrix notation

Buj+1 = uj + bj+1 ,
uj+1 = Auj + aj+1 , (A = B −1 , aj+1 = B −1 bj+1 ). (2.3.63)

The Implicit Euler Method is unconditionally stable, i.e., being stable


without any restriction on the parameter r. This result will be proved in
Sec. 2.3.7.

Example 2.3.13 This example presents a function that applies the


Implicit Euler Method to the Dirichlet problem for the heat equation.

function u = euler i(alpha, L, T, nx, nt, phi, g1, g2)


% This is the function file euler i.m.
% Implicit Euler Method is applied to solve the Dirichlet problem:
% Ut = alpha*Uxx, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).
% The input arguments are: thermal diffusivity, length of the solid, final time,
% number of points on the space-time grid, initial-boundary conditions.
% The function returns a vector with the solution at the final time.
% Check data
if any([alpha L T nx-2 nt-2] <= 0)
error(’Check alpha, L, T, nx, nt’)
end
% Initialization
dx = L/nx; dt = T/nt; r = alpha*dt/dx2;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = phi(x’);
B = [-r*ones(nx-1,1) (1+2*r)*ones(nx-1,1) -r*ones(nx-1,1)];
A = spdiags(B, -1:1, nx-1, nx-1);
b = zeros(nx-1,1);

% Implicit Euler Method


for j=2:nt+1
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 82

82 Matlab for Engineering

t=1
0.114
Implicit
Exact

0.11

0.106

0.102
0 0.2 0.4 x 0.6 0.8 1

Figure 2.3.9. Graph of solution.

u(1) = g1(t(j)); u(nx+1) = g2(t(j));


b(1) = r*u(1); b(nx-1) = r*u(nx+1);
u(2:nx) = A\(u(2:nx)+b);
end
u = u’;
end

Example 2.3.14 A way to apply euler i is suggested in the following


listing, where euler i is called with the initial-boundary conditions

U (x, 0) = ϕ̄ = constant if x ∈]x1 , x2 [⊂ [0, L],


0 if x ∈ [0, L]−]x1 , x2 [,
   
ϕ̄ −x x2
U (0, t) = erf √ 1
+ erf √ , t > 0,
2 4αt 4αt
   
ϕ̄ L − x1 x2 − L
U (L, t) = erf √ + erf √ , t > 0.
2 4αt 4αt
The graph of the solution is shown in Fig. 2.3.9.
function u = euler i ex1
% This is the function file euler i ex1.m.
% Euler i function is called to solve the special Dirichlet problem:
% Ut = alpha*Uxx,
% U(x,0) = 0, x <= x1; U(x,0) = phibar, x1 < x < x2; U(x,0) = 0, x >= x2;
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 83

The Finite Difference Method 83

% U(0,t) = .5*phibar* (erf(-x1/sqrt(4*alpha*t)) + erf(x2/sqrt(4*alpha*t)) ),


% U(L,t) = .5*phibar*(erf((L-x1)/sqrt(4*alpha*t))
% + erf((x2-L)/sqrt(4*alpha*t))).
% The approximating solution is plotted together with the exact solution:
% U=.5*phibar*(erf((x-x1)/sqrt(4*alpha*T))+erf((x2-x)/sqrt(4*alpha*T))).
% The error is evaluated.
alpha = 1; L = 1; T = 1; nx = 10; nt = 200;
x = linspace(0,L,nx+1); x1=x(3); x2=x(7); phibar = 1;
phi = @(x) phibar*((x2-x)>0).*((x-x1)>0);
g1=@(t) phibar/2*(erf(-x1/sqrt(4*alpha*t)) + erf(x2/sqrt(4*alpha*t)));
g2=@(t) phibar/2*(erf((L-x1)/sqrt(4*alpha*t)+ erf((x2-L)/sqrt(4*alpha*t)));
u = euler i(alpha, L, T, nx, nt, phi, g1, g2);
U = .5*phibar*(erf((x-x1)/sqrt(4*alpha*T)) + erf((x2-x)/sqrt(4*alpha*T)));
fprintf( ’Maximum error = %g\n’,max(abs(U-u)))
plot(x,u,’k*’,x,U,’r’);
xlabel(’x’); ylabel(’U’);
title([’t = ’,num2str( T )]); legend(’Implicit’,’Exact’);
end

Consider the Robin problem for Eq. (2.3.57) with initial Condition
(2.3.58) and Robin boundary conditions

−Ux (0, t) + h1 U (0, t) = g1 (t), Ux (L, t) + h2 U (L, t) = g2 (t). (2.3.64)

These conditions simplify to the Neumann conditions for h1 = h2 = 0.


Applying the central approximations to the derivatives in (2.3.64) yields

uj+1
1 − uj+1
−1 uj+1 j+1
n+1 − un−1
− + h1 uj+1
0 = g j+1
1 , + h2 uj+1
n = g2j+1 . (2.3.65)
2Δx 2Δx

Formulas (2.3.65) cannot be used because of the ghost terms uj+1−1 and
uj+1
n+1 . These terms can be eliminated. Indeed, consider Eq. (2.3.61) for
i = 0 and i = n

−ruj+1 j+1
−1 + (1 + 2r)u0 − ruj+1
1 = uj0 + f0j+1 Δt, (2.3.66)

−ruj+1 j+1
n−1 + (1 + 2r)un − ruj+1 j j+1
n+1 = un + fn Δt. (2.3.67)
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 84

84 Matlab for Engineering

Solving Eq. (2.3.65)1 with respect to uj+1


−1 and the result substituted into
(2.3.66) yields

[1 + 2r(1 + h1 Δx)]uj+1
0 − 2ruj+1
1 = uj0 + 2rΔxg1j+1 + f0j+1 Δt. (2.3.68)

A similar reasoning for the other ghost value leads to

−2ruj+1 j+1
n−1 + [1 + 2r(1 + h2 Δx)]un = ujn + 2rΔxg2j+1 + fnj+1 Δt. (2.3.69)

Equations (2.3.68), (2.3.69) and (2.3.61) are a system of n + 1 equations in


n + 1 unknowns, written in matrix form as
⎡ ⎤⎡ j+1 ⎤ ⎡ j ⎤ ⎡ ⎤
H1 −2r u0 u0 2rΔxg1j+1 + f0j+1 Δt
⎢ −r 1 + 2r −r ⎥⎢uj+1 ⎥ ⎢ uj ⎥ ⎢ f1j+1 Δt

⎢ ⎥⎢ 1 ⎥=⎢ 1 ⎥+⎢ ⎢

⎥,
⎣ . . . ⎦⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦
j+1
−2r H2 uj+1 n u j
n 2rΔxg 2 + f j+1
n Δt

where

H1 = 1 + 2r(1 + h1 Δx), H2 = 1 + 2r(1 + h2 Δx).

The previous system solves the Robin problem. In matrix notation, it is


written as

Buj+1 = uj + bj+1 ,

uj+1 = Auj + aj+1 , (A = B −1 , aj+1 = B −1 bj+1 ). (2.3.70)

An application for this problem is suggested in Exercise 2.4.27.


Alternatively, the Robin problem can be solved by using the forward
and backward approximations for the derivatives in Formulas (2.3.64). We
get two equations

uj1 − uj0 ujn − ujn−1


− + h1 uj0 = g1j , + h2 ujn = g2j , (2.3.71)
Δx Δx
that solve the Robin problem with less accuracy.

2.3.6 Crank–Nicolson Method


Consider the Explicit Euler Method and Implicit Euler Method for the heat
equation

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


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 85

The Finite Difference Method 85

0 x L

Figure 2.3.10. Crank–Nicolson Method.

rewritten for convenience


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

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


i
− α i+1 i i−1
= fij+1 .
Δt (Δx)2
Summing the two, the previous equations yields

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 ).
Δt 2(Δx)2 2
(2.3.73)

This is a new method, named the Crank13 –Nicolson14 Method. (2.3.73). This
method is even more accurate than the other two, since the time derivative
approximation can be considered a central approximation. To understand
this, consider Eq. (2.3.72) on the point (i, j + 1/2) of the space-time mesh
j+1/2 j+1/2 j+1/2
(Ut )i − α(Uxx )i = Fi ,

and replace the space derivative and source term with the average on the
points (i, j) and (i, j + 1), see Fig. 2.3.10,
j+1/2 α 1
(Ut )i − [(Uxx )ji + (Uxx )j+1
i ] = (Fij + Fij+1 ).
2 2

13 John Crank, a British scientist, 1916–2006. He was also a mathematical physicist. His
scientific research centered on numerical solutions of partial differential equations. He
worked at the Courtaulds Fundamental Research Laboratory.
14 Phyllis Lockett Nicolson, a British scientist, 1917–1968. She worked on numerical

solutions of partial differential equations, with special attention paid to stability analysis.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 86

86 Matlab for Engineering

Using the central approximation for all derivatives, we get Formula


(2.3.73).
Lastly,
−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 , (2.3.74)

where r = αΔt/(Δx)2 and Gji = Δt(fij + fij+1 ). The Crank–Nicolson


Method is implicit. Furthermore, it is unconditionally stable, as shown in
Sec. 2.3.7.
Consider the Dirichlet problem for Eq. (2.3.72) and assign the following
boundary conditions

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

In this case, Formula (2.3.74) can be arranged in matrix form as follows


⎡ ⎤ ⎡ j+1 ⎤
2(1 + r) −r u1
⎢ ⎥ ⎢ j+1 ⎥
⎢ −r 2(1 + r) −r ⎥ ⎢ u2 ⎥
⎢ ⎥⎢ ⎥
⎣ . . . ⎦⎣ · ⎦
−r 2(1 + r) uj+1
n−1

⎡ ⎤ ⎡
⎤⎡ ⎤
2(1 − r) r uj1 r(g1j + g1j+1 ) + Gj1
⎢ ⎥⎢ j ⎥ ⎢ ⎥
⎢ r 2(1 − r) r ⎥⎢ u2 ⎥ ⎢ Gj2 ⎥
=⎢ ⎥⎢ ⎥+⎢ ⎥,
⎣ . . . ⎦⎣ · ⎦ ⎣ · ⎦
j j j+1 j
r 2(1 − r) un−1 r(g2 + g2 ) + Gn−1

Buj+1 = Cuj + bj ,

uj+1 = Auj + aj , (A = B −1 C, aj = B −1 bj ). (2.3.76)

Formula (2.3.76) solves the Dirichlet problem. Indeed, aj is a known vector


depending on boundary conditions, a source term and matrix B. When the
initial conditions are given, the computational process can start and Eq.
(2.3.76) provides the solution uj ∀ j.

Example 2.3.15 The example presents a function that applies the


Crank–Nicolson Method to solve the Dirichlet problem for the heat
equation. A way to call the function is suggested in Exercise 2.4.28.

function u = crank(alpha, L, T, nx, nt, phi, g1, g2)


% This is the function file crank.m.
% Crank–Nicolson Method is applied to solve the Dirichlet problem:
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 87

The Finite Difference Method 87

% Ut = alpha*Uxx, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).


% The input arguments are: thermal diffusivity, length of the solid, final time,
% number of points on the space-time grid, initial-boundary conditions.
% The function returns a vector with the solution at the final time.
% Check data
if any([alpha L T nx-2] <= 0)
error(’Check alpha, L, T, nx, nt’)
end
% Initialization

dx = L/nx; dt = T/nt; r = alpha*dt/dx2;


x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = phi(x’);
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)];
C = spdiags(CC, -1:1, nx-1,nx-1);
b = zeros(nx-1,1);
% Crank–Nicolson Method
for j = 2:nt+1
b(1) = r*(g1(t(j-1)) + g1(t(j))); b(nx-1) = r*(g2(t(j-1)) + g2(t(j)));
u(2:nx) = B\(C*u(2:nx)+b);
u(1) = g1(t(j)); u(nx+1) = g2(t(j));
end
u = u’;
end
Let us apply the Crank–Nicolson Method to the Robin problem where
the following boundary conditions are assigned

−Ux (0, t) + h1 U (0, t) = g1 (t), Ux (L, t) + h2 U (L, t) = g2 (t). (2.3.77)

Consider the first condition and apply the central approximation for t = tj
and t = tj+1

uj1 − uj−1 uj+1 − uj+1


− + h1 uj0 = g1j , − 1 −1
+ h1 uj+1
0 = g1j+1 .
2Δx 2Δx
Summing these formulas yields

uj−1 + uj+1 j j+1


−1 − u1 − u1 + 2Δxh1 (uj0 + uj+1 j j+1
0 ) = 2Δx(g1 + g1 ). (2.3.78)
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 88

88 Matlab for Engineering

The ghost sum uj−1 + uj+1


−1 can be eliminated by considering (2.3.74) for
i=0

r(uj−1 + uj+1 j+1


−1 ) = 2(1 + r)u0 − 2(1 − r)uj0 − r(uj1 + uj+1 j
1 ) − G0 . (2.3.79)

Solving (2.3.78) with respect to uj−1 + uj+1


−1 and the result substituted into
(2.3.79) yields

(1 + r + rΔxh1 )uj+1
0 − ruj+1
1

= ruj1 + (1 − r − rΔxh1 )uj0 + rΔx(g1j + g1j+1 ) + Gj0 /2. (2.3.80)

A similar reasoning for the second boundary condition leads to

(1 + r + rΔxh2 )uj+1
n − ruj+1
n−1

= rujn−1 + (1 − r − rΔxh2 )ujn + rΔx(g2j + g2j+1 ) + Gjn /2. (2.3.81)

From Formulas (2.3.80)–(2.3.81), it follows that

(1+H1 )uj+1
0 −ruj+1
1 = ruj1 +(1−H1 )uj0 +rΔx(g1j +g1j+1 )+Gj0 /2, (2.3.82)

(1 + H2 )uj+1
n − ruj+1 j j j j+1
n−1 = run−1 + (1 − H2 )un + rΔx(g2 + g2 ) + Gjn /2,
(2.3.83)
where

Hi = r + rΔxhi , i = 1, 2.

Equations (2.3.82), (2.3.83) and (2.3.74) can be arranged in matrix form as


follows
⎡ ⎤ ⎡ j+1 ⎤
1 + H1 −r u0
⎢ ⎥ ⎢ j+1 ⎥
⎢ −r 2(1 + r) −r ⎥ ⎢ u1 ⎥
⎢ ⎥⎢ ⎥
⎣ . . . ⎦⎣ · ⎦
−r 1 + H2 uj+1
n

⎡ ⎤⎡ j ⎤ ⎡ ⎤
1 − H1 r u0 rΔx(g1j + g1j+1 ) + Gj0 /2
⎢ ⎥⎢ j ⎥ ⎢ ⎥
⎢ 2(1 − r) ⎥⎢u1 ⎥ ⎢ Gj1 ⎥
=⎢ r r
⎥⎢ ⎥+⎢ ⎥,
⎣ . . . ⎦⎣ · ⎦ ⎣ · ⎦
j j j+1 j
r 1 − H2 u n rΔx(g2 + g2 ) + Gn /2

Buj+1 = Cuj + bj,j+1 ,

uj+1 = Auj + aj,j+1 , (A = B −1 C, aj,j+1 = B −1 bj,j+1 ). (2.3.84)


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 89

The Finite Difference Method 89

Formula (2.3.84) solves the Robin problem. Finally, note that boundary
Conditions (2.3.77) can be also approximated with the less accurate
formulas
uj1 − uj0 ujn − ujn−1
− + h1 uj0 = g1j , + h2 ujn = g2j . (2.3.85)
Δx Δx

2.3.7 Von Neumann Stability Criterium


Von Neumann15 approach to stability analysis is based on the assumption
that the solution of a numerical method can be expressed by a finite Fourier
series
n−1
 √
uji = bp ξpj eIpπiΔx , (I = −1). (2.3.86)
p=−(n−1)

For example, this result for the Explicit Euler Method is shown in Exercise
2.4.29. If each term of the Fourier series is controlled by the initial condition,
the numerical solution is controlled as well and the method is stable.
Therefore, following the Von Neumann approach, it is sufficient to consider
only one term of the series

uji = ξ j eIβiΔx , (2.3.87)

and investigate its stability. The error behavior on time depends only on
the factor ξ. If it is

|ξ| ≤ 1, (2.3.88)

the error does not amplify and the method is stable. For this reason,
ξ is named the amplification factor. Formula (2.3.88) expresses the Von
Neumann criterium for the numerical stability. Its application is relatively
easy — it is probably the most used tool for the stability analysis of
numerical methods for partial differential equations. Condition (2.3.88) is
very strong. A less restrictive condition is the following

|ξ| ≤ 1 + CΔt, (2.3.89)

where the positive constant C is independent of Δt and Δx.

15 János Von Neumann, a Hungarian-American scientist, 1903–1957. He was Professor at

Princeton University. He worked on Numerical Analysis and Quantum Mechanics.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 90

90 Matlab for Engineering

Finally, let us recall the Euler formulas that are frequently applied in
the following examples

eIz = cos z + I sin z, e−Iz = cos z − I sin z, (2.3.90)

cos z = (eIz + e−Iz )/2, sin z = (eIz − e−Iz )/(2I). (2.3.91)

Example 2.3.16 As the first application, consider the Explicit Euler


Method

uj+1
i = r(uji+1 + uji−1 ) + (1 − 2r)uji .

Substituting (2.3.87) into the the previous equation yields

ξ j+1 eIβiΔx = ξ j eIβiΔx [1 − 2r + r(e−IβΔx + eIβΔx)],

ξ = 1 − 2r + 2r cos(βΔx) = 1 − 2r(1 − cos βΔx) = 1 − 4r sin2 (βΔx/2).

Therefore, the Von Neumann condition |ξ| ≤ 1 is written

−1 ≤ 1 − 4r sin2 (βΔx/2) ≤ 1.

The second inequality is always satisfied. The first is equivalent to

2r sin2 (βΔx/2) ≤ 1,

that is satisfied if

r ≤ 1/2.

Of course, this condition is the same as that found in Sec. 2.3.2.

Example 2.3.17 Consider the Implicit Euler Method

−ruj+1 j+1
i−1 + (1 + 2r)ui − ruj+1 j
i+1 = ui .

Substituting uji = ξ j eIβiΔx in the previous equation yields

−ξre−IβΔx + ξ(1 + 2r) − ξreIβΔx = 1,

ξ[1 + 2r − 2r cos(βΔx)] = 1,

ξ = 1/(1 + 4r sin2 (βΔx/2)).


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 91

The Finite Difference Method 91

The Von Neumann condition |ξ| ≤ 1 is satisfied for any value of r. The
Implicit Euler Method is unconditionally stable, i.e., stable for any value
of r. However, since the method is accurate as O((Δx)2 ) + O(Δt), the error
grows with Δx and much more with Δt.

Example 2.3.18 Consider the Crank–Nicolson Method

−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 .

Substituting uji = ξ j eIβiΔx into the previous equation yields

ξ[1 + r − r cos(βΔx)] = 1 − r + r cos(βΔx),

ξ = [1 − 2r sin2 (βΔx/2)]/[1 + 2r sin2 (βΔx/2)].

The Von Neumann condition |ξ| ≤ 1 is satisfied for any value of r. The
Crank–Nicolson Method is unconditionally stable.

Example 2.3.19 Consider the equation

Ut + λU − αUxx = 0, λ = constant. (2.3.92)

Following the Explicit Euler Method, we get

uj+1 − uji α
i
+ λuji − (uj − 2uji + uji−1 ) = 0,
Δt (Δx)2 i+1

uj+1
i = (1 − 2r)uji − λΔtuji + r(uji+1 + uji−1 ), (2.3.93)

where r = αΔt/(Δx)2 . Use the Von Neumann criterium for the stability
analysis. Substituting uji = ξ j eIβiΔx into the previous equation yields

ξ = 1 − 2r + 2r cos(βΔx) − λΔt = 1 − 2r(1 − cos βΔx) − λΔt,

ξ = 1 − 4r sin2 (βΔx/2) − λΔt.

If r ≤ 1/2, then it is |1 − 4r sin2 (βΔx/2)| ≤ 1, as shown in Example 2.3.16.


Therefore, from the last formula, we get

|ξ| ≤ 1 + |λ|Δt.

Condition (2.3.89) is satisfied and Method (2.3.92) is conditionally stable.


September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 92

92 Matlab for Engineering

Note that the change of unknown function

W = U exp(λt) ⇔ U = W exp(−λt), (2.3.94)

simplifies Eq. (2.3.92) to the heat equation

Wt − αWxx = 0. (2.3.95)

Example 2.3.20 Formulas (2.3.94)–(2.3.95) suggest that a function for


Eq. (2.3.92) can be obtained easily from euler e in Example 2.3.1.
function u = euler el(alpha, L, T, nx, phi, g1, g2, lambda)
% This is the function file euler el.m.
% Explicit Euler Method is applied to solve the Dirichlet problem:
% Ut+lambda*U=alpha*Uxx, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t)=G2(t).
% The input arguments are: thermal diffusivity, length of the solid, final time,
% number of points on the space grid, initial-boundary conditions, parameter
% lambda. The function returns a vector with the solution at the final time.
Same code as euler e
% Explicit Euler Method
for j=2:nt+1
u(2:nx)=(1-2*r)*u(2:nx)+r*(u(1:nx-1)+u(3:nx+1));
u(1) = g1(t(j))*exp(lambda*t(j)); u(nx+1) = g2(t(j))*exp(lambda*t(j));
end
u = u*exp(-lambda*T);
end
A way to apply euler el is illustrated in the following listing where the simple
initial-boundary conditions are considered

U (x, 0) = x2 , U (0, t) = 2t exp(−λt), U (L, t) = (L2 + 2t) exp(−λt). (2.3.96)

function u = euler el ex1


% This is the function file euler el ex1.m
% Euler el function is called to solve the special Dirichlet problem:
% Ut + lambda*U = Uxx, U(x,0) = x2 ,
% U(0,t) = 2*t*exp(-lambda*t), U(L,t) = (L2 +2*t)*exp(-lambda*t).
% The approximating solution is plotted together with the exact solution:
% U = (x2 +2*T)*exp(-lambda*T). The error is evaluated.
alpha = 1; L = 1; T = 1; nx = 10; lambda = -2;
phi = @(x) x.2 ;
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 93

The Finite Difference Method 93

g1 = @(t) 2*t*exp(-lambda*t);
g2 = @(t) (L2 +2*t)*exp(-lambda*t);
u = euler el(alpha, L, T, nx, phi, g1, g2, lambda);
x = linspace(0,L,nx+1); U = (x.2 +2*T)*exp(-lambda*T);
fprintf( ’Maximum error = %g\n’,max(abs(U-u)))
plot(x,u,’k*’,x,U,’r’);
xlabel(’x’); ylabel(’U’);
legend(’Euler’,’Exact’);
title([’time = ’,num2str(T)]);
end
Another application is suggested in Exercise 2.4.30.

2.4 Exercises
Exercise 2.4.1 Execute forward ex1.m twice. First, with nx = 32 and
then with nx = 64, which is double the amount of the previous number.
Consider the ratio of the second error to the first and explain the result.

Exercise 2.4.2 Derive Formula (2.1.7) related to the backward approx-


imation.
Hint. Consider the Taylor series for f (xi − Δx).

Exercise 2.4.3 Write a function that returns the backward approxima-


tion of derivatives.
Answer.
function y = backward(u,h)
% This is the function file backward.m.
% It returns the backward approximation of derivatives. Since the backward
% approximation cannot be applied in the first point, the vector length
% returned by the backward function is equal to that of vector u minus 1.
% Example
% a = 0; b = 1; nx = 40; x = linspace(a,b,nx+1); dx = (b - a)/nx;
% u = x.^2;
% dbu = backward(u,dx)

n = length(u) - 1;
y = (u(2:n+1) - u(1:n))/h;
end
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 94

94 Matlab for Engineering

Exercise 2.4.4 Write a listing, say backward ex1.m, that calls and applies
the backward function.

Exercise 2.4.5 Execute central ex1.m twice. First, with nx = 32 and then
with nx = 64, which is double the amount of the previous number. Consider
the ratio of the second central error on the first and explain the result.

Exercise 2.4.6 Derive three-point approximation Formulas (2.1.9) and


(2.1.10).
Answer. Consider the Taylor series

fi+1 − fi = fi h + fi h2 /2 + O(h3 ), (2.4.1)

fi+2 − fi = fi 2h + fi 2h2 + O(h3 ). (2.4.2)

Subtract (2.4.2) from (2.4.1)×4 and solve the result with respect to fi
4fi+1 − 3fi − fi+2
fi = + O(h2 ).
2h
Hence, Formula (2.1.9). Similarly, Formula (2.1.10) is derived.

Exercise 2.4.7 Derive the four-point forward and backward approxima-


tion formulas
−fi+2 + 6fi+1 − 3fi − 2fi−1 fi−2 − 6fi−1 + 3fi + 2fi+1
fi ≈ , fi ≈ ,
6h 6h
with an error of order h3 .

Exercise 2.4.8 Obtain the numerical values of gradient in the first


and last points from central ex2.m. Try to understand the approximating
derivatives used to get those values.

Exercise 2.4.9 Derive Formula (2.1.12)1 related to the forward approx-


imation of ut .
Answer. From Taylor’s series

u(xi , tj + Δt) = u(xi , tj ) + ut (xi , tj )Δt + O((Δt)2 ),

uj+1
i = uji + (ut )ji Δt + O((Δt)2 ),

uj+1 − uji
(ut )ji = i
+ O(Δt).
Δt
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 95

The Finite Difference Method 95

Exercise 2.4.10 Derive forward and backward three-point approxima-


tion formulas for ux and ut .

Exercise 2.4.11 Derive Formula (2.1.19) for the central approximation


of uxx .

Exercise 2.4.12 Derive Formulas (2.1.21)-(2.1.22) for the backward and


central approximations of uxt .

Exercise 2.4.13 Calculate the gradient of the function

u(x1 , x2 , x3 ) = x3 . (2.4.3)

Answer. ∇u = (0, 0, 1). ∇u is perpendicular to the surface u = 0 and


directed towards the region where u > 0.

Exercise 2.4.14 Calculate the gradient of the function

u = x21 + x22 + x23 − R2 . (2.4.4)

Answer. ∇u = (2x1 , 2x2 , 2x3 ). ∇u is perpendicular to the surface u = 0. In


addition, it is directed towards the region where u > 0.

Exercise 2.4.15 The function sin(x) is continuous on B = [0, 2π] and


it is
 2π
sin(x) dx = 0.
0

Explain why the previous result does not imply sin(x) = 0 on [0, 2π].

Exercise 2.4.16 Explain why ∂u/∂n is related to the heat flux.

Exercise 2.4.17 Show the identity ∇ · (uv) = v · ∇u + u∇ · v used to


derive Formula (2.2.19) from (2.2.18).

∂(uv1 ) ∂(uv2 ) ∂(uv3 )


Answer. ∇ · (uv) = + +
∂x1 ∂x2 ∂x3

∂u ∂u ∂u ∂v ∂v ∂v
= v + v + v + u 1 + u 2 + u 3 = ∇u · v + u∇ · v.
∂x1 1 ∂x2 2 ∂x3 3 ∂x1 ∂x2 ∂x3
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 96

96 Matlab for Engineering

Exercise 2.4.18 Use the logical function any to write the following code
line of the euler e function in a compact manner.
if alpha <= 0 || L <= 0 || T <= 0 || nx <= 2

Exercise 2.4.19 Write a function, say euler e ex2, that calls euler e to
solve the Dirichlet problem
Ut − Uxx = 2t − 6x, (2.4.5)

U (x, 0) = x3 , U (0, t) = t2 , U (L, t) = L3 + t2 . (2.4.6)


Compare the results with the exact solution: U = x3 + t2 .

Exercise 2.4.20 The norm ||v||∞ is calculated in Matlab with the


norm(v,Inf) command. Use it after creating a vector. Also, try these two
commands: norm(v,2) and norm(v,1).

Exercise 2.4.21 Modify the euler en function by considering the source


term.

Exercise 2.4.22 Modify the euler en function so that it can return a


matrix with the approximating solution during the whole computational
process.

Exercise 2.4.23 Call the euler enm function presented in Example 2.3.8.
Hint. Consider the euler en ex1 function in Example 2.3.7.

Exercise 2.4.24 Consider Example 2.3.10 and the graph of the solution
in Fig. 2.3.5. Explain why the solution near x = L behaves according to
the Neumann boundary condition.

Exercise 2.4.25 Write a function, say euler end, for the Neumann-
Dirichlet problem and apply it.

Exercise 2.4.26 Write the matrix form for the Dirichlet–Neumann


problem and Neumann–Dirichlet problem.

Exercise 2.4.27 Write a function that solves, the Robin Problem (2.3.70)
for the Implicit Euler Method.

Exercise 2.4.28 Write a listing that calls and applies the crank function
presented in Example 2.3.15.
Hint. Consider Example 2.3.14.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 97

The Finite Difference Method 97

Exercise 2.4.29 Show that the solution of the following Dirichlet


problem for the Explicit Euler Method

uj+1
i = r(uji+1 + uji−1 ) + (1 − 2r)uji , i = 1, . . . , n − 1, (2.4.7)

u0i = ϕ(xi ) = ϕi , i = 0, . . . , n, nΔx = L = 1, (2.4.8)

uj0 = 0, ujn = 0, j = 1, . . . , m, (2.4.9)

can be expressed by a finite Fourier series.


Answer. Consider the solution of System (2.4.7)–(2.4.9) in the following
form
n−1

uji = ap (ξp )j sin(pπiΔx), i = 0, . . . , n, (2.4.10)
p=1

where ap , p = 1, . . . , n − 1, are unknown coefficients, and


pπΔx
ξp = 1 − 4r sin2 . (2.4.11)
2
Imposing that (2.4.10) satisfies initial conditions, (2.4.8) yields
n−1

ap sin(pπiΔx) = ϕi , i = 1, . . . , n − 1.
p=1

Solving the previous system gives the coefficients ap . In addition, boundary


Conditions (2.4.9) are satisfied; as for i = 0 and i = n from (2.4.10), it
follows uj0 = ujn = 0. Now, use the Euler Formulas (2.3.90)–(2.3.91) and get
 
sin(pπiΔx) = eIpπiΔx − e−IpπiΔx /(2I).

Substituting the previous result into (2.4.10) yields


n−1
 n−1

uji = (ap /2I)ξpj eIpπiΔx − (ap /2I)ξpj e−IpπiΔx ,
p=1 p=1

n−1
 −1

uji = (ap /2I)ξpj eIpπiΔx − (a−p /2I)ξpj eIpπiΔx .
p=1 p=−(n−1)

Hence,
n−1

uji = bp ξpj eIpπiΔx , (2.4.12)
p=−(n−1)
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 98

98 Matlab for Engineering

where
bp = ap /2I if p > 0, b0 = 0, bp = −a−p /2I if p < 0.
While Formula (2.4.12) shows that uji is expressed by a finite Fourier series,
uji as the solution of (2.4.7) remains to be shown. Preliminary, note that
pπΔx
ξp = 1 − 4r sin2 = 1 − 2r[1 − cos(pπΔx)]
2
 
ξp = 1 − 2r + 2r cos(pπΔx) = 1 − 2r + r eIpπΔx + e−IpπΔx . (2.4.13)
Now, from (2.4.12)–(2.4.13), we get
n−1

uj+1
i = bp ξp ξpj eIpπiΔx
p=−(n−1)

n−1
   
= bp ξpj eIpπiΔx 1 − 2r + r eIpπΔx + e−IpπΔx
p=−(n−1)

= (1 − 2r) uji + r(uji+1 + uji−1 ),


that is the desired result.

Exercise 2.4.30 Write a function for Method (2.3.93).


Hint. Consider the euler e function, see Example 2.3.1.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 99

Chapter 3

Diffusion and Convection

The convection-diffusion physical process is very important since it is


involved in many engineering problems. The convection-diffusion equation,
named the advection-diffusion equation in some contexts, was derived in
Chap. 2. This equation models two physical processes: the diffusive process,
governed by the diffusion equation, and the convection (or advection)
process, governed by the convection (or advection) equation Crank (1979).
The Finite Difference Method for the convection-diffusion equation is
presented in the first section of this chapter Lapidus and Pinter (1982);
Mitchell and Griffiths (1995); Necati Ozisik (1994).
The second section introduces the Method of Lines. This method is
a semi-discrete numerical method for the integration of partial differential
equations, where only some variables are discretrized. The Method of Lines
is illustrated with engineering applications.
Finally, some sections are devoted to Matlab functions that can help
to save and load data and figures.

3.1 Convection-diffusion Equation


3.1.1 Upwind Method
Consider the convection-diffusion equation
Ut + vUx − αUxx = 0, 0 < x < L, 0 < t ≤ T, (3.1.1)
introduced in Sec. 2.2.1. The same equation is named the advection-
diffusion equation, depending on the context. Equation (3.1.1) models two
physical processes: diffusion and advection. The first is governed by the
parabolic partial differential equation
Ut − αUxx = 0,

99
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 100

100 Matlab for Engineering

and the second by the hyperbolic partial differential equation

Ut + vUx = 0.

The last equation, the advection equation, or convection equation, governs


the process of the transport of matter in the absence of diffusion. In this
context, the parameter v represents velocity.
The prevailing process depends on the parameters v and α. More
precisely, it depends on a suitable ratio of the two parameters: the Péclet1
number
P = vL/α.

To understand why, a change of variables


ξ = ξ(x, t), x = x(ξ, τ ),

τ = τ (x, t), t = t(ξ, τ ),
must be performed that converts Eq. (3.1.1) to a non-dimensional form. As
shown in the worked Exercise 3.4.1, a special change of variables converts
Eq. (3.1.1) to
Wτ + P Wξ = Wξξ .

This equation emphasizes that the prevailing process depends on |P |. A


high value of |P | indicates that advection is the prevailing process. On the
contrary, a low value of |P | indicates that diffusion is the prevailing process.
We now introduce the Upwind Method for Eq. (3.1.1). If the parameter
v is positive, then by using the forward approximation for Ut , the backward
for Ux and the central for Uxx yields

Uij+1 − Uij U j − Ui−1


j
U j − 2Uij + Ui−1
j
+v i = α i+1 + O(Δt + Δx).
Δt Δx (Δx)2
Therefore, the finite difference method
uj+1 − uji uj − uji−1 uj − 2uji + uji−1
i
+v i = α i+1 (3.1.2)
Δt Δx (Δx)2
is consistent with Eq. (3.1.1). With the positions r = Δt/(Δx)2 and s =
Δt/Δx, it follows from (3.1.2) that

uj+1
i = (rα + sv)uji−1 + (1 − 2rα − sv)uji + rαuji+1 . (3.1.3)

1 Jean Claude Eugène Péclet, a French scientist, 1793–1857. He was Professor at the

Collège de Marseille and at the École Normale Supérièure in Paris.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 101

Diffusion and Convection 101

Method (3.1.3) is conditionally stable. Indeed, it is stable under the


hypothesis
2rα + sv ≤ 1, (3.1.4)

as proved later on. If the parameter v in (3.1.1) is negative, then using the
forward approximation for Ut and Ux , and the central for Uxx yields

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


i
+ v i+1 = α i+1 ,
Δt Δx (Δx)2

uj+1
i = rαuji−1 + (1 − 2rα + sv)uji + (rα − sv)uji+1 . (3.1.5)

Method (3.1.5) is conditionally stable, as it is stable under the hypothesis

2rα − sv ≤ 1. (3.1.6)

Equations (3.1.3) and (3.1.5) are named the Upwind Method. Setting
 
rα + s|v| if v ≥ 0, rα if v ≥ 0,
p= q= (3.1.7)
rα if v < 0, rα + s|v| if v < 0,
Eqs. (3.1.3) and (3.1.5) are grouped into a single equation

uj+1
i = puji−1 + (1 − p − q)uji + quji+1 . (3.1.8)

Let us show that the Upwind Method (3.1.8) is stable under the hypothesis

p+q ≤1 ⇔ 2rα + s|v| ≤ 1. (3.1.9)

Indeed,

|uj+1
i | ≤ p||uj ||∞ + (1 − p − q)||uj ||∞ + q||uj ||∞ ≤ ||uj ||∞ ,

and therefore,

||uj+1 ||∞ ≤ ||uj ||∞ · · · ≤ ||u0 ||∞ .

The stability Condition (3.1.9) is the same as (3.1.4) if v ≥ 0 and (3.1.6) if


v ≤ 0. See Exercise 3.4.2.

Example 3.1.1 A function is presented that applies Method (3.1.8) to


solve the Dirichlet problem for Eq. (3.1.1).

function u = upwind(alpha, v, L, T, nx, phi, g1, g2)


% This is the function file upwind.m.
% Upwind Method is applied to solve the Dirichlet problem:
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 102

102 Matlab for Engineering

% Ut +v Ux = alpha Uxx, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).


% The input arguments are: thermal diffusivity, velocity v, length of the solid,
% final time, number of points on the space grid, initial-boundary conditions.
% The function returns a vector with the solution at the final time.
% Check data
if any([alpha L T nx-2] <= 0)
error(’Check alpha, L, T, nx’)
end
% Stability
nt = 150; st = 1; dx = L/nx;
while 2*alpha*T/nt/dx2 + T/nt/dx*abs(v) > st
nt = nt + 1;
end
% Initialization
dt = T/nt; r = dt/dx2 ; s = dt/dx;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u = feval(phi,x);
if v >= 0
p = alpha*r+s*v; q = alpha*r;
else
p = alpha*r; q = alpha*r-s*v;
end
% Upwind Method
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);
u(1) = g1(t(j)); u(end) = g2(t(j));
end
end

Example 3.1.2 The listing below illustrates a way to call the upwind
function. It considers the special Dirichlet problem
Ut + vUx − αUxx = 0, 0 < x < L, 0 < t ≤ T, (3.1.10)

0 if x ∈ [0, x1 [ ∪ ]x2 , L],
U (x, 0) = (3.1.11)
k if x ∈ [x1 , x2 ],
U (0, t) = 0, U (L, t) = 0, 0 < t ≤ T. (3.1.12)
The graph of the numerical solution is shown in Fig. 3.1.1.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 103

Diffusion and Convection 103

1
t=2
0.9 t=0

0.8

0.7

0.6
U
0.5

0.4

0.3

0.2

0.1

0
0 10 20 30 40 50 60 70 80 90 100
x

Figure 3.1.1. Graph of the numerical solution to Problem (3.1.10) to (3.1.12).

function u = upwind ex1


% This is the function file upwind ex1.m.
% Upwind function is called to solve the special Dirichlet problem:
% Ut + v Ux = alpha Uxx, U(0,t) = 0, U(L,t) = 0,
% U(x,0) = 0, if x < x1,
% U(x,0) = k, if x1 <= x <= x2,
% U(x,0) = 0, if x > x2.
% The approximating solution is plotted.
alpha = .1; v = 10; L = 100; T = 2; nx = 150;
x = linspace(0,L,nx+1);
i1 = 73; i2 = 79; x1 = x(i1); x2 = x(i2); k = 1;
phi = @(x) k*(x >= x1).*(x <= x2);
g1 = @(t) 0*t;
g2 = @(t) 0*t;
U = feval(phi,x);
nt = 30; t = linspace(0,T,nt+1);
for j = 2:nt+1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 104

104 Matlab for Engineering

u = upwind(alpha, v, L, t(j), nx, phi, g1, g2);


plot(x,u,’k*:’,x,U,’k’,’LineWidth’,.1);
xlabel(’x’); ylabel(’U’);
legend([’t = ’,num2str(t(j))],’t = 0’,’Location’,’NorthWest’);
pause(.01);
end
end

Example 3.1.3 Another application is provided below that considers the


Dirichlet problem

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

U (x, 0) = sin(πx), 0 ≤ x ≤ L, (3.1.14)



U (0, t) = sin(−πvt) exp(−απ 2 t),
0 < t ≤ T. (3.1.15)
U (L, t) = sin(π(L − vt)) exp(−απ 2 t),

The Upwind function is called and Problem (3.1.13) to (3.1.15) is solved.


The graph of the numerical solution is shown in Fig. 3.1.2, together with
the analytical solution

U (x, t) = sin(π(x − vt)) exp(−απ 2 t).

function u = upwind ex2


% This is the function file upwind ex2.m.
% Upwind function is called to solve the special Dirichlet problem:
% Ut +v Ux = alpha Uxx, U(x,0) = sin(pi*x),
% U(0,t) = sin(-pi*v*t)*exp(-pi2 *t),
% U(L,t) = sin(pi*(L-v*t))*exp(-pi2 *t).
% The approximating solution is plotted. The error is evaluated.
alpha = 1; v = .2; L = 1; T = 1; nx = 40;
phi = @(x) sin(pi*x);
g1 = @(t) sin(-pi*v*t)*exp(-pi2*t);
g2 = @(t) sin(pi*(L-v*t))*exp(-pi2 *t);
u = upwind(alpha, v, L, T, nx, phi, g1, g2);
x = linspace(0,L,nx+1);
U = sin(pi*(x - v*T))*exp(-pi2 *T);
plot(x,u,’k’,x,U,’r*’,’LineWidth’,.1);
xlabel(’x’); ylabel(’U’); title([’t = ’,num2str(T)]);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 105

Diffusion and Convection 105

10-5 t=1
6

2
U
1

-1

-2

-3 Upwind
Exact
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x

Figure 3.1.2. Graph of the numerical solution of Problem (3.1.13) to (3.1.15).

legend(’Upwind’,’Exact’,’Location’,’Best’);
fprintf( ’Maximum error = %g\n’,max(abs(U - u)))
end

Remark 3.1.1 The Upwind Method simplifies to the Explicit Euler


Method when v = 0. The stability Condition (3.1.9) simplifies to the stability
condition of that method as well. In addition, the upwind function simplifies
to the euler e function (Sec. 2.3.1) with the consequence that the upwind
function works even when it is called with v = 0, as the reader is invited
to try. Now, let us investigate what happens when α goes to zero. Equation
(3.1.1) simplifies to the advection equation
Ut + vUx = 0,

and stability Condition (3.1.9) to


s|v| ≤ 1.

In Sec. 3.1.3, it will be proved that the Upwind Method for the advection
equation is stable under the condition above. Nevertheless, the situation
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 106

106 Matlab for Engineering

is completely different. Indeed, the advection equation is a first-order


equation that requires one boundary condition, whereas the advection-
diffusion equation is a second-order equation that requires two boundary
conditions.
As noted, the Upwind Method simplifies to the Explicit Euler Method
when v = 0. Now, the upwind version of the Implicit Euler Method is
presented. If v > 0, using the backward approximation for Ut and Ux , and
the central for Uxx in Eq. (3.1.1) then 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 usual positions r = Δt/(Δx)2 , s = Δt/Δx, we get

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


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

If v < 0, using the forward approximation for Ux then yields

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


i
+ v i+1 i
= α i+1 i i−1
,
Δt Δx (Δx)2

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

Methods (3.1.16) and (3.1.17) are unconditionally stable. See Exercise 3.4.3.
Moreover, they can be combined into a single formula. See Exercise 3.4.4.
Let us present the upwind version of the Crank–Nicolson Method. Of
course, the method depends on the sign of v. If v > 0, we get
 j+1 j+1

uj+1
i − uji v ui − ui−1 uji − uji−1
+ +
Δt 2 Δx Δx

 j+1 j+1 j+1



α ui+1 − 2ui + ui−1 uji+1 − 2uji + uji−1
= + .
2 (Δx)2 (Δx)2

Hence,

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


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

= (rα + sv)uji−1 + (2 − 2rα − sv)uji + rαuji+1 . (3.1.18)


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 107

Diffusion and Convection 107

Method (3.1.18) is unconditionally stable. See Exercise 3.4.5. Similarly,


when v < 0, we get

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

= rαuji−1 + (2 − 2rα + sv)uji + (rα − vs)uji+1 . (3.1.19)

Method (3.1.19) is unconditionally stable. See Exercise 3.4.6.

3.1.2 Other Finite Difference Methods for the


Convection-Diffusion Equation
This section presents some finite difference methods that use a different
approach for the approximation of the derivative Ux in Eq. (3.1.1). The
first method is derived from the Explicit Euler Method and uses the central
approximation for Ux . We get

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


i
+ v i+1 = α i+1 ,
Δt 2Δx (Δx)2
uj+1
i = (rα + sv/2)uji−1 + (1 − 2rα)uji + (rα − sv/2)uji+1 , (3.1.20)

where
r = Δt/(Δx)2 , s = Δt/Δx. (3.1.21)

Method (3.1.20) could be named the Central Explicit Euler Method. It is


stable under the hypothesis
s|v| ≤ 2rα, 2rα ≤ 1. (3.1.22)

Indeed, if conditions in (3.1.22) are satisfied, the terms in parentheses are


non-negative and

|uj+1
i | ≤ (rα+sv/2)||uj ||∞ +(1−2rα)||uj ||∞ +(rα−sv/2)||uj ||∞ ≤ ||uj ||∞ ,
||uj+1 ||∞ ≤ ||uj ||∞ · · · ≤ ||u0 ||∞ .

The second method is derived from the Implicit Euler Method and
again uses the central approximation for Ux . We get

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


i
+ v i+1 i−1
= α i+1 i i−1
,
Δt 2Δx (Δx)2
−(rα + vs/2)uj+1 j+1
i−1 + (1 + 2rα)ui − (rα − vs/2)uj+1 j
i+1 = ui , (3.1.23)

where Notations (3.1.21) were used. Method (3.1.23) could be named the
Central Implicit Euler Method. It is unconditionally stable, as proved by
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 108

108 Matlab for Engineering

the Von Neumann criterium. Indeed, substituting uji = ξ j eIβiΔx in (3.1.23)


yields

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

|ξ|2 = 1/{[1 + 4rα sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)} < 1.

Setting p = rα + sv/2 and q = rα − sv/2, Method (3.1.23) is written as

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

and in compact matrix form


⎡ ⎤ ⎡ j+1 ⎤ ⎡ j ⎤ ⎡ j+1 ⎤
1+p+q −q u1 u1 pu0
⎢ ⎥ ⎢ j+1 ⎥ ⎢ j ⎥ ⎢ ⎥
⎢ −p 1 + p + q −q ⎥ ⎢ u2 ⎥ ⎢ u2 ⎥ ⎢ 0 ⎥
⎢ ⎥⎢ ⎥=⎢ ⎥+⎢ ⎥,
⎣ . . . ⎦⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦
−p 1 + p + q uj+1
n−1 ujn−1 quj+1
n

Buj+1 = uj + bj+1 ,

uj+1 = Auj + aj+1 , (A = B −1 , aj+1 = B −1 bj+1 ). (3.1.24)

Example 3.1.4 A function is provided that applies Method (3.1.24) to


solve the Dirichlet problem for Eq. (3.1.1).

function u = central implicit(alpha, v, L, T, nx, phi, g1, g2)


% This is the function file central implicit.m.
% Central Implicit Euler Method is applied to solve the Dirichlet problem:
% Ut + v Ux = alpha Uxx, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t).
% The input arguments are: thermal diffusivity, velocity v, length of the solid,
% final time, number of points on the space grid, initial-boundary conditions.
% The function returns a vector with the solution at the final time.
% Check data
if any([alpha L T nx-2] <= 0)
error(’Check alpha, L, T, nx’)
end
% Initialization
nt = 1000; dx = L/nx; dt = T/nt; r = dt/dx2 ; s = dt/dx;
p = alpha*r+v*s/2; q = alpha*r-v*s/2;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
b = zeros(nx-1,1);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 109

Diffusion and Convection 109

BB = [-p*ones(nx-1,1) (1+p+q)*ones(nx-1,1) -q*ones(nx-1,1)];


B = spdiags(BB, -1:1, nx-1, nx-1);
u = feval(phi,x’);
% Central Implicit Euler Method
for j = 2:nt + 1
b(1) = p*g1(t(j)); b(end) = q*g2(t(j));
u(2:nx) = B\(u(2:nx) + b);
u(1) = g1(t(j)); u(end) = g2(t(j));
end
end

Example 3.1.5 Consider the Dirichlet Problem (3.1.10) to (3.1.12),


already discussed with the Upwind Method in Example 3.1.2. The listing
below illustrates a way to call the central implicit function to solve Problem
(3.1.10) to (3.1.12). The graph of the numerical solution is shown in Fig.
3.1.3. The oscillating behavior is due to the central approximation of Ux .
Oscillations do not grow without bounds as the method is stable. However,
they are undesirable.

1.2

0.8

0.6
t=2
U
t=0
0.4

0.2

-0.2
0 10 20 30 40 50 60 70 80 90 100
x

Figure 3.1.3. Numerical solution of Problem (3.1.10) to (3.1.12).


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 110

110 Matlab for Engineering

function central implicit ex1


% This is the function file central implicit ex1.m.
% Central implicit function is called to solve the special Dirichlet problem:
% Ut +v Ux = alpha Uxx, U(0,t) = 0, U(L,t) = 0,
% U(x,0) = 0, if x < x1,
% U(x,0) = k, if x1 <= x <= x2,
% U(x,0) = 0, if x > x2.
% The approximating solution is plotted.
alpha = 0.1; v = 10; L = 100; T = 2; nx = 150;
x = linspace(0,L,nx+1);
i1 = 73; i2 = 79; x1 = x(i1); x2 = x(i2); k = 1;
phi = @(x) k*(x >= x1).*(x <= x2);
g1 = @(t) 0*t;
g2 = @(t) 0*t;
U = feval(phi,x’);
u = central implicit(alpha, v, L, T, nx, phi, g1, g2);
plot(x,u,’k*:’,x,U,’k’,’LineWidth’,.1);
xlabel(’x’); ylabel(’U’);
legend([’t = ’,num2str(T)],’t = 0’,’Location’,’Best’);
end
The last method of this section is the central version of the Crank–
Nicolson Method for Eq. (3.1.1)

uj+1 − uji v
i
+ (uj+1 − uj+1 j j
i−1 + ui+1 − ui−1 )
Δt 4Δx i+1

α
= (uj+1 − 2uj+1 + uj+1 j j j
i−1 + ui+1 − 2ui + ui−1 ).
2(Δx)2 i+1 i

Hence, by using Notations (3.1.21),

−(rα + sv/2)uj+1 j+1


i−1 + 2(1 + rα)ui − (rα − sv/2)uj+1
i+1

= (rα + sv/2)uji−1 + 2(1 − rα)uji + (rα − sv/2)uji+1 . (3.1.25)

Method (3.1.25) could be named the Central Crank–Nicolson Method. It is


unconditionally stable, as proved by the Von Neumann criterium. Indeed,
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 111

Diffusion and Convection 111

substituting uji = ξ j eIβiΔx in (3.1.25) 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)
Moreover, setting
p = rα + sv/2, q = rα − sv/2, (3.1.26)

Method (3.1.25) is written as

−puj+1 j+1
i−1 + 2(1 + rα)ui − quj+1 j j j
i+1 = pui−1 + 2(1 − rα)ui + qui+1 ,

and in compact matrix form


⎡ ⎤
2(1 + rα) −q  
⎢ ⎥ uj+1
⎢ −p 2(1 + rα) −q ⎥ 1
⎢ ⎥ uj+1
⎣ . . . ⎦ 2

−p 2(1 + rα) ·
uj+1
n−1
⎡ ⎤
2(1 − rα) q ⎡ j ⎤ ⎡ ⎤
⎢ ⎥ u1 p(uj+1
0 + uj0 )
⎢ 2(1 − rα) ⎥
=⎢ p q ⎥⎣ uj2 ⎦ +⎣ 0 ⎦,
⎣ . . . ⎦
· ·
p 2(1 − rα)
ujn−1 q(uj+1
n + ujn )

Buj+1 = Cuj + bj ,
uj+1 = Auj + aj , (A = B −1 C, aj = B −1 bj ).

3.1.3 Advection Equation


Consider the advection equation
Ut + vUx = 0, (3.1.27)
also named the convection equation. The Upwind Method for Eq. (3.1.27)
can be derived from that related to Eq. (3.1.1) as a special case. Therefore,
for v > 0, we get (Fig. 3.1.4)

uj+1 − uji uj − uji−1


i
+v i = 0,
Δt Δx
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 112

112 Matlab for Engineering

t t

x x

Figure 3.1.4. Upwind Method for v > 0 (left) and v < 0 (right).

and for v < 0, it is

uj+1 − uji uj − uji


i
+ v i+1 = 0.
Δt Δx
Setting s = Δt/Δx, the equations above are written as

uj+1
i = (1 − sv)uji + svuji−1 . (3.1.28)

uj+1
i = (1 + sv)uji − svuji+1 . (3.1.29)

Methods (3.1.28) and (3.1.29) are named the FTBS Method (forward in
time, backward in space) and FTFS Method (forward in time, forward in
space), respectively. They are stable under the hypothesis

|v|s ≤ 1. (3.1.30)

Condition (3.1.30) can be proved easily, as suggested in Exercise 3.4.7. Of


course, it follows from the stability condition of the Upwind Method for the
advection-diffusion as a special case.
Consider the following initial-boundary value problem

Ut + vUx = 0, x > 0, 0 < t ≤ T, v > 0, (3.1.31)

U (x, 0) = ϕ(x), x ≥ 0, (3.1.32)

U (0, t) = g(t), 0 < t ≤ T. (3.1.33)

The analytical solution of Problem (3.1.31) to (3.1.33) is suggested in


Exercise 3.4.8.

Example 3.1.6 This example presents a function that applies Method


(3.1.28) to solve Problem (3.1.31) to (3.1.33).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 113

Diffusion and Convection 113

function [u, nt] = ftbs(v, L, T, nx, phi, g)


% This is the function file ftbs.m.
% FTBS Method is applied to solve the Dirichlet problem:
% Ut +v Ux = 0, U(x,0) = phi(x), U(0,t) = G(t).
% The input arguments are: velocity v, length, final time,
% number of points on the space grid, initial-boundary conditions.
% It returns a matrix with the approximating solutions at tj , j = 1, ..., nt + 1.
% Check data
if any([v L T nx-2] <= 0)
error(’Check v, L, T, nx’)
end
% Stability
nt = 5; dx = L/nx; st = 1;
while v*T/nt/dx > st
nt = nt+1;
end
% Initialization
dt = T/nt; s = dt/dx;
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u(:,1) = feval(phi,x);
% FTBS Method
for j = 2:nt + 1
u(2:nx+1,j) = (1 - s*v)*u(2:nx+1,j-1) + s*v*u(1:nx,j-1);
u(1,j) = g(t(j));
end
end

Example 3.1.7 Consider the following initial-boundary value problem

Ut + vUx = 0, 0 < x < L, 0 < t ≤ T, v > 0, (3.1.34)



K if 0 < t < t0 ,
U (x, 0) = 0, 0 ≤ x ≤ L, U (0, t) = (3.1.35)
0 if t0 ≤ t ≤ T.

Problem (3.1.34) and (3.1.35) has the following analytical solution



K, if v(t − t0 ) < x < vt,
U (x, t) =
0, if x ≥ vt.

The listing below calls the ftbs function and solves Problem (3.1.34) and
(3.1.35). The graph of the numerical solution is shown in Fig. 3.1.5.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 114

114 Matlab for Engineering

t = 1.4
1.2
FTBS
Exact
1

0.8

U
0.6

0.4

0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x

Figure 3.1.5. Graph of the numerical solution of Problem (3.1.34) and (3.1.35).

function u = ftbs ex1


% This is the function file ftbs ex1.m.
% Ftbs function is called to solve the special Dirichlet problem:
% Ut +v Ux = 0, U(x,0) = 0,
% U(0,t) = K, if t < t0, U(0,t) = 0, if t >= t0.
% The approximating solution is plotted.
% Exact solution:
% U = K, if v*(t-t0) < x < v*t, U = 0, if x >= v*t.
v = .4; L = 1; T = 1.4; nx = 300;
t0 = .5; K = 1;
phi = @(x) 0*x;
g = @(t) K*(t0 - t > 0);
[u, nt] = ftbs(v, L, T, nx, phi, g);
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
U(:,1) = K*(x - v*(T - t0) > 0).*(x - v*T < 0);
for j = 2:nt + 1
plot(x,u(:,j),’k’,x,U,’r*:’);
axis([0 L 0 K+.201]);
xlabel(’x’); ylabel(’U’);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 115

Diffusion and Convection 115

title([’t = ’,num2str(t(j))]);
legend(’FTBS’,’Exact’);
pause(.01);
end
fprintf( ’Maximum error = %g\n’,max(abs(U - u(:,nt+1))))
end
Other applications are suggested in Exercises 3.4.9 and 3.4.10.
Consider the following initial-boundary value problem

Ut + vUx = 0, x < L, 0 < t ≤ T, v < 0, (3.1.36)

U (x, 0) = ϕ(x), x ≤ L, (3.1.37)

U (L, t) = g(t), 0 < t ≤ T. (3.1.38)

The analytical solution of Problem (3.1.36) to (3.1.38) is suggested in


Exercise 3.4.11.

Example 3.1.8 A function is provided that applies Method (3.1.29) to


solve Problem (3.1.36) to (3.1.38).

function [u, nt] = ftfs(v, L, T, nx, phi, g)


% This is the function file ftfs.m.
% FTFS Method is applied to solve the Dirichlet problem:
% Ut +v Ux = 0, U(x,0) = phi(x), U(L,t) = G(t).
% The input arguments are: velocity v, length, final time,
% number of points on the space grid, initial-boundary conditions.
% It returns a matrix with the approximating solutions at tj , j = 1, ..., nt + 1.
% Check data
if any([-v L T nx-2] <= 0)
error(’Check v, L, T, nx’)
end
% Stability
nt = 5; dx = L/nx; st = 1;
while abs(v)*T/nt/dx > st
nt = nt+1;
end
% Initialization
dt = T/nt; s = dt/dx;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 116

116 Matlab for Engineering

x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
u(:,1) = feval(phi,x);
% FTFS Method
for j = 2:nt + 1
u(1:nx,j) = (1 + s*v)*u(1:nx,j-1) - s*v*u(2:nx+1,j-1);
u(nx+1,j) = g(t(j));
end
end

Example 3.1.9 Let us illustrate a way to call the ftfs function. Consider
the special initial-boundary value problem

Ut + vUx = 0, 0 < x < L, 0 < t ≤ T, v < 0, (3.1.39)


U (x, 0) = 0, 0 ≤ x ≤ L, U (L, t) = sin(ωt), 0 < t ≤ T, (3.1.40)

that has the following analytical solution



sin(ω(t − x/v + L/v)), if x > vt + L,
U (x, t) =
0, if x ≤ vt + L.
The listing below calls the ftfs function and solves Problem (3.1.39 and
3.1.40). The graph of the numerical solution is shown in Fig. 3.1.6.

function u = ftfs ex1


% This is the function file ftfs ex1.m.
% Ftfs function is called to solve the special Dirichlet problem:
% Ut +v Ux = 0, U(x,0) = 0, U(L,t) = sin(om*t).
% The approximating solution is plotted.
% Exact solution:
% U(x,t) = sin(om*(t-x/v+L/v)), if x > vt + L,
% U(x,t) = 0, if x <= vt + L.
v = -.4; L = 1; T = 2.5; nx = 50;
om = 5;
phi = @(x) 0*x;
g = @(t) sin(om*t);
[u, nt] = ftfs(v, L, T, nx, phi, g);
x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
U(:,1) = sin(om*(- x/v + L/v)).*(T - x/v + L/v > 0);
fprintf( ’Maximum error = %g\n’,max(abs(U - u(:,nt+1))))
for j = 2:nt + 1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 117

Diffusion and Convection 117

t = 2.5
1

0.8

0.6

0.4

0.2

U 0

-0.2

-0.4

-0.6

-0.8 FTFS
Exact
-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x

Figure 3.1.6. Graph of the numerical solution of Problem (3.1.39 and 3.1.40).

plot(x,u(:,j),’k’,x,U,’ro’,’LineWidth’,.1);
xlabel(’x’); ylabel(’U’);
title([’t = ’,num2str(t(j))]);
legend(’FTFS’,’Exact’,’Location’,’SouthWest’);
pause(.001);
end
end
Consider the advection equation with decay
Ut + vUx = λU, (3.1.41)

where λ is the decay coefficient. The change of unknown function

W = U exp(−λt), (3.1.42)
converts Eq. (3.1.41) to the advection equation

Wt + vWx = 0.
Consequently, all Matlab functions, introduced for the advection equation,
can be used for Eq. (3.1.41) as well, after small modifications. See Exercise
3.4.12.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 118

118 Matlab for Engineering

Consider the advection equation with a variable coefficient

Ut + v(x, t)Ux = 0, (3.1.43)

where v(x, t) is a given function. If v(x, t) > 0, applying the FTBS Method
yields

uj+1
i = (1 − svij )uji + svij uji−1 , (3.1.44)

where vij = v(xi , tj ). Method (3.1.44) is stable under the hypothesis

s|vij | ≤ 1, ∀ i, j, (3.1.45)

as it is

|uj+1
i | ≤ (1 − svij )|uji | + svij |uji−1 | ≤ ||uj ||∞ ⇒ ||uj+1 ||∞ ≤ ||uj ||∞ .

If v(x, t) < 0, applying the FTFS Method yields

uj+1
i = (1 + svij )uji − svij uji+1 , (3.1.46)

that is stable under the same Hypothesis (3.1.45).

3.2 Method of Lines


3.2.1 Heat Equation
The Method of Lines is a semi-discrete finite difference method for the
numerical integration of partial differential equations. Only the space
variables are discretized, whereas the time variable remains continuous.
We begin to illustrate the method by considering the one-dimensional heat
equation

Ut = αUxx , 0 < x < L, t > 0. (3.2.1)

The variable x is discretized and the variable t is not. Using the central
approximation for Uxx yields

u̇i = p(ui+1 − 2ui + ui−1 ), i = 1, . . . , n − 1, nΔx = L, (3.2.2)

where p = α/(Δx)2 , and the following notations have been introduced

ui = u(xi , t), u̇i = ut (xi , t).

Equations (3.2.2) are a system of n − 1 ordinary differential equations to


be integrated along the lines x = xi (Fig. 3.2.1). System (3.2.2) has n + 1
unknown functions: u0 (t),..., un (t), two more than equations. In the case of
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 119

Diffusion and Convection 119

0 L
x

Figure 3.2.1. Method of Lines.

the Dirichlet problem, two functions are known, as given by the boundary
conditions u0 = u(0, t) = g1 (t) and un = u(L, t) = g2 (t), and the differential
system can be solved. In addition, System (3.2.2) can be arranged in matrix
form as follows
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇1 −2 1 u1 u0
⎢ u̇2 ⎥ ⎢ 1 −2 1 ⎥ ⎢ u2 ⎥ ⎢ 0 ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ · ⎦ = p⎣ . . . ⎦⎣ · ⎦ + p⎣ · ⎦,
u̇n−1 1 −2 un−1 un

u̇ = Au + a, (3.2.3)

where a is a known term. System (3.2.3) solves the Dirichlet problem.


Consider the Neumann problem for Eq. (3.2.1) with the following
boundary conditions

−Ux (0, t) = g1 (t), Ux (L, t) = g2 (t). (3.2.4)

Apply the central approximations to the derivatives in (3.2.4)


u1 − u−1 un+1 − un−1
− = g1 , = g2 . (3.2.5)
2Δx 2Δx
Formulas (3.2.5) are not applicable because of the ghost terms u−1 and
un+1 . Therefore, consider the equations obtained from (3.2.2) for i = 0 and
i = n:

u̇0 = p(u1 − 2u0 + u−1 ), u̇n = p(un+1 − 2un + un−1 ). (3.2.6)

Solving the algebraic Equations (3.2.5) with respect to the ghost terms and
the result substituted into the differential Eqs. (3.2.6) yields

u̇0 = 2p(u1 − u0 ) + 2pΔxg1 , u̇n = 2p(un−1 − un ) + 2pΔxg2 . (3.2.7)


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 120

120 Matlab for Engineering

Equations (3.2.2) and (3.2.7) are the system that solves the Neumann
problem. In matrix notation, it is written as
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇0 −2 2 u0 g1
⎢ u̇1 ⎥ ⎢ 1 −2 1 ⎥ ⎢ u1 ⎥ ⎢0⎥
⎢ ⎥ = p⎢ ⎥ ⎢ ⎥ + 2pΔx ⎢ ⎥ ,
⎣ · ⎦ ⎣ . . . ⎦⎣ · ⎦ ⎣ · ⎦
u̇n 2 −2 un g2

u̇ = Au + a. (3.2.8)
Another way to approximate boundary Conditions (3.2.4) is the following
u1 − u0 un − un−1
− = g1 , = g2 ,
Δx Δx
where forward and backward approximations were applied. Conditions
above do not present ghost terms, but are less accurate. The Robin problem
is discussed similarly.

Example 3.2.1 Consider the process of consolidation in a clay layer due


to a uniform load q (Fig. 3.2.2). The evolution of the overpressure, excess
pore pressure U , is governed by the following Dirichlet problem

Ut − cν Uzz = 0, 0 < z < L, 0 < t ≤ T, (3.2.9)


U (z, 0) = q, 0 ≤ z ≤ L, (3.2.10)
U (0, t) = 0, U (L, t) = 0, 0 < t ≤ T. (3.2.11)
where cν = 10−7 m2 /s is the consolidation coefficient. A function that
applies the Method of Lines to solve Problem (3.2.9) to (3.2.11) is provided
below. For L = 8 m, T = 3 years, the graph of the numerical solution is
shown in Fig. 3.2.3.

H sand
z draining
boundary
L
clay

Figure 3.2.2. Consolidation.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 121

Diffusion and Convection 121

8
t = 0 years
t = 3 years
7

5
z
4

0
0 5 10 15 20 25 30 35 40
u

Figure 3.2.3. Graph of the numerical solution to Problem (3.2.9) to (3.2.11).

function u = consolidation1
% This is the function file consolidation1.m.
% Method of Lines is applied to solve the Dirichlet problem
% Ut - cv Uzz = 0, U(z,0) = q, U(0,t) = U(L,t) = 0.

% Initialization
cv = 10^(-7); L = 8; T = 3*365*24*3600; % 1 year = 365*24*3600 seconds
q = 40;
n = 50; dz = L/n; p = cv/dz^2; z = linspace(0,L,n+1);
AA = [ones(n-1,1) -2*ones(n-1,1) ones(n-1,1)];
A = p*spdiags(AA, -1:1, n-1,n-1);
u = q*ones(n+1,1);% Vector u is initialized with the initial condition.
plot(u,z,’ro’); % The initial condition is plotted.
hold on;% Retains plots. New plots do not delete previous plots.

% Method of Lines
[~,y] = ode45(@system, [0 T ], u(2:end-1), [ ], A);
% [t,y] = ode45(@fun, ti, ic, options, p1, p2,...)
% This function solves the systems of ordinary differential equations;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 122

122 Matlab for Engineering

% fun is the local function where the differential system is defined;


% ti is the vector containing the initial and final times;
% ic is the vector containing the initial values;
% The symbol [ ] replaces the structure ‘options’ that is not used in
% this case;
% p1, p2,... are other parameters passed to ode45, in this example A.
% The function returns the column vector t and the matrix y that has the
% same number of rows as t. In this example, t has been replaced by the
% symbol ~ as not used. The first row in y contains the solution
% at the initial time and the last at the final time. The other rows in y
% contain the solution at the time specified by the corresponding row
% in t.
u(2:n) = y(end,:);% The final solution is copied in u.
u(1) = 0; u(n+1) = 0;% Boundary conditions.
plot(u,z,’k’);
xlabel(’u’); ylabel(’z’);
year = 365*24*3600; legend(’t = 0 years’,[’t = ’,num2str(T/year),’ years’]);
hold off;% The default behavior is restored.
end
%———- Local function ————-
function Du = system(~, u, B)
% The symbol ~ replaces the variable t that is not used in this case.
Du = B*u;
end

When the boundary conditions depend on time, the application of the


Method of Lines may present some difficulty, as outlined in the example
below.

Example 3.2.2 Consider the following Dirichlet problem

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


U (x, 0) = 0, 0 ≤ x ≤ L, (3.2.13)

t/t0 − (t/t0 )2 if t ≤ t0 ,
U (0, t) = U (L, t) = 0, 0 < t ≤ T. (3.2.14)
0 if t > t0 ,
A way to apply the Method of Lines to Problem (3.2.12) to (3.2.14) is
illustrated below. The graph of the numerical solution is shown in Fig.
3.2.4.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 123

Diffusion and Convection 123

0.012
t=2

0.01

0.008

U
0.006

0.004

0.002

0
0 1 2 3 4 5 6 7 8 9 10
x

Figure 3.2.4. Graph of the numerical solution to Problem (3.2.12) to (3.2.14).

function u = lines heat1


% This is the function file lines heat1.m.
% Method of Lines is applied to solve the Dirichlet problem:
% Ut = alpha Uxx, U(x,0) = 0, U(L,t) = 0.
% U(0,t) = t/t0 - t2 /t02 , if t <= t0,
% U(0,t) = 0, if t > t0.
% Initialization
alpha = 3; L = 10; T = 2; nx = 50;
dx = L/nx; p = alpha/dx2 ; x = linspace(0,L,nx+1);
AA = [ones(nx-1,1) -2*ones(nx-1,1) ones(nx-1,1)];
A = p*spdiags(AA, -1:1, nx-1,nx-1);
a = zeros(nx-3,1);
u = zeros(nx+1,1); % Initial condition.
% Method of Lines
tic % tic and toc functions measure the time elapsed between the two.
[~,y] = ode45(@system, [0 T], u(2:end-1), [ ], A, a, p);
u(2:nx) = y(end,:);
u(1) = g1(T); u(nx+1) = 0;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 124

124 Matlab for Engineering

toc % See tic


% Plot
plot(x,u,’k’);
xlabel(’x’); ylabel(’U’);
legend([’t = ’,num2str(T)],’Location’,’NorthEast’);
end
% ———- Local functions ————-
function f = g1(t)
t0 = .5;
f = (t/t0 - t2 /t02 )*(t <= t0);
end
function Du = system(t, u, A, a, p)
Du = A*u + [p*g1(t); a; 0];
end
Other applications are suggested in Exercises 3.4.13–3.4.17.
Consider the Neumann–Dirichlet problem

−Ux (0, t) = g1 (t), U (L, t) = g2 (t). (3.2.15)


The equations for the Method of Lines are derived from Formulas (3.2.7)1
and (3.2.2), rewritten for convenience, are
u̇0 = 2p(u1 − u0 ) + 2pΔxg1 ,
u̇i = p(ui+1 − 2ui + ui−1 ), i = 1, . . . , n − 2, u̇n−1 = p(un−2 − 2un−1 ) + pg2 .
Equivalently, in matrix form, we have
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇0 −2 2 u0 2Δxg1
⎢ u̇1 ⎥ ⎢ 1 −2 1 ⎥ ⎢ u1 ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ + p⎢ 0 ⎥,
⎣ · ⎦ = p⎣ . . . ⎦ ⎣ · ⎦ ⎣ · ⎦
u̇n−1 1 −2 un−1 g2

u̇ = Au + a. (3.2.16)
Equation (3.2.16) will be applied in the example below. The Dirichlet–
Neumann problem

−Ux (0, t) = g1 (t), U (L, t) = g2 (t), (3.2.17)


is discussed similarly. See Exercise 3.4.18.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 125

Diffusion and Convection 125

q
sand
H
z draining
boundary
L clay
impervious
boundary

Figure 3.2.5. Consolidation.

Example 3.2.3 Consider again the consolidation process due to a


uniform load q, as in Example 3.2.1. Let us discuss a different mechanical
situation: a layer of clay between an upper draining boundary and a lower
impervious boundary (Fig. 3.2.5). The evolution of the pore overpressure
U is governed by the following Neumann-Dirichlet problem

Ut − cv Uzz = 0, 0 < z < L, 0 < t ≤ T, (3.2.18)

U (z, 0) = q, 0 ≤ z ≤ L, (3.2.19)

Uz (0, t) = 0, U (L, t) = 0, 0 < t ≤ T. (3.2.20)

A function that applies the Method of Lines to solve Problem (3.2.18 to


3.2.20) is presented below. The graph of the numerical solution is shown in
Fig. 3.2.6.
function u = consolidation2
% This is the function file consolidation2.m.
% Method of Lines is applied to solve the Neumann–Dirichlet problem:
% Ut - cv*Uzz = 0, U(z,0) = q, Ux(0,t) = 0, U(L,t) = 0.
% Initialization
cv = 10^(-7); L = 8; T = 3*365*24*3600;
n = 50; dz = L/n; p = cv/dz^2; z = linspace(0,L,n+1);
q = 40; z = linspace(0,L,n+1);
AA = [ones(n,1) -2*ones(n,1) [0; 2; ones(n-2,1)]];
A = p*spdiags(AA, -1:1, n,n);
a = zeros(n-2,1);
u = q*ones(n+1,1);
plot(u,z,’r*’);
hold on;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 126

126 Matlab for Engineering

z 4

1 t = 0 years
t = 3 years
0
0 5 10 15 20 25 30 35 40
U

Figure 3.2.6. Graph of the numerical solution to Problem (3.2.18) to (3.2.20).

% Method of Lines
[~,y] = ode45(@system, [0 T ], u(1:end-1), [ ], A, a);
u(1:n) = y(end,:);
u(n+1) = 0;
plot(u,z,’k’);
xlabel(’u’); ylabel(’z’);
year = 365*24*3600;
legend(’t = 0 year’,[’t = ’,num2str(T/year),’ years’],’Location’,’SouthWest’);
hold off;
end
%———- Local function ————-
function Du = system(~, u, B, b)
Du = B*u + [0; b; 0];
end
Other applications are suggested in Exercises 3.4.19 and 3.4.20.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 127

Diffusion and Convection 127

3.2.2 Nonlinear Equations


Consider the equation
Ut = αUxx + F (U, Ux ), (3.2.21)

where F can depend nonlinearly on U and Ux . For example, if F = −U Ux ,


Eq. (3.2.21) simplifies to the Burgers2 equation

Ut + U Ux = αUxx , (3.2.22)

and when F = Ux2 /(1 + U ), Eq. (3.2.21) simplifies to

Ut = Ux2 /(1 + U ) + αUxx . (3.2.23)

Let us apply the Method of Lines to Eq. (3.2.21). Using the central
approximations for the space derivatives, we get the following system of
ordinary differential equations

u̇i = p(ui+1 − 2ui + ui−1 ) + F (ui , (ui+1 − ui−1 )/(2Δx)), (3.2.24)

where p = α/(Δx)2 .
For the Burgers Eq. (3.2.22), System (3.2.24) is written as

u̇i = p(ui+1 − 2ui + ui−1 ) − qui (ui+1 − ui−1 ), i = 1, . . . , n − 1,

where q = 1/2/Δx. More explicitly,



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




⎨ u̇i = p(ui+1 − 2ui + ui−1 ) − qui (ui+1 − ui−1 ),
(3.2.25)

⎪ i = 2, . . . , n − 2,




u̇n−1 = p(un − 2un−1 + un−2 ) − qun−1 (un − un−2 ).

Consider the Dirichlet problem with the boundary conditions

U (0, t) = g1 (t), U (L, t) = g2 (t). (3.2.26)

In this case, Eqs. (3.2.25) are a system of n − 1 equations with the n − 1


unknown functions u1 , . . . , un−1 , since the functions u0 , un are known and

2 Johannes Martinus Burgers, a Dutch scientist, 1895–1981. He explored the equation

that bears his name. The Burgers equation occurs in gas and fluid mechanics, and traffic
flow.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 128

128 Matlab for Engineering

given by
u0 (t) = g1 (t), un (t) = g2 (t).

Example 3.2.4 Consider the Burgers Eq. (3.2.22) with the special initial-
boundary conditions

U (x, 0) = 2α[1 − tanh(x)], 0 ≤ x ≤ L, (3.2.27)



U (0, t) = 2α[1 − tanh(−2αt)],
0 < t < T. (3.2.28)
U (L, t) = 2α[1 − tanh(L − 2αt)],
A function that applies the Method of Lines to solve Problem (3.2.27 and
3.2.28) is presented below. Since the index 0 cannot be used in Matlab, all
indices in System (3.2.25) will be rescaled, u0 → u1 , un → un+1 . Next, the
following notations

w(1) = u(2), ..., w(n − 1) = u(n), g1 = u(1), g2 = u(n + 1),

will be introduced. Considering this, System (3.2.25) is written in Matlab


notation as

Dw(1) = p ∗ (w(2) − 2 ∗ w(1) + g1(t)) − q ∗ w(1) ∗ (w(2) − g1(t)),

Dw(2 : n − 2) = p ∗ (w(3 : n − 1) − 2 ∗ w(2 : n − 2) + w(1 : n − 3))

−q ∗ w(2 : n − 2). ∗ (w(3 : n − 1) − w(1 : n − 3)),

Dw(n−1) = p∗(w(n−2)−2∗w(n−1)+g2(t))−q∗w(n−1)∗(g2(t)−w(n−2)).

The graph of the numerical solution provided by the Method of Lines is


shown in Fig. 3.2.7, together with the exact solution: U (x, t) = 2α[1 −
tanh(x − 2αt)].

function u = burgers
% This is the function file burgers.m.
% Method of Lines is applied to solve the following Dirichlet problem:
% Ut + U Ux = alpha Uxx, U(x,0) = 2*alpha - 2*alpha*tanh(x)
% U(0,t) = 2*alpha - 2*alpha*tanh(-2*alpha*t),
% U(L,t) = 2*alpha - 2*alpha*tanh(L - 2*alpha*t),
% Analytical solution: U = 2*alpha - 2*alpha*tanh(x - 2*alpha*t).
% Initialization
L = 1; alpha = 1; T = 1; n = 20;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 129

Diffusion and Convection 129

t=1
3.95

3.9

3.85

3.8

3.75
U
3.7

3.65

3.6

3.55 MOL
Exact
3.5
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x

Figure 3.2.7. Graph of the numerical solution to Problem (3.2.27 and 3.2.28).

x = linspace(0,L,n+1);
u = 2*alpha - 2*alpha*tanh(x’);
% Method of Lines
[~,y] = ode45(@system,[0 T],u(2:end-1),[ ],L,n,alpha);
u(2:n) = y(end,:);
u(1) = g1(T, alpha);
u(n+1) = g2(T, alpha, L);
U = 2*alpha - 2*alpha*tanh(x’ - 2*alpha*T);
plot(x,u,’k’,x,U,’ro’);
title([’t = ’,num2str(T)]); xlabel(’x’); ylabel(’U’);
fprintf( ’Maximum error = %g\n’,max(abs(U - u)))
legend(’MOL’,’Exact’,’Location’,’SouthWest’);
end
%———- Local functions ————-
function f = g1(t, alpha)
f = 2*alpha - 2*alpha*tanh(-2*alpha*t);
end
function f = g2(t, alpha, L)
f = 2*alpha - 2*alpha*tanh(L-2*alpha*t);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 130

130 Matlab for Engineering

end
function Dw = system(t, w, L, n, alpha)
dx = L/n; p = alpha/dx2 ; q = 1/2/dx;
Dw(1,1) = p*(w(2) - 2*w(1) + g1(t,alpha)) - q*w(1)*(w(2) - g1(t,alpha));
Dw(2:n-2,1) = p*(w(3:n-1) - 2*w(2:n-2) + w(1:n-3))...
-q*w(2:n-2).*(w(3:n-1) - w(1:n-3));
Dw(n-1,1) = p*(w(n-2) - 2*w(n-1) + g2(t,alpha,L))...
-q*w(n-1)*(g2(t,alpha,L) - w(n-2));
% Note that w(1) = u(2),...,w(n-1) = u(n), g1 = u(1), g2 = u(n+1).
end
Consider the nonlinear Eq. (3.2.23). System (3.2.24) simplifies to

p(ui+1 − ui−1 )2
u̇i = p(ui+1 − 2ui + ui−1 ) + , i = 1, . . . , n − 1, (3.2.29)
4(1 + ui )

and, more explicitly,


⎧ 2
⎪ u̇1 = p(u2 − 2u1 + u0 ) + p(u2 − u0 ) /4(1 + u1 ),




⎨ u̇i = p(ui+1 − 2ui + ui−1 ) + p(ui+1 − ui−1 )2 /4(1 + ui ),
(3.2.30)

⎪ i = 2, . . . , n − 2,




u̇n−1 = p(un − 2un−1 + un−2 ) + p(un − un−2 )2 /4(1 + un−1 ).

In the Dirichlet problem with boundary Conditions (3.2.26), the functions


u0 and un are known

u0 (t) = g1 (t), un (t) = g2 (t),

and System (3.2.30) contains n − 1 unknown functions u1 , . . . , un−1 .

Example 3.2.5 Consider Eq. (3.2.23) with the following initial-boundary


conditions

U (x, 0) = −1 + 1 + 2x2 , 0 ≤ x ≤ L, (3.2.31)
 √
U (0, t) = −1 + 1 + 4t,
 0 < t < T. (3.2.32)
U (L, t) = −1 + 1 + 2(L2 + 2t),

A function that applies the Method of Lines to solve Problem (3.2.31 and
3.2.32) is presented below. Since the index 0 cannot be used in Matlab, all
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 131

Diffusion and Convection 131

indices in System (3.2.30) will be rescaled: u0 → u1 , un → un+1 . Next, the


following notations

w(1) = u(2), ..., w(n − 1) = u(n), g1 = u(1), g2 = u(n + 1),

will be introduced. Considering this, System (3.2.30) is written in Matlab


notation as

Dw(1) = p ∗ (w(2) − 2 ∗ w(1) + g1(t)) + p/4 ∗ (w(2) − g1(t))2 /(1 + w(1))


Dw(2 : n − 2, 1) = p ∗ (w(3 : n − 1) − 2 ∗ w(2 : n − 2) + w(1 : n − 3))
+p/4∗(w(3 : n−1)−w(1 : n−3)).2 ./(1+w(2 : n−2))
Dw(n − 1) = p ∗ (w(n − 2) − 2 ∗ w(n − 1) + g2(t, L))
+p/4 ∗ (g2(t) − w(n − 2))2 /(1 + w(n − 1)).

The graph of the numerical solution provided by the Method of Lines


is shown in Fig. 3.2.8, together with the exact solution: U (x, t) = −1 +

1 + 2(x2 + 2t).
function u = nonlinear
% This is the function file nonlinear.m.

t=1
1.65
MOL
1.6 Exact

1.55

1.5

1.45
U
1.4

1.35

1.3

1.25

1.2
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x

Figure 3.2.8. Graph of the numerical solution to Problem (3.2.31 and 3.2.32).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 132

132 Matlab for Engineering

% Method of Lines is applied to solve the following Dirichlet problem:


% Ut = (Ux)2 /(1 + U) + Uxx, U(x,0) = -1 + sqrt(1 + 2*x2 ),
% U(0,t) = -1 + sqrt(1 + 4*t), U(L,t) = -1 + sqrt(1 + 2*(L2 + 2*t)).
% Analytical solution: U = -1 + sqrt(1 + 2*(x2 + 2*t)),
% Initialization
L = 1; T = 1; n = 20;
x = linspace(0,L,n+1);
u = -1 + sqrt(1 + 2*x’.2 );
% Method of Lines
[~,y] = ode15s(@system,[0 T],u(2:end-1),[ ],L,n);
% For stiff problems ode15s can work better than ode45
u(2:n) = y(end,:);
u(1) = g1(T); u(n+1) = g2(T, L);
U = -1 + sqrt(1 + 2*(x’.2 + 2*T));
plot(x,u,’k’,x,U,’ro’);
title([’t = ’,num2str(T)]); xlabel(’x’); ylabel(’U’);
legend(’MOL’,’Exact’,’Location’,’NorthWest’);
fprintf( ’Maximum error = %g\n’,max(abs(U - u)))
end
% ———- Local functions ————-
function f = g1(t)
f = -1 + sqrt(1 + 4*t);
end
function f = g2(t, L)
f = -1 + sqrt(1 + 2*(L2 + 2*t));
end
function Dw = system(t, w, L, n)
dx = L/n; p = 1/dx2 ;
Dw(1,1) = p*(w(2) - 2*w(1) + g1(t))+ p/4*(w(2) - g1(t))2 /(1 + w(1));
Dw(2:n-2,1) = p*(w(3:n-1) - 2*w(2:n-2) + w(1:n-3))...
+ p/4*(w(3:n-1) - w(1:n-3)).2 ./(1 + w(2:n-2));
Dw(n-1,1) = p*(w(n-2) - 2*w(n-1) + g2(t,L))...
+ p/4*(g2(t, L) - w(n-2))2 /(1 + w(n-1));
% Note that w(1) = u(2),...,w(n-1) = u(n), g1 = u(1), g2 = u(n+1).
end

Another application is suggested in Exercise 3.4.21.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 133

Diffusion and Convection 133

Finally, let us present a finite difference method for equation

Ut = αUxx + F (U, Ux ). (3.2.33)

Using the forward approximation for the time derivative and the central
approximation for the space derivatives yields
 j j

j+1 j j j j ui+1 − ui−1
ui = (1 − 2rα)ui + rα(ui+1 + ui−i ) + ΔtF ui , , (3.2.34)
2Δx

where r = Δt/(Δx)2 . It can be proved that Method (3.2.34) is stable under


the hypotheses

1 − M Δt − 2rα ≥ 0, rα − M1 s/2 ≥ 0,

where

M = sup |Fu |, M1 = sup |Fux |, s = Δt/Δx.


(x,t) (x,t)

3.2.3 Variable Diffusivity Coefficient


Consider the one-dimensional heat equation with variable diffusivity coef-
ficient

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

where α(x, t) is a strictly positive function. Let us apply the Method of


Lines to Eq. (3.2.35). We get the following system of ordinary differential
equations

u̇i = pi (ui+1 − 2ui + ui−1 ), i = 1, . . . , n − 1, (3.2.36)

where

ui (t) = u(xi , t), αi (t) = α(xi , t), pi (t) = αi (t)/(Δx)2 , i = 1, . . . , n − 1.

In matrix form, System (3.2.36) is written as


⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇1 −2p1 p1 u1 p1 u 0
⎢ · ⎥ ⎢ p2 −2p2 p2 ⎥⎢ · ⎥ ⎢ 0 ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥,
⎣ · ⎦=⎣ . . . ⎦⎣ · ⎦+ ⎣ · ⎦
u̇n−1 pn−1 −2pn−1 un−1 pn−1 un

u̇ = A(p)u + a(p), (3.2.37)


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 134

134 Matlab for Engineering

where the notations A(p) and a(p) outline that A and a depend on p =
(p1 , ..., pn−1 ).

Example 3.2.6 A function that applies the Method of Lines to solve the
following Dirichlet problem
Ut − (1 + t)Uxx = 0, 0 < x < L, 0 < t ≤ T, (3.2.38)
U (x, 0) = sin(πx/L), 0 ≤ x ≤ L, (3.2.39)
U (0, t) = 0, U (L, t) = 0, t > 0, (3.2.40)
is provided below. The graph of the numerical solution is shown in Fig. 3.2.9,
together with the analytical solution U (x, t) = sin(πx/L) exp(−(π/L)2 (t +
t2 /2)).
function u = variable1
% This is the function file variable1.m.
% Method of Lines is applied to solve the following Dirichlet problem:
% Ut = (1 + t) Uxx, U(x,0) = sin(pi*x/L), U(0,t) = 0, U(L,t) = 0.
% Analytical solution: U = sin(pi*x/L)*exp(-(pi/L)^2*(t+t^2/2)).

t=1
0.9
MOL
0.8 Exact

0.7

0.6

0.5
U
0.4

0.3

0.2

0.1

0
0 1 2 3 4 5 6 7 8 9 10
x

Figure 3.2.9. Graph of the numerical solution to Problem (3.2.38 and 3.2.40).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 135

Diffusion and Convection 135

% Initialization
L = 10; T = 1; n = 100;
x = linspace(0,L,n+1);
u = sin(pi*x’/L);
U = sin(pi*x’/L)*exp(-(pi/L)^2*(T+T^2/2));
% Method of Lines
[~,y] = ode45(@system,[0 T],u(2:end-1),[ ],L,n);
u(2:n) = y(end,:);
plot(x,u,’k’,x,U,’ro’);
xlabel(’x’); ylabel(’U’);
title([’t = ’,num2str(T)]); legend(’MOL’,’Exact’);
fprintf( ’Maximum error = %g\n’,max(abs(U - u)))
end
% ———- Local function ————-
function Du = system(t, u, L, n)
dx = L/n;
p = (1 + t)*ones(n+1,1)/dx2;
AA = [p(3:n+1) -2*p(2:n) p(1:n-1)];
A = spdiags(AA, -1:1, n-1, n-1);
Du = A*u + [0; zeros(n-3,1); 0];
end
Another application is suggested in Exercise 3.4.22.
Lastly, a finite difference method for Eq. (3.2.35) is presented. The
method is derived from the Explicit Euler Method and is expressed by
j j j
uj+1 − uji u − 2ui + ui−1
i
= αji i+1 . (3.2.41)
Δt Δx2

where αji = α(xi , tj ). Hence, with the usual position r = Δt/Δx2 , we have

uj+1
i = (1 − 2rαji )uji + rαji (uji+1 + uji−1 ). (3.2.42)

Method (3.2.42) is stable under the hypothesis

2rαji ≤ 1, ∀ i, j. (3.2.43)

See Exercises 3.4.23 and 3.4.24.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 136

136 Matlab for Engineering

3.2.4 Convection-Diffusion Equation


Consider the convection-diffusion equation

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

discussed with the Upwind Method in Sec. 3.1.1. The Method of Lines for
Eq. (3.2.44) leads to different expressions that depend on the approximation
of Ux . If v > 0, the backward approximation is used and we get
v α
u̇i = − (ui − ui−1 ) + (u − 2ui + ui−1 ), i = 1, . . . , n − 1.
Δx (Δx)2 i+1
(3.2.45)
If v < 0, using the forward approximation yields
v α
u̇i = − (u − ui ) + (u − 2ui + ui−1 ), i = 1, . . . , n − 1.
Δx i+1 (Δx)2 i+1
(3.2.46)
From Formulas (3.2.45 and 3.2.46), it follows that
   
α v 2α v α
u̇i = + ui−1 − + ui + u , i = 1, . . . , n − 1,
(Δx)2 Δx (Δx)2 Δx (Δx)2 i+1

   
α 2α v α v
u̇i = u − − u+ − u , i = 1, . . . , n − 1.
(Δx)2 i−1 (Δx)2 Δx i (Δx)2 Δx i+1
The equations above can be combined into a single equation

u̇i = pui−1 − (p + q)ui + qui+1 , i = 1, . . . , n − 1, (3.2.47)

where
 
α/(Δx)2 + |v|/Δx if v > 0, α/(Δx)2 if v > 0,
p= 2
q= 2
α/(Δx) if v < 0, α/(Δx) + |v|/Δx if v < 0.

Equation (3.2.47) is the upwind version of the Method of Lines for Eq.
(3.2.44). It can be arranged in the following matrix form
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
u̇1 −(p + q) q u1 pu0
⎢ · ⎥ ⎢ p −(p + q) q ⎥⎢ · ⎥ ⎢ 0 ⎥
⎢ ⎥=⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ · ⎦ ⎣ . . . ⎦⎣ · ⎦ + ⎣ · ⎦,
u̇n−1 p −(p + q) un−1 qun

u̇ = Au + a. (3.2.48)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 137

Diffusion and Convection 137

Of course, the central approximation for Ux can be used as well. In this


case the Method of Lines for Eq. (3.2.44) leads to
v α
u̇i = − (u − ui−1 ) + (u − 2ui + ui−1 ), i = 1, . . . , n − 1.
2Δx i+1 (Δx)2 i+1
(3.2.49)

Example 3.2.7 Consider the Dirichlet problem


Ut + vUx − αUxx = 0, v > 0, 0 < x < L, 0 < t ≤ T, (3.2.50)

U (x, 0) = 0, 0 ≤ x ≤ L, (3.2.51)

t/t0 − (t/t0 )2 if t ≤ t0 ,
U (0, t) = U (L, t) = 0, t > 0. (3.2.52)
0 if t > t0 ,
A function that applies Method (3.2.48) to solve Problem (3.2.50 to 3.2.52)
is presented below. The graph of the numerical solution is shown in Fig.
3.2.10.

function u = lines c d 1
% This is the function file lines c d 1.m.m.
% Method of Lines is applied to solve the special Dirichlet problem:

0.07
t=4
0.06

0.05

0.04
U
0.03

0.02

0.01

0
0 10 20 30 40 50 60 70 80 90 100
x

Figure 3.2.10. Graph of the numerical solution to Problem (3.2.50 to 3.2.52).


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 138

138 Matlab for Engineering

% Ut +v*Ux = alpha*Uxx, U(x,0) = 0, U(L,t) = 0.


% U(0,t) = t/t0 - t^2/t0^2, if t < t0,
% U(0,t) = 0, if t >= t0.
% Initialization
alpha = 0.01; v = 10; L = 100; T = 4; n = 150;
if v <= 0
error(’v must be positive’)
end
dx = L/n; x = linspace(0,L,n+1);
t0 = .5;
g1 = @(t) (t/t0 - t2 /t02 ).*(t < t0);
p = alpha/dx + v/dx; q = alpha/dx2 ;
2

a = zeros(n-3,1);
AA = [p*ones(n-1,1) -(p + q)*ones(n-1,1) q*ones(n-1,1)];
A = spdiags(AA, -1:1, n-1,n-1);
u = zeros(n+1,1);
% Method of Lines
[~,y] = ode45(@system, [0 T], u(2:end-1), [ ], A, a, p, g1);
u(2:n) = y(end,:); u(1) = g1(T);
plot(x,u,’k’); xlabel(’x’); ylabel(’U’);
legend([’t = ’,num2str(T)]);
end
% ———- Local function ————-
function Du = system(t, u, A, a, p, g1)
Du = A*u + [p*g1(t); a; 0];
end

Example 3.2.8 Consider the Dirichlet problem


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

U (x, 0) = 0 if x ∈ [0, x1 [ ∪ ]x2 , L], (3.2.54)
k if x ∈ [x1 , x2 ],

U (0, t) = 0, U (L, t) = 0, t > 0. (3.2.55)


A function that applies Method (3.2.48) to solve Problem (3.2.53 and
3.2.55) is presented below. The graph of the numerical solution is shown in
Fig. 3.2.11.
function u = lines c d 2
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 139

Diffusion and Convection 139

1
t=2
0.9 t=0

0.8

0.7

0.6

U 0.5

0.4

0.3

0.2

0.1

0
0 20 40 60 80 100
x

Figure 3.2.11. Graph of the numerical solution to Problem (3.2.53 to 3.2.55).

% This is the function file lines c d 2.m.m.


% Method of Lines is applied to solve the special Dirichlet problem:
% Ut +v Ux = alpha Uxx, U(0,t) = 0, U(L,t) = 0,
% U(x,0) = 0, if x < x1,
% U(x,0) = k, if x1 <= x <= x2,
% U(x,0) = 0, if x > x2.
% Initialization
alpha = 0.1; v = 10; L = 100; T = 2; n = 150;
dx = L/n; x = linspace(0,L,n+1);
if v >= 0
p = alpha/dx2 + v/dx; q = alpha/dx2 ;
else
p = alpha/dx2 ; q = alpha/dx2 - v/dx;
end
i1 = 73; i2 = 79; x1 = x(i1); x2 = x(i2);
k = 1;
phi = @(x) k*(x >= x1).*(x <= x2);
U = feval(phi,x);
a = zeros(n-3,1);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 140

140 Matlab for Engineering

AA = [p*ones(n-1,1) -(p + q)*ones(n-1,1) q*ones(n-1,1)];


A = spdiags(AA, -1:1, n-1,n-1);
% Method of Lines
[~,y] = ode45(@system, [0 T], U(2:end-1), [ ], A, a);
u(2:n) = y(end,:); u(1) = 0; u(n+1) = 0;
plot(x,u,’ko’,x,U,’k’,’LineWidth’,2);
xlabel(’x’); ylabel(’U’);
legend([’t = ’,num2str(T)],’t = 0’);
end
% ———- Local function ————-
function Du = system(~, u, A, a)
Du = A*u + [0; a; 0];
end
Other applications are suggested in Exercises 3.4.25 and 3.4.26.
Consider the convection-diffusion equation with decay

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


where λ is the decay coefficient. The change of unknown function

W = U exp(λt), (3.2.57)

simplifies Eq. (3.2.56) to the convection-diffusion equation

Wt + vWx = αWxx . (3.2.58)

Consequently, the Method of Lines can be applied to Eq. (3.2.56) using


Transformation (3.2.56), as suggested in Exercise 3.4.27. Of course, the
Method of Lines can be applied to Eq. (3.2.56) and gives

u̇i = pui−1 − (p + q + λ)ui + qui+1 , i = 1, . . . , n − 1. (3.2.59)

Finally, note that the result (3.2.58) holds even when λ = λ(t), as shown
in Exercise 3.4.28.

3.3 Saving Data and Figures


3.3.1 Save Function
Variables created in the Command Window can be saved with the save
function. The simplest syntax is the following
save name of file var1 var2 ....
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 141

Diffusion and Convection 141

For example, after creating the variables


a = 1; M = [1 2; 3 4]; s = ’string’;
the command
save sv 1 a M s
creates the sv 1.mat file in the current directory and saves the variables a,
M. s in that file. As noted, the extension mat can be omitted. Variables
saved in a file can be inspected with the command
whos -file name of file.
For example, after saving a, M. s in the sv 1 file, the command
whos -file sv 1
produces
Name Size Bytes Class Attributes
M 2x2 32 double
a 1x1 8 double
s 1x6 12 char
If the file already exists, then the save function overwrites the file and the
previous contents are lost. See Exercise 3.4.29. If the user wants to save
new variables in an existing file, then the command
save -append name of file n var1 n var2 ...
must be used. In the command above, n var1 n var2 ... are the new variables
that will be appended to those already saved. If some new variables are
already in the file, their values are updated. See Exercise 3.4.30. The
command
save name of file
saves all variables of the Command Window in the name of file file. As
noted, the command does not require the variables to be specified. The
command above can be shortened to
save
that saves all variables in the matlab.mat file. See Exercise 3.4.31.
The save function saves the variables in binary format. It is possible to
save the variables in Ascii format by specifying that as follows
save name of file -ascii.
For example, after creating the variable a = pi;, the command
save sv 4.txt a -ascii
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 142

142 Matlab for Engineering

saves a in Ascii format using 8 digits. Note that the whos command does not
work with these files. However, they can be opened with any text editor,
or with the Matlab command
type(’sv 4.txt’).
See Exercise 3.4.32. The save function can be called by using the typical
function syntax as well
save(’name of file’,’var1’,’var2’,...).
Therefore, commands equivalent to those introduced above are the follow-
ing,
save(’name of file’,’n var1’,’n var2’,’-append’),
save(’name of file’,’var1’,’var2’,’-ascii’).
See Exercise 3.4.33. The save function may be useful in applications of real
interest. Since executing complex programs can last days, it is important
to save final and intermediate results.

3.3.2 Load Function


The load function performs the reverse operations of save. It imports the
variables saved by the save function in the Command Window. The syntax
to call the load function is analogous to that of save
load name of file var1 var2 ....
Equivalently, the load function can be called using the typical function
syntax
load(’name of file’, ’var1’, ’var2’, ...).
If the load function loads a variable already existing in the Command
Window, the value of such a variable is updated and the variable takes
the value that it has in the loaded file. For example, create the variable x
= pi;. Next, load the variable x from sv 1.mat file
load sv 1 x.
Note that it is now
x = 15.
If the variable does not exist in the file indicated in the load function,
an error message is sent by Matlab. See Exercise 3.4.34. The following
command
load name of file
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 143

Diffusion and Convection 143

imports all variables of the name of file file in the Command Window. As
noted, the variable name was omitted. The above commands work only
with binary files, i.e., those with mat extension.

3.3.3 Saving Figures


The simplest way to save a figure is to select File/Save as ... in the menu
of the figure. Next, specify the name, directory and extension (fig, eps, jpg,
pdf, tif, ...). For example, let us produce a figure with the command
fplot(@sin, [-pi pi])
and save it as figure 1.fig in the current directory. The saved figure can
be opened by selecting Open in the Matlab menu and looking for the file.
Alternatively, the following command can be used
open(’C:\... \current directory\figure 1.fig’).
The figures produced by Matlab can be easily copied to other programs.
Select
File/Preferences
in the figure menu and follow the indications.

3.4 Exercises
Exercise 3.4.1 Derive the non-dimensional form of Eq. (3.1.1).
Answer. Consider the following change of variables

ξ = x/L, x = Lξ,
2
⇔ (3.4.1)
τ = αt/L , t = L2 τ /α,

and notations

W (ξ, τ ) = U (x(ξ, τ ), t(ξ, τ )) ⇔ U (x, t) = W (ξ(x, t), τ (x, t)). (3.4.2)

From (3.4.1 and 3.4.2), we get

Ux = Wξ /L, Uxx = Wξξ /L2 , Ut = Wτ α/L2 .

Substituting the formulas above into (3.1.1) leads to the desired equation

Wτ + P Wξ = Wξξ , (P = vL/α).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 144

144 Matlab for Engineering

Exercise 3.4.2 Derive the stability Condition (3.1.4) directly from


(3.1.3) and stability Condition (3.1.6) directly from (3.1.5).

Exercise 3.4.3 Prove the unconditional stability of Methods (3.1.16)


and (3.1.17).
Hint. Apply the Von Neumann criterium. Substituting uji = ξ j eIβiΔx in
(3.1.16) yields

ξ{1 + 2rα + vs − 2rα cos(βΔx) − vs exp(−IβΔx)} = 1,

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

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

Prove that the same result holds for Method (3.1.17).

Exercise 3.4.4 Group Methods (3.1.16) and (3.1.17) in one formula.


Answer.

−puj+1 j+1
i−1 + (1 + p + q)ui − quj+1 j
i+1 = ui , (3.4.3)

where
 
rα + s|v| if v ≥ 0, rα if v ≥ 0,
p= q= (3.4.4)
rα if v < 0, rα + s|v| if v < 0.

Exercise 3.4.5 Prove the unconditional stability of Method (3.1.18).


Answer. Apply the Von Neumann criterium and obtain

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


|ξ|2 = < 1.
4[1 + (2rα + vs) sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)

Exercise 3.4.6 Prove the unconditional stability of Method (3.1.19).

Exercise 3.4.7 Derive the stability Condition (3.1.30).


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 145

Diffusion and Convection 145

t
x < vt x > vt
(x, t) (x, t)

vt
t - x/v

=
x

x - vt x

Figure 3.4.1. Regions where x < vt and x > vt.

Exercise 3.4.8 Verify that the function



ϕ(x − vt) if x ≥ vt,
U (x, t) = (3.4.5)
g(t − x/v) if x < vt,

is the solution of Problem (3.1.31 to 3.1.33).


Hint. Consider Fig. 3.4.1.

Exercise 3.4.9 Consider the ftbs ex1 function. Replace T = 0.4 with
T = 3.4 and execute the listing. Explain what happens.
Hint. Consider the error.

Exercise 3.4.10 Consider the Dirichlet problem

Ut + vUx = 0, 0 < x < L, 0 < t ≤ T, v > 0, (3.4.6)


0 if x ∈ [0, x1 [ ∪ ]x2 , L],
U (x, 0) = U (0, t) = 0, 0 < t ≤ T, (3.4.7)
K if x ∈ [x1 , x2 ],

that has the following analytical solution



0 if x ∈ [0, x1 + vt[ ∪ ]x2 + vt, L],
U=
K if x ∈ [x1 + vt, x2 + vt].

Write a listing, say ftbs ex2, that applies the ftbs function to solve Problem
(3.4.6 and 3.4.7).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 146

146 Matlab for Engineering

t x < vt + L x > vt + L
(x, t) (x, t)

x
t + (L - x)/v

=
vt
+
L
x - vt x

Figure 3.4.2. Regions where x < vt + L and x > vt + L.

Exercise 3.4.11 Verify that the function



ϕ(x − vt) if x ≤ vt + L,
U (x, t) = (3.4.8)
g(t + (L − x)/v) if x > vt + L,
is the solution of Problem (3.1.36 to 3.1.38).
Hint. Consider the Fig. 3.4.2.

Exercise 3.4.12 Write a listing, say ftbs ex3, that calls the fbfs function
and solves the following initial-boundary value problem

Ut + vUx = λU, 0 < x < L, 0 < t ≤ T, v > 0, (3.4.9)



sin(πx/x1 ), if 0 ≤ x ≤ 2x1 ,
U (x, 0) = U (0, t) = 0, 0 < t ≤ T. (3.4.10)
0, if 2x1 < x ≤ L,

The analytical solution of Problem (3.4.9 and 3.4.10) is the function




⎨ 0, if 0 ≤ x ≤ vt,
U (x, t) = exp(λt) sin(π(x − vt)/x1 ), if vt < x ≤ vt + 2x1 ,


0, if vt + 2x1 < x ≤ L.

Hint. The listing is partially provided below and the graph of the numerical
solution is shown in Fig. 3.4.3.
function u = ftbs ex3
v = .5; L = 1; T = 1; nx = 150;
x1 = 0.2; lambda = 1;
phi = @(x) sin(pi*x/x1).*(2*x1 >= x);
g = @(t) 0*t;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 147

Diffusion and Convection 147

t=1
3

U 0

-1

-2
FTBS
Exact
-3
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x

Figure 3.4.3. Graph of the numerical solution of Problem (3.4.9 and 3.4.10).

[u, nt] = ftbs(v, L, T, nx, phi, g);


x = linspace(0,L,nx+1); t = linspace(0,T,nt+1);
U(1:nx+1,1)=exp(lambda*T)*sin(pi*(x-
v*T)/x1).*(2*x1+v*T>=x).*(x>=v*T);
for j=1:nt+1
u(:,j) = u(:,j).*exp(lambda*t(j));
end
...
end

Exercise 3.4.13 Write a listing, say consolidation1 ee, that solves the
consolidation Problem (3.2.9 to 3.2.11) by calling the euler e function in
Example 2.3.1.
Hint. See Example 2.3.2. A way to introduce the initial condition may be
phi = @(z) q*ones(1,nz+1);

Exercise 3.4.14 Write a listing, say consolidation1 ie, where the consoli-
dation Problem the (3.2.9 to 3.2.11) is solved by calling the euler i function
in Example 2.3.13.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 148

148 Matlab for Engineering

Hint. See Example 2.3.14. Another way to introduce the initial condition
could be
phi = @(z) 0*z + q;

Exercise 3.4.15 Write a listing, say consolidation1 c, that solves consol-


idation Problem (3.2.9 to 3.2.11) by calling the crank function in Example
2.3.15.

Exercise 3.4.16 Write a listing, say lines heat2, that applies the Method
of Lines to solve the following problem

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

U (x, 0) = x2 , 0 ≤ x ≤ L, (3.4.12)

U (0, t) = 2t, U (L, t) = 2t + L2 , 0 < t ≤ T. (3.4.13)

Exercise 3.4.17 Write listings that solve Problem (3.4.11 to 3.4.13) by


calling the euler e, euler i and crank functions.

Exercise 3.4.18 Consider the Dirichlet–Neumann Problem (3.2.17).


Write the equations for the Method of Lines in matrix form similar to
(3.2.16).

Exercise 3.4.19 Write a listing, say consolidation2 end, that solves


the consolidation Problem (3.2.18 to 3.2.20) by using the Explicit Euler
Method.

Exercise 3.4.20 Write a listing that solves the consolidation Problem


(3.2.18 to 3.2.20) by using the Implicit Euler Method.

Exercise 3.4.21 Verify that U = −1 − 1 + 2(x2 + 2t) is a solution of
Eq. (3.2.23). Write a Dirichlet problem for (3.2.23) that has U as a solution.
Then, provide a function that applies the Method of Lines to solve the
Dirichlet problem above.

Exercise 3.4.22 Write a function that applies the Method of Lines to


solve the Dirichlet problem

Ut − (1 + 2t)Uxx = 0, 0 < x < L, 0 < t ≤ T, (3.4.14)

U (x, 0) = x2 , 0 ≤ x ≤ L, (3.4.15)

U (0, t) = 2t + 2t2 , U (L, t) = L2 + 2t + 2t2 , t > 0, (3.4.16)


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 149

Diffusion and Convection 149

that has the following analytical solution

U (x, t) = x2 + 2t + t2 .

Hint. Some code lines are provided below.


function u = variable2
...
g1 = @(t) 2*t + t2 ;
g2 = @(t) 2*t + t2 + L2 ;
% Method of Lines
[~,y] = ode15s(@system,[0 T],u(2:end-1),[ ], L, n, g1, g2);
...
end
% ———- Local function ————-
function Du = system(t, u, L, n, g1, g2)
dx = L/n;
p = (1 + t)*ones(n+1,1)/dx2;
AA = [p(3:n+1) -2*p(2:n) p(1:n-1)];
A = spdiags(AA, -1:1, n-1, n-1);
Du = A*u + [p(2)*g1(t); zeros(n-3,1); p(n)*g2(t)];
end

Exercise 3.4.23 Prove the stability Condition (3.2.43).

Exercise 3.4.24 Write a function that applies Method (3.2.42) to solve


Problem (3.2.38 to 3.2.40).
Hint. Note that the stability Condition (3.2.43) is the satisfied if 2rM ≤ 1,
where M is the maximum of the function α(x, t).

Exercise 3.4.25 Consider the Dirichlet problem

Ut + vUx − αUxx = 0, v < 0, 0 < x < L, 0 < t ≤ T, (3.4.17)

U (x, 0) = 0, 0 ≤ x ≤ L, (3.4.18)

t/t0 − (t/t0 )2 if t ≤ t0 ,
U (0, t) = 0, U (L, t) = t > 0. (3.4.19)
0 if t > t0 ,

Write a function that applies Method (3.2.48) to solve Problem (3.4.17 to


3.4.19).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 150

150 Matlab for Engineering

Exercise 3.4.26 Consider the Dirichlet problem

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

U (x, 0) = x2 , 0 ≤ x ≤ L, (3.4.21)

U (0, t) = v 2 t2 + 2t, U (L, t) = (L − vt)2 + 2t, t > 0. (3.4.22)

Write a function that applies Method (3.2.48) to solve the problem above.
The analytical solution of Problem (3.4.20 to 3.4.22) is U = (x − vt)2 + 2t.

Exercise 3.4.27 Write a function that applies the Method of Lines


(3.2.48) to solve Eq. (3.2.56) using Transformation (3.2.57 and 3.2.58).

Exercise 3.4.28 Simplify Eq. (3.2.56), where λ = λ(t), to the convection-


diffusion equation.
Hint. Use the change of unknown function
 t 
W = U exp λ(τ )dτ ,
0
and get
 t   t 
Wt = Ut exp λ(τ )dτ + λ(t)U exp λ(τ )dτ ,
0 0
 t   t 
Wx = Ux exp λ(τ )dτ , Wxx = Uxx exp λ(τ )dτ .
0 0

Exercise 3.4.29 Create the variable x = 15;. Use the save sv 1 x


command to save it in the already existing file. Use the whos -file sv 1
command to verify that sv 1 x file has been updated.

Exercise 3.4.30 Create the variable a = pi;. Save it with the save sv 2
a command. Create the vector v = [1 2 3]; and append it in the same file.
Inspect the sv 2 file. Modify v into v = 123;. Append v in the sv 2 file and
inspect the file again.

Exercise 3.4.31 Use the commands


save sv 3
save
and verify that the two files have the same content.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 151

Diffusion and Convection 151

Exercise 3.4.32 Create the variable a = pi; and save it in the sv 4.txt
file. Next, save the same variable with the command
save sv 5.txt a -ascii -double.
Open both files and note the difference.

Exercise 3.4.33 Try the commands


save(’name of file’,’var1’,’var2’,...)
save(’name of file’,’n var1’,’n var2’,’-append’).

Exercise 3.4.34 Use the load sv 1 z command to load the variable z that
does not exist in the sv 1 file. Note the message
Warning: Variable ’z’ not found.
B1948 Governing Asia

This page intentionally left blank

B1948_1-Aoki.indd 6 9/22/2014 4:24:57 PM


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 153

Chapter 4

Introduction to the Finite Element Method

The Finite Element Method (FEM) is introduced in this chapter Hutton


(2004). Scientific research on the FEM began after the Second World
War and was partially supported by the American aerospace industry.
The name appeared for the first time in the paper by Clough (1960).
The Engineering community immediately understood the power of the
new method. The structural engineers made a great contribution to its
development. Expressions such as stiffness matrix, load vector, and others
initially introduced for applications in Structural Mechanics, are used today
for FEM applications in different scientific fields Fenner (2005). Matlab
applications are presented in this book by Kwon and Bang (2000).
The concept of a weak solution to differential equations is closely related
to the FEM. Examples of engineering applications that lead to consider
weak solutions will be discussed. It will be shown that the FEM is able to
provide satisfactory answers in these situations too.
The immediate section below is devoted to the numerical integration
that often occurs in FEM applications.

4.1 Numerical Integration


Consider the function f (x), defined and integrable on the interval (a, b).
Recalling the integral additive property, the interval (a, b) is divided into n
subintervals (xi , xi+1 ), x0 = a, xn = b, with equal h = xi+1 − xi spacing.
The numerical integration is performed on each subinterval separately. The
integral on (a, b) is obtained as the sum of all partial integrals.
The simplest method of numerical integration is the Rectangle Rule.
See Fig. 4.1.1 (left). This method approximates the integral on (xi , xi+1 )
by replacing f (x) with the zero-order polynomial, i.e., the constant function

153
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 154

154 Matlab for Engineering

Figure 4.1.1. Rectangle Rule (left) and Trapezium Rule (right).

p0 (x) = f (xi ) = fi ,
 xi+1  xi+1
f (x) dx ≈ p0 (x) dx = hfi .
xi xi

A similar method is Trapezium Rule. See Fig. 4.1.1 (right). The integral on
(xi , xi+1 ) is approximated by replacing f (x) with the first-order polynomial,
i.e., the linear function p1 (x) = (fi+1 − fi )(x − xi )/h + fi ,
 xi+1  xi+1
f (x) dx ≈ p1 (x) dx = (fi+1 + fi )h/2.
xi xi

The two previous methods are rarely applied, as they provide unsatisfactory
approximations of integrals.
A more accurate method is the Simpson1 Rule. See Fig. 4.1.2. This
method approximates the integral on (xi , xi+2 ) by replacing f (x) with the
second-order polynomial, i.e., the parabola p2 (x) passing through the points
(xi , fi ), (xi+1 , fi+1 ), (xi+2 , fi+2 ),
fi + fi+2 − 2fi+1 f − fi
p2 (x) = 2
(x − xi+1 )2 + i+2 (x − xi+1 ) + fi+1 . (4.1.1)
2h 2h
See Exercise 4.4.1. Therefore, the integral of f (x) on (xi , xi+2 ) is approxi-
mated with
 xi+2  xi+2 
f + fi+2 − 2fi+1 xi+2
f (x) dx ≈ p2 (x) dx = i 2
(x − xi+1 )2 dx
xi xi 2h xi

fi+2 − fi xi+2 h
+ (x − xi+1 ) dx + 2hfi+1 = (fi + 4fi+1 + fi+2 ). (4.1.2)
2h xi 3
The integral on (a, b) is obtained by repeatedly applying (4.1.2). Since a
single application of the Simpson Rule needs two subintervals, it is clear

1 Thomas Simpson, a British scientist, 1710–1761.


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 155

Introduction to the Finite Element Method 155

Figure 4.1.2. Simpson Rule.

that the total number of subintervals must be even. Lastly, the following
formula is obtained for the integral of f (x) on (a, b)
 b
f (x) dx ≈ (h/3)(f0 + 4f1 + 2f2 + · · · + 2fn−2 + 4fn−1 + fn ). (4.1.3)
a

Let us present two Matlab functions able to calculate defined integrals:


integral and int. The second function uses symbolic computation. The
essential syntax for both is outlined below.

integral(f, a, b) int(f(x), x, a, b)
f integrand function f(x) integrand function
a, b integration limits a, b integration limits
x integration variable

Example 4.1.1 Consider the integrals


 b  b
x+1
(px + q) dx, √ dx. (4.1.4)
a a x
The two following listings apply the integral and int functions to calculate
Integrals (4.1.4). In the first, anonymous functions are used to define
integrand functions. Local functions are used in the second.
function integral 1
% This is the function file integral 1.m.
% Integral and int Matlab functions are applied to calculate two integrals.
% Anonymous functions are used to define integrand functions.
a = 1; b = 4; p =1 ; q = 2;
f1 = @(x) p.*x+q;
f2 = @(x) (1+x)./sqrt(x);
Q1 = integral(f1,a,b);
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 156

156 Matlab for Engineering

Q2 = integral(f2,a,b);
fprintf(’integral 1 = %g; integral 2 = %g\n’, Q1, Q2)
x = sym(’x’);% The int function needs symbolic variables.
E1 = int(f1(x),x,a,b);
E2 = int(f2(x),x,a,b);
fprintf(’int 1 = %g; int 2 = %g\n’, double(E1), double(E2))
end
function integral 2
% This is the function file integral 2.m.
% Integral and int Matlab functions are applied to calculate two integrals.
% Local functions are used to define integrand functions.
a = 1; b = 4; p =1 ; q = 2;
Q1 = integral(@(x)f1(x, p, q), a, b);
Q2 = integral(@(x)f2(x),a,b);
x = sym(’x’)
E1 = int(f1(x, p, q), x, a, b);
E2 = int(f2(x),x,a,b);
fprintf(’integral 1 = %g; integral 2 = %g\n’, Q1, Q2)
fprintf(’int 1 = %g; int 2 = %g\n’, double(E1), double(E2))
end
% ———- Local functions ————-
function f = f1(x, p, q)
f = p.*x + q;
end
function f = f2(x)
f = (1+x)./sqrt(x);
end
Other applications are suggested in Exercise 4.4.2.

Example 4.1.2 Let us present a function that applies Simpson Rule to


calculate integrals.
function integral = simpson(f, a, b, n, varargin)
% This is the function file simpson.m.
% Simpson Rule is applied to calculate integrals.
% integral = simpson(f, a, b, n, p1, p2,...)
% f is the integrand function,
% a, b are the integration limits,
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 157

Introduction to the Finite Element Method 157

% n is an even number,
% p1, p2,... are parameters different from the previous ones.
nn = n;% The input value of n is saved in nn.
w = 0;% The test variable w is initialized.
n = double(uint16(abs(n)));% n is modified to a positive integer, if it is not.
if n ~= nn
w = 1;% if n = nn, the value of w changes to 1.
end
if rem(n,2) ~= 0
n = n+1;% n is modified to an even number, if it is not.
w = 1;
end
if n == 0
n = 20; % if n = 0, the value of n changes to 20.
w = 1;
end
x = linspace(a,b,n+1); h = (b - a)/n; integral = 0;
for i = 1:2:n-1 % Simpson Rule.
fi = feval(f, x(i), varargin{:});
fip1 = feval(f, x(i+1), varargin{:});
fip2 = feval(f, x(i+2), varargin{:});
integral = integral + (fi + 4*fip1 + fip2)*h/3;
end
if w > 0 % This means that n is changed. The change must be sent to user.
msg = ’n must be a positive even number; input n changed to:’;
msg = strcat(msg, num2str(n));
warning(msg); % The message is sent to the User.
end
end
Example 4.1.3 The following listing applies the simpson function to
calculate Integrals (4.1.4).
function integral 3
% This is the function file integral 3.m.
% Simpson function is called to calculate two integrals.
a = 1; b = 4; p = 1; q = 2; n = 18;
f1 = @(x) p.*x+q;
f2 = @(x) (1+x)./sqrt(x);
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 158

158 Matlab for Engineering

S1 = simpson(f1,a,b,n);
S2 = simpson(f2,a,b,n);
fprintf(’simpson 1 = %g; simpson 2 = %g\n’, S1, S2)
end
The next example considers the Bessel2 function Jn with n = 1. The
Bessel functions Jn are characterized by a sinusoidal behavior. Some of
them, J0 , J1 and J2 , are plotted in Fig. 4.1.3. The Matlab code that
produces Fig. 4.1.3 is suggested in Exercise 4.4.3.

Example 4.1.4 Consider the integral function


 1−x 
g(x) = (x/z) sin(t)J1 (z) dt, z = (1 − t)2 − x2 , 0 ≤ x ≤ 1.
0

The following listing applies the integral and simpson functions to calculate
g(x). The graphs of g(x) generated by the functions are plotted in Fig.
4.1.4. See Exercise 4.4.4 devoted to a limit that occurs in the listing.
function Q = integral 4
% This is the function file integrale4.m.
% Integral and simpson functions are called to calculate the integral function.

1
J0
J1
J2
0.6

0.2

-0.2

-0.6
-10 -6 -2 2 6 10

Figure 4.1.3. Bessel functions.

2 Friedrich Wilhelm Bessel, a German scientist, 1784–1846. He introduced the functions

that bear his name, together with Daniel Bernoulli.


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 159

Introduction to the Finite Element Method 159

0.035
simpson
0.03 integral

0.025

0.02

0.015

0.01

0.005

0
0 0.2 0.4 0.6 0.8 1

Figure 4.1.4. Graph of the integral function.

L = 1; n = 10; nx = 20;
x = linspace(0,L,nx+1); a = x(1);
S = zeros(nx+1,1); Q = zeros(nx+1,1);
for i=1:nx+1
b =1-x(i);
S(i) = simpson(@f, a, b, n, x(i));
Q(i) = integral(@(t)f(t, x(i)), a, b);
end
plot(x,S,’k’,x,Q,’r*’,’LineWidth’,2);
legend(’simpson’,’integral’,’Location’,’NorthEast’);
end
% ———- Local function ————-
function y = f(t, x)
z = sqrt((1-t).2 - x.2 );
if z > 0
y = sin(t).*besselj(1,z).*x./z;
else
y = sin(t).*x/2; % besselj(1,z)/z –> .5 when z–> 0. See Exercise 4.4.4.
end
end
Consider the double integral
 b  β(x)
dx f (x, y) dy. (4.1.5)
a α(x)
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 160

160 Matlab for Engineering

Let us present two Matlab functions able to calculate Integral (4.1.5):


integral2 and int. The syntax is outlined below.

integral2(f, a, b, α, β) int(int(f(x,y), y, α, β), x, a, b)


f integrand function f(x,y) integrand function
a, b integration limits on x
α, β integration limits on y
x, y integration variables

Example 4.1.5 Consider the integral


 b  2√x
dx xy dy. (4.1.6)
a x2 /4

The following listing applies the integral2 and int functions to calculate
Integral (4.1.6).
function integral 2D 1
% This is the function file integral 2D 1.m.
% Integral2 and int functions are called to calculate the integral.
% Anonymous functions are used to define f(x, y), alfa(x), beta(x).
a = 0; b = 4;
alfa = @(x) x.2 /4;;
beta = @(x) 2*sqrt(x);
f = @(x, y) x.*y;
Q = integral2(f, a, b, alfa, beta);
x = sym(’x’); y = sym(’y’);
E = int(int(f(x,y), y, alfa, beta), x, a, b);
fprintf(’integral2 = %g; int = %g\’, Q, double(E))
end
Another application is suggested in Exercise 4.4.5.
Consider Integral (4.1.5). By setting
 β(x)
g(x) = f (x, y) dy, (4.1.7)
α(x)

Integral (4.1.5) simplifies to


 b  β(x)  b
dx f (x, y) dy = g(x) dx. (4.1.8)
a α(x) a
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 161

Introduction to the Finite Element Method 161

Figure 4.1.5. Mesh for 2D integrals.

Formula (4.1.8) is used to generalize the Simpson Rule to a 2D Integral


(4.1.5). The interval (a, b) is divided into n subintervals (xi , xi+1 ), x0 = a,
xn = b, with equal h = xi+1 − xi spacing. See Fig. 4.1.5. In addition,
for any i = 0, . . . , n, the interval (αi , βi ) = (α(xi ), β(xi )) is divided into
m subintervals (yij , yij+1 ), yi0 = αi , yim = βi , with equal hi spacing. Let
us apply the Simpson Rule and recall that n and m must be even. By
considering (4.1.8), Integral (4.1.5) is approximated by
 b  β(x)
h
dx f (x, y) dy ≈ (g +4g1 +2g2 +· · ·+2gn−2 +4gn−1 +gn ) (4.1.9)
a α(x) 3 0

with gi given by

gi ≈ (hi /3)(fi0 + 4fi1 + 2fi2 + · · · + 2fim−2 + 4fim−1 + fim ). (4.1.10)

A function that generalizes the simpson function to a 2D case is illustrated


in the following example.

Example 4.1.6 The following function applies Formulas (4.1.9)–(4.1.10)


to solve Integral (4.1.5).
function integral = simpson2d(f, a, b, alfa, beta, n, m, varargin)
% This is the function file simpson2d.m.
% Simpson Rule is applied to calculate double integrals.
% integral = simpson2d(f, a, b, alfa, beta, n, m, p1, p2,...)
% f is the integrand function,
% a, b are the integration limits on x,
% alfa, beta are the integration limits on y,
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 162

162 Matlab for Engineering

% n, m are even numbers,


% p1, p2,... are parameters different from the previous ones.
nn = n; mm = m;
w = 0;
n = double(uint16(abs(n))); m = double(uint16(abs(m)));
if n ~= nn || m ~= mm
w = 1;
end
if rem(n,2)~ = 0
n = n + 1; w = 1;
end
if rem(m,2)~ = 0
m = m + 1; w = 1;
end
if n == 0
n = 10; w = 1;
end
if m == 0
m = 10; w = 1;
end
x = linspace(a, b, n+1); h = (b-a)/n;
g = zeros(n+1,1);
for i = 1:n+1
xx = x(i); c = feval(alfa,xx); d = beta(xx);
yi = linspace(c, d, m+1); hi = (d-c)/m;
for j = 1:2:m-1
fi = feval(f, xx, yi(j), varargin{:});
fip1 = feval(f, xx, yi(j+1), varargin{:});
fip2 = feval(f, xx, yi(j+2), varargin{:});
g(i) = g(i) + (fi + 4*fip1 + fip2)*hi/3;
end
end
integral = 0;
for i = 1:2:n-1
integral = integral + (g(i) + 4*g(i+1) + g(i+2))*h/3;
end
if w>0
msg = ’n, m must be positive even numbers; n, m changed to:’;
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 163

Introduction to the Finite Element Method 163

msg = strcat(msg, num2str(n), ’;’, num2str(m));


warning(msg);
end
end

Example 4.1.7 The following listing calls the simpson2d function to


calculate Integral (4.1.6).
function integral 2D 3
% This is the function file integral 2D 3.m.
% Simpson2d function is called to calculate the integral.
% Anonymous functions are used to define f(x, y), alfa(x), beta(x).
a = 0; b = 4; m = 10; n = 30;
alfa = @(x) x.2 /4;
beta = @(x) 2*sqrt(x);
f = @(x,y) x.*y;
S = simpson2d(f, a, b, alfa, beta, n, m);
fprintf(’ simpson2d = %g\n’, S)
end

Example 4.1.8 Consider the integral function


 L  x−ξ+L
g(x) = dξ f (ξ, t)J0 (z) dt, 0 < x < L,
x 0
where

 sin(t − ξ), if τ > ξ,
z = (L − t)2 − (x − ξ)2 , f (ξ, τ ) =
0, if τ ≤ ξ.
The following listing calls the integral2 and simpson2d functions to calculate
g(x). The graphs of g(x) returned by the functions are shown in Fig. 4.1.6.

function integral 2D 4
% This is the function file integral 2D 4.m.
% Simpson2d and integral2 functions are called to calculate
% the integral function.
L = 5; m = 20; n = 44;
nx = 20; x = linspace(0,L,nx+1);
alfa = @(xi) 0*xi;
S = zeros(nx+1,1); Q = zeros(nx+1,1);
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 164

164 Matlab for Engineering

0.6

0.2

simpson2d
integral2
-0.2
0 1 2 3 4 5

Figure 4.1.6. Integral function.

for i=1:nx+1
a = x(i);
beta = @(xi) a + L - xi;
f = @(xi, t) besselj(0,sqrt((L - t).2 - (a - xi).2 )).*sin(t - xi).*(t - xi> 0);
Q(i) = integral2(f, a, L, alfa, beta);
S(i) = simpson2d(f, a, L, alfa, beta, n, m);
end
plot(x,S,’k’,x,Q,’r*’,’LineWidth’,2);
legend(’simpson2d’,’integral2’,’Location’,’SouthWest’);
end

4.2 Finite Element Method


4.2.1 Axial Motion of a Bar
Consider the axial motion of a prismatic bar having a straight longitudinal
axis and constant cross-sectional area A, subjected to an axial distributed
force per unit length F (x, t). In Fig. 4.2.1, U (x, t) is the displacement and
σ(x, t) is the stress. The equation for the axial motion of a bar is derived
from Newton’s Second Law

 x2  x2
∂2U
A ρ dx = Aσ(x2 , t) − Aσ(x1 , t) + F dx, (4.2.1)
x1 ∂t2 x1
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 165

Introduction to the Finite Element Method 165

Figure 4.2.1. Axial motion.

where ρ is the density of the bar. From the previous equation, it follows
that
 x2 2  x2  x2
∂ U ∂σ
A ρ 2 dx = A + F dx.
x1 ∂t x1 ∂x x1

Note that the previous equation holds for any control volume V = A(x2 −
x1 ). Therefore, it implies
∂2U ∂σ
Aρ 2
=A + F. (4.2.2)
∂t ∂x
If the bar is a linear-elastic material, then the relationship between stress
σ and strain ε = ∂U/∂x is given by Hooke’s3 law
∂U
σ = Eε = E , (4.2.3)
∂x
where E is Young’s4 modulus. Substituting (4.2.3) into (4.2.2), one arrives
at the equation for the axial motion of a bar
 
∂2U ∂ ∂U
Aρ 2 = A E + F. (4.2.4)
∂t ∂x ∂x
Equation (4.2.4) is named the wave equation. It is a hyperbolic partial
differential equation. If E is constant, Eq. (4.2.4) simplifies to
∂2U ∂2U
Aρ − AE = F, (4.2.5)
∂t2 ∂x2
∂ 2U 2
2∂ U
− c = F/A/ρ,
∂t2 ∂x2

where c = E/ρ is the velocity of wave propagation.

3 Robert Hooke, a British scientist, 1635–1761. He introduced the law of elasticity that

bears his name.


4 Thomas Young, a British scientist, 1773–1829. He introduced the constant of elasticity

that bears his name.


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 166

166 Matlab for Engineering

Suppose that the bar is subjected to axial concentrated forces too

{(Pi , Fi ), i = 1, . . . , N }.

In this case Eq. (4.2.1) is modified to


 x2  x2 n2

∂2U
A ρ 2 dx = Aσ(x2 , t) − Aσ(x1 , t) + F dx + Fi , (4.2.6)
x1 ∂t x1 i=n1

where Fi , i = n1 , . . . , n2 , are the axial concentrated forces between x1 and


x2 . To understand what happens in this situation, let us consider an interval
where there is only one concentrated force, say (xh , Fh ). Assuming, without
loss of generality, that x1 = xh −Δx and x2 = xh +Δx, Eq. (4.2.6) is written
as
 xh +Δx 2  xh +Δx
∂ U
A ρ 2 dx = Aσ(xh +Δx, t)−Aσ(xh −Δx, t)+ F dx+Fh .
xh −Δx ∂t xh −Δx

For Δx → 0, the previous equation gives

Aσ(x+ −
h , t) − Aσ(xh , t) = −Fh , (4.2.7)

i.e., the stress σ is discontinuous for x = xh . Therefore, σ cannot be


differentiated for x = xh and U cannot be a (classical) solution of Eq.
(4.2.5).
In Statics Eq. (4.2.5) simplifies to

−AEU  (x) = F (x). (4.2.8)

Appropriate boundary conditions are associated to Eq. (4.2.8). For example,


for a bar with both ends fixed, they are (Fig. 4.2.2)

U (xA ) = 0, U (xB ) = 0. (4.2.9)

RA RB
A B
Figure 4.2.2. Fixed end-fixed end bar.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 167

Introduction to the Finite Element Method 167

FB

A B A B

Figure 4.2.3. Fixed end-free end bar.

Moreover, the constraint reactions RA and RB are expressed as

RA = −Aσ(xA ) = −AEU  (xA ), RB = Aσ(xB ) = AEU  (xB ). (4.2.10)

The first relationship in (4.2.10) is derived by considering (4.2.1) on the


control volume V = A(x2 − x1 ) with x1 = xA and x2 = xA + Δx
 xA +Δx
Aσ(xA + Δx) + RA + F dx = 0.
xA

When Δx → 0, the previous equation gives (4.2.10)1 . The second


relationship in (4.2.10) is similarly derived.
Consider a bar with one end fixed and the other end free. If the free
end is subjected to a known external force FB , the boundary conditions are
(Fig. 4.2.3, left)

U (xA ) = 0, AEU  (xB ) = FB . (4.2.11)

The second boundary Condition follows from (4.2.10)2 . If FB = 0, boundary


Conditions (4.2.11) simplify to (Fig. 4.2.3, right)

U (xA ) = 0, U  (xB ) = 0.

See Exercises 4.4.6–4.4.10.

4.2.2 Weak Solution


The equation governing the axial displacements of a bar will be assumed
as an equation model for the introduction to the FEM. Therefore, consider
the equation

−U  (x) = F (x), 0 < x < L, (4.2.12)

where the constant AE was included in F . Multiply Eq. (4.2.12) by a


smooth function v(x), named test function, and integrate over [0, L]
 L  L

− U (x)v(x) dx = F (x)v(x) dx. (4.2.13)
0 0
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 168

168 Matlab for Engineering

Integrate by parts, the first integral and obtain


 L  L
  L
U (x)v (x) dx = F (x)v(x) dx + [U  v]0 . (4.2.14)
0 0

Equation (4.2.14) is the weak form of differential Eq. (4.2.12). By contrast,


the original form is named the strong form. It is important to note that the
integration by parts has lowered the greatest order of differentiation, with
the consequence that solutions to the weak form can now exist that do not
have the necessary regularity to be solutions to the strong form. A solution
to the weak form is named a weak solution. A solution to the strong form
is named a classical solution or strong solution.
The FEM considers the weak form. The approximated solution u is
expressed as a finite series
n
u(x) = uj Φj (x), (4.2.15)
j=0

where uj are unknown coefficients and Φj are known functions. Imposing


that u satisfies Eq. (4.2.14) with v = Φi , ∀Φi yields
n
  L  L
L
uj Φj (x)Φi (x) dx = F (x)Φi (x) dx + [u Φi ]0 , ∀Φi . (4.2.16)
j=0 0 0

By introducing the definitions


 L  L
L
Kij = Φi (x)Φj (x) dx, fi = F (x)Φi (x) dx + [u Φi ]0 . (4.2.17)
0 0

System (4.2.16) can be written in matrix notation


n

Kij uj = fi , i = 0, . . . , n, (4.2.18)
j=0

and, in compact form, as follows

Ku = f , (4.2.19)

where u and f are column vectors. The matrix K is named the stiffness
matrix and the vector f load vector. The names come from Mechanics of
Structures where the FEM was initially introduced.
Consider Eq. (4.2.13). If F is a force and v is a displacement, Eq.
(4.2.13) is the Principle of Virtual Work. The approximating solution
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 169

Introduction to the Finite Element Method 169

Figure 4.2.4. Shape functions.

provided by the FEM is in agreement with the weak form of such a principle.
The Principle of Virtual Work was introduced by Lagrange5.

4.2.3 Shape Functions


The shape functions Φi are the linear functions defined by (see Fig. 4.2.4)

⎨ (x − xi−1 )/(xi − xi−1 ), xi−1 ≤ x ≤ xi ,
Φi (x) = (xi+1 − x)/(xi+1 − xi ), xi ≤ x ≤ xi+1 , (4.2.20)

0, x ∈ [xi−1 , xi+1 ].
For a regular grid with step h = xi+1 − xi , Formula (4.2.20) simplifies to

⎨ (x − xi−1 )/h, xi−1 ≤ x ≤ xi ,
Φi (x) = (xi+1 − x)/h, xi ≤ x ≤ xi+1 , (4.2.21)

0, x ∈ [xi−1 , xi+1 ].
In addition, the derivative is⎧
⎨ 1/h, xi−1 < x < xi ,
Φi (x) = −1/h, xi < x < xi+1 , (4.2.22)

0, x ∈]xi−1 , xi+1 [.
From Definition (4.2.20), it follows that
1, i = j,
Φi (xj ) = δij = (4.2.23)
 j,
0, i =
where δij is a Kronecker6 δ function. Property (4.2.23) characterizes the
shape functions. Indeed, if it is assumed that Φi is linear

5 Giuseppe Luigi Lagrange, an Italian scientist, 1736–1813. He studied at the University

of Turin. He was Professor at the University of Paris and the University of Berlin. He
made significant contributions in Mechanics and the Calculus of Variations. He published
Mécanique Analitique, the most comprehensive treatise since Newton’s on Mechanics.
6 Leopold Kronecker, a German scientist, 1823–1891. He made scientific contributions in

Algebra and Logic.


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 170

170 Matlab for Engineering

Φi (x) = ai x + bi , (4.2.24)

and has Property (4.2.23), then Φi is given by (4.2.20). To prove the


statement above, use Property (4.2.23) in (4.2.24) and obtain

Φi (xi ) = 1, ai xi + bi = 1,
xi ≤ x ≤ xi+1 .
Φi (xi+1 ) = 0, ai xi+1 + bi = 0,

Solving with respect to ai , bi and the result substituted into (4.2.24) yields

x xi+1 x −x
Φi (x) = − + = i+1 , xi ≤ x ≤ xi+1 ,
xi+1 − xi xi+1 − xi xi+1 − xi

that is the first part of the desired result. Similarly, from

Φi (xi ) = 1, ai xi + bi = 1,
xi−1 ≤ x ≤ xi ,
Φi (xi−1 ) = 0, ai xi−1 + bi = 0,

it follows that
x − xi−1
Φi (x) = , xi−1 ≤ x ≤ xi .
xi − xi−1

The interval [xi , xi+1 ] is named element and the point xi is named node
in the FEM context. The shape functions are a basis for the linear functions
defined on the element (Fig. 4.2.5). Indeed, consider the linear function

u(x) = ax + b, x ∈ ei = [xi , xi+1 ], (4.2.25)

and let ui = u(xi ) be the values of u in the nodes xi . The coefficients a and
b can be expressed in terms of ui and ui+1 by considering the system

axi + b = ui ,
axi+1 + b = ui+1 .

Solving with respect to a and b and the result substituted into (4.2.25)
yields
ui+1 − ui ux − ui+1 xi x − xi x −x
u(x) = x + i i+1 = u + i+1 u,
xi+1 − xi xi+1 − xi xi+1 − xi i+1 xi+1 − xi i

u(x) = ui Φi (x) + ui+1 Φi+1 (x), x ∈ ei = [xi , xi+1 ], (4.2.26)


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 171

Introduction to the Finite Element Method 171

Figure 4.2.5. Shape functions on an element.

proving that u is a linear combination of Φi . It remains to show that

u(x) = 0, x ∈ ei = [xi , xi+1 ], ⇔ ui = ui+1 = 0. (4.2.27)

The condition ⇐ is apparent. Prove ⇒. Evaluate u(xi ) from (4.2.26) and


obtain u(xi ) = ui . The hypothesis u(xi ) = 0 implies ui = 0. Similarly,
ui+1 = 0 is derived from u(xi+1 ) = 0.
Let u(x) be a continuous function defined on [0, L]. If the function
is linear on each element ei = [xi , xi+1 ], the function is named piecewise
linear. Consider the approximating solution in the FEM

n

u(x) = uj Φj (x), x ∈ [0, L], (4.2.28)
j=0

where Φj is the shape function. From the reasoning above, we immediately


realize that the shape functions are a basis for the piecewise linear function
on [0, L]. Therefore, the FEM approximates the exact solution with a
piecewise linear function.

4.2.4 Boundary Value Problems


Let us discuss the Dirichlet problem

−U  = F (x), 0 < x < L, U (0) = U0 , U (L) = UL . (4.2.29)

Consider the weak form written in terms of the shape functions defined in
the previous section
 L  L
 L
u (x)Φi (x) dx = F (x)Φi (x) dx + [u Φi ]0 , i = 0, . . . , n. (4.2.30)
0 0
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 172

172 Matlab for Engineering

The approximating solution u is expressed as


n

u(x) = uj Φj (x), (4.2.31)
j=0

with uj unknown coefficients. Note that

u(0) = u0 , u(L) = un ,

because of Property (4.2.23) of the shape functions. Therefore, if it is


assumed that

u 0 = U0 , u n = UL ,

the approximating solution satisfies the boundary conditions in (4.2.29) and


can be written as
n−1

u(x) = uj Φj (x) + U0 Φ0 (x) + UL Φn (x), (4.2.32)
j=1

where the last two terms are known. Substituting (4.2.32) into (4.2.30)
yields
  L
n−1  L  L
uj Φi (x)Φj (x) dx + U0 Φi (x)Φ0 (x) dx + UL Φi (x)Φn (x) dx
j=1 0 0 0

 L
= F (x)Φi (x) dx, ∀Φi , i = 1, . . . , n − 1. (4.2.33)
0
as

Φi (0) = 0, Φi (L) = 0, i = 1, . . . , n − 1.

Setting
 L  L
Kij = Φi (x)Φj (x) dx, fi = F (x)Φi (x) dx, i, j = 1, . . . , n − 1,
0 0
 L  L
gi = U0 Φi (x)Φ0 (x) dx + UL Φi (x)Φn (x) dx, i = 1, . . . , n − 1,
0 0

System (4.2.33) is written as


n−1

Kij uj = fi − gi , i = 1, . . . , n − 1.
j=1
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 173

Introduction to the Finite Element Method 173

It is a linear algebraic system of n − 1 equations in the n − 1 unknowns


u1 , . . . , un−1 . Let us evaluate the elements of the symmetric matrix Ki,j .
Consider the main diagonal
 xi+1  xi+1
2
Ki,i = (Φi )2 (x) dx = (1/h2 ) dx = , i = 1, . . . , n−1, (4.2.34)
xi−1 xi−1 h

as the support of Φi is the interval [xi−1 , xi+1 ]. Consider Ki,i+1


 xi+1  xi+1
  dx 1
Ki,i+1 = Φi (x)Φi+1 (x)dx = − 2
= − , i = 1, . . . , n − 2,
xi xi h h
(4.2.35)
as the support of Φi Φi+1 is the interval [xi , xi+1 ]. The remaining elements
are zero. For example, consider
 L
K1,3 = Φ1 (x)Φ3 (x) dx.
0

The support of Φ1 is the interval [x0 , x2 ], the support of Φ3 is the interval


[x2 , x4 ], their intersection is void and K1,3 = 0. Let us evaluate the elements
of the vector gi . Consider
 L  L
g1 = U0 Φ1 (x)Φ0 (x) dx + UL Φ1 (x)Φn (x) dx. (4.2.36)
0 0

The supports of Φ0 , Φ1 and Φn are the intervals [x0 , x1 ], [x0 , x2 ] and


[xn−1 , xn ], respectively. Hence, Formula (4.2.36) simplifies to
 x1
g1 = U0 Φ0 (x)Φ1 (x) dx = −U0 /h. (4.2.37)
x0

Consider
 L  L
gn−1 = U0 Φn−1 (x)Φ0 (x) dx + UL Φn−1 (x)Φn (x) dx. (4.2.38)
0 0

The supports of Φ0 , Φn−1 and Φn are the intervals [x0 , x1 ], [xn−2 , xn ] and
[xn−1 , xn ], respectively. Hence, Formula (4.2.38) simplifies to
 xn
gn−1 = UL Φn−1 (x)Φn (x) dx = −UL /h. (4.2.39)
xn−1

In addition, gi = 0, i = 2, . . . , n − 2. For example, consider


 L  L
 
g2 = U0 Φ2 (x)Φ0 (x) dx + UL Φ2 (x)Φn (x) dx.
0 0
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 174

174 Matlab for Engineering

The supports of Φ0 , Φ2 and Φn are the intervals [x0 , x1 ], [x1 , x3 ] and


[xn−1 , xn ], respectively. The integrals are zero and g2 = 0. In conclusion, by
considering (4.2.34)–(4.2.39), one arrives at the following algebraic system
for the unknown coefficients uj
⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
2 −1 u1 f1 U0
1⎢⎢ −1 2 −1 ⎥ ⎢ u2 ⎥ ⎢ f2 ⎥ 1 ⎢ 0 ⎥
⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥
h ⎣ . . . ⎦⎣ · ⎦ = ⎣ . ⎦ + h ⎣ . ⎦. (4.2.40)
−1 2 un−1 fn−1 UL

Example 4.2.1 The following listing provides a function that applies the
FEM to the Dirichlet Problem (4.2.29).
function u = fem dd(L, u0, uL, F, n)
% This is the function file fem dd.m.
% FEM is applied to solve the Dirichlet problem:
% -U”(x) = F(x), U(0) = U0, U(L) = UL.
% The input arguments are: length, boundary conditions, forcing term,
% number of elements. The function returns a vector with the solution.
% Initialization
h = L/n; x = linspace(0,L,n+1);
u = zeros(n+1,1); f = zeros(n-1,1);
Phil = @(xi, x1, x2) (xi-x1)/(x2-x1);
Phir = @(xi, x2, x3) -(xi-x3)/(x3-x2);
% FEM
KK = [-ones(n-1,1) 2*ones(n-1,1) -ones(n-1,1)];
K = spdiags(KK, -1:1, n-1, n-1)/h;
for i = 2:n
f(i-1) = integral(@(xi)F(xi).*Phil(xi,x(i-1),x(i)), x(i-1), x(i));
f(i-1) = f(i-1) + integral(@(xi)F(xi).*Phir(xi,x(i),x(i+1)), x(i), x(i+1));
end
f(1) = f(1) + u0/h; f(n-1) = f(n-1) + uL/h;
u(2:end-1) = K\f;
u(1) = u0; u(n+1) = uL;
end

Example 4.2.2 Consider the Dirichlet problem

−U  = c sin(ωx), 0 < x < L, U (0) = U0 , U (L) = UL . (4.2.41)


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 175

Introduction to the Finite Element Method 175

Figure 4.2.6. Graph of the approximating solution of Problem (4.2.41).

The following listing applies the fem dd function to Problem (4.2.41). The
graph of the approximating solution is shown in Fig. 4.2.6.
function u = fem dd ex1
% This is the function file fem dd ex1.m.
% fem dd function is called to solve the special Dirichlet problem:
% -U”(x) = c * sin(om*x), U(0) = U0, U(L) = UL.
% Analytical solution:
% U = c*sin(om*x)/om2 + x*(UL- U0 -c*sin(om*L)/om2)/L +U0;
L = 2; n = 20; U0 = 1; UL = 2; om = 5; c = -3;
F = @(xi) c*sin(xi*om);
u = fem dd(L, U0, UL, F, n);
x = linspace(0,L,n+1);
U = c*sin(om*x’)/om2 + x’*(UL - U0 - c*sin(om*L)/om2)/L + U0;
fprintf( ’Maximum error = %g\n’,max(abs(U - u)))
plot(x,u,’k’,x,U,’r*’,’LineWidth’,2);
legend(’FEM’,’Exact’,’Location’,’SouthEast’);
xlabel(’x’); ylabel(’U’);
title([’-U”= c sin(\omega x), U(0)=’,num2str(U0),’, U(L)=’,num2str(UL)]);
end
Another application is suggested in Exercise 4.4.11.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 176

176 Matlab for Engineering

Let us discuss the Dirichlet–Neumann problem

−U  = F (x), 0 < x < L, U (0) = U0 , U  (L) = UL . (4.2.42)

Consider the weak form


 L  L
u (x)Φi (x) dx = F (x)Φi (x) dx+UL Φi (L)−U  (0)Φi (0), i = 0, . . . , n.
0 0
(4.2.43)
The approximating solution u that satisfies the boundary condition u(0) =
U0 is given by
n

u(x) = uj Φj (x) + U0 Φ0 (x). (4.2.44)
j=1

Substituting (4.2.44) into (4.2.43) yields


n  L  L
uj Φi (x)Φj (x) dx + U0 Φi (x)Φ0 (x) dx = UL Φi (L)
j=1 0 0

 L
+ F (x)Φi (x) dx, ∀Φi , i = 1, . . . , n. (4.2.45)
0
as

Φi (0) = 0, i = 1, . . . , n.

By introducing the definitions


 L  L
 
Kij = Φi (x)Φj (x) dx, fi = F (x)Φi (x) dx, i, j = 1, . . . , n,
0 0
 L
gi = UL Φi (L) − U0 Φi (x)Φ0 (x) dx, i = 1, . . . , n,
0

System (4.2.45) is written as


n

Kij uj = fi + gi , i = 1, . . . , n, ⇔ Ku = f + g. (4.2.46)
j=1

It is a linear algebraic system of n equations with n unknowns u1 , . . . , un .


The elements of K different from Kn,n were calculated in Formulas (4.2.34)–
(4.2.35). Let us evaluate Kn,n
 xn  xn
Kn,n = (Φn )2 (x) dx = (1/h2 ) dx = 1/h, (4.2.47)
xn−1 xn−1
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 177

Introduction to the Finite Element Method 177

as the support of Φn is the interval [xn−1 , xn ]. The elements of the vector


f are given by
 xi+1  xn
fi = F (x)Φi (x) dx, i = 1, . . . , n − 1, fn = F (x)Φn (x) dx,
xi−1 xn−1
(4.2.48)
as the supports of Φi , i = 1, . . . , n − 1, are the intervals [xi−1 , xi+1 ] and the
support of Φn is the interval [xn−1 , xn = L]. Let us evaluate the elements
of the vector g. Consider g1 and note that Φ1 (L) = 0. Therefore,
 x1
g1 = −U0 Φ1 (x)Φ0 (x) dx = U0 /h.
x0

Consider gn . Note that Φn (L) = 1 and the support of Φn (x)Φ0 (x) is void.
Therefore,

gn = UL .
In addition,
gi = 0, i = 2, . . . , n − 1
as Φi (L) = 0, i = 2, . . . , n−1, and the supports of Φi (x)Φ0 (x), i = 2, . . . , n−
1, are void. By considering the previous results, linear System (4.2.46) is
specified in
⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
2 −1 u1 f1 U0 /h
⎢ −1 2 −1 ⎥⎢ u2 ⎥ ⎢ f2 ⎥ ⎢ 0 ⎥
1⎢

⎥⎢ ⎥ ⎢ ⎥ ⎢
⎥⎢ · ⎥ = ⎢ . ⎥ + ⎢ . ⎥ . (4.2.49)

⎢ . . . ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
h⎣
−1 2 −1 ⎦⎣ un−1 ⎦ ⎣ fn−1 ⎦ ⎣ 0 ⎦
−1 1 un fn UL

Example 4.2.3 The following listing presents a function that applies the
FEM to the Dirichlet–Neumann Problem (4.2.42).
function u = fem dn(L, u0, uxL, F, n)
% This is the function file fem dn.m.
% The FEM is applied to solve the Dirichlet–Neumann problem:
% -U”(x) = F(x), U(0) = U0, Ux(L) = UxL.
% The input arguments are: length, boundary conditions, forcing term,
% number of elements. The function returns a vector with the solution.
% Initialization
h = L/n; x = linspace(0,L,n+1);
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 178

178 Matlab for Engineering

u = zeros(n+1,1); f = zeros(n,1);
Phil = @(xi, x1, x2) (xi-x1)/(x2-x1);
Phir = @(xi, x2, x3) -(xi-x3)/(x3-x2);
% FEM
KK = [-ones(n,1) [2*ones(n-1,1);1] -ones(n,1)];
K = spdiags(KK, -1:1, n, n)/h;
for i = 2:n
f(i-1) =integral(@(xi)F(xi).*Phil(xi,x(i-1),x(i)), x(i-1), x(i));
f(i-1) = f(i-1) + integral(@(xi)F(xi).*Phir(xi,x(i),x(i+1)), x(i), x(i+1));
end
f(n) = integral(@(xi)F(xi).*Phil(xi,x(n),x(n+1)), x(n), x(n+1)) + uxL;
f(1) = f(1) + u0/h;
u(2:end) = K\f;
u(1) = u0;
end

Example 4.2.4 Consider the Dirichlet–Neumann problem

−U  = cx2 , 0 < x < L, U (0) = U0 , U  (L) = UL . (4.2.50)

The following listing applies the fem dn function to Problem (4.2.50). The
graph of the approximating solution is shown in Fig. 4.2.7.

- U" = c x 2 , U(0) = 1, Ux(L) = -1.5


2.4

2.2

1.8
U
1.6

1.4

1.2 FEM
Exact
1
0 0.4 0.8 x 1.2 1.6 2

Figure 4.2.7. Graph of the approximating solution of the Problem (4.2.50).


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 179

Introduction to the Finite Element Method 179

function u = fem dn ex1


% This is the function file fem dn ex1.m.
% fem dn function is called to solve the special Dirichlet-Neumann problem:
% -U”(x) = c * x2 , U(0) = U0, Ux(L) = UxL.
% Analytical solution: U = -c*x.4 /12 + (c*L3 /3 + UxL)*x + U0;
L = 2; n = 20; U0 = 1; UxL = -1.5; c = 1;
F = @(xi) c*xi.2 ;
u = fem dn(L, U0, UxL, F, n);
x = linspace(0,L,n+1);
U = -c*x’.4 /12 + (c*L3 /3 + UxL)*x’ + U0;
fprintf( ’Maximum error = %g\n’,max(abs(U - u)))
plot(x,u,’k’,x,U,’r*’,’LineWidth’,2);
legend(’FEM’,’Exact’,’Location’,’SouthEast’);
xlabel(’x’); ylabel(’U’);
title([’- U” = c x^2, U(0) = ’,num2str(U0),’, Ux(L) = ’, num2str(UxL)]);
end
Another application is suggested in Exercise 4.4.12.
Consider the pure Neumann problem

−U  = F (x), 0 < x < L, U  (0) = U0 , U  (L) = UL . (4.2.51)

Integrate the differential equation over (0, L) and use the boundary
conditions
 L
 
U0 − UL = F (x)dx. (4.2.52)
0

The two constants U0


and UL ,
and the function F (x) are known assigned
data. Therefore, Relationship (4.2.52) is a compatibility condition for
Problem (4.2.51). If such a condition is not satisfied from the data, the pure
Neumann problem has no solution. In addition, suppose that Condition
(4.2.52) is satisfied and let U1 be a solution to Problem (4.2.51). Consider
the function

U2 = U1 + c, (4.2.53)

where c is an constant. As the derivatives of U2 are the same as those of U1 ,


function U2 is the solution to Problem (4.2.51) as well. Let us summarize.
Problem (4.2.51) has no solutions unless Condition (4.2.52) is satisfied.
When the compatibility condition on data is satisfied, Problem (4.2.51)
has infinite solutions in agreement with Formula (4.2.53). Both Formulas
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 180

180 Matlab for Engineering

F0 q FL

Figure 4.2.8. Pure Neumann problem in Statics.

(4.2.52) and (4.2.53) have an interesting meaning in Statics, illustrated


below. Rewrite (4.2.51) using the equation for the axial displacement of a
bar in Statics

−AEU  = q(x), 0 < x < L, −AEU  (0) = F0 , AEU  (L) = FL , (4.2.54)

where q(x) is the axial load and F0 and FL are known external forces (Fig.
4.2.8). Integrate the differential equation over (0, L) and use the boundary
conditions
 L
F0 + FL + q(x)dx = 0. (4.2.55)
0

Relationship (4.2.55) is the version in Statics of compatibility Condition


(4.2.52). The meaning is clear. An equilibrium implies that the total sum
of the forces must be zero. The meaning of (4.2.53) in Statics is clear as
well. The displacement is determined unless a rigid translation does not
influence the stress.

4.2.5 Axial Displacement and Stress in a Bar


Consider the equation for the axial displacement of a bar in Statics (Sec.
4.2.1)

−AEU  = q(x), 0 < x < L, (4.2.56)

where q(x) is the axial load and L is the length of the bar. Appropriate
boundary conditions are associated to Eq. (4.2.56). For example, for the
fixed end-fixed end bar in Fig. 4.2.9, the boundary conditions are

U (0) = 0, U (L) = 0. (4.2.57)

Boundary value Problem (4.2.56)–(4.2.57) is similar to those already


discussed in the previous sections. Therefore, the FEM is able to provide
the axial displacement of a bar. Now, consider the axial stress σ = EU 
that may be even more important in engineering applications. It would be
desirable that the FEM could calculate σ at least with the same accuracy as
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 181

Introduction to the Finite Element Method 181

U . Let us show that σ can be calculated by using the coefficients ui again,


i.e., with the same accuracy. Consider the element ei = [xi , xi+1 ] and the
related approximated solution

u = ui Φi + ui+1 Φi+1 .

Inserting the previous function in the weak formula restricted to ei yields


(see Sec. 4.2.2)
x x x x
ui x i+1 Φi Φi dx + ui+1 x i+1 Φi+1 Φi dx = x i+1 F Φi dx + [U  Φi ]xi+1 ,
i i i i

 xi+1 x x x
ui xi Φi Φi+1 dx + ui+1 x i+1 Φi+1 Φi+1 dx = x i+1 F Φi+1 dx + [U  Φi+1 ]xi+1 .
i i i

Hence,
 xi+1
(ui − ui+1 )/h = xi
F Φi dx − U  (xi ),
 xi+1 (4.2.58)
(−ui + ui+1 )/h = xi
F Φi+1 dx + U  (xi+1 ).

After finding ui , System (4.2.58) gives the stress on the ends of any element
ei . Lastly, the reactive forces are derived from Formulas (4.2.10).

Example 4.2.5 Consider the bar in Fig. 4.2.9 and the related boundary
value Problem (4.2.56)–(4.2.57). Suppose the bar is subjected to the
trapezoidal load

q(x) = qA + (qB − qA )x/L. (4.2.59)

The following listing applies the FEM to calculate displacement, stress with
Formula (4.2.58), and reactive forces. The graphs of the solutions are shown
in Fig. 4.2.10. The analytical solution is provided in Exercise 4.4.6.
function [u, sigma] = stress 1
% This is the function file stress 1.m.
% FEM is applied to solve the following problem:
% -AEU”(x) = q(x), q(x) = qA + (qB - qA)*x/L, U0 = U(L) = 0,

RA q RB

A B
Figure 4.2.9. Fixed end-fixed end bar subjected to axial load.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 182

182 Matlab for Engineering

Displacement Stress
0.09 8

0.06 2
U
0.03 -4
FEM FEM
Exact Exact
0 -10 0
0 1000 x 2000 3000 1000 x 2000 3000

Figure 4.2.10. Displacement and stress of the bar subjected to axial load (4.2.59).

% Analytical solution:
% U = qA/2/A/E*(Lx - x2 ) + (qB - qA)/6/A/E/L(L2x - x3 );
% Initialization
n = 10; u0 = 0; uL = 0;
L = 3000; % mm
E = 80000; % N/mm2
A = 1000; % mm2
qA = 4; % N/mm
qB = 8; % N/mm
Phil = @(xi, x1, x2) (xi-x1)/(x2-x1);
Phir = @(xi, x2, x3) -(xi-x3)/(x3-x2);
F = @(xi) qA/A/E + (qB - qA)*xi/L/A/E;
h = L/n; x = linspace(0,L,n+1);
u = zeros(n+1,1); sigma = zeros(n+1,1); f = zeros(n-1,1);
U = qA/2/A/E*(L*x’-x’.2) + (qB - qA)/6/A/E/L*(L2*x’ - x’.3 );
S = qA/A*(L/2-x’) + (qB - qA)/A*(L/6-x’.2 /2/L);% Exact stress
% FEM
KK = [-ones(n-1,1) 2*ones(n-1,1) -ones(n-1,1)];
K = spdiags(KK, -1:1, n-1, n-1)/h;
for i = 2:n
f(i-1) =integral(@(xi)F(xi).*Phil(xi,x(i-1),x(i)), x(i-1), x(i));
f(i-1) = f(i-1) + integral(@(xi)F(xi).*Phir(xi,x(i),x(i+1)), x(i), x(i+1));
end
f(1) = f(1) + u0/h; f(n-1) = f(n-1) + uL/h;
u(2:end-1) = K\f;
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 183

Introduction to the Finite Element Method 183

u(1) = u0; u(n+1) = uL;


for i=1:n
sigma(i,1) = E*integral(@(xi)F(xi).*Phir(xi,x(i),x(i+1)), x(i), x(i+1))...
- u(i)*E/h + u(i+1)*E/h;
end
sigma(i+1) = -E*integral(@(xi)F(xi).*Phil(xi,x(n),x(n+1)), x(n), x(n+1))...
- u(n)*E/h;
rA = -A*sigma(1); rB = A*sigma(n+1);% Reactive forces
plot(x,u,’k’,x,U,’r*’,’LineWidth’,2);
legend(’Fem’,’Exact’,’Location’,’SouthWest’);
xlabel(’x’); ylabel(’U’); title(’Displacement’);
figure(2); plot(x,sigma,’k’,x,S,’r*’,’LineWidth’,2);
legend(’Fem’,’Exact’,’Location’,’SouthWest’); xlabel(’x’);
ylabel(’\sigma’); title(’Stress’);
fprintf(’RA=%f\n’,rA)
fprintf(’RB=%f\n’,rB)
fprintf( ’Maximum error for displacement = %g\n’,max(abs(U - u)))
fprintf( ’Maximum error for stress= %g\n’,max(abs(S - sigma)))
end

Example 4.2.6 Consider the bar in Fig. 4.2.11 and the related boundary
value problem
−AEU  = q(x), 0 < x < L, U (0) = 0, U  (L) = 0, (4.2.60)
where q(x) is the triangular load q(x) = qA (L − x)/L. The analytical
solution of Problem (4.2.60) is given in Exercise 4.4.7. A function that
applies the FEM to calculate displacement and stress is illustrated in the
worked Exercise 4.4.13. The graphs of displacement and stress are shown
in Fig. 4.2.12.

RA q

A B
Figure 4.2.11. Fixed end-free end bar subjected to an axial distributed load.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 184

184 Matlab for Engineering

Displacement Stress
0.015 4.5
FEM
Exact
0.01 3
U
0.005 1.5
FEM
Exact
0 0
0 200 400 x 600 800 1000 0 200 400 x 600 800 1000

Figure 4.2.12. Displacement and stress of the bar subjected to an triangular load.

4.2.6 Concentrated Force and Dirac Function


Consider a function v(x) with compact support K. The Dirac function δ is
defined as

δ(x − x̄)v(x)dx = v(x̄). (4.2.61)
K

For x̄ = 0, Definition (4.2.61) gives



δ(x)v(x)dx = v(0). (4.2.62)
K

The δ function was introduced by Dirac7 in Quantum Mechanics. Some


years later, it was outlined that the δ function is a generalized function, or
distribution Schwartz (1950).
Let us show that δ can model the concentrated forces on a bar. Consider
a fixed end-fixed end bar in Statics (Fig. 4.2.13). The bar has length L and
is subjected to the axial concentrated force (xh , Fh ). The formula for the
axial displacement was derived in Exercise 4.4.9 and is expressed as

(L − xh )Fh x/(AEL), if 0 < x < xh ,
U (x) = (4.2.63)
(L − x)Fh xh /(AEL), if xh < x < L.

7 Paul Adrien Maurice Dirac, a British scientist, 1902–1984. He was Professor at the

University of Cambridge. His most important book is The Principles of Quantum


Mechanics. He was awarded the Nobel Prize in 1933.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 185

Introduction to the Finite Element Method 185

R0 Fh RL

xh

Figure 4.2.13. Fixed end-fixed end bar subjected to an axial concentrated force.

Hence, stress and reactive forces




(L − xh )Fh /(AL), if 0 < x < xh ,
σ(x) = EU (x) (4.2.64)
−Fh xh /(AL), if xh < x < L,

R0 = −(L − xh )Fh /L, RL = −Fh xh /L. (4.2.65)

Note that U  is discontinuous in xh :

AEU  (x+  −
h ) − AEU (xh ) = −Fh . (4.2.66)

Formula (4.2.66) follows from the general Result (4.2.7). Of course, it follows
from the particular Result (4.2.64) as well. As a consequence, U cannot be
a classical solution of the bar equation on the whole interval (0, L). Let us
show that U is a weak solution. Indeed, consider a smooth test function v
with support on K = [0, L] and note that
 L  xh  L
   
U v dx = U v dx + U  v  dx
0 0 xh

(L − xh )Fh F x F v(xh )
= v(xh ) + h h v(xh ) = h .
AEL AEL AE
Hence,
 L  L
Fh
U  v  dx = δ(x − xh )v(x) dx, (4.2.67)
0 AE 0

because of (4.2.61). Formula (4.2.67) shows that U is a weak solution of

−AEU  = Fh δ(x − xh ). (4.2.68)

As the FEM considers weak solutions, it can be applied to find the axial
displacements of a bar subjected to axial concentrated forces.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 186

186 Matlab for Engineering

Example 4.2.7 As first application, consider the bar in Fig. 4.2.13. The
axial displacement is a weak solution of Eq. (4.2.68) with homogeneous
boundary conditions

U (0) = 0, U (L) = 0. (4.2.69)

Let us apply the FEM. The approximating solution is expressed as


n−1

u(x) = uj Φj (x).
j=1

The unknown coefficients uj are found by solving the system


⎡ ⎤⎡ ⎤ ⎡ ⎤
2 −1 u1 f1
1⎢⎢ −1 2 −1 ⎥ ⎢ u2 ⎥ ⎢ f2 ⎥
⎥⎢ ⎥ ⎢ ⎥
h ⎣ . . . ⎦⎣ · ⎦ = ⎣ . ⎦, (4.2.70)
−1 2 un−1 fn−1

where
 L
Fh Fh
fi = δ(x − xh )Φi (x) dx = Φi (xh ).
AE 0 AE

Since Φi (xh ) = 0 only when xh ∈ (xi−1 , xi+1 ), it is convenient to consider


a grid where xh is the same as the node. Therefore, we get

Fh /(AE), if i = h,
fi = Φi (xh )Fh /(AE) =
0, if i = h.

Moreover, as linear displacements are expected, we can consider the simple


grid illustrated in Fig. 4.2.14, where xh = h = L/3. In such a case, System

Fh
0 1 2 3
xh = h h h

L = 3h
Figure 4.2.14. Simple grid with xh = x1 .
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 187

Introduction to the Finite Element Method 187

(4.2.70) simplifies to

2u1 /h − u2 /h = Fh /(AE),
−u1 /h + 2u2 /h = 0.

Solving the previous system yields

u1 = 2hFh /(3AE), u2 = hFh /(3AE).

The values of ui are the same as Ui obtained from (4.2.63). To understand


why, let us recall that the FEM approximates the exact solution with a
piecewise linear function. If the analytical solution is also piecewise linear,
then Ui = ui . Lastly, a rapid calculation shows that stress and reactive
forces are exact too, as expected.

Example 4.2.8 Another application is given by the fixed end-free end bar
in Statics (Fig. 4.2.15). The bar has length L and is subjected to the axial
concentrated force (xh , Fh ). The axial displacements are weak solutions of
Eq. (4.2.68) with boundary conditions

U (0) = 0, U  (L) = 0. (4.2.71)

The analytical solution was derived in Exercise 4.4.10 and is expressed as



Fh x/(AE), if 0 < x < xh ,
U (x) = (4.2.72)
Fh xh /(AE), if xh < x < L.

Hence, stress and reactive force



Fh /A, if 0 < x < xh ,
σ(x) = R0 = −Fh . (4.2.73)
0, if xh < x < L,

R0 Fh

xh
Figure 4.2.15. Fixed end-free end bar subjected to axial concentrated force.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 188

188 Matlab for Engineering

Fh
0 1 2 3
h h xh = 2h

L = 3h
Figure 4.2.16. Simple grid with xh = x2 .

Consider the FEM. The approximating solution is expressed as (Sec. 4.2.4)


n

u(x) = uj Φj (x).
j=1

The unknown coefficients satisfy the system


⎡ ⎤⎡ ⎤ ⎡ ⎤
2 −1 u1 f1
⎢ −1 2 −1 ⎥ ⎢ u2 ⎥ ⎢ f2 ⎥
1⎢⎢
⎥⎢ ⎥ ⎢ ⎥
⎥⎢ · ⎥ = ⎢ . ⎥,
. . . (4.2.74)
h⎢⎣
⎥⎢ ⎥ ⎢ ⎥
−1 2 −1 ⎦ ⎣ un−1 ⎦ ⎣ fn−1 ⎦
−1 1 un fn
where
 L
Fh Fh
fi = δ(x − xh )Φi (x) dx = Φi (xh ).
AE 0 AE
Consider the simple grid in Fig. 4.2.16. It is

Fh /(AE), if i = h,
fi =
0, if i = h,

and

⎨ 2u1 /h − u2 /h = 0,

−u1 /h + 2u2 /h − u3 /h = Fh /(AE),


−u2 /h + u3 /h = 0.
Solving the previous system yields

u1 = hFh /(AE), u2 = u3 = 2hFh /(AE).

These values are the same as the analytical values Ui in (4.2.72), as


expected. Another application is suggested in Exercise 4.4.14.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 189

Introduction to the Finite Element Method 189

4.3 Partial Differential Equations


This section introduces the FEM for some parabolic and hyperbolic partial
differential equations.

4.3.1 Diffusion Equation


Consider the Dirichlet problem for the diffusion equation
Ut − αUxx = F (x, t), 0 < x < L, 0 < t ≤ T, (4.3.1)
U (x, 0) = ϕ(x), 0 ≤ x ≤ L, (4.3.2)
U (0, t) = G1 (t), U (L, t) = G2 (t), 0 < t ≤ T. (4.3.3)
Multiply Eq. (4.3.1) by the smooth test function v(x)
Ut v − αUxx v − F v = 0, ⇔ Ut v − α(Ux v)x + αUx v  − F v = 0,
and integrate over [0, L]
 L  L  L
Ut , vdx + αUx v  dx = F vdx + α [Ux v]x=L
x=0 . (4.3.4)
0 0 0

Equation (4.3.4) is the weak form of Eq. (4.3.1) that is named strong form
by contrast. The FEM considers Eq. (4.3.4). The approximating solution
to the Dirichlet problem is expressed as a finite series
n

u(x, t) = uj (t)Φj (x), (4.3.5)
j=0

where uj (t), j = 0, ..., n, are unknown functions and Φj (x), j = 0, ..., n,


are the shape functions introduced in Sec. 4.2.3. Note that u(0, t) = u0 (t)
and u(L, t) = un (t) because of Property (4.2.23) of the shape functions.
Therefore, if it is assumed u0 (t) = G1 (t) and un (t) = G2 (t), the function u
satisfies boundary Conditions (4.3.3). In conclusion,
n−1

u(x, t) = uj (t)Φj (x) + G1 (t)Φ0 (x) + G2 (t)Φn (x), (4.3.6)
j=1

where the last two terms are known. Substitute u(x, t) into Eq. (4.3.4) and
assume v = Φi , i = 1, . . . , n − 1,
n−1
  L n−1
  L  L  L
u̇j (t) Φj Φi dx+ uj (t) αΦj Φi dx = F Φi dx−Ġ1 (t) Φ0 Φi dx
j=1 0 j=1 0 0 0
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 190

190 Matlab for Engineering

 L  L  L
−Ġ2 (t) Φn Φi dx − αG1 (t) Φ0 Φi dx − αG2 (t) Φn Φi dx,
0 0 0
i = 1, . . . , n − 1,

as Φi (0) = 0, Φi (L) = 0, i = 1, . . . , n − 1. Hence, in matrix notation


n−1
 n−1

Mij u̇j (t) + Kij uj (t) = fi (t) − gi (t), i = 1, . . . , n − 1, (4.3.7)
j=1 j=1

where
 L  L
Mij = Φj Φi dx, Kij = αΦj Φi dx, i, j = 1, . . . , n − 1, (4.3.8)
0 0
 L  L  L
fi (t) = F, Φi dx, gi (t) = Ġ1 (t) Φ0 Φi dx + Ġ2 (t) Φn Φi dx
0 0 0
 L  L
+ αG1 (t) Φ0 Φi dx+αG2 (t) Φn Φi dx, i = 1, . . . , n−1. (4.3.9)
0 0

In compact form, System (4.3.7) is written as

M u̇(t) + Ku(t) = f (t) − g(t), (4.3.10)

where u, f and g are column vectors. The matrix M is named a mass matrix
with reference to Mechanics. The initial conditions ui (0) for the system of
ordinary differential Eqs. (4.3.10) are derived from (4.3.2). Indeed, note
that
n

u(xi , 0) = uj (0)Φj (xi ) = ui (0), i = 1, . . . , n.
j=1

Therefore, from (4.3.2), it follows

ui (0) = ϕ(xi ) = ϕi , i = 1, . . . , n. (4.3.11)

The elements fi (t) of the vector f are given by


 xi+1
fi (t) = F (x, t)Φi (x) dx, i = 1, . . . , n − 1, (4.3.12)
xi−1

as the support of Φi is the interval [xi−1 , xi+1 ]. Let us evaluate the


vector g. Consider g1 (t) and note that the supports of Φ0 , Φ1 and Φn
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 191

Introduction to the Finite Element Method 191

are the intervals [x0 , x1 ], [x0 , x2 ] and [xn−1 , xn ], respectively. Therefore,


 x1  x1
g1 (t) = Ġ1 (t) Φ0 (x)Φ1 (x) dx + αG1 (t) Φ0 (x)Φ1 (x) dx,
x0 x0

g1 (t) = Ġ1 (t)h/6 − αG1 (t)/h. (4.3.13)

Consider gn−1 (t) and note that the supports of Φ0 , Φn−1 and Φn are the
intervals [x0 , x1 ], [xn−2 , xn ] and [xn−1 , xn ], respectively. Therefore,
 xn  xn
gn−1 (t) = Ġ2 (t) Φn (x)Φn−1 (x) dx + αG2 (t) Φn (x)Φn−1 (x) dx,
xn−1 xn−1

gn−1 (t) = Ġ2 (t)h/6 − αG2 (t)/h. (4.3.14)

The remaining elements of g are zero

gi (t) = 0, i = 2, . . . , n − 2. (4.3.15)

The matrices M and K are given by


⎡ ⎤ ⎡ ⎤
2 1/2 2 −1
h⎢ 1/2 2 1/2 ⎥ ⎢
⎥, K = α ⎢ −1 2 −1

M= ⎢ ⎥.
3⎣ . . . ⎦ h ⎣ . . . ⎦
1/2 2 −1 2
(4.3.16)
K was evaluated in Sec. 4.2.4. The elements of M are calculated in worked
Exercise 4.4.15. The mass matrix is nonsingular. System (4.3.10) can be
written in normal form

u̇(t) = −M −1 Ku(t) + M −1 [f (t) − g(t)]. (4.3.17)

Example 4.3.1 The following listing presents a function that applies the
FEM to Problem (4.3.1)–(4.3.3). The listing considers the initial-boundary
conditions

U (x, 0) = sin(πx/L), 0 ≤ x ≤ L, (4.3.18)

U (0, t) = 0, U (L, t) = 0, 0 < t ≤ T. (4.3.19)

However, the code can be easily modified for other applications. The graph
of the numerical solution is shown in Fig. 4.3.1.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 192

192 Matlab for Engineering

t=1
0.014
FEM
0.012 Exact

0.01

0.008
U
0.006

0.004

0.002

0
0 0.5 x 1 1.5

Figure 4.3.1. Graph of the numerical solution to Problem (4.3.18) and (4.3.19).

function u = diffusion d
% This is the function file diffusion d.m.
% FEM is applied to the following Dirichlet problem:
% Ut - alpha Uxx = 0, U(x,0) = phi(x), U(0,t) = G1(t), U(L,t) = G2(t),
% where phi(x) = sin(pi*x), G1(t) = 0, G2(t)=0.
% Analytical solution: U = sin(pi*x)*exp(-pi^2*t).
% The code can be easily modified for other applications. It is
% enough to modify the lines related to the initial-boundary conditions
% and to eliminate the line related to the analytical solution.
% Initialization
alpha = 1; L = 1.5; T = 1; nx = 50;
phi = @(x) sin(pi*x/L);
G1 = @(t) 0*t;
G2 = @(t) 0*t;
G1t = @(t) 0*t;
G2t = @(t) 0*t;
dx = L/nx; x = linspace(0,L,nx+1);
KK = [-ones(nx-1,1) 2*ones(nx-1,1) -ones(nx-1,1)];
K = spdiags(KK, -1:1, nx-1, nx-1)*alpha/dx;
MM = [ones(nx-1,1)/6 2/3*ones(nx-1,1) ones(nx-1,1)/6];
M = spdiags(MM, -1:1, nx-1, nx-1)*dx;
B = M^(-1); A = B*K;
g = zeros(nx-1,1); u = feval(phi,x’);
% FEM
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 193

Introduction to the Finite Element Method 193

[~,y] = ode45(@system,[0 T],u(2:nx),[ ],A,B,g,dx,alpha,G1,G2,G1t,G2t);


u(2:nx) = y(end,:);
u(1) = G1(T); u(nx+1) = G2(T);
U = sin(pi*x’/L)*exp(-(pi/L)^2*T);
plot(x,u,’k’,x,U,’r*’,’LineWidth’,2);
legend(’FEM’,’Exact’); xlabel(’x’); ylabel(’U’);
title([’t = ’,num2str(T)]);
fprintf( ’Maximum error= %g\n’,max(abs(U - u)))
end
% ———- Local function ————-
function Du = system(t, u, A, B, g, dx, alpha, G1, G2, G1t, G2t)
g(1) = G1t(t)*dx/6 - alpha*G1(t)/dx;
g(end) = G2t(t)*dx/6 - alpha*G2(t)/dx;
Du = -A*u - B*g;
end
Another application is suggested in Exercise 4.4.16.
Consider the Neumann problem
Ut − αUxx = F (x, t), 0 < x < L, 0 < t ≤ T, (4.3.20)

U (x, 0) = ϕ(x), 0 ≤ x ≤ L, (4.3.21)

−Ux (0, t) = G1 (t), Ux (L, t) = G2 (t), 0 < t ≤ T. (4.3.22)


The weak form is given by
 L  L  L
Ut vdx+ αUx v  dx = F vdx+αG1 (t)v(0)+αG2 (t)v(L). (4.3.23)
0 0 0

The approximating solution to the Neumann problem is expressed as a


finite series n

u(x, t) = uj (t)Φj (x), (4.3.24)
j=0

where uj (t), j = 0, ..., n, are unknown functions and Φj (x), j = 0, ..., n,


are the shape functions introduced in Sec. 4.2.3. Substitute u(x, t) into Eq.
(4.3.23) and assume v = Φi , i = 0, . . . , n,
n  L n
  L  L
u̇j (t) Φj Φi dx + uj (t) αΦj Φi dx = F Φi dx+
j=0 0 j=0 0 0

+αG1 (t)Φi (0) + αG2 (t)Φi (L), ∀Φi , i = 0, . . . , n.


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 194

194 Matlab for Engineering

Hence, the matrix notation


n
 n

Mij u̇j (t) + Kij uj (t) = fi (t) + gi (t), i = 0, . . . , n, (4.3.25)
j=0 j=0

where
 L  L  L
Mij = Φj Φi dx, Kij = αΦj Φi dx, fi (t) = F Φi )dx, i, j = 0, . . . , n,
0 0 0

gi (t) = αG1 (t)Φi (0) + αG2 (t)Φi (L), i = 0, . . . , n.

In compact form, System (4.3.25) is written as

M u̇(t) + Ku(t) = f (t) + g(t), (4.3.26)

where u, f and g are column vectors. The elements of f are given by


 x1  xn
f0 (t) = Φi dx, fn (t) = F Φi dx,
x0 xn−1

 xi+1
fi (t) = F Φi dx, i = 1, . . . , n − 1,
xi−1

as the support of Φ0 is the interval [x0 , x1 ], the support of Φn is the interval


[xn−1 , xn ] and the support of Φi , i = 1, . . . , n−1, is the interval [xi−1 , xi+1 ].
In addition, the elements of g are given by

g0 (t) = αG1 (t), gn (t) = αG2 (t), gi (t) = 0, i = 1, . . . , n − 1. (4.3.27)

Let us evaluate the mass matrix M . Consider


 x1  h  h
(h − x)2 ξ2
M0,0 = Φ20 (x) dx = dx = dξ = h/3,
x0 0 h2 0 h2

as the support of Φ0 is the interval [x0 , x1 ]. In addition,


 xn  xn  h
(x − xn−1 )2 ξ2
Mn,n = Φ2n (x) dx = dx = dξ = h/3,
xn−1 xn−1 h2 0 h2
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 195

Introduction to the Finite Element Method 195

as the support of Φn is the interval [xn−1 , xn ]. The other elements of the


(n + 1)-by-(n + 1) matrix M are shown in Formula (4.3.16). In conclusion:
⎡ ⎤
1 1/2
⎢ 1/2 2 1/2 ⎥
h⎢ ⎥
M= ⎢ ⎢ . . . ⎥.
⎥ (4.3.28)
3⎣ ⎦
1/2 2 1/2
1/2 1
Let us evaluate the stiffness matrix K. Consider
 x1

K0,0 = α Φ02 (x) dx = α/h,
x0

as the support of Φ0 is the interval [x0 , x1 ]. In addition,


 xn

Kn,n = α Φn2 (x) dx = α/h,
xn−1

as the support of Φn is the interval [xn−1 , xn ]. The other elements of the


(n + 1)-by-(n + 1) matrix K are in Formula (4.3.16). In conclusion:
⎡ ⎤
1 −1
⎢ −1 2 −1 ⎥
α⎢⎢

⎥.
K= ⎢ . . . ⎥ (4.3.29)
h⎣
−1 2 −1 ⎦
−1 1

Example 4.3.2 The following listing presents a function that applies the
FEM to solve the Neumann problem

Ut − Uxx = x(x − L) − 2t, 0 < x < L, 0 < t ≤ T, (4.3.30)

U (x, 0) = 0, 0 ≤ x ≤ L, (4.3.31)

−Ux (0, t) = tL, Ux (L, t) = tL, 0 < t ≤ T. (4.3.32)

The numerical solution is plotted in Fig. 4.3.2. The listing considers the
problem above, but the code can be easily modified for other applications.
function u = diffusion n
% This is the function file diffusion n.m.
% FEM is applied to the following Neumann problem:
% Ut - alpha Uxx = F, U(x,0) = phi(x), Ux(0,t) = - G1(t), Ux(L,t) = G2(t),
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 196

196 Matlab for Engineering

t =1
0
FEM
Exact

-0.2

-0.4

-0.6
0 0.5 x 1 1.5

Figure 4.3.2. Graph of the numerical solution to Problem (4.3.30) to (4.3.32).

% where phi(x) = 0, G1(t) = tL, G2(t) = tL, F = x(x - L) - 2t.


% Analytical solution: U = x*(x - L)*t.
% The code can be easily adapted to other applications by modifying the data.
% Initialization
alpha = 1; L = 1.5; T = 1; n = 20;
phi = @(x) 0*x;
G1 = @(t) t*L;
G2 = @(t) t*L;
F = @(xi,t) xi.*(xi - L) - 2*t;
Phil = @(xi, x1, x2) (xi - x1)/(x2 - x1);
Phir = @(xi, x2, x3) -(xi - x3)/(x3 - x2);
n = L/n; x = linspace(0,L,n+1);
KK = [-ones(n+1,1) [1; 2*ones(n-1,1); 1] -ones(n+1,1)];
K = spdiags(KK, -1:1, n+1, n+1)*alpha/dx;
MM = [ones(n+1,1)/2 [1; 2*ones(n-1,1);1] ones(n+1,1)/2];
M = spdiags(MM, -1:1, n+1, n+1)*h/3;
B = M^(-1); A = B*K;
u = feval(phi,x’);
% FEM
[~,y] = ode15s(@system, [0 T], u, [ ], A, B, alpha, x, n, G1, G2, Phil, Phir, F);
u(1:end,1) = y(end,:);
U = x’.*(x’ - L)*T;
plot(x,u,’k’,x,U,’r*’,’LineWidth’,2);
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 197

Introduction to the Finite Element Method 197

legend(’FEM’,’Exact’,’Location’,’North’); xlabel(’x’); ylabel(’U’);


title([’t = ’,num2str(T)]);
fprintf( ’Maximum error= %g\n’,max(abs(U - u)))
end
% ———- Local function ————-
function Du = system(t, u, A, B, alpha, x, n, G1, G2, Phil, Phir, F)
f = zeros(n+1,1);
i = 1; f(i) = integral(@(xi)F(xi,t).*Phir(xi,x(i),x(i+1)), x(i), x(i+1));
for i=2:n
f(i) = integral(@(xi)F(xi, t).*Phil(xi, x(i-1), x(i)), x(i-1), x(i));
f(i) = f(i) + integral(@(xi)F(xi, t).*Phir(xi, x(i), x(i+1)), x(i), x(i+1));
end
i = n+1; f(i) = integral(@(xi)F(xi, t).*Phil(xi, x(i-1), x(i)), x(i-1), x(i));
f(1) = f(1) + alpha*G1(t); f(end) = f(end) + alpha*G2(t);
Du = -A*u+B*f;
end

4.3.2 Wave Equation


Let us discuss FEM for the following wave equation
Utt − c2 Uxx = F (x, t) − λU, 0 < x < L, 0 < t ≤ T. (4.3.33)
Consider the Dirichlet problem and assign initial position and velocity
U (x, 0) = ϕ(x), Ut (x, 0) = ψ(x), 0 ≤ x ≤ L, (4.3.34)
and boundary conditions
U (0, t) = G1 (t), U (L, t) = G2 (t), 0 < t ≤ T. (4.3.35)
Multiply Eq. (4.3.33) by a smooth test function v(x) and integrate over
[0, L]
 L
[Utt (x, t)v(x) + c2 Ux (x, t)v  (x) + λU (x, t)v(x)]dx
0
 L
= F (x, t)v(x)dx + c2 [Ux v]x=L
x=0 . (4.3.36)
0

The FEM considers the weak form above. The approximating solution that
satisfies boundary Conditions (4.3.35) is given by
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 198

198 Matlab for Engineering

n−1

u(x, t) = uj (t)Φj (x) + G1 (t)Φ0 (x) + G2 (t)Φn (x), (4.3.37)
j=1

where uj (t), j = 1, ..., n − 1, are unknown functions and Φj (x), j = 0, ..., n,


are the shape functions introduced in Sec. 4.2.3. Substitute (4.3.37) into
(4.3.36) and assume v = Φi , i = 1, . . . , n − 1,
n−1
  L n−1
  L
[üj (t) + λuj (t)] Φj (x)Φi (x)dx + uj (t) c2 Φj (x)Φi (x)dx
j=1 0 j=1 0

 L  L
= −[G̈1 (t) + λG1 (t)] Φ0 (x)Φi (x) dx − c2 G1 (t) Φ0 (x)Φi (x) dx
0 0
 L  L
−[G̈2 (t) + λG2 (t)] Φn (x)Φi (x) dx − c2 G2 (t) Φn (x)Φi (x) dx,
0 0
 L
+ F (x, t)Φi (x)dx, i = 1, . . . , n − 1,
0

as Φi (0) = 0, Φi (L) = 0, i = 1, . . . , n − 1. Hence, in matrix notation,


n−1
 n−1

Mij [üj (t) + λuj (t)] + Kij uj (t) = fi (t) − gi (t), i = 1, . . . , n − 1,
j=1 j=1
(4.3.38)
where
 L  L
Mij = Φj (x)Φi (x)dx, Kij = c2 Φj (x)Φi (x)dx, i, j = 1, . . . , n − 1,
0 0
  (4.3.39)
L L
fi (t) = F (x, t)Φi (x)dx, gi (t) = [G̈1 (t) + λG1 (t)] Φ0 (x)Φi (x)dx
0 0
 L  L
+ c2 G1 (t) Φ0 (x)Φi (x)dx + [G̈2 (t) + λG2 (t)] Φn (x)Φi (x)dx
0 0
 L
+ c2 G2 (t) Φn (x)Φi (x)dx, i = 1, . . . , n − 1. (4.3.40)
0

In compact form, System (4.3.38) is written as

M [ü(t) + λu(t)] + Ku(t) = f (t) − g(t), (4.3.41)

where u, f and g are column vectors. The integration of the previous second-
order system of ordinary differential equations needs the initial conditions
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 199

Introduction to the Finite Element Method 199

ui (0) and u̇i (0). They are derived from (4.3.34):

ui (0) = ϕ(xi ) = ϕi , u̇i (0) = ψ(xi ) = ψi , i = 1, . . . , n. (4.3.42)

The first condition was derived in (4.3.11). The second condition is derived
similarly. The elements of f are given by
 xi+1
fi (t) = F (x, t)Φi (x)dx, i = 1, . . . , n − 1. (4.3.43)
xi−1

Let us evaluate g. Consider


 x1  x1
2
g1 (t) = [G̈1 (t) + λG1 (t)] Φ0 (x)Φ1 (x) dx + c G1 (t) Φ0 (x)Φ1 (x) dx,
x0 x0

g1 (t) = [G̈1 (t) + λG1 (t)]h/6 − c2 G1 (t)/h, (4.3.44)

as the supports of Φ0 , Φ1 and Φn are the intervals [x0 , x1 ], [x0 , x2 ] and


[xn−1 , xn ], respectively. In addition,
 xn
gn−1 (t) = [G̈2 (t) + λG2 (t)] Φn (x)Φn−1 (x) dx
xn−1
 xn 2
h c
+ c2 G2 (t) Φn (x)Φn−1 (x) dx = ¨[G̈2 (t)+λG2 (t)] − G2 (t), (4.3.45)
xn−1 6 h
as the supports of Φ0 , Φn−1 and Φn are the intervals [x0 , x1 ], [xn−2 , xn ]
and [xn−1 , xn ], respectively. The other elements are zero

gi (t) = 0, i = 2, . . . , n − 2. (4.3.46)

The matrices M and K are given by (see Formula (4.3.16))


⎡ ⎤ ⎡ ⎤
2 1/2 2 −1
h ⎢ 1/2 2 1/2 ⎥ 2 ⎢
⎥, K = c ⎢ −1 2 −1

M= ⎢ ⎣ ⎦ ⎣
⎥.

3 . . . h . . .
1/2 2 −1 2
Finally, the second-order differential system in (4.3.41) can be converted to
a first-order system. Indeed, if the new unknown function

w = u̇

is introduced, we get
      
u̇ 0 I u 0
= + . (4.3.47)
ẇ −(λI + M −1 K) 0 w M −1 (f − g)
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 200

200 Matlab for Engineering

Example 4.3.3 Consider the Dirichlet problem

Utt − c2 Uxx + λU = 0, 0 < x < L, 0 < t ≤ T. (4.3.48)

U (x, 0) = sin(pπx/L), Ut (x, 0) = 0, 0 < x < L, (4.3.49)

U (0, t) = 0, U (L, t) = 0, 0 < t ≤ T. (4.3.50)

The following listing presents a function that applies the FEM to solve
Problem (4.3.48)–(4.3.50). The numerical solution is plotted in Fig. 4.3.3.
function u = wave d
% This is the function file wave d.m.
% The FEM is applied to the following Dirichlet problem:
% Utt - c^2 Uxx + lambda U = 0,
% U(x,0) = sin(x*pi*p/L), Ut(x,0) = 0, U(0,t) = 0, U(L,t) = 0.
% Analytical solution:
% U = sin(x*pi*p/L)*cos(t*sqrt(lambda + c^2*pi^2*p^2/L^2) ).
% Initialization
c = 1; L = 1; T = 1; lambda = 20; n = 100; p = 4;
phi = @(x) sin(x*pi*p/L); psi = @(x) x*0;
G1 = @(t) 0*t; G1tt = @(t) 0*t;
G2 = @(t) 0*t; G2tt = @(t) 0*t;
h = L/n; x = linspace(0,L,n+1);
KK = [-ones(n-1,1) 2*ones(n-1,1) -ones(n-1,1)];

t=1
1
FEM
0.8 Exact
0.6
0.4
0.2
U 0
-0.2
-0.4
-0.6
-0.8
-1
0 0.2 0.4 x 0.6 0.8 1

Figure 4.3.3. Graph of the numerical solution to Problem (4.3.48) to (4.3.50).


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 201

Introduction to the Finite Element Method 201

K = spdiags(KK, -1:1, n-1, n-1)*c^2/h;


MM = [ones(n-1,1)/6 2/3*ones(n-1,1) ones(n-1,1)/6];
M = spdiags(MM, -1:1, n-1, n-1)*h;
B = M^(-1);
A = [zeros(n-1,n-1) eye(n-1,n-1); -lambda*eye(n-1,n-1)-B*K zeros(n-1,n-1)];
u = feval(phi,x’); w = feval(psi,x’);
g = zeros(n-1,1);
U = sin(x’*pi*p/L)* cos(T*sqrt(lambda + c^2*pi^2*p^2/L^2) );
% FEM
nt = 40; tt = linspace(0,T,nt+1);
for j=1:nt
[~,y] = ode45(@system, [tt(j) tt(j+1)], [u(2:n); w(2:n)], [ ], A, B, g,...
h, c, lambda, G1, G2,G1tt,G2tt);
u(2:n) = y(end,1:n-1);
u(1) = G1(tt(j+1)); u(n+1) = G2(tt(j+1));
w(2:n) = y(end,n:end);
plot(x,u,’k’,x,U,’r*’);
xlabel(’x’); ylabel(’U’); axis([0 L -1 1]);
title([’t = ’,num2str(tt(j+1))]);
pause(.1);
end
legend(’FEM’,’Exact’);
fprintf( ’Maximum error= %g\n’,max(abs(U - u)))
end
% ———- Local function ————-
function Du = system(t, uw, A, B, g, h, c, lambda, G1, G2, G1tt, G2tt)
gg = g;
g(1) = (G1tt(t) + lambda*G1(t))*h/6 - c2 *G1(t)/h;
g(end) = (G2tt(t) + lambda*G2(t))*h/6 - c2 *G2(t)/h;
Du = A*uw + [gg; -B*g];
end
Another application is suggested in Exercise 4.4.17.
Consider the Neumann problem

Utt − c2 Uxx + λU = F (x, t), 0 < x < L, 0 < t ≤ T, (4.3.51)

U (x, 0) = ϕ(x), Ut (x, 0) = ψ(x), 0 ≤ x ≤ L, (4.3.52)

−Ux (0, t) = G1 (t), Ux (L, t) = G2 (t), 0 < t ≤ T. (4.3.53)


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 202

202 Matlab for Engineering

The weak form is given by


 L
[Utt (x, t)v(x) + c2 Ux (x, t)v  (x) + λU (x, t)v(x) − F (x, t)v(x)] dx
0

= c2 G2 (t)v(L) + c2 G1 (t)v(0). (4.3.54)

The FEM considers the weak form. The approximating solution to the
Neumann problem is expressed as a finite series
n

u(x, t) = uj (t)Φj (x), (4.3.55)
j=0

where uj (t), j = 0, ..., n, are unknown functions and Φj (x), j = 0, ..., n,


are the shape functions introduced in Sec. 4.2.3. Substitute u(x, t) into Eq.
(4.3.54) and assume v = Φi , i = 0, . . . , n,
n  L  n  L
[üj (t) + λuj (t)] Φj (x)Φi (x)dx + uj (t) c2 Φj (x)Φi (x)dx
j=0 0 j=0 0

 L
= F (x, t)Φi (x)dx + c2 G1 (t)Φi (0) + c2 G2 (t)Φi (L), ∀Φi , i = 0, . . . , n.
0

Hence, using the matrix notation,


n
 n

Mij [üj (t)+λuj (t)]+ Kij uj (t) = fi (t)+gi (t), i = 0, . . . , n, (4.3.56)
j=0 j=0

where
 L  L
Mij = Φj (x)Φi (x)dx, Kij = c2 Φj (x)Φi (x)dx, i, j = 0, . . . , n,
0 L
0

fi (t) = F (x, t)Φi (x)dx, gi (t) = c2 G1 (t)Φi (0)+c2 G2 (t)Φi (L), i = 0, . . . , n.


0

In compact form, System (4.3.56) is written as

M [ü(t) + λu(t)] + Ku(t) = f (t) + g(t), (4.3.57)

where u, f and g are column vectors. The elements of f are given by


 x1  xn
f0 (t) = F (x, t)Φ0 (x) dx, fn (t) = F (x, t)Φn (x) dx, (4.3.58)
x0 xn−1
 xi+1
fi (t) = F (x, t)Φi (x) dx, i = 1, . . . , n − 1, (4.3.59)
xi−1
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 203

Introduction to the Finite Element Method 203

as the support of Φ0 is the interval [x0 , x1 ], the support of Φn is the interval


[xn−1 , xn ] and the support of Φi , i = 1, . . . , n−1, is the interval [xi−1 , xi+1 ].
The vector g depends on the boundary conditions. Its elements are given
by

g0 (t) = c2 G1 (t), gi (t) = 0, i = 1, . . . , n − 1, gn (t) = c2 G2 (t).

The matrices M and K were evaluated in Formulas (4.3.28)–(4.3.29) and


are given by
⎡ ⎤ ⎡ ⎤
1 1/2 1 −1
h ⎢ 1/2 2 1/2 ⎥ 2 ⎢
⎥, K = c ⎢ −1 2 −1

M= ⎢ ⎣ ⎦ ⎣
⎥.

3 . . . h . . .
1/2 1 −1 1
Finally, note that if the new unknown function

w = u̇,

is introduced, then the second-order differential System (4.3.57) is converted


to the first-order system
      
u̇ 0 I u 0
= + . (4.3.60)
ẇ −(λI + M −1 K) 0 w M −1 (f + g)

Example 4.3.4 Consider the Neumann problem

Utt − Uxx = F sin(ωt)δ(x − xh ), 0 < x < L, 0 < t ≤ T, (4.3.61)

U (x, 0) = 0, Ut (x, 0) = 0, 0 < x < L, (4.3.62)

Ux (0, t) = 0, Ux (L, t) = 0, 0 < t ≤ T. (4.3.63)

The following listing presents a function that applies the FEM to Problem
(4.3.61)–(4.3.63). The solution has a discontinuity for Ux in xh equal to
F sin(ωt). Therefore, the load vector (4.3.61)–(4.3.63) is given by

fi = 0, xi = xh , fh = F sin(ωt), xi = xh .

The graphs of u and u(xh , t) are plotted in Fig. 4.3.4.

function u = wave n
% This is the function file wave n.m.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 204

204 Matlab for Engineering

0.4 0.2
t=1
0.16
0.3
U 0.12
0.2
0.08
0.1 0.04
U(x h ,t)
0 0
0 0.2 0.4 x 0.6 0.8 1 0 0.2 0.4 t 0.6 0.8 1

Figure 4.3.4. Graph of the numerical solution (left); graph of u(xh , t) (right).

% FEM is applied to the following Neumann problem:


% Utt - Uxx = F*delta(x - xh),
% U(x,0) = 0, Ut(x,0) = 0, -Ux(0,t) = 0, Ux(L,t) = 0.
% Initialization
n = 80; L = 1; F = 1; om = 5; T = 1; ih = 31;
h = L/n; x = linspace(0,L,n+1);
KK = [-ones(n+1,1) [1;2*ones(n-1,1);1] -ones(n+1,1)];
K = spdiags(KK, -1:1, n+1, n+1)/h;
MM = [ones(n+1,1)/6 [1/3;2/3*ones(n-1,1);1/3] ones(n+1,1)/6];
M = spdiags(MM, -1:1, n+1, n+1)*h;
C = M^(-1);
B = [zeros(n+1,n+1) eye(n+1,n+1); -C*K zeros(n+1,n+1)];
u = zeros(n+1,1); w = zeros(n+1,1);
g = zeros(n+1,1); f = zeros(n+1,1);
% FEM
nt = 20; tt = linspace(0,T,nt+1); uh = zeros(nt+1,1);
for j = 1:nt % Loop animates the graph.
[~,y] = ode15s(@system, [tt(j) tt(j+1)], [u(1:n+1); w(1:n+1)], [ ],...
B, C, g, f, ih, F, om);
u(1:n+1) = y(end,1:n+1); w(1:n+1)=y(end,n+2:end);
plot(x,u,’k’,’LineWidth’,2);
axis([0 L 0 0.4]); xlabel(’x’); ylabel(’U’);
legend([’t = ’,num2str(tt(j+1))],’Location’,’NorthEast’);
pause(.1);
end
figure(2); plot(tt,uh,’ko’,’LineWidth’,2);
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 205

Introduction to the Finite Element Method 205

xlabel(’t’); legend(’U(x h, t)’,’Location’,’SouthEast’);


end
% ———- Local function ————-
function Du = system(t, uw, B, C, g, f, ih, Fh, om)
f(ih)= Fh*sin(om*t);
Du = B*uw + [g; C*f];
end

4.4 Exercises
Exercise 4.4.1 Find Eq. (4.1.1) of the parabola p2 (x).

Exercise 4.4.2 Apply integral and int functions to calculate the integrals
 1  π
sin(x)
dx, x cos(|x| + x) dx.
0 x −π

Exercise 4.4.3 Write the Matlab code that produces Fig. 4.1.3.
Answer.
x = linspace(-10, 10, 101);
plot(x, besselj(0,x), ’k’, x, besselj(1,x), ’r*:’, x, besselj(2,x), ’bo:’);
legend(’J 0’,’J 1’,’J 2’);

Exercise 4.4.4 Use Matlab to verify that

lim J1 (z)/z = .5.


z→0

Answer. z = sym(’z’); limit(besselj(1,z)/z,z,0).

Exercise 4.4.5 Consider the integral 2D 1 listing. Use the local functions
to introduce the functions f(x, y), alpha(x), beta(x).
Answer.
function integral 2D 2
% This is the function file integral 2D 2.m.
% Integral2 and int functions are called to calculate the integral.
% Local functions are used to define f(x, y), alfa(x), beta(x).
a = 0; b = 4;
Q = integral2(@f, a, b, @alfa, @beta);
x = sym(’x’); y = sym(’y’);
E = int(int(f(x,y), y, alfa(x), beta(x)), x, a, b);
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 206

206 Matlab for Engineering

fprintf(’integral2 = %g; int = %g\’, Q, double(E))


end
% ———- Local functions ————-
function z = f(x, y)
z = x.*y;
end
function y = alfa(x)
y = x.2 /4;
end
function y = beta(x)
y = 2*sqrt(x);
end

Exercise 4.4.6 Consider a fixed end-fixed end bar in Statics. See Fig.
4.4.1. The bar has length L and is subjected to the axial load q(x). The
axial deformation is found by solving the following boundary value problem

−AEU  (x) = q(x), 0 < x < L, U (0) = U (L) = 0.

Assume that

q(x) = qA + (qB − qA )x/L. (4.4.1)

Prove that U and σ are given by


1
U (x) = [q (Lx − x2 )/2 + (qB − qA )(L2 x − x3 )/6/L],
AE A
1
σ(x) = [q (L − 2x)/2 + (qB − qA )(L2 − 3x2 )/6/L].
AE A
Deduce from the last equation that the reactive forces RA and RB are given
by

RA = −qA L/2 − (qB − qA )L/6, RB = −qA L/2 − 2(qB − qA )L/6.

RA q RB

A B
Figure 4.4.1. Fixed end-fixed end bar subjected to an axial load.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 207

Introduction to the Finite Element Method 207

RA q

A B
Figure 4.4.2. Fixed end-free end bar subjected to an axial load.

q RB

A B
Figure 4.4.3. Free end-fixed end bar subjected to an uniform load.

Note that the trapezoidal load (4.4.1) simplifies to a uniform load for qB =
qA and to a triangular load for qA = 0.

Exercise 4.4.7 Consider a fixed end-free end bar in Statics. See Fig.
4.4.2. The bar has length L and is subjected to the axial load q(x).
The axial deformation is found by solving the following boundary value
problem

−AEU  (x) = q(x), 0 < x < L, U (0) = 0, U  (L) = 0.

Assume that q(x) is the triangular load

q(x) = qA (L − x)/L.

Prove that U , σ and RA are given by


qA x3 q x2 q Lx q x2 q x q L q L
U (x) = − A + A , σ = A − A + A , RA = − A .
6AEL 2AE 2AE 2AL A 2A 2

Exercise 4.4.8 Consider a free end-fixed end bar in Statics. See Fig.
4.4.3. The bar has length L and is subjected to the axial uniform load
q(x) = qA . Find displacement, stress and reactive force.

Exercise 4.4.9 Consider a fixed end-fixed end bar in Statics. See Fig.
4.4.4. The bar has length L and is subjected to the axial concentrated force
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 208

208 Matlab for Engineering

R0 Fh RL

xh

Figure 4.4.4. Fixed end-fixed end bar subjected to an axial concentrated force.

Fh . The axial deformation is found by solving the following boundary value


problem
−AEU  = 0, x ∈]0, xh [ ∪ ]xh , L[, (4.4.2)

U (0) = 0, U (L) = 0, U (x− + + −


h ) = U (xh ), Aσ(xh )−Aσ(xh ) = −Fh . (4.4.3)

Find the displacement, stress and reactive forces.


Answer. U is linear on both intervals (0, xh ) and (xh , L) because of (4.4.2).
In addition, U must satisfy the first two conditions in (4.4.3). Therefore,

C1 x, if 0 < x < xh ,
U (x) = (4.4.4)
C2 (L − x), if xh < x < L.
The constants C1 and C2 are founded by using the last two conditions in
(4.4.3):
C1 xh = C2 (L − xh ), −AEC2 − AEC1 = −Fh .

Solving with respect to C1 , C2 yields


C1 = (L − xh )Fh /(AEL), C2 = Fh xh /(AEL).

Substituting the last result into (4.4.4), one arrives at the expression of U

(L − xh )Fh x/(AEL), if 0 < x < xh ,
U (x) =
(L − x)Fh xh /(AEL), if xh < x < L.
Hence, stress and reactive forces are


(L − xh )Fh /(AL), if 0 < x < xh ,
σ(x) = EU (x) =
−Fh xh /(AL), if xh < x < L,

R0 = −(L − xh )Fh /L, RL = −Fh xh /L.


September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 209

Introduction to the Finite Element Method 209

R0 Fh

xh
Figure 4.4.5. Fixed end-free end bar subjected to an axial concentrated force

Exercise 4.4.10 Consider a fixed end-free end bar in Statics. See Fig.
4.4.5. The bar has length L and is subjected to the axial concentrated force
Fh . The axial deformation is found by solving the following boundary value
problem

−EAU  = 0, x ∈]0, xh [ ∪ ]xh , L[,

U (0) = 0, U  (L) = 0, U (x− +


h ) = U (xh ), Aσ(x+ −
h ) − Aσ(xh ) = −Fh .

Prove that

Fh x/(AE), if 0 < x < xh ,
U (x) =
Fh xh /(AE), if xh < x < L,


Fh /A, if 0 < x < xh ,
σ(x) = R0 = −Fh .
0, if xh < x < L,

Exercise 4.4.11 Write a listing that calls the fem dd function and solves
the boundary value problem

−U  = cx2 , 0 < x < L, U (0) = U0 , U (L) = UL .

Exercise 4.4.12 Write a function, say fem nd, that solves the Neumann–
Dirichlet problem

−U  = F (x), 0 < x < L, U  (0) = U0 , U (L) = UL .

Exercise 4.4.13 Write a function that applies the FEM to calculate the
displacement, stress and reactive force for the bar in Example 4.2.6.
Answer.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 210

210 Matlab for Engineering

function [u, sigma] = stress 2


% This is the function file stress 2.m.
% FEM is applied to solve the following problem:
% -AE*U”(x) = q(x), q(x) = qA + (L - x)/L, U(0) = 0, U’(L) = 0.
% Analytical solution:
% U = qA*x3 /6/E/A/L - qA*x2 /2/E/A + qA*L*x/2/A/E.
% Initialization
n = 30; u0 = 0; uxL = 0;
L = 1000; % mm
E = 90000; % N/mm2
A = 1000; % mm2
qA = 8; % N/mm
Phil = @(xi, x1, x2) (xi-x1)/(x2-x1);
Phir = @(xi, x2, x3) -(xi-x3)/(x3-x2);
F = @(xi) qA/A/E/L*(L - xi);
h = L/n; x = linspace(0,L,n+1);
u = zeros(n+1,1); sigma = zeros(n+1,1); f = zeros(n-1,1);
U = qA*x’.3 /6/E/A/L - qA*x’.2 /2/E/A + qA*L*x’/2/A/E;
S = qA/A/L/2*x’.2 - qA/A*x’ + qA*L/2/A;
% FEM
KK = [-ones(n,1) [2*ones(n-1,1);1] -ones(n,1)];
K = spdiags(KK, -1:1, n, n)/h;
for i = 2:n
f(i-1) =integral(@(xi)F(xi).*Phil(xi,x(i-1),x(i)), x(i-1), x(i));
f(i-1) = f(i-1) + integral(@(xi)F(xi).*Phir(xi,x(i),x(i+1)), x(i), x(i+1));
end
f(n) = integral(@(xi)F(xi).*Phil(xi,x(n),x(n+1)), x(n), x(n+1))+uxL;
f(1) = f(1) + u0/h;
u(2:end) = K\f; u(1) = u0;
for i=1:n
sigma(i,1) = E*integral(@(xi)F(xi).*Phir(xi,x(i),x(i+1)), x(i), x(i+1))...
- u(i)*E/h + u(i+1)*E/h;
end
sigma(n+1) = 0;
rA = -A*sigma(1);
plot(x,u,’k’,x,U,’r*’,’LineWidth’,2);
legend(’FEM’,’Exact’,’Location’,’SouthEast’);
xlabel(’x’); ylabel(’U’); title(’Displacement’);
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 211

Introduction to the Finite Element Method 211

figure(2); plot(x,sigma,’k’,x,S,’r*’,’LineWidth’,2);
legend(’FEM’,’Exact’); xlabel(’x’); ylabel(’\sigma’);title(’Stress’);
fprintf(’RA=%f\n’,rA)
fprintf( ’Maximum error for displacement = %g\n’,max(abs(U - u)))
fprintf( ’Maximum error for stress= %g\n’,max(abs(S - sigma)))
end

Exercise 4.4.14 Apply the FEM to discuss the bar in Fig. 4.4.6, which
is being subjected to an axial uniform load q and axial concentrated force
(xh , Fh ).

Exercise 4.4.15 Calculate the elements of the matrix M shown in


Formula (4.3.16).
Answer. The matrix M is symmetric. Consider the main diagonal
 xi+1  xi  xi+1
(x − xi−1 )2 (xi+1 − x)2
Mi,i = Φ2i (x) dx = dx + dx,
xi−1 xi−1 h2 xi h2

as the support of Φi is the interval [xi−1 , xi+1 ]. Hence,


 h  h
ξ2 ξ2
Mi,i = dξ + dξ = 2h/3, i = 1, . . . , n − 1.
0 h2 0 h2
Consider Mi,i+1
 xi+1  xi+1
(x − xi+1 )(x − xi )
Mi,i+1 = Φi (x)Φi+1 (x) dx = − dx,
xi xi h2

as the support of Φi Φi+1 is the interval [xi , xi+1 ]. Hence,


 h
(h − ξ)ξ h
Mi,i+1 = dξ = , i = 1, . . . , n − 2.
0 h2 6

xh Fh
L
Figure 4.4.6. Fixed end-fixed end bar subjected to an axial uniform load and a
concentrated force.
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 212

212 Matlab for Engineering

The remaining elements of M are zero. For example, consider


 L
M1,3 = Φ1 (x)Φ3 (x) dx.
0

The supports of Φ1 and Φ3 are the intervals [x0 , x2 ] and [x2 , x4 ], respec-
tively. Their intersection is void and M1,3 = 0.

Exercise 4.4.16 Write a function, say diffusion d A, that applies the


FEM to solve the following Dirichlet problem
Ut − Uxx = 0, 0 < x < L, 0 < t ≤ T,

U (x, 0) = x2 , 0 ≤ x ≤ L,

U (0, t) = 2t, U (L, t) = 2t + L2 , 0 < t ≤ T.

Exercise 4.4.17 Write a function, say wave d A, that applies the FEM
to the simple Dirichlet problem
Utt − Uxx = 0, 0 < x < L, 0 < t ≤ T,

U (x, 0) = x2 , Ut (x, 0) = 0, 0 < x < L,

U (0, t) = t2 , U (L, t) = t2 + L2 , 0 < t ≤ T.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 213

Chapter 5

Introduction to the Finite Element Method


in Two Spatial Dimensions

The Finite Element Method (FEM) in two spatial dimensions is presented.


Since the method is applied to elliptic partial differential equations, the
section below is devoted to this topic, where the uniqueness of the solutions
of the main boundary value problems is discussed. In addition, Green’s
identities are introduced, as they are related to the weak formulation.
The FEM in two spatial dimensions is introduced in Sec. 5.2 and the
shape functions on the triangle element are defined. A number of examples
and exercises will help the reader to familiarize themselves with the new
concept. The weak formulation is discussed and applied to the boundary
value problems. Engineering applications are provided.
The last section is devoted to the Finite Difference Method for elliptic
partial differential equations Knabner and Angermann (2003). The Five-
Point Method is presented and illustrated with examples and applications.

5.1 Elliptic Partial Differential Equations


5.1.1 Green’s Identities
Consider the divergence theorem
 
∇ · q dΩ = q · n dS, (5.1.1)
Ω ∂Ω

where Ω is a bounded domain, q is a smooth vector function and n is the


outward unit normal vector. Some important identities can be derived from
(5.1.1). If it is assumed that q = ∇u, then from (5.1.1), it follows
 
∂u
Δu dΩ = dS, (5.1.2)
Ω ∂Ω ∂n

213
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 214

214 Matlab for Engineering

where ∂u/∂n = ∇u · n is the outward normal derivative to ∂Ω. Moreover,


if q = v∇u, then (5.1.1) gives
 
∂u
(vΔu + ∇v · ∇u) dΩ = v dS. (5.1.3)
Ω ∂Ω ∂n

Similarly, if q = u∇v, then


 
∂v
(uΔv + ∇u · ∇v) dΩ = u dS.
Ω ∂Ω ∂n
Subtracting the last equation to (5.1.3) yields
   
∂u ∂v
(vΔu − uΔv) dΩ = v −u dS. (5.1.4)
Ω ∂Ω ∂n ∂n
Integral Relationships (5.1.2) to (5.1.4) are named Green’s 1 identities. They
are used to discuss the uniqueness of the solutions to boundary value
problems for the Poisson2 equation in Sec. 5.1.2. In addition, Green’s
identities will be applied to derive the weak form of the Poisson equation
in Sec. 5.2.2.

5.1.2 Boundary Value Problems


Consider the Dirichlet problem for the Poisson equation

ΔU = F (x, y, z), (x, y, z) ∈ Ω,
(5.1.5)
U = g(x, y, z), (x, y, z) ∈ ∂Ω.
When F = 0, the Poisson’s equation is named Laplace’s equation. Let U1
and U2 be two solutions to Problem (5.1.5), and V be their difference

V = U1 − U2 .

The function V is the solution of the homogeneous problem



ΔV (x, y, z) = 0, (x, y, z) ∈ Ω,
(5.1.6)
V (x, y, z) = 0, (x, y, z) ∈ ∂Ω.

1 George Green, a British scientist, 1793–1841. He made fundamental scientific contribu-

tions in Electromagnetism in An Essay on the Application of Mathematical Analysis to


the Theories of Electricity and Magnetism (1828).
2 Siméon Denis Poisson, a French scientist, 1781–1840. He was Professor at École

Polytechnique in Paris. He made scientific contributions in Electromagnetism and


Analytical Mechanics.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 215

Introduction to the Finite Element Method in Two Spatial Dimensions 215

Consider Identity (5.1.3) for u = v = V ,


 
∂V
(V ΔV + ∇V · ∇V ) dΩ = V dS.
Ω ∂Ω ∂n
Using (5.1.6) we have

(Vx2 + Vy2 + Vz2 ) dΩ = 0.
Ω

Hence,

Vx2 + Vy2 + Vz2 = 0, on Ω ⇒ Vx = Vy = Vz = 0, on Ω.

Therefore,

V = U1 − U2 = c, on Ω,

where the constant c must be zero because of (5.1.6)2 . In conclusion, it


is U1 = U2 , proving that the solution to the Dirichlet Problem (5.1.5) is
unique.
Consider the Neumann problem for the Poisson equation

ΔU = F (x, y, z), (x, y, z) ∈ Ω,
(5.1.7)
∂U/∂n = g(x, y, z), (x, y, z) ∈ ∂Ω.

If U1 and U2 are two solutions to Problem (5.1.7), then their difference


V = U1 − U2 is the solution of the homogeneous problem

ΔV (x, y, z) = 0, (x, y, z) ∈ Ω,
∂V (x, y, z)/∂n = 0, (x, y, z) ∈ ∂Ω.

By using the same reasoning as in the previous problem, one arrives at

V = U1 − U2 = c, on Ω.

Nothing else can be deduced, as we cannot know the value of V on the


boundary in this case. In addition, consider Identity (5.1.2) for u = U ,
 
∂U
ΔU dΩ = dS,
Ω ∂Ω ∂n

and use (5.1.7)


 
F dΩ = g dS. (5.1.8)
Ω ∂Ω
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 216

216 Matlab for Engineering

Relationship (5.1.8) is a necessary condition for the existence of solutions


to Problem (5.1.7). If the data F and g are not assigned in agreement with
(5.1.8), then Problem (5.1.7) has no solution. In conclusion, the Neumann
problem has solutions only when compatibility Condition (5.1.8) is satisfied.
In this case, the problem has infinite solutions that differ by a constant.
Let us consider the Dirichlet–Neumann problem


⎪ ΔU = F (x, y, z), (x, y, z) ∈ Ω,

U = g1 (x, y, z), (x, y, z) ∈ ∂Ω1 , (5.1.9)



∂U/∂n = g2 (x, y, z), (x, y, z) ∈ ∂Ω2 ,
where ∂Ω1 ∪ ∂Ω2 = ∂Ω and ∂Ω1 ∩ ∂Ω2 = ∅. From the previous reasoning,
it is easily realized that Problem (5.1.9) has a unique solution.
Let us consider the Robin problem

ΔU = F (x, y, z), (x, y, z) ∈ Ω,
(5.1.10)
∂U/∂n + αU = g(x, y, z), (x, y, z) ∈ ∂Ω,
where α is a positive function

α(x, y, z) > 0, (x, y, z) ∈ ∂Ω

The difference V = U1 − U2 of two solutions to Problem (5.1.10) satisfies


the homogeneous problem

ΔV = 0, (x, y, z) ∈ Ω,
(5.1.11)
∂V /∂n + αV = 0, (x, y, z) ∈ ∂Ω.
Consider Identity (5.1.3) for u = v = V ,
 
∂V
(V ΔV + ∇V · ∇V ) dΩ = V dS.
Ω ∂Ω ∂n
Using (5.1.11), we have
 
2 2 2
(Vx + Vy + Vz ) dΩ + αV 2 dS = 0.
Ω ∂Ω

This relationship implies

V = 0, on ∂Ω, (5.1.12)

and

Vx2 + Vy2 + Vz2 = 0, on Ω.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 217

Introduction to the Finite Element Method in Two Spatial Dimensions 217

As in the previous problems, the last equation leads to

V = U1 − U2 = c, on Ω,

where the constant c must be zero because of (5.1.12). The Robin problem
has a unique solution.

5.2 Finite Element Method in Two Spatial Dimensions


5.2.1 Shape Functions
The FEM in two spatial dimensions considers the triangle element. The 2D
domain is subdivided into triangles. Examples of simple triangulations are
illustrated in Figs. 5.2.1 and 5.2.3. Some Matlab functions able to create
and plot a triangulation will be provided in the next examples.

Example 5.2.1 The following listing creates the triangulation in Fig.


5.2.1, and produces the plots in Fig. 5.2.2.
function triangulation plot1
% This is the function file triangulation plot1.m.
% A triangulation is created manually and by calling the delaunay
% function. The triangulation is plotted.
h = 1; k = 1;
x = [h; 2*h; h; 2*h; h; 2*h; h; 2*h; 3*h; 3*h; 3*h; 2*h; h; 0; 0; 0];
y = [k; k; 2*k; 2*k; 3*k; 3*k; 0; 0; k; 2*k; 3*k; 4*k; 4*k; 3*k; 2*k; k];

13 12
T19
T17 T18 T20
14 5 6 11
T12 T14 T16
T11 3 T13 4 T15
15 10
T6 T8 T10
T5 1 T7 2 T9
16 9
k T1 T3 T4
T2
h 7 8

Figure 5.2.1. Example of triangulation.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 218

218 Matlab for Engineering

1
3
y
0
2

-1 1
4
3 3
2 2
y 1 1 x 0
0 0 0 1 x 2 3

Figure 5.2.2. Triangulation plots.

T= [1 16 7; 1 7 8; 1 8 2; 2 8 9; 1 15 16;% Triangulation created manually.


1 3 15; 1 2 3; 2 4 3; 2 9 4; 9 10 4; 3 14 15; 3 5 14; 3 4 5; 4 6 5; 4 10 6;
10 11 6; 5 13 14; 5 6 13; 6 12 13; 6 11 12];
z = zeros(length(x), 1);
trimesh(T, x, y, z,’LineWidth’,1, ’edgecolor’, ’k’, ’Marker’,’*’);
xlabel(’x’); ylabel(’y’);
Td = delaunay(x,y); % Triangulation created by calling the delaunay function.
figure(2);
triplot(Td, x, y, ’k’,’LineWidth’,1); xlabel(’x’); ylabel(’y’);
end

Example 5.2.2 The following listing creates the triangulation in Fig.


5.2.3. The undesirable triangulation is shown in Fig. 5.2.4.
function triangulation plot2
% This is the function file triangulation plot2.m.
% A triangulation is created by calling the delaunay function.
% The triangulation is plotted.
n = 4; m = 8; h = 1;
x1(1:n*m,1) = repmat([0; h; 2*h; 3*h], 8, 1);
for k=1:m
y1(1+(k-1)*n:k*n,1) = (k-1)*h*ones(n,1);
end
x2 = [4*h; 4*h; 4*h]; y2 =[0; h; 2*h]; x = [x1; x2]; y = [y1; y2];
Tw = delaunay(x,y); triplot(Tw, x, y, ’k’,’LineWidth’,1); axis(’equal’);
% The delaunay function can generate undesirable triangulations when
% the domain is nonconvex. In this situation, constrained triangulations
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 219

Introduction to the Finite Element Method in Two Spatial Dimensions 219

29 30 31 32

25 28
21 24
17 20
13 16
9 35
5 34

1 2 3 4 33

Figure 5.2.3. Example of triangulation.

Figure 5.2.4. Undesirable triangulation.

% should be created and the delaunayTriangulation class should be used.


% However, in this simple example, the triangulation is corrected manually.
T1 = delaunay(x1,y1);
T2 = [4 33 8; 33 34 8; 8 34 12; 34 35 12]; T = [T1; T2];
figure(2); triplot(T, x, y, ’k’,’LineWidth’,1); axis(’equal’);
end
Consider the generic triangle element. Its vertices, named nodes in this
contest, are locally numbered 1, 2 and 3, counterclockwise. The coordinates
of a node are referred to a global reference system and indicated with
(x1 , y1 ), (x2 , y2 ) and (x3 , y3 ), as shown in Fig. 5.2.5. Consider the linear
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 220

220 Matlab for Engineering

3 (x3, y3)

y
T

1 (x1, y1) 2 (x2, y2)

Figure 5.2.5. Triangle element.

functions on triangle T

Φi (x, y) = ai x + bi y + ci , i = 1, 2, 3, (x, y) ∈ T, (5.2.1)

defined by the Kronecker δ functions


1, i = j,
Φi (xj , yj ) = δij = i, j = 1, 2, 3. (5.2.2)
 j,
0, i =

The explicit expression of Φi is derived from Property (5.2.2). Indeed,


consider Φ1 and note that
Φ1 (x1 , y1 ) = 1, a1 x1 + b1 y1 + c1 = 1,
Φ1 (x2 , y2 ) = 0, ⇒ a1 x2 + b1 y2 + c1 = 0,
Φ1 (x3 , y3 ) = 0, a1 x3 + b1 y3 + c1 = 0.
Hence,
⎡ ⎤⎡ ⎤ ⎡ ⎤
x1 y1 1 a1 1
⎣ x2 y2 1 ⎦ ⎣ b1 ⎦ = ⎣ 0 ⎦ . (5.2.3)
x3 y3 1 c1 0

Solving System (5.2.3) with respect to a1 , b1 and c1 , and the result


substituted into (5.2.1) yields (see Exercise 5.4.1)

Φ1 (x, y) = [(y2 − y3 )x − (x2 − x3 )y + x2 y3 − x3 y2 ]/(2A), (5.2.4)

where A is the area of the triangle


 
x y 1
1  1 1 
A =  x2 y2 1  . (5.2.5)
2
x3 y3 1 
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 221

Introduction to the Finite Element Method in Two Spatial Dimensions 221

The functions Φ2 and Φ3 are derived similarly

Φ2 (x, y) = [(y3 − y1 )x − (x3 − x1 )y + x3 y1 − x1 y3 ]/(2A),


(5.2.6)
Φ3 (x, y) = [(y1 − y2 )x − (x3 − x2 )y + x1 y2 − x2 y1 ]/(2A).

The gradient of Φi
 
∂Φi ∂Φi
∇Φi (x, y) = , , (x, y) ∈ T, (5.2.7)
∂x ∂y

is a constant vector function, as Φi is linear. The partial derivatives are the


same as the coefficients of x and y of the linear function Φi .
The functions Φi , i = 1, 2, 3, are a basis for the linear functions defined
on T . Indeed, any linear function u(x, y) defined on T can be expressed as
a linear combination of Φ1 , Φ2 and Φ3 ,

u(x, y) = u1 Φ1 (x, y) + u2 Φ2 (x, y) + u3 Φ3 (x, y), (x, y) ∈ T, (5.2.8)

where ui = u(xi , yi ). Formula (5.2.8) follows from the straightforward


calculations shown in Exercise 5.4.2. Moreover,

u1 Φ1 (x, y) + u2 Φ2 (x, y) + u3 Φ3 (x, y) = 0, (x, y) ∈ T, (5.2.9)

if and only if

ui = 0, i = 1, 2, 3. (5.2.10)

It must be proved that (5.2.9) ⇒ (5.2.10), as ⇐ is obvious. From (5.2.9)


and Property (5.2.2), it follows

0 = u1 Φ1 (xi , yi ) + u2 Φ2 (xi , yi ) + u3 Φ3 (xi , yi ) = ui Φi (xi , yi ) = ui ,

that is the desired result.


Consider a triangulation with m triangles and n nodes, for example,
see Fig. 5.2.1. From the previous discussion related to a single element, it
follows that the linear functions definined by the Kronecker δ functions

1, i = j,
Φi (xj , yj ) = δij = i, j = 1, . . . , n, (5.2.11)
0, i = j,
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 222

222 Matlab for Engineering

are a global basis for the continuous functions u that are linear on each
element, i.e., the piecewise linear continuous functions u defined on the
triangulated domain. Therefore, u can be expressed as
n

u(x, y) = ui Φi (x, y), (5.2.12)
i=1

where ui = u(xi , yi ). The support of Φi related to node i is composed of


the triangles that have a vertex that is the same as node i. The expression
of Φi on each triangle of the support is deduced from the general Formulas
(5.2.4) and (5.2.6).

Example 5.2.3 Let us determine Φi and ∇Φi for the triangulation in


Fig. 5.2.1. Consider the node 1. The support of Φ1 is {T1 , T2 ,T3 , T7 , T6 ,
T5 }. See Fig. 5.2.6. Let us add the local numbering 1, 2 and 3 to the nodes
of the triangles belonging to the support, as shown in Fig. 5.2.7 for the
triangles T1 , T2 and T3 . In this way, it is easy to apply Formula (5.2.4), and

3 2
T6 1 T7 T3
T5 T1 T2 8
15
16 7

Figure 5.2.6. Support of Φ1 .

16 (2) 1 (1) 1 (1) 1 (1) 2 (3)

T1 T3

T2

7 (3) 7 (2) 8 (3) 8 (2)

Figure 5.2.7. T1 , T2 , T3 : global and local numbering.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 223

Introduction to the Finite Element Method in Two Spatial Dimensions 223

for node 1, we get


⎧ ⎧
⎪ x/h + y/k − 1,


on T1 , ⎪


(1/h, 1/k), on T1 ,

⎪ ⎪



y/k, on T2 , ⎪

(0, 1/k), on T2 ,

⎪ ⎪

⎨ −x/h + 2, on T3 , ⎨ (−1/h, 0), on T3 ,
Φ1 = −x/h − y/k + 3, on T7 , ∇Φ1 = (−1/h, −1/k), on T7 ,

⎪ ⎪

⎪ −y/k + 2,
⎪ on T6 , ⎪
⎪ (0, −1/k), on T6 ,

⎪ ⎪


⎪ x/h, on T5 ⎪
⎪ (1/h, 0), on T5 ,

⎩ ⎪

0, otherwise, 0, otherwise.

Consider node 2. The support of Φ2 is {T3 , T4 , T9 , T8 , T7 }. Applying Formula


(5.2.4) yields
⎧ ⎧

⎪ x/h + y/k − 2, on T3 , ⎪
⎪ (1/h, 1/k), on T3 ,

⎪ ⎪


⎪ −x/h + y/k + 2, on T 4 , ⎪
⎪ (−1/h, 1/k), on T4 ,

⎨ ⎪

−x/h − y/k + 4, on T9 , (−1/h, −1/k), on T9 ,
Φ2 = ∇Φ2 =

⎪ −y/k + 2, on T8 , ⎪
⎪ (0, −1/k), on T8 ,

⎪ ⎪


⎪ x/h − 1, on T 7 , ⎪
⎪ (1/h, 0), on T7 ,

⎩ ⎪

0, otherwise, 0, otherwise.

The support of Φ3 is {T6 , T7 , T8 , T13 , T12 , T11 }. Applying Formula (5.2.4)


yields
⎧ ⎧

⎪ x/h + y/k − 2, on T6 , ⎪
⎪ (1/h, 1/k), on T6 ,

⎪ ⎪


⎪ y/k − 1, on T7 , ⎪
⎪ (0, 1/k), on T7 ,

⎪ ⎪


⎨ −x/h + 2, on T8 , ⎪
⎨ (−1/h, 0), on T8 ,
Φ3 = −x/h − y/k + 4, on T13 , ∇Φ3 = (−1/h, −1/k), on T13 ,

⎪ ⎪


⎪ −y/k + 3, on T12 , ⎪
⎪ (0, −1/k), on T12 ,

⎪ ⎪


⎪ x/h, on T 11 , ⎪
⎪ (1/h, 0), on T11 ,

⎩ ⎪

0, otherwise, 0, otherwise.

See Exercise 5.4.3 for the remaining nodes.

Example 5.2.4 The following listing presents a function that calculates


Φi and ∇Φi for a given triangulation.
function [Phi, nablaPhi] = pyramid(T, x, y, node)
% This is the function file pyramid.m.
% The input arguments are: triangulation T, coordinates x, y of the nodes
% and the node related to Φ to calculate. The function returns two
% matrices with the coefficients of Φ and ∇Φ. The number of rows
% in the matrices is the same as the number of triangles in the support of Φ.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 224

224 Matlab for Engineering

% For example, consider that triangulation in Example 5.2.1 is passed


% and node = 1. If it is assumed h = k = 1, then the function returns
⎡ ⎤ ⎡ ⎤
1 1 −1 1 1 1 1
⎢ 0 1 0 2⎥ ⎢ 0 1 2⎥
⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥
⎢ −1 0 2 3⎥ ⎢ −1 0 3 ⎥
%Φ1 = ⎢ ⎥ , ∇Φ1 = ⎢ ⎥.
⎢ 1 0 0 5⎥ ⎢ 1 0 5⎥
⎢ ⎥ ⎢ ⎥
⎣ 0 −1 2 6 ⎦ ⎣ 0 −1 6 ⎦
−1 −1 3 7 −1 −1 7

% The first three elements of a row in Φ1 are the coefficients of the linear
% function Φ1 related to the triangle specified by the fourth element in the
% row. The first two elements of a row in ∇Φ1 are the components of the
% gradient of Φ1 related to the triangle specified by the third element in
% the row.
[support, index] = find(T == node);
mt = length(support);
Phi = zeros(mt, 4); nablaPhi = zeros(mt, 3);
for i=1:mt
local = local base(T, support(i), index(i));
% Local base for support(i) with node = 1
area= det([ x(T(support(i),:)’) y(T(support(i),:)’) [1;1;1]])/2;
% area of support(i)
a = (y(local(2)) - y(local(3)))/2/area;
b = -(x(local(2)) - x(local(3)))/2/area;
c = (x(local(2))*y(local(3))-x(local(3))*y(local(2)) )/2/area;
Phi(i,:) = [a b c support(i)]; nablaPhi(i,:) = [a b support(i)];
end
end
% ———- Local function ————-
function first = local base(T, triangle, index)
first = T(triangle, :); % The nodes of ’triangle’ are saved in ’first’.
if index > 1 % Local base (counterclockwise).
if index > 2
first(1) = T(triangle, 3);
first(2) = T(triangle, 1);
first(3) = T(triangle, 2);
else
first(1) = T(triangle, 2);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 225

Introduction to the Finite Element Method in Two Spatial Dimensions 225

first(2) = T(triangle, 3);


first(3) = T(triangle, 1);
end
end
end

Example 5.2.5 The following listing applies the pyramid function to the
domain in Fig. 5.2.1. A specific Φ is returned and plotted. See Fig. 5.2.8.
function [Phi, nablaPhi] = pyramid phi
% This is the function file pyramid phi.m.
% Pyramid function is called to determine the function Phi related to
% the node specified by the User. Phi is plotted.
h = 1; k = 1;
x = [h; 2*h; h; 2*h; h; 2*h; h; 2*h; 3*h; 3*h; 3*h; 2*h; h; 0; 0; 0];
y = [k; k; 2*k; 2*k; 3*k; 3*k; 0; 0; k; 2*k; 3*k; 4*k; 4*k; 3*k; 2*k; k];
T= [1 16 7; 1 7 8; 1 8 2; 2 8 9; 1 15 16; 1 3 15; 1 2 3;
2 4 3; 2 9 4; 9 10 4; 3 14 15; 3 5 14; 3 4 5; 4 6 5;
4 10 6; 10 11 6; 5 13 14; 5 6 13; 6 12 13; 6 11 12];
node = 4; % Specify the node here.
[Phi, nablaPhi] = pyramid(T, x, y, node);
z = zeros(length(x), 1); z(node) = 1; trimesh(T, x, y, z, ’edgecolor’, ’k’);
xlabel(’x’); ylabel(’y’); zlabel(’\Phi’);
hidden off;
end

5.2.2 Weak Form of the Poisson Equation


Consider the Dirichlet problem for the Poisson’s equation in two dimensions

−ΔU (x, y) = F (x, y), (x, y) ∈ Ω, (5.2.13)

U (x, y) = g(x, y), (x, y) ∈ ∂Ω. (5.2.14)

The minus sign in Eq. (5.2.13) could be eliminated and included in F .


However, Eq. (5.2.13) is usually written like this in the FEM context.
Multiply Eq. (5.2.13) by the smooth test function v(x, y) and integrate
over Ω
 
− vΔU dΩ = F v dΩ. (5.2.15)
Ω Ω
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 226

226 Matlab for Engineering

0.8

0.6
Φ
0.4

0.2

0
4
3 3
2 2
y 1 1 x
0 0

Figure 5.2.8. Φ(4).

Let us use Green’s identity (5.1.3) in the previous equation and get
  
∂U
∇v · ∇U dΩ = v ds + F v dΩ, (5.2.16)
Ω ∂Ω ∂n Ω

where ∂U/∂n is the outward normal derivative to ∂Ω. The last equation is
the weak form of Eq. (5.2.13) that is named the strong form by contrast. The
FEM considers Eq. (5.2.16). The approximating solution to the Dirichlet
problem is expressed as a finite series
n

u(x, y) = uj Φj (x, y), (5.2.17)
j=1

where uj , j = 1, ..., n are unknown coefficients and Φj (x, y) are given shape
functions. Substituting (5.2.17) into (5.2.16) and assuming v = Φi , i =
1, . . . , n, yields
 n   
∂u
uj ∇Φi ·∇Φj dΩ = Φi ds+ F Φi dΩ, i = 1, . . . , n. (5.2.18)
j=1 Ω ∂Ω ∂n Ω

Hence,
n

Kij uj = fi , i = 1, . . . , n, (5.2.19)
j=1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 227

Introduction to the Finite Element Method in Two Spatial Dimensions 227

where
  
∂u
Kij = ∇Φi · ∇Φj dΩ, fi = Φi ds + F Φi dΩ, i = j = 1, . . . , n.
Ω ∂Ω ∂n Ω
(5.2.20)
In compact form, System (5.2.19) is written as

Ku = f ,

where K is the stiffness matrix and f is the load vector. The nodes where
the solution is known (as given by the boundary conditions) are named
constrained nodes. By contrast, the nodes where the solution is unknown
are named free nodes. For example, consider the Dirichlet problem. The
nodes internal to the domain are free and the nodes on the boundary are
constrained.

Example 5.2.6 Let us discuss the following Dirichlet problem

−ΔU (x, y) = 0, (x, y) ∈ Ω, (5.2.21)

U (x, y) = x2 − y 2 , (x, y) ∈ ∂Ω, (5.2.22)

where Ω is the domain in Fig. 5.2.1. The free nodes are nf = 6, namely,
the nodes from 1 to 6. The remaining nodes from 7 to 15 are constrained.
The solution on these nodes is given by boundary Condition (5.2.22)

u 7 = h2 , u8 = 4h2 , u9 = 9h2 − k 2 ,
u10 = 9h − 4k , u11 = 9h − 9k , u12 = 4h2 − 16k 2 ,
2 2 2 2
(5.2.23)
u13 = h2 − 16k 2 , u14 = −9k 2 , u15 = −4k 2 ,
2
u16 = −k .

System (5.2.19) for Problem (5.2.21) and (5.2.22) has nf = 6 unknowns


u1 , . . . , u6 . Therefore, it simplifies to
nf
  n
 
uj ∇Φi · ∇Φj dΩ = − uj ∇Φi · ∇Φj dΩ, i = 1, . . . , nf ,
j=1 Ω j=nf +1 Ω

(5.2.24)
as F = 0 and Φi (x, y) = 0, i = 1, . . . , nf on the boundary. System (5.2.24)
can be written in matrix notation as follows
nf

Kij uj = gi , i = 1, . . . , nf , (5.2.25)
j=1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 228

228 Matlab for Engineering

where
n
  n

gi = − uj ∇Φi · ∇Φj dΩ = − bij uj , i = 1, . . . , nf .
j=nf +1 Ω j=nf +1
(5.2.26)
A listing that applies the FEM to solve Problem (5.2.21) and (5.2.22) will be
illustrated in Example 5.2.7. Now, the integrals in Formulas (5.2.25) and
(5.2.26) are calculated manually in order to better understand the code
in the listing. Note that the integrals over Ω in (5.2.21) and (5.2.22) are
calculated on smaller domains. Indeed, if the integrand function is ∇Φi ·
∇Φi , then the integral is calculated over the support of Φi . If the integrand
function is ∇Φi ·∇Φj , then the integral is calculated over the intersection of
the two supports and it is zero if the intersection is void. Consider i = 1 and
use the results provided in Example 5.2.3 and Exercise 5.4.4. Therefore, we
get
  
1 1
K11 = ∇Φ1 · ∇Φ1 = 2hk 2 + 2 ,
T1 ∪T2 ∪T3 ∪T7 ∪T6 ∪T5 h k
 
1 1
K12 = ∇Φ1 ·∇Φ2 = −hk 2 , K13 = ∇Φ1 ·∇Φ3 = −hk 2 ,
T3 ∪T7 h T6 ∪T7 k
K14 = K15 = K16 = 0,

1
b17 = ∇Φ1 · ∇Φ7 = −hk 2 , b18 = b19 = b1,10 = b1,11 = 0,
T1 ∪T2 k

1
b1,12 = b1,13 = b1,14 = b1,15 = 0, b1,16 = ∇Φ1 · ∇Φ16 = −hk .
T1 ∪T5 h2
Lastly, consider boundary Values (5.2.23) and obtain
h k h k
u + u = h2 − k 2 .
g1 =
k 7 h 16 k h
The other integrals are calculated in Exercise 5.4.4.

Example 5.2.7 The following listing applies the FEM to solve Problem
(5.2.21) and (5.2.22). If it is assumed h = k = 1, then the following values
are obtained for the unknown coefficients:
u1 = 0.0, u2 = 3.0, u3 = −3.0, u4 = 0.0, u5 = −8.0, u6 = −5.0.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 229

Introduction to the Finite Element Method in Two Spatial Dimensions 229

The numerical solution is very accurate as it can be immediately verified


by considering the analytical solution of the problem: U (x, y) = x2 − y 2 .
The graph of the numerical solution is shown in Fig 5.2.9.

function u = laplace
% This is the function file laplace.m.
% FEM is applied to solve the Dirichlet problem in Example 5.2.7, Sec. 5.2.2.
h = 1; k = 1;
x = [h; 2*h; h; 2*h; h; 2*h; h; 2*h; 3*h; 3*h; 3*h; 2*h; h; 0; 0; 0];
y = [k; k; 2*k; 2*k; 3*k; 3*k; 0; 0; k; 2*k; 3*k; 4*k; 4*k; 3*k; 2*k; k];
T= [1 16 7; 1 7 8; 1 8 2; 2 8 9; 1 15 16; 1 3 15; 1 2 3; 2 4 3; 2 9 4; 9 10 4;
3 14 15; 3 5 14; 3 4 5; 4 6 5; 4 10 6; 10 11 6; 5 13 14; 5 6 13; 6 12 13;
6 11 12];
n = length(x); % Number of nodes.
nf = 6;% Free nodes.
K = zeros(nf,nf);% Stiffness matrix.
b = zeros(nf,n-nf);
ub = [h2 ; 4*h2 ; 9*h2 -k2 ; 9*h2 -4*k2 ; 9*h2 -9*k2; 4*h2 -16*k2;
h2 -16*k2; -9*k2 ; -4*k2 ; -k2 ]; % Boundary conditions.

10

0
u
-5

-10

-15
4 3

3 2
2
1 x
y 1
0 0

Figure 5.2.9. Graph of the numerical solution to Problem (5.2.21) and (5.2.22).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 230

230 Matlab for Engineering

% FEM
for i=1:nf
[~,nablaPhii] = pyramid(T, x, y, i);
support i = nablaPhii(:,3);
for j=i:n
[~,nablaPhij] = pyramid(T, x, y, j);
support j = nablaPhij(:,3);
mt = length(support j);
for r=1:mt
if ismember(support j(r), support i)==1
index = find(support i == support j(r));
area=det([x(T(support j(r),:)’,:) y(T(support j(r),:)’,:) [1;1;1]])/2;
% Area of support j(r).
if j <= nf % If j is a free node, matrix K is updated.
K(i,j)=K(i,j)+nablaPhij(r,1:2)*nablaPhii(index,1:2)’*area;
K(j,i)=K(i,j);
else % If j is a constrained node, matrix b is updated.
b(i,j-nf)=b(i,j-nf)+nablaPhij(r,1:2)*nablaPhii(index,1:2)’*area;
end
end
end
end
end
g = -b * ub; u = K\g; u = [u; ub];
U = [h2 -k2 ; 4*h2 -k2 ; h2 -4*k2 ; 4*h2 -4*k2; h2 -9*k2 ; 4*h2 -9*k2 ;ub];
% Analytical solution.
fprintf(’Maximum error = %g\n’,max( abs(U-u) ) )
trimesh(T, x, y, u, ’edgecolor’, ’b’, ’Marker’,’*’); view(-46,43); hold on;
trimesh(T, x, y, U, ’edgecolor’, ’b’, ’Marker’, ’*’ ,’MarkerEdgecolor’, ’r’);
xlabel(’x’); ylabel(’y’); zlabel(’u’); hold off
end

5.2.3 Dirichlet–Neumann Problem


Consider the Dirichlet–Neumann problem for the Poisson equation in two
dimensions

−ΔU (x, y) = F (x, y), (x, y) ∈ Ω. (5.2.27)


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 231

Introduction to the Finite Element Method in Two Spatial Dimensions 231

The function U is assigned on ∂Ω1 ⊂ ∂Ω

U (x, y) = G1 (x, y), (x, y) ∈ ∂Ω1 , (5.2.28)

and the normal derivative ∂U/∂n is assigned on ∂Ω2 ⊂ ∂Ω

∂U
(x, y) = G2 (x, y), (x, y) ∈ ∂Ω2 , (5.2.29)
∂n

where ∂Ω1 ∪ ∂Ω2 = ∂Ω and ∂Ω1 ∩ ∂Ω2 = ∅. Multiply Eq. (5.2.27) by the
smooth test function v(x, y) and integrate over Ω
 
− vΔU dΩ = F v dΩ.
Ω Ω

Let us use Green’s identity (5.1.3) in the previous equation and get
   
∂U
∇v · ∇U dΩ = v ds + vG2 ds + F v dΩ, (5.2.30)
Ω ∂Ω1 ∂n ∂Ω2 Ω

where boundary Condition (5.2.29) was used. The last equation is the
weak form of Eq. (5.2.27) that is named the strong form by contrast. The
FEM considers the weak form (5.2.30) and the approximating solution is
expressed as a finite series
n

u(x, y) = uj Φj (x, y),
j=1

where uj , j = 1, ..., n are unknown coefficients and Φj (x, y) are given shape
functions.

Example 5.2.8 Consider the following Dirichlet–Neumann problem

−ΔU (x, y) = 0, (x, y) ∈ Ω, (5.2.31)

U =0 on segment x = y, (∂Ω1 ), (5.2.32)



∂U 0 on segment y = 0, (∂Ω2 ),
= (5.2.33)
∂n 4h on segment x = 2h, (∂Ω2 ),
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 232

232 Matlab for Engineering

Figure 5.2.10. Dirichlet–Neumann Problem (5.2.31) to (5.2.33).

where Ω is the domain in Fig. 5.2.10. By considering boundary Condition


(5.2.32), the approximating solution to Problem (5.2.31)–(5.2.33) simplifies
to
3

u(x, y) = uj Φj (x, y). (5.2.34)
j=1

Substituting (5.2.34) into (5.2.30) and assuming v = Φi , i = 1, 2, 3, yields


3  
∂u
uj ∇Φj · ∇Φi dΩ = Φi ds, i = 1, 2, 3,
j=1 Ω ∂Ω2 ∂n

as F = 0 and Φi = 0, i = 1, 2, 3, on segment x = y (∂Ω1 ). By considering


boundary Condition (5.2.33), it follows from the previous equation that
3  y6
Kij uj = 4h Φi (2h, y)dy, i = 1, 2, 3. (5.2.35)
j=1 y2

The supports of Φ1 , Φ2 and Φ3 are {T1 , T2 }, {T2 , T3 } and {T3 , T4 },


respectively. In addition, the functions Φ1 , Φ2 and Φ3 are given by (see
Formula (5.2.4))
 
x/h − y/h, on T1 , x/h − 1, on T2 ,
Φ1 = Φ2 =
−x/h − y/h + 2/h, on T2 , −y/h + 1, on T3 ,
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 233

Introduction to the Finite Element Method in Two Spatial Dimensions 233


x/h + y/h − 2, on T3 ,
Φ3 =
x/h − y/h, on T4 .

By using the previous formulas, we can calculate the stiffness matrix

K11 = 2, K12 = −0.5, K13 = 0, K22 = 1, K23 = −0.5, K33 = 2. (5.2.36)

Calculate the integrals in the known term of System (5.2.35). For i = 1


 y6
4h Φ1 (2h, y)dy = 0, (5.2.37)
y2

as Φ1 = 0 on {x = 2h, 0 < y < 2h}. Consider i = 2

 y6  h
4h Φ2 (2h, y)dy = 4h (1 − y/h)dy = 2h2 , (5.2.38)
y2 0

as Φ2 = 1 − y/h on {x = 2h, 0 < y < h} and Φ2 = 0 on {x = 2h, h < y <


2h}. Lastly, for i = 3

 y6  h  2h
4h Φ3 (2h, y)dy = 4h y/h dy + 4h (2 − y/h)dy = 4h2 , (5.2.39)
y2 0 h

as Φ3 = y/h on {x = 2h, 0 < y < h} and Φ3 = 2 − y/h on {x = 2h, h <


y < 2h}. Substituting (5.2.36) to (5.2.39) into (5.2.35), one arrives at the
system

⎨ 2u1 − 0.5u2 = 0,

−0.5u1 + u2 − 0.5u3 = 2h2 ,


−0.5u2 + 2u3 = 4h2 .

Solving the previous system yields

u1 = h2 , u2 = 4h2 , u3 = 3h2 .

The solution provided by the FEM is very accurate, as it can be verified by


using the analytical solution of Problem (5.2.31)–(5.2.33): U = x2 − y 2 .
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 234

234 Matlab for Engineering

H2

H3 H1

L x
L1
L2

Figure 5.2.11. Simple model of a dam.

5.2.4 Applications to the Dam and Sheet Pile Wall


Consider the simple model of a dam in Fig. 5.2.11. The steady state seepage
motions are governed by the following Dirichlet–Neumann problem for the
piezometric head h = h(x, y)

Δh(x, y) = 0, (x, y) ∈ Ω, (5.2.40)

h = H1 on ∂Ω1 , h = H2 on ∂Ω2 , (5.2.41)

hx = 0 on ∂Ω3 ∪ ∂Ω4 ∪ ∂Ω5 ∪ ∂Ω6 , hy = 0 on ∂Ω7 ∪ ∂Ω8 , (5.2.42)

where

∂Ω1 = {L2 ≤ x ≤ L, y = H1 }, ∂Ω2 = {0 ≤ x ≤ L1 , y = H1 },

∂Ω3 = {x = 0, 0 < y < H1 }, ∂Ω4 = {x = L, 0 < y < H1 },

∂Ω5 = {x = L1 , H3 < y < H1 }, ∂Ω6 = {x = L2 , H3 < y < H1 },

∂Ω7 = {0 ≤ x ≤ L, y = 0}, ∂Ω8 = {L1 ≤ x ≤ L2 , y = H3 }.

Consider the FEM. The approximating solution h is given by the finite


series
nf n
 
h(x, y) = hj Φj (x, y) + hj Φj (x, y), (5.2.43)
j=1 j=nf +1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 235

Introduction to the Finite Element Method in Two Spatial Dimensions 235

where n is the total number of nodes and nf is the number of free nodes.
Substituting (5.2.43) into (5.2.30) and assuming v = Φi , i = 1, ..., nf , yields
nf n
 
Kij hj = − Kij hj , i = 1, . . . , nf , (5.2.44)
j=1 j=nf +1

where the parameters hj for the constrained nodes j = nf + 1, . . . , n, are


given by boundary Conditions (5.2.41).

Example 5.2.9 The following listing presents a function that applies the
FEM to Problem (5.2.40) to (5.2.42). The numerical solution is plotted in
Fig. 5.2.12. The streamlines and the equipotential lines are illustrated in
Fig. 5.2.13. Lastly, Fig. 5.2.14 shows the simple triangulation used in the
listing.
function [u, hh] = dam
% This is the function file dam.m.
% The FEM is applied to solve the Dirichlet–Neumann problem in Example
% 5.2.9. The function returns the matrix and the vector expressions of the
% numerical solution.

20

15
h

10
14

12

10

8
y
6

4
50
2 40
30
20
0 10 x
0

Figure 5.2.12. Numerical solution to Problem (5.2.40) to (5.2.42).


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 236

236 Matlab for Engineering

19.8
19.8 14.2
19

15
10

14
.4
19

.6
17
16
18

15
19

14
.4

5
19

.6
16
18

17

15
0
0 5 10 15 20 25 30 35 40

Figure 5.2.13. Streamlines and equipotential lines.

Figure 5.2.14. Triangulation.

% Initialization
L = 42; H1 = 14; H2 = 20; d = 2; nx = L/d; ny = H1/d;
% L = 42; H1 = 14; H2 = H1; d = 2; nx = L/d; ny = H1/d; % test
x = linspace(0,L,nx+1); y = linspace(0,H1,ny+1);
k1 = 6; i1 = 10; i2 = 13;
H3 = y(k1); L1 = x(i1); L2 = x(i2); % H3 = 10; L1 = 18; L2 = 24;
P = Points(x, y, nx+1, ny+1, i1, k1); xt = P(:,1); yt = P(:,2);
n = length(xt); hb = [H2*ones(i1,1); H1*ones(nx + 2 - i2,1)];
nf = n - length(hb); K = zeros(nf,nf); b = zeros(nf,n-nf);
T = Triangulation(nx, nx+1, ny+1, i1, i2, k1);
% FEM
for i=1:nf
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 237

Introduction to the Finite Element Method in Two Spatial Dimensions 237

[~,nablaPhii] = pyramid(T, xt, yt, i);


support i = nablaPhii(:,3);
for j=i:n
[~,nablaPhij] = pyramid(T, xt, yt, j);
support j = nablaPhij(:,3); mt = length(support j);
for r=1:mt
if ismember(support j(r), support i) == 1
index = find(support i == support j(r));
area = det([xt(T(support j(r),:)’,:) yt(T(support j(r),:)’,:)...
[1;1;1]])/2;
if j<nf+1
K(i,j)=K(i,j)+ nablaPhij(r,1:2)*nablaPhii(index,1:2)’*area;
K(j,i)=K(i,j);
else
b(i,j-nf)=b(i,j-nf)+nablaPhij(r,1:2)*nablaPhii(index,1:2)’*area;
end
end
end
end
end
f = -b * hb; v = K\f; hh = [v;hb];
H = [v(1:k1*(nx+1)+i1); nan*ones(i2-i1-1,1); v(k1*(nx+1)+i1+1:nf);...
H2*ones(i1,1); nan*ones(i2-i1-1,1); H1*ones(nx + 2 - i2,1)];
u = reshape(H,nx+1,ny+1);
% Plot
trimesh(T, xt, yt, hh,’LineWidth’,2); view(-15,69);
xlabel(’x’); ylabel(’y’); zlabel(’h’);
[hx, hy] = gradient(u’,d,d);
KK =10^(-8); qx = -KK*hx; qy = -KK*hy;
figure(2);
[~,cc] = contour(x,y,u’,[14.2,14.6,15,16,17,18,19,19.4,19.8]);
set(cc,’ShowText’,’on’); colormap cool;
axis(’equal’); axis([0 L 0 H1]);
rectangle(’position’,[0, 0, L, H1])
rectangle(’position’,[L1, H3 ,L2-L1 ,H1-H3],...
’FaceColor’,[0.8 0.8 0.8],’LineWidth’,1);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 238

238 Matlab for Engineering

hold on;
streamline(x,y,qx,qy,x(2:8),y(ny+1)*ones(1,length(x(2:8))));
end
% ———- Local functions ————-
function P = Points(x, y, n1, n2, i1, k1)
P = zeros(n1*n2,2);
P(1:n1*n2,1)= repmat(x’,n2,1);
for k=1:n2
P(1+(k-1)*n1:k*n1,2)= y(k)*ones(n1,1);
end
np1 = k1*n1+i1; np2 = (k1+1)*n1+i1;
P = P([1:np1,np1+3:np2,np2+3:n1*n2],:);
end
function T = Triangulation(nx, n1, n2, i1, i2, k1)
m = nx*2*(n2-1)-(i2-i1)*2*(n2-k1); T = zeros(m,3);
for k=1:k1-
for i=1:nx
T((k-1)*2*nx+2*i-1,:)= [(k-1)*n1+i (k-1)*n1+i+1 k*n1+i];
T((k-1)*2*nx+2*i,:) = [(k-1)*n1+i+1 k*n1+i+1 k*n1+i];
end
end
lt = nx*2*(k1-1); lp1 = n1*(k1-1); lp2 = n1*k1;
for i=1:i1-1
T(lt+2*i-1,:)= [lp1+i lp1+i+1 lp2+i];
T(lt+2*i,:) = [lp1+i+1 lp2+i+1 lp2+i];
end
lt = lt+2*(i1-1); lp1 = lp1+i1+2; lp2 = lp2+i1;
for i=1:n1-i2
T(lt+2*i-1,:)= [lp1+i lp1+i+1 lp2+i];
T(lt+2*i,:) = [lp1+i+1 lp2+i+1 lp2+i];
end
lt = lt+2*(n1-i2); lp1 = lp1+n1-i2+1; lp2 = lp2+n1-i2+1;
for i=1:i1-1
T(lt+2*i-1,:)= [lp1+i lp1+i+1 lp2+i];
T(lt+2*i,:) = [lp1+i+1 lp2+i+1 lp2+i];
end
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 239

Introduction to the Finite Element Method in Two Spatial Dimensions 239

lt = lt+2*(i1-1); lp1 = lp1+i1; lp2 = lp2+i1;


for i=1:n1-i2
T(lt+2*i-1,:)= [lp1+i lp1+i+1 lp2+i];
T(lt+2*i,:) = [lp1+i+1 lp2+i+1 lp2+i];
end
end
See Exercise 5.4.5.

Example 5.2.10 Consider the simple model of a sheet pile wall in Fig.
5.2.15. The steady state seepage analysis leads to considering the following
Dirichlet–Neumann problem for the piezometric head h = h(x, y)
Δh(x, y) = 0, (x, y) ∈ Ω, (5.2.45)

16 on {y = 12, 0 ≤ x ≤ x1 },
h= (5.2.46)
13 on {y = 12, x2 ≤ x ≤ 22},


⎪ {x = 0, 0 ≤ y < 12}, {x = 22, 0 ≤ y < 12},

∂h/∂n = 0 on {y = 0, 0 ≤ x ≤ 22}, {y = 6, x1 ≤ x ≤ x2 }, (5.2.47)


⎩ {x = x , 6 ≤ y ≤ 12}, {x = x , 6 ≤ y ≤ 12},
1 2

where x1 is the abscissa of the left side of the sheet pile wall and x2 is
the abscissa of the right side. Problem (5.2.45) to (5.2.47) is similar to the
problem discussed in Example 5.2.9. A function that applies the FEM to
Problem (5.2.45) to (5.2.47) is suggested in Exercise 5.4.6.

x
impervious layer

Figure 5.2.15. Sheet pile wall.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 240

240 Matlab for Engineering

5.3 Finite Difference Method


5.3.1 Five-Point Method
Consider the Dirichlet problem for Laplace’s equation in two dimensions

ΔU (x, y) = 0, (x, y) ∈ Ω, (5.3.1)

U (x, y) = g(x, y), (x, y) ∈ ∂Ω. (5.3.2)

When the domain Ω is a rectangle

Ω = {0 < x < L, 0 < y < H},

boundary Condition (5.3.2) is written as



U (0, y) = g1 (y), U (L, y) = g3 (y), 0 ≤ y ≤ H,
(5.3.3)
U (x, 0) = g2 (x), U (x, H) = g4 (x), 0 ≤ x ≤ L.

Let us apply the central approximation to the derivatives in Eq. (5.3.1)

(ui+1,k − 2ui,k + ui−1,k )/(Δx)2 + (ui,k+1 − 2ui,k + ui,k−1 )/(Δy)2 = 0.

The previous finite-difference equation holds for all points within the
domain. Moreover, setting

σ = Δy/Δx, s = −2(1 + σ 2 ), (5.3.4)

we get

sui,k + σ 2 (ui−1,k + ui+1,k ) + ui,k−1 + ui,k+1 = 0,
(5.3.5)
i = 1, . . . , n − 1, k = 1, . . . , m − 1, n = L/Δx, m = H/Δy.

The finite-difference equation in (5.3.5) is named the Five-Point Method


(Fig. 5.3.1). As outlined in (5.3.5), the Five-Point Method provides a linear
algebraic system of (n − 1)(m − 1) equations in (n − 1)(m − 1) unknowns.
For example, for the mesh in Fig. 5.3.1, the equations and unknowns are
15. For k = 1, System (5.3.5) can be written in matrix form as follows
⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤
s σ2 u1,1 u1,2 u1,0 u0,1
⎢ σ2 s σ2 ⎥ ⎢ u2,1 ⎥ ⎢ u2,2 ⎥ ⎢ u2,0 ⎥ ⎢
2⎢ 0 ⎥

⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎣ · · · ⎦ ⎣ · ⎦ + ⎣ · ⎦ + ⎣ · ⎦ + σ ⎣ · ⎦ = 0.
σ2 s un−1,1 un−1,2 un−1,0 un,1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 241

Introduction to the Finite Element Method in Two Spatial Dimensions 241

k=m
ui,k+1

ui-1,k ui,k ui+1,k


y
ui,k-1

k=0
i=0 x i=n

Figure 5.3.1. Five-Point Method.

Hence,

Bu1 + u2 + b1 = 0, (5.3.6)

where
⎡ ⎤ ⎡ ⎤ ⎡ ⎤
s σ2 u1,1 u1,2
⎢ σ2 s σ2 ⎥ ⎢ ⎥ ⎢ ⎥
B=⎢ ⎥ , u = ⎢ u2,1 ⎥ , u = ⎢ u2,2 ⎥ ,
⎣ · · · ⎦ 1 ⎣ · ⎦ 2 ⎣ · ⎦
σ2 s un−1,1 un−1,2

⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤
u1,0 u0,1 g2,1 g1,1
⎢ u2,0 ⎥ ⎢
2⎢ 0 ⎥
⎥ ⎢ g2,2 ⎥ ⎢
2⎢ 0 ⎥

b1 = ⎢ ⎥ ⎢ ⎥
⎣ · ⎦+ σ ⎣ · ⎦ = ⎣ · ⎦+ σ ⎣ · ⎦.
un−1,0 un,1 g2,n−1 g3,1

Note that b1 is known, as expressed in terms of boundary Conditions


(5.3.3). For k = 2, . . . , m − 2, System (5.3.5) implies
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
u1,k−1 s σ2 u1,k u1,k+1 u0,k
⎢ u2,k−1 ⎥ ⎢ σ 2 s σ 2 ⎥ ⎢ u2,k ⎥ ⎢ u2,k+1 ⎥ 2 ⎢ 0 ⎥
⎢ ⎥+⎢ ⎥⎢ ⎥ ⎢ ⎥+σ ⎢ ⎥
⎣ · ⎦ ⎣ · · · ⎦ ⎣ · ⎦+⎣ · ⎦ ⎣ · ⎦ = 0,
un−1,k−1 σ2 s un−1,k un−1,k+1 un,k

uk−1 + Buk + uk+1 + bk = 0, k = 2, . . . , m − 2, (5.3.7)

where bk is a known term, as u0,k = g1,k and un,k = g3,k . Lastly, for
k = m − 1, System (5.3.5) implies
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 242

242 Matlab for Engineering

⎡ ⎤ ⎡ ⎤⎡ ⎤
u1,m−2 s σ2 u1,m−1
⎢ u2,m−2 ⎥ ⎢ σ 2 s σ 2 ⎥ ⎢ u2,m−1 ⎥
⎢ ⎥+⎢ ⎥⎢ ⎥
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦
2
un−1,m−2 σ s un−1,m−1
⎡ ⎤ ⎡ ⎤
u1,m u0,m−1
⎢ u2,m ⎥ ⎢ ⎥
+⎢ ⎥ + σ 2 ⎢ 0 ⎥ = 0,
⎣ · ⎦ ⎣ · ⎦
un−1,m un,m−1

um−2 + Bum−1 + bm−1 = 0, (5.3.8)

where the known term bm−1 is given by


⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤
u1,m u0,m−1 g4,1 g1,m−1
⎢ u2,m ⎥ ⎢ ⎥ ⎢ ⎥ ⎢ ⎥
bm−1 = ⎢ ⎥ + σ 2 ⎢ 0 ⎥ = ⎢ g4,2 ⎥ + σ 2 ⎢ 0 ⎥ .
⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦
un−1,m un,m−1 g4,n−1 g3,m−1

Equations (5.3.6) to (5.3.8) can be combined in a single equation by using


the block matrices
⎡ ⎤⎡ ⎤ ⎡ ⎤
B In−1 0n−1 u1 b1
⎢ In−1 B In−1 ⎥ ⎢ u2 ⎥ ⎢ b2 ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ · · · ⎦ ⎣ · ⎦ + ⎣ · ⎦ = 0,
0n−1 In−1 B um−1 bm−1

where In−1 is the (n−1)-by-(n−1) unit matrix and 0n−1 is the zero matrix.
Hence, with the clear meaning of the symbols,

Au + b = 0. (5.3.9)

It is a linear system of (n − 1)(m − 1) equations in (n − 1)(m − 1) unknowns.

Example 5.3.1 Consider the special Dirichlet problem

ΔU (x, y) = 0, 0 < x < L, 0 < y < H, (5.3.10)



U (0, y) = g1 (y) = −y 2 , U (L, y) = g3 (y) = L2 − y 2 , 0 ≤ y ≤ H,
U (x, 0) = g2 (x) = x2 , U (x, H) = g4 (x) = x2 − H 2 , 0 ≤ x ≤ L.
(5.3.11)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 243

Introduction to the Finite Element Method in Two Spatial Dimensions 243

2
U
1

-1
1
2
0.5 1.5
1
y 0.5 x
0 0

Figure 5.3.2. Numerical solution to Problem (5.3.10) to (5.3.11).

The following listing presents a function that applies the Five-Point Method
to Problem (5.3.10) and (5.3.11). The graph of the numerical solution is
shown in Fig. 5.3.2.
u = five point
% This is the function file five point.m.
% Five-Point Method is applied to solve the following Dirichlet problem for
% the Laplace equation: Uyy + Uxx = 0,
% U(0,y) = -y.^2, U(x,0) = x^2, U(L,y) = L^2-y^2, U(x,H) = x^2-H^2.
% Initialization
L = 2; H = 1; n = 20; m = 10;
dx = L/n; dy = H/m; sigma = dy/dx;
x = linspace(0,L,n+1); y = linspace(0,H,m+1);
b = zeros((n-1)*(m-1),1); u = zeros(n+1, m+1);
g1 = @(y) -y.^2;
g2 = @(x) x.^2;
g3 = @(y) L^2 - y.^2;
g4 = @(x) x.^2 - H^2;
AA = repmat([ones(n-1,1) [sigma2 *ones(n-2,1);0]...
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 244

244 Matlab for Engineering

-2*(1+sigma2)*ones(n-1,1)[0;sigma2*ones(n-2,1)] ones(n-1,1)], (m-1),1);


A = spdiags(AA, [-(n-1) -1:1 (n-1)], (n-1)*(m-1), (n-1)*(m-1));
% Five-Point Method
u(1,:) = g1(y); % Boundary values.
u(:,1) = g2(x); u(n+1,:) = g3(y); u(:,m+1) = g4(x);
b(1:n-1) = u(2:n,1); % Known term.
b( (n-1)*(m-2)+1 :(m-1)*(n-1) ) = u(2:n,m+1);
for k = 2:m
b((k-2)*(n-1)+1) = b((k-2)*(n-1)+1) + sigma2 *u(1,k);
b((k-1)*(n-1)) = b((k-1)*(n-1)) + sigma2 *u(n+1,k);
end
v = -A\b;
u(2:n,2:m) = reshape(v,n-1,m-1); u = u’;
U = @(x,y) x.2 -y.2 ; U = feval(U, x, y’);% Analytical solution.
mesh(x,y,u,’edgecolor’, ’b’); mesh(x,y,U,’edgecolor’, ’b’, ’Marker’,’*’);
xlabel(’x’); ylabel(’y’); zlabel(’U’);
fprintf(’Maximum error = %g\n’,max(max(abs(U -u ))))
end
See Exercise 5.4.7.
Consider the Dirichlet problem for the Poisson equation
ΔU (x, y) = F (x, y), (x, y) ∈ Ω,

U (x, y) = g(x, y), (x, y) ∈ ∂Ω.


The Five-Point Method for the previous problem is given by
ui+1,k − 2ui,k + ui−1,k ui,k+1 − 2ui,k + ui,k−1
+ = fi,k ,
(Δx)2 (Δy)2

sui,k + σ 2 (ui−1,k + ui+1,k ) + ui,k−1 + ui,k+1 = fi,k (Δy)2 . (5.3.12)


When the domain is a rectangle Ω = {0 < x < L, 0 < y < H}, System
(5.3.9) is generalized to
⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
B In−1 0n−1 u1 b1 f1
⎢ In−1 B In−1 ⎥ ⎢ u2 ⎥ ⎢ b2 ⎥ ⎢ f2 ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎣ · · · ⎦⎣ · ⎦+ ⎣ · ⎦ = ⎣ · ⎦,
0n−1 In−1 B um−1 bm−1 fm−1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 245

Introduction to the Finite Element Method in Two Spatial Dimensions 245

where
⎡ ⎤
f1,k
⎢ f2,k ⎥
fk = (Δy)2 ⎢ ⎥
⎣ · ⎦ , k = 1, ..., m − 1.
fn−1,k

Example 5.3.2 Consider the special Dirichlet problem

ΔU (x, y) = −sin x − cos y, 0 < x < L, 0 < y < H, (5.3.13)



U (0, y) = g1 (y) = cos y,
0 ≤ y ≤ H, (5.3.14)
U (L, y) = g3 (y) = sin L + cos y,

U (x, 0) = g2 (x) = sin x + 1,
0 ≤ x ≤ L. (5.3.15)
U (x, H) = g4 (x) = sin x + cos H,
A function that applies the Five-Point Method to Problem (5.3.13) to
(5.3.15) is provided below. The numerical solution is shown in Fig. 5.3.3.
u = five point f1

0
U
-1

-2

-3
6
10
4 8
6
y 2 4
2 x
0 0

Figure 5.3.3. Numerical solution to Problem (5.3.13) to (5.3.15).


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 246

246 Matlab for Engineering

% This is the function file five point f1.m.


% The Five-Point Method is applied to solve the following Dirichlet problem
% for the Poisson equation: Uyy + Uxx = - sin x - cos y,
% U(0,y) = cos y, U(L,y) = sin L + cos y,
% U(x,0) = sin x + 1, U(x,H) = sin x + cos H.
% Analytical solution: U = sin x + cos y.
% Initialization
L = 9; H = 6; n = 30; m = 20;
dx = L/n; dy = H/m; sigma = dy/dx;
x = linspace(0,L,n+1); y = linspace(0,H,m+1);
b = zeros((n-1)*(m-1),1); u = zeros(n+1,m+1);
g1 = @(y) cos(y);
g2 = @(x) sin(x) + 1;
g3 = @(y) sin(L) + cos(y);
g4 = @(x) sin(x) + cos(H);
F = @(x,y) - sin(x) - cos(y);
f = zeros((n-1)*(m-1),1);
for k=2:m
f((k-2)*(n-1)+1:(k-1)*(n-1),1) = F(x(2:n)’,y(k))*dy^2;
end
Same code as five point function
v = A\(f-b);
u(2:n,2:m) = reshape(v,n-1,m-1); u = u’;
mesh(x,y,u,’LineWidth’,2); xlabel(’x’); ylabel(’y’); zlabel(’U’);
U = @(x,y) sin(x) + cos(y); U = feval(U,x,y’);
fprintf(’Maximum error = %g\n’,max(max(abs(U -u ))))
end
Another application is suggested in Exercise 5.4.8.
Consider the 3D Poisson equation

ΔU (x, y, z) = F (x, y, z), (x, y, z) ∈ Ω. (5.3.16)


The generalization of the Five-Point Method to Eq. (5.3.16) is named the
Seven-Point Method. See Exercise 5.4.9.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 247

Introduction to the Finite Element Method in Two Spatial Dimensions 247

H2

H3 H1

L x
L1
L2

Figure 5.3.4. Simple model of dam.

5.3.2 Model of a Dam


Consider the simple model of a dam in Fig. 5.3.4. The steady state seepage
motions are governed by the following Dirichlet–Neumann problem for the
piezometric head h = h(x, y)

Δh(x, y) = 0, (x, y) ∈ Ω, (5.3.17)

h = H1 on ∂Ω1 , h = H2 on ∂Ω2 , (5.3.18)

hx = 0 on ∂Ω3 ∪ ∂Ω4 ∪ ∂Ω5 ∪ ∂Ω6 , hy = 0 on ∂Ω7 ∪ ∂Ω8 , (5.3.19)

where

∂Ω1 = {L2 ≤ x ≤ L, y = H1 }, ∂Ω2 = {0 ≤ x ≤ L1 , y = H1 },

∂Ω3 = {x = 0, 0 < y < H1 }, ∂Ω4 = {x = L, 0 < y < H1 },

∂Ω5 = {x = L1 , H3 < y < H1 }, ∂Ω6 = {x = L2 , H3 < y < H1 },

∂Ω7 = {0 ≤ x ≤ L, y = 0}, ∂Ω8 = {L1 ≤ x ≤ L2 , y = H3 }.

Problem (5.3.17)–(5.3.19) was discussed with the FEM in Sec. 5.2.4. Now,
the Five-Point Method is applied. The numerical solution is indicated with

hi,k = h(xi , yk ), i = 1, ..., n + 1, k = 1, ..., m + 1.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 248

248 Matlab for Engineering

k = m+1
k1

k=1
i=1 i1 i2 i = n+1

Figure 5.3.5. Mesh for the dam problem.

The mesh is shown in Fig. 5.3.5. Note that x1 = 0, xn+1 = L, xi1 = L1 ,


xi2 = L2 , y1 = 0, ym+1 = H1 and yk1 = H3 . The Neumann boundary
conditions are approximated by using the forward (f) and backward (b)
approximations:
{x = 0, 0 < y < H1 }, h2,k = h1,k , (f),
2 < k < m, (5.3.20)
{x = L, 0 < y < H1 }, hn+1,k = hn,k , (b),

{x = L1 , H3 < y < H1 }, hi1 ,k = hi1 −1,k , (b),


k1 < k < m, (5.3.21)
{x = L2 , H3 < y < H1 }, hi2 +1,k = hi2 ,k , (f),

{0 ≤ x ≤ L, y = 0}, hi,2 = hi,1 , (f), 1 ≤ i ≤ n + 1, (5.3.22)

{L1 ≤ x ≤ L2 , y = H3 }, hi,k1 = hi,k1 −1 , (b), i1 ≤ i ≤ i2 . (5.3.23)

The linear system provided by the Five-Point Method is illustrated in


Fig. 5.3.6. Note that the vectors hk , k < k1 , have a greater length than the
vectors h∗k , k ≥ k1 . Attention is needed for k = k1 . Consider the Five-Point
Method in matrix form
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
h2,k−1 s σ2 h2,k h2,k+1 h1,k
⎢ h3,k−1 ⎥ ⎢ σ 2 s σ 2 ⎥ ⎢ h3,k ⎥ ⎢ h3,k+1 ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥+⎢ ⎥ + σ 2 ⎢ 0 ⎥ = 0.
⎣ · ⎦+⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦
hn,k−1 σ2 s hn,k hn,k+1 hn+1,k
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 249

Introduction to the Finite Element Method in Two Spatial Dimensions 249

Figure 5.3.6. Graphical representation of the linear system.

Using (5.3.20) in the previous formula yields


⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
h2,k−1 s + σ2 σ2 h2,k h2,k+1
⎢h3,k−1 ⎥ ⎢ σ 2 s σ2 ⎥⎢h3,k ⎥ ⎢h3,k+1 ⎥
⎢ ⎥+⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ · ⎦ ⎣ . . . ⎦⎣ · ⎦+⎣ · ⎦= 0. (5.3.24)
hn,k−1 σ 2 s + σ 2 hn,k hn,k+1

If k = 2, then hi,2 = hi,1 because of (5.3.22), hence Formula (5.3.24) is


written as
⎡ ⎤⎡ ⎤ ⎡ ⎤
s + 1 + σ2 σ2 h2,2 h2,3
⎢ σ2 s + 1 σ2 ⎥ ⎢ h3,2 ⎥ ⎢ h3,3 ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ . . . ⎦ ⎣ · ⎦ + ⎣ · ⎦ = 0,
σ2 s + 1 + σ2 hn,2 hn,3

B1 h2 + h3 = 0. (5.3.25)

In addition, for 3 ≤ k ≤ k1 − 2, Formula (5.3.24) is written as


hk−1 + B2 hk + hk+1 = 0, 3 ≤ k ≤ k1 − 2. (5.3.26)

For k = k1 − 1, Formula (5.3.24) implies


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 250

250 Matlab for Engineering

⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
h2,k1 −2 s + σ2 σ2 h2,k1 −1 h2,k1
⎢ h3,k −2 ⎥ ⎢ σ 2 s σ2 ⎥⎢ h3,k −1 ⎥ ⎢ h3,k ⎥
⎢ 1 ⎥ ⎢ ⎥⎢ 1 ⎥ ⎢ 1 ⎥
⎢ · ⎥ ⎢ · · · ⎥⎢ · ⎥ ⎢ · ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ hi1 ,k1 −2 ⎥ ⎢ σ2 s σ2 ⎥⎢ hi1 ,k1 −1 ⎥ ⎢ hi1 ,k1 ⎥
⎢ ⎥+⎢ ⎥⎢ ⎥+⎢ ⎥= 0.
⎢ · ⎥ ⎢ · · · ⎥⎢ · ⎥ ⎢ · ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ hi2 ,k1 −2 ⎥ ⎢ σ2 s σ2 ⎥⎢ hi2 ,k1 −1 ⎥ ⎢ hi2 ,k1 ⎥
⎢ ⎥ ⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
hn,k1 −2 σ2 s + σ2 hn,k1 −1 hn,k1
The elements hi1 ,k1 . . . hi2 ,k1 of the last column vector are equal to
hi1 ,k1 −1 . . . hi2 ,k1 −1 , respectively, because of (5.3.23). Therefore, the men-
tioned elements must be moved to the preceding column vector. The
resulting vector h∗k1 has length i1 − 2 + n − i2 , less than the other vectors in
the same formula, and cannot be added to those. Therefore, we introduce
the (n−1)-by-(i1 −2+n−i2) matrix C such that the product Ch∗k1 is equal to
the vector [h2,k1 , . . . , hi1 −1,k1 , 0, . . . , 0, hi2 +1,k1 , . . . , hnx ,k1 ]T that has same
length as the other vectors. Considering this in the previous formula, one
arrives at
⎡ ⎤⎡ ⎤
s + σ2 σ2 h2,k1 −1
⎢ . . . ⎥⎢ · ⎥
⎢ ⎥⎢ ⎥
⎢ 2 2 ⎥ ⎢ ⎥
⎢ σ s σ ⎥ ⎢ hi1 −1,k1 −1 ⎥
⎢ 2 2 ⎥ ⎢ ⎥
⎢ σ s+1 σ ⎥ ⎢ hi1 ,k1 −1 ⎥
⎢ ⎥⎢ ⎥
⎢ . . . ⎥⎢ · ⎥
⎢ 2 2 ⎥⎢ ⎥
⎢ σ s+1 σ ⎥ ⎢ hi2 ,k1 −1 ⎥
⎢ ⎥ ⎢ ⎥
⎢ σ2 s σ2 ⎥ ⎢ hi2 +1,k1 −1 ⎥
⎢ ⎥⎢ ⎥
⎣ . . . ⎦⎣ · ⎦
2 2
σ s+σ hn,k1 −1
⎡ ⎤ ⎡ ⎤
1 h2,k1 −2
⎢ · ⎥⎡ ⎤ ⎢ · ⎥
⎢ ⎥ h2,k1 ⎢ ⎥
⎢ ⎥ ⎢ hi −1,k −2 ⎥
⎢ 1 ⎥⎢ · ⎥ ⎢ 1 1 ⎥
⎢ ⎥⎢ ⎥ ⎢ h ⎥
⎢ 0 ⎥⎢ ⎥ ⎢ i1 ,k1 −2 ⎥
⎢ ⎥ ⎢ hi1 −1,k1 ⎥ ⎢ ⎥
+⎢ · ⎥⎢ ⎥+⎢ · ⎥ = 0,
⎢ ⎥ ⎢ hi2 +1,k1 ⎥ ⎢ ⎥
⎢ 0 ⎥⎢ ⎥ ⎢ hi2 ,k1 −2 ⎥
⎢ ⎥⎣ · ⎦ ⎢ ⎥
⎢ 1 ⎥ ⎢ hi2 +1,k1 −2 ⎥
⎢ ⎥ h n,k ⎢ ⎥
⎣ · ⎦ 1 ⎣ · ⎦
1 hn,k1 −2

hk1 −2 + B3 hk1 −1 + Ch∗k1 = 0. (5.3.27)


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 251

Introduction to the Finite Element Method in Two Spatial Dimensions 251

Consider the Five-Point Method for k = k1 and i = 2, . . . , i1 − 1,


⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤
h2,k1 −1 s σ2 h2,k1 h2,k1 +1 h1,k1
⎢ h3,k −1 ⎥ ⎢ σ 2 s σ 2 ⎥⎢ h3,k ⎥ ⎢ h3,k +1 ⎥ 2 ⎢ 0 ⎥
⎢ 1 ⎥+⎢ ⎥⎢ 1 ⎥ ⎢
+ 1 ⎥+σ ⎢ ⎥
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦ ⎣ · ⎦= 0,
hi1 −1,k1 −1 σ 2 s hi1 −1,k1 hi1 −1,k1 +1 hi1 ,k1

and use Formulas (5.3.20) and (5.3.21),


⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
h2,k1 −1 s + σ2 σ2 h2,k1 h2,k1 +1
⎢ h3,k −1 ⎥ ⎢ σ 2 s σ2 ⎥⎢ h3,k ⎥ ⎢ h3,k +1 ⎥
⎢ 1 ⎥+⎢ ⎥⎢ 1 ⎥+⎢ 1 ⎥= 0.
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦ ⎣ · ⎦
hi1 −1,k1 −1 σ 2 s + σ 2 hi1 −1,k1 hi1 −1,k1 +1

Similarly, for k = k1 and i = i2 + 1, . . . , n,


⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
hi2 +1,k1 −1 s + σ2 σ2 hi2 +1,k1 hi2 +1,k1 +1
⎢ hi +2,k −1⎥ ⎢ σ 2 s σ2 ⎥⎢ hi +2,k ⎥ ⎢ hi +2,k +1 ⎥
⎢ 2 1 ⎥+⎢ ⎥⎢ 2 1 ⎥ ⎢ 2 1 ⎥= 0.
⎣ · ⎦ ⎣ · · · ⎦⎣ · ⎦+ ⎣ · ⎦
hn,k1 −1 σ2 s + σ2 hn,k1 hn,k1 +1

By combining the two previous formulas into a single matrix equation, we


get
⎡ ⎤⎡ ⎤
s + σ2 σ2 h2,k1
⎢ σ2 s σ2 ⎥ ⎢ h3,k ⎥
⎢ ⎥⎢ 1 ⎥
⎢ · · · ⎥⎢ · ⎥
⎢ ⎥⎢ ⎥
⎢ 2 2 ⎥⎢ ⎥
⎢ s+σ σ ⎥ ⎢ hi1 −1,k1 ⎥
⎢ ⎥ ⎢ ⎥
⎢ s + σ2 σ2 ⎥ ⎢ hi2 +1,k1 ⎥
⎢ ⎥ ⎢ ⎥
⎢ σ2 s σ2 ⎥ ⎢ hi2 +2,k1 ⎥
⎢ ⎥⎢ ⎥
⎣ · · · ⎦⎣ · ⎦
σ2 s + σ2 hn,k1

⎡ ⎤ ⎡ ⎤
h2,k1 −1 h2,k1 +1
⎢ h3,k1 −1 ⎥ ⎢ ⎥
h3,k1 +1
⎢ ⎥ ⎢ ⎥
⎢ · ⎥ ⎢ ·⎥
⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥
⎢ hi1 −1,k1 −1 ⎥ ⎢ hi1 −1,k1 +1 ⎥
+⎢ ⎥+⎢ ⎥ = 0.
⎢ hi2 +1,k1 −1 ⎥ ⎢ hi2 +1,k1 +1 ⎥
⎢ ⎥ ⎢ ⎥
⎢ hi2 +2,k1 −1 ⎥ ⎢ hi2 +2,k1 +1 ⎥
⎢ ⎥ ⎢ ⎥
⎣ · ⎦ ⎣ · ⎦
hn,k1 −1 hn,k1 +1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 252

252 Matlab for Engineering

The vector in the first part of the previous formula is h∗k1 . The last vector
is h∗k1 +1 . The remaining vector is a subset of hk1 −1 . Since it can be written
as
⎡ ⎤
h2,k1 −1
⎢ h3,k −1 ⎥
⎢ 1 ⎥
⎢ · ⎥
⎢ ⎥
⎢ ⎥
⎢ hi1 −1,k1 −1 ⎥
⎢ ⎥ = C T hk1 −1 ,
⎢ hi2 +1,k1 −1 ⎥
⎢ ⎥
⎢ hi2 +2,k1 −1 ⎥
⎢ ⎥
⎣ · ⎦
hn,k1 −1

we obtain

C T hk1 −1 + B4 h∗k1 + h∗k1 +1 = 0. (5.3.28)

For k1 + 1 ≤ k ≤ m − 1 and i = 1, . . . , i1 , i = i2 , . . . , n + 1, the Five-Point


Method gives
⎡ ⎤⎡ ⎤
s + σ2 σ2 h2,k
⎢ σ2 s σ2 ⎥ ⎢ h3,k ⎥
⎢ ⎥⎢ ⎥
⎢ · · · ⎥⎢ · ⎥
⎢ ⎥⎢ ⎥
⎢ 2 2 ⎥⎢ ⎥
⎢ s+σ σ ⎥ ⎢ hi1 −1,k ⎥
⎢ ⎥ ⎢ ⎥
⎢ s + σ2 σ2 ⎥ ⎢ hi2 +1,k ⎥
⎢ ⎥ ⎢ ⎥
⎢ σ2 s σ2 ⎥ ⎢ hi2 +2,k ⎥
⎢ ⎥⎢ ⎥
⎣ · · · ⎦⎣ · ⎦
σ2 s + σ2 hn,k

⎡ ⎤ ⎡ ⎤
h2,k−1 h2,k+1
⎢ h3,k−1⎥ ⎢ ⎥
h3,k+1
⎢ ⎥ ⎢ ⎥
⎢ · ⎥ ⎢ ⎥ ·
⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥
⎢ hi1 −1,k−1 ⎥ ⎢ hi1 −1,k+1 ⎥
+⎢ ⎥+⎢ ⎥ = 0,
⎢ hi2 +1,k−1 ⎥ ⎢ hi2 +1,k+1 ⎥
⎢ ⎥ ⎢ ⎥
⎢ hi2 +2,k−1 ⎥ ⎢ hi2 +2,k+1 ⎥
⎢ ⎥ ⎢ ⎥
⎣ · ⎦ ⎣ · ⎦
hn,k−1 hn,k+1

where Formulas (5.3.20)–(5.3.21) were applied. Hence,

h∗k−1 + B4 h∗k + h∗k+1 = 0, k1 + 1 ≤ k ≤ m − 1. (5.3.29)


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 253

Introduction to the Finite Element Method in Two Spatial Dimensions 253

Lastly, for k = m,
⎡ ⎤⎡ ⎤
s + σ2 σ2 h2,m
⎢ σ2 s σ2 ⎥ ⎢ h3,m ⎥
⎢ ⎥⎢ ⎥
⎢ · · · ⎥⎢ · ⎥
⎢ ⎥⎢ ⎥
⎢ 2 2 ⎥⎢ ⎥
⎢ s+σ σ ⎥ ⎢ hi1 −1,m ⎥
⎢ ⎥ ⎢ ⎥
⎢ s + σ2 σ2 ⎥ ⎢ hi2 +1,m ⎥
⎢ ⎥ ⎢ ⎥
⎢ σ2 s σ2 ⎥ ⎢ hi2 +2,m ⎥
⎢ ⎥⎢ ⎥
⎣ · · · ⎦⎣ · ⎦
2 2
σ s+σ hn,m

⎡ ⎤⎤ ⎡
h2,m−1 H2
⎢ h3,m−1 ⎥ ⎢ H2 ⎥
⎢ ⎥ ⎢ ⎥
⎢ · ⎥ ⎢ · ⎥
⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥
⎢ hi1 −1,m−1 ⎥ ⎢ H2 ⎥
+⎢ ⎥+⎢ ⎥ = 0.
⎢ hi2 +1,m−1 ⎥ ⎢ H1 ⎥
⎢ ⎥ ⎢ ⎥
⎢ hi2 +2,m−1 ⎥ ⎢ H1 ⎥
⎢ ⎥ ⎢ ⎥
⎣ · ⎦ ⎣ · ⎦
hn,m−1 H1
Hence,

h∗m−1 + B4 h∗m + g = 0. (5.3.30)

Equations (5.3.25)–(5.3.30) can be assembled by using a block matrix


⎡ ⎤⎡ ⎤ ⎡ ⎤
B 1 I1 h2 0
⎢ I1 B 2 I1 ⎥ ⎢ h3 ⎥ ⎢ · ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ · · · ⎥⎢ · ⎥ ⎢ ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎢ I1 B 3 C ⎥ ⎢ hk1 −1 ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥ + ⎢ ⎥ = 0,
⎢ C T B 4 I2 ⎥ ⎢ h∗k1 ⎥ ⎢ ⎥
⎢ ⎥⎢ ∗ ⎥ ⎢ ⎥
⎢ I2 B 4 I2 ⎥ ⎢ hk1 +1 ⎥ ⎢ ⎥
⎢ ⎥⎢ ⎥ ⎢ ⎥
⎣ · · · ⎦⎣ · ⎦ ⎣0⎦
I2 B 4 h∗m g
where I1 and I2 are the n − 1-by-n − 1 and i1 − 2 + n − i2 -by-i1 − 2 + n − i2
unit matrices, respectively. Hence, the desired system

Ah + a = 0. (5.3.31)

Example 5.3.3 The following listing presents a function that applies


the Five-Point Method to Problem (5.3.17) to (5.3.19). The graph of the
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 254

254 Matlab for Engineering

numerical solution is shown in Fig. 5.3.7. The equipotential lines and the
stream lines are illustrated in Fig. 5.3.8.
function H = dam 5p
% This is the function file dam 5p.m.
% Five-Point Method is applied to the dam problem in Sec. 5.3.2.
% Initialization
L = 42; H1 = 14; H2 = 20; dx = .5; dy = dx; n = 84; m = 28;
x = linspace(0,L,n+1); y = linspace(0,H1,m+1);
k1 = 21; i1 = 37; i2 = 49; % H3 = y(k1)=10; L1 = x(i1)=18; L2 = x(i2)=24;
sigma = dy/dx; s = -2*(1+sigma2);
p1 = i1 - 2; p2 = n - i2; p = p1+ p2;
q = (n - 1)*(k1-2) + (m-k1+1)*p; % Number of unknowns.
H = zeros(n+1,m+1);
A = zeros(q,q); % Block matrix.
a = [zeros(q-p,1); H2*ones(p1,1); H1*ones(p2,1)]; % Known term.
% Matrices B1, B2, B3, B4, C.
BB1=[sigma2 *ones(n-1,1) [s/2;(s+1)*ones(n-3,1);s/2] sigma2 *ones(n-1,1)];
B1 = spdiags(BB1,-1:1,n-1,n-1);
a2 = [sigma2 +s; s*ones(n - 3,1); sigma2 +s];

20

15
h

10
14

12

10

8
y
6

4 50
40
2 30
20
0 10 x
0

Figure 5.3.7. Graph of the numerical solution to Problem (5.3.17) to (5.3.19).


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 255

Introduction to the Finite Element Method in Two Spatial Dimensions 255

19.8
19.8 14.2
10

14
.4
19

.6
17

15
19

16
18

14
.4

5
19

.6
15
19

16
18

17
0
0 5 10 15 20 25 30 35 40

Figure 5.3.8. Equipotential lines and the stream lines.

BB2 = [sigma2 *ones(n - 1,1) a2 sigma2 *ones(n - 1,1)];


B2 = spdiags(BB2,-1:1,n - 1,n - 1);
a3 = [s+sigma2;s*ones(i1-3,1);(s+1)*ones(i2-i1+1,1);s*ones(n-i2-1,1);...
s+sigma2 ];
BB3 = [sigma2 *ones(n - 1,1) a3 sigma2 *ones(n - 1,1)];
B3 = spdiags(BB3,-1:1,n - 1,n - 1);
bb41 = [sigma2 *ones(p1,1) [sigma2 +s;s*ones(p1-2,1);sigma2+s]...
sigma2 *ones(p1,1)];
b41 = spdiags(bb41,-1:1,p1,p1);
bb42 = [sigma2 *ones(p2,1) [sigma2 +s;s*ones(p2-2,1);sigma2+s]...
sigma2 *ones(p2,1)];
b42 = spdiags(bb42,-1:1,p2,p2);
B4(1:p1,1:p1) = b41; B4(p1+1:p,p1+1:p) = b42;
C = zeros(n - 1,p);
C(1:i1-2,1:i1-2) = eye(i1-2); C(i2:n - 1,i1-2+1:p) = eye(n - 1-(i2-1));
% Matrix A
A(1:n - 1,1:n - 1) = B1; A(1:n - 1,n:2*(n - 1)) = eye(n - 1);
for k=3:k1-2
A((k-2)*(n - 1)+1:(k-1)*(n - 1),(k-3)*(n - 1)+1:(k-2)*(n - 1)) = eye(n-1);
A((k-2)*(n - 1)+1:(k-1)*(n - 1),(k-2)*(n - 1)+1:(k-1)*(n - 1)) = B2;
A((k-2)*(n - 1)+1:(k-1)*(n - 1),(k-1)*(n - 1)+1:k*(n - 1)) = eye(n - 1);
end
k = k1 - 1;
A((k-2)*(n - 1)+1:(k-1)*(n - 1),(k-3)*(n - 1)+1:(k-2)*(n - 1)) = eye(n - 1);
A((k-2)*(n - 1)+1:(k-1)*(n - 1),(k-2)*(n - 1)+1:(k-1)*(n - 1)) = B3;
A((k-2)*(n - 1)+1:(k-1)*(n - 1),(k-1)*(n - 1)+1:(k-1)*(n -1)+p) = C;
k = k1; k2 = (k1-2)*(n - 1);
A(k2+1:k2+p,(k-3)*(n - 1)+1:(k-2)*(n - 1)) = C’;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 256

256 Matlab for Engineering

A(k2+1:k2+p,k2+1:k2+p) = B4; A(k2+1:k2+p,k2+p+1:k2+2*p) = eye(p);


for k=k1+1:m-1
h = k - k1;
A(k2+h*p+1:k2+h*p+p,k2+h*p-p+1:k2+h*p) = eye(p);
A(k2+h*p+1:k2+h*p+p,k2+h*p+1:k2+h*p+p) = B4;
A(k2+h*p+1:k2+h*p+p,k2+h*p+p+1:k2+h*p+2*p) = eye(p);
end
k = m; h = k - k1;
A(k2+h*p+1:k2+h*p+p,k2+h*p-p+1:k2+h*p) = eye(p);
A(k2+h*p+1:k2+h*p+p,k2+h*p+1:k2+h*p+p) = B4;
% Five-Point Method
v = -A\a;
for k=2:k1-1
H(2:n,k) = v((k-2)*(n - 1)+1:(k-1)*(n - 1));
end
for k=k1:m
h = k-k1;
H(2:i1-1,k) = v(k2+h*p+1:k2+h*p+p1);
H(i2+1:n,k) = v(k2+h*p+p1+1:k2+h*p+p);
H(i1+1:i2-1,k+1) = nan; % nan means Not A Number.
end
H(2:i1-1,m+1)=H2*ones(p1,1); % Boundary values (Bv) for k=m+1, i=2:i1-1
H(i2+1:n,m+1) = H1*ones(p2,1); % Bv for k = m + 1, i = i2+1:n
H(i1,k1+1:m+1) = H(i1-1,k1+1:m+1); % Bv for i = i1, k = k1+2:m+1
H(i2,k1+1:m+1) = H(i2+1,k1+1:m+1); % Bv for i = i2, k = k1+2:m+1
H(i1:i2,k1) = H(i1:i2,k1-1); % Bv for k = k1, i = i1:i2
H(2:n,1) = H(2:n,2); % Bv for k = 1, i = 2:n
H(1,1:m+1) = H(2,1:m+1); % Bv for i = 1, k = 1:m+1
H(n+1,1:m+1) = H(n,1:m+1); % Bv for i = n+1, k=1:m+1
%Plot
mesh(x,y,H’,’LineWidth’,1); view(-17,69);
xlabel(’x’); ylabel(’y’); zlabel(’h’);
[hx, hy] = gradient(H’,dx,dy);
K = 10^(-8); qx = -K*hx; qy = - K*hy;
figure(2)
[~,cc] = contour(x,y,H’,[14.2,14.6,15,16,17,18,19,19.4,19.8]);
axis(’equal’); axis([0 L 0 H1]);
set(cc,’ShowText’,’on’); colormap cool;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 257

Introduction to the Finite Element Method in Two Spatial Dimensions 257

rectangle(’position’,[x(i1),y(k1),x(i2)-x(i1),H1-y(k1)],...
’Facecolor’, [0.5 0.5 0.5]);
hold on;
streamline(x, y, qx, qy, x(5:4:i1-8), y(m+1)*ones(1,length(x(5:4:i1-8))));
end
See Exercises 5.4.10 and 5.4.11.

5.4 Exercises
Exercise 5.4.1 Solve Algebraic System (5.2.3).
Answer.
⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤
x1 y1 1 a1 1 a1 1 a1 1
⎣ x2 y2 1 ⎦ ⎣ b1 ⎦ = ⎣ 0 ⎦ ⇒ B ⎣ b1 ⎦ = ⎣ 0 ⎦ ⇒ ⎣ b1 ⎦ = B −1 ⎣ 0 ⎦ .
x3 y3 1 c1 0 c1 0 c1 0
A simple calculation shows that
⎡ ⎤
y2 − y3 y3 − y1 y1 − y2
1 ⎣ −(x2 − x3 )
B −1 = −(x3 − x1 ) −(x1 − x2 ) ⎦ .
det(B)
x2 y3 − x3 y2 x3 y1 − x1 y3 x1 y2 − x2 y1
Hence,
⎡ ⎤ ⎡ ⎤
a1 y2 − y3
⎣ b1 ⎦ = 1 ⎣ −(x2 − x3 ) ⎦ ,
det(B)
c1 x2 y3 − x3 y2
that is the desired result.

Exercise 5.4.2 Prove Formula (5.2.8).


Answer. The function u(x, y) is linear
u(x, y) = ax + by + c, (5.4.1)

and such that ui = u(xi , yi ), i = 1, 2, 3. Therefore,


⎧ ⎡ ⎤⎡ ⎤ ⎡ ⎤ ⎡ ⎤ ⎡ ⎤
⎨ ax1 + by1 + c = u1 x1 y1 1 a u1 a u1
ax2 + by2 + c = u2 ⇒ ⎣ x2 y2 1 ⎦ ⎣ b ⎦ = ⎣ u2 ⎦ ⇒ B ⎣ b ⎦ = ⎣ u2 ⎦ ,

ax3 + by3 + c = u3 x3 y3 1 c u3 c u3
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 258

258 Matlab for Engineering


⎡ ⎤ ⎡ ⎤ ⎪ a = (B −1 )1,l u1 + (B −1 )1,2 u2 + (B −1 )1,3 u3
a u1 ⎪

⎣ b ⎦ = B −1 ⎣ u2 ⎦ ⇒ b = (B −1 )2,l u1 + (B −1 )2,2 u2 + (B −1 )2,3 u3


c u3 ⎩ c = (B −1 ) u + (B −1 ) u + (B −1 ) u .
3,l 1 3,2 2 3,3 3

Using the values of (B −1 )i,j provided in Exercise 5.4.1 and substituting a, b


and c into (5.4.1) yields the desired result.

Exercise 5.4.3 Determine all Φi (x, y) for the triangulation in Fig. 5.2.1.

Answer. The support of Φ4 is {T8 , T9 , T10 , T15 , T14 , T13 } and the support of
Φ5 is {T12 , T13 , T14 , T18 , T17 }. Applying Formula (5.2.4) yields

⎪ x/h + y/k − 3, on T8 , ⎧

⎪ ⎪ x/h + y/k − 3, on T12 ,

⎪ y/k − 1, ⎪



on T9 , ⎪
⎪ − 2,

⎪ ⎪
⎪ y/k on T13 ,
⎨ −x/h + 2, on T10 , ⎨
−x/h + 2, on T14 ,
Φ4 = −x/h − y/k + 5, on T15 , Φ5 =

⎪ ⎪
⎪ −x/h − y/k + 5, on T18 ,

⎪ −y/k + 3, on T14 , ⎪
⎪ x/h − y/k + 3, on T ,

⎪ ⎪
⎪ 17

⎪ x/h − 1, on T13 , ⎪


⎩ 0, otherwise.
0, otherwise,

The support of Φ6 is {T14 , T15 , T16 , T20 , T19 , T18 } and the support of Φ7 is
{T1 , T2 }. Applying Formula (5.2.4) yields


⎪ x/h + y/k − 4, on T14 ,



⎪ y/k − 2, on T15 ,

⎪ ⎧

⎨ −x/h + 3, on T16 , ⎨ −y/k + 1, on T1 ,
Φ6 = −x/h − y/k + 6, on T20 , Φ7 = −x/h − y/k + 2, on T2 ,

⎪ ⎩

⎪ −y/k + 4, on T19 , 0, otherwise.



⎪ x/h − 1, on T 18 ,


0, otherwise,

The support of Φ8 is {T2 , T3 , T4 } and the support of Φ9 is {T4 , T9 , T10 }.


Applying Formula (5.2.4) yields
⎧ ⎧

⎪ x/h − 1, on T2 , ⎪
⎪ x/h − 2, on T4 ,
⎨ ⎨
−y/k + 1, on T3 , x/h − 2, on T9 ,
Φ8 = Φ9 =

⎪ −y/k + 1, on T4 , ⎪
⎪ −y/k + 2, on T10 ,
⎩ ⎩
0, otherwise, 0, otherwise.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 259

Introduction to the Finite Element Method in Two Spatial Dimensions 259

The support of Φ10 is {T10 , T15 , T16 } and the support of Φ11 is {T16 , T20 }.
Applying Formula (5.2.4) yields


⎪ x/h + y/k − 4, on T10 , ⎧

⎨ ⎨ x/h + y/k − 5, on T16 ,
x/h − 2, on T15 ,
Φ10 = Φ11 = x/h − 2, on T20 ,

⎪ −y/k + 3, on T16 , ⎩
⎩ 0, otherwise.
0, otherwise,

The support of Φ12 is {T19 , T20 } and the support of Φ13 is {T17 , T18 , T19 }.
Applying Formula (5.2.4) yields


⎧ ⎪ y/k − 3, on T17 ,
⎨ x/h + y/k − 5, on T19 , ⎪

y/k + 3, on T18 ,
Φ12 = y/k − 3, on T20 , Φ13 =
⎩ ⎪
⎪ −x/h + 2, on T19 ,
0, otherwise, ⎩
0, otherwise.

The support of Φ14 is {T11 , T12 , T17 } and the support of Φ16 is {T1 , T5 }.
Applying Formula (5.2.4) yields


⎪ y/k − 2, on T11 , ⎧

⎨ ⎨ −x/h + 1, on T1 ,
−x/h + 1, on T12 ,
Φ14 = Φ16 = −x/h − y/k + 2, on T5 ,

⎪ −x/h + 1, on T17 , ⎩
⎩ 0, otherwise.
0, otherwise,

The support of Φ15 is {T5 , T6 , T11 } (Fig. 5.4.1). Applying Formula (5.2.4)
yields


⎪ y/k − 1, on T5 ,

−x/h + 1, on T6 ,
Φ15 =

⎪ −x/h − y/k + 3, on T11 ,

0, otherwise.

Exercise 5.4.4 Calculate the integrals in Formulas (5.2.21) and (5.2.22)


for i = 2, . . . , 6.
Answer. For i = 2, it is
  
1 1 1
K21 = K12 = −hk 2 , K22 = ∇Φ2 ·∇Φ2 = 2hk 2 + 2 ,
h T3 ∪T4 ∪T7 ∪T8 ∪T9 h k
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 260

260 Matlab for Engineering

3
14 T11 T6 1
15 T5
16

Figure 5.4.1. Φ15 .


1
K23 = 0; K24 = ∇Φ2 · ∇Φ4 = −hk , K25 = K26 = 0,
T8 ∪T9 k2

1
b27 = 0; b28 = ∇Φ2 · ∇Φ8 = −hk ,
T3 ∪T4 k2

1
b29 = ∇Φ2 · ∇Φ9 = −hk ,
T4 ∪T9 h2

b2,10 = b2,11 = b2,12 = b2,13 = b2,14 = b2,15 = b2,16 = 0,


h k h k
g2 = u8 + u9 = 4h2 + (9h2 − k 2 ).
k h k h
For i = 3, it is
1
K31 = K13 = −hk 2 , K32 = K23 = 0,
k
  
1 1
K33 = ∇Φ3 · ∇Φ3 = 2hk + ,
T6 ∪T7 ∪T8 ∪T11 ∪T12 ∪T13 h2 k2

1
K34 = ∇Φ3 · ∇Φ4 = −hk ,
T8 ∪T13 h2

1
K35 = ∇Φ3 · ∇Φ5 = −hk , K36 = 0,
T12 ∪T13 k2
b37 = b38 = b39 = b3,10 = b3,11 = b3,12 = b3,13 = b3,14 = 0,

1
b3,15 = ∇Φ3 · ∇Φ15 = −hk 2 , b3,16 = 0,
T6 ∪T11 h
k k
g3 = u = − 4k 2 .
h 15 h
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 261

Introduction to the Finite Element Method in Two Spatial Dimensions 261

For i = 4, it is
1 1
K41 = K14 = 0, K42 = K24 = −hk , K43 = K34 = −hk ,
k2 h2
  
1 1
K44 = ∇Φ4 · ∇Φ4 = 2hk + ,
T8 ∪T9 ∪T10 ∪T11 ∪T12 ∪T13 h2 k2

1
K45 = 0, K46 = ∇Φ4 · ∇Φ6 = −hk ,
T14 ∪T15 k2

1
b47 = b48 = b49 = 0, b4,10 = ∇Φ4 · ∇Φ10 = −hk ,
T10 ∪T15 h2

b4,11 = b4,12 = b4,13 = b4,14 = b4,15 = b4,16 = 0,

k k
g4 = u10 = (9h2 − 4k 2 ).
h h
For i = 5, it is
1
K51 = K15 = 0, K52 = K25 = 0, K53 = K35 = −hk , K54 = K45 = 0,
k2
  
1 1
K55 = ∇Φ5 · ∇Φ5 = 2hk 2 + 2 ,
T12 ∪T13 ∪T14 ∪T17 ∪T18 h k
  
hk 1 1
K56 = ∇Φ5 · ∇Φ6 = − + 2 , b57 = b58 = b59 = 0,
T14 ∪T18 2 h2 k

1
b5,10 = b5,11 = b5,12 = 0, b5,13 = ∇Φ5 · ∇Φ13 = −hk ,
T17 ∪T18 k2

1
b5,14 = ∇Φ5 · ∇Φ14 = −hk , b5,15 = b5,16 = 0,
T12 ∪T17 h2

h k h k
g5 = u + u = (h2 − 16k 2 ) − 9k 2 .
k 13 h 14 k h
For i = 6, it is

K61 = K16 = 0, K62 = K26 = 0, K63 = K36 = 0,


 
1 hk 1 1
K64 = K46 = −hk , K65 = K56 =− + ,
k2 2 h2 k2
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 262

262 Matlab for Engineering

  
1 1
K66 = ∇Φ6 · ∇Φ6 = 2hk + ,
T14 ∪T15 ∪T16 ∪T18 ∪T19 ∪T20 h2 k2

1
b67 = b68 = b69 = b6,10 = 0, b6,11 = ∇Φ6 · ∇Φ11 = −hk ,
T16 ∪T20 h2

1
b6,12 = ∇Φ6 · ∇Φ12 = −hk , b6,13 = b6,14 = b6,15 = b6,16 = 0,
T19 ∪T20 k2

k h k h
g6 = u + u = (9h2 − 9k 2 ) + (4h2 − 16k 2 ).
h 11 k 12 h k

Exercise 5.4.5 Consider the dam function in Example 5.2.9. Replace the
code line
L = 42; H1 = 14; H2 = 20; d = 2; nx = L/d; ny = H1/d;
with
L = 42; H1 = 14; H2 = H1; d = 2; nx = L/d; ny = H1/d;
What happens?
Answer. The numerical solution is to H = 14, which is the same as
the analytical solution. This result indirectly shows the accuracy of the
numerical method.

Exercise 5.4.6 Write a function, say sheet pile wall, that applies the
FEM to Problem (5.2.45) to (5.2.47) in Example 5.2.10.
Hint. The following listing considers the simple triangulation in Fig. 5.4.2.
The numerical solution is plotted in Fig. 5.4.3.
function [u, hh] = sheet pile wall
% This is the function file sheet pile wall.m.
% The FEM is applied to solve the Dirichlet–Neumann problem in Example
% 5.2.10. The function returns the matrix and the vector expressions of
% the numerical solution.
% Initialization
Lx = 22; Ly = 12; d = 2; nx = Lx/d; ny = Ly/d;
n = (nx+1)*(ny+1);% Number of nodes.
x = linspace(0,Lx,nx+1); y = linspace(0,Ly,ny+1);
P = Nodes(x, y, nx+1, ny+1); xt = P(:,1); yt = P(:,2);
T = Triangulation(nx, nx+1, ny);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 263

Introduction to the Finite Element Method in Two Spatial Dimensions 263

Figure 5.4.2. Triangulation for Problem (5.2.45) to (5.2.47).

20

15

h 10

5 13.1

15.9 13.5
0
15.9
10
15

14

15.5
14.5

20
15
y 5
10
5 x
0 0

Figure 5.4.3. Numerical solution to Problem (5.2.45) to (5.2.47).

hb = [16*ones(6,1); 13*ones(6,1)];% Boundary conditions.


nf = n - length(hb);% Free nodes.
K = zeros(nf,nf);% Stiffness matrix.
b = zeros(nf,n-nf);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 264

264 Matlab for Engineering

% FEM
for i=1:nf
same code as dam function
end
f = -b * hb; v = K\f; hh = [v;hb];
u = reshape(hh,nx+1,ny+1);
% Plot
trimesh(T, xt, yt, hh,’LineWidth’,2); view(-6,55);
xlabel(’x’); ylabel(’y’); zlabel(’h’);
[hx, hy] = gradient(u’,d,d);
KK =10^(-8); qx = -KK*hx; qy = -KK*hy;
hold on;
[~,cc] = contour(x,y,u’,[13.1,13.5,14,14.5,15,15.5,15.9]);
set(cc,’ShowText’,’on’); colormap cool;
axis([0 Lx 0 Ly]); rectangle(’position’,[0, 0, Lx, Ly])
rectangle(’position’,[x(6),y(4),d,3*d ],’FaceColor’,’black’,’LineWidth’,1)
hold on;
streamline(x,y,qx,qy,x(2:4),y(ny+1)*ones(1,3));
end
% ———- Local functions ————-
function P = Nodes(x, y, n1, n2)
P = zeros(n1*n2,2);
P(1:n1*n2,1) = repmat(x’,n2,1);
for jj=1:n2
P(1+(jj-1)*n1:jj*n1,2) = y(jj)*ones(n1,1);
end
end
function T = Triangulation(nx, n1, ny)
T = zeros(2*nx*ny,3);
for k=1:ny
for i=1:nx
T((k-1)*2*nx+2*i-1,:)= [(k-1)*n1+i (k-1)*n1+i+1 k*n1+i];
T((k-1)*2*nx+2*i,:) = [(k-1)*n1+i+1 k*n1+i+1 k*n1+i];
end
end
T = T([1:76, 79:98, 101:120, 123:2*nx*ny],:);
end
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 265

Introduction to the Finite Element Method in Two Spatial Dimensions 265

Exercise 5.4.7 Consider the five point function in Example 5.3.1. Verify
that matrix A was introduced with the following code
AA = repmat([ones(n-1,1) [sigma2 *ones(n-2,1);0]...
-2*(1+sigma2)*ones(n-1,1) [0;sigma2 *ones(n-2,1)] ones(n-1,1)], (m-1),1);
A = spdiags(AA, [-(n-1) -1:1 (n-1)], (n-1)*(m-1), (n-1)*(m-1));
can also be introduced with the following loop
B1 = sigma2 *ones(n-2,1); B2 = -2*(1+sigma2)*ones(n-1,1);
B = diag(B1,-1) + diag(B2) + diag(B1,1);
A(1:n-1,1:n-1) = B; A(1:n-1,n:2*(n-1)) = eye(n-1);
for k=2:m-2
A((k-1)*(n-1)+1:k*(n-1),(k-1)*(n-1)+1:k*(n-1)) = B;
A((k-1)*(n-1)+1:k*(n-1),k*(n-1)+1:(k+1)*(n-1)) = eye(n-1);
A((k-1)*(n-1)+1:k*(n-1),(k-2)*(n-1)+1:(k-1)*(n-1)) = eye(n-1);
end
A((m-2)*(n-1)+1:(m-1)*(n-1),(m-2)*(n-1)+1:(m-1)*(n-1)) = B;
A((m-2)*(n-1)+1:(m-1)*(n-1),(m-3)*(n-1)+1:(m-2)*(n-1)) = eye(n-1);

Exercise 5.4.8 Consider the following Dirichlet problem

ΔU (x, y) = x + y, 0 < x < L, 0 < y < H, (5.4.2)

U (0, y) = g1 (y) = y 3 /6,


0 ≤ y ≤ H, (5.4.3)
U (L, y) = g3 (y) = (L3 + y 3 )/6,

U (x, 0) = g2 (x) = x3 /6,
0 ≤ x ≤ L. (5.4.4)
U (x, H) = g4 (x) = (x3 − H 3 )/6,

Write a function, say five point f2, that applies the Five-Point Method to
Problem (5.4.2) to (5.4.4).

Exercise 5.4.9 Write the Seven-Point Method for Poisson’s Eq. (5.3.16).
Hint. Consider Fig. 5.4.4.

Exercise 5.4.10 Consider the dam 5p function, Example 5.3.3. Check


the accuracy of the numerical result.
Hint. See Exercise 5.4.5.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 266

266 Matlab for Engineering

z ui,k,j+1
ui-1,k,j

ui,k,j
ui,k-1,j ui,k+1,j

ui+1,k,j y
x ui,k,j-1

Figure 5.4.4. Seven-Point Method.

Exercise 5.4.11 The dam problem was discussed with the FEM in Sec.
5.2.4 and with the Five-Point Method in Sec. 5.3.2. Compare the results
provided by the two methods.
Hint. Let us consider the matrices u and H returned by dam and dam 5p
functions, respectively. The matrices u and H have different sizes. Consider
the matrix H f = H(1 : 4 : n + 1, 1 : 4 : m + 1) that has the same size as u.
The elements of the matrices u and H f refer to the same points and can
be compared.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 267

Chapter 6

The Euler–Bernoulli Beam

The equation governing the lateral vibrations of a beam is derived in the


framework of the Euler–Bernoull theory. The weak formulation is presented
and the Finite Element Method (FEM) is introduced Fenner (2005); Rao
(2005). The application of the method is illustrated with problems in
Statics. The Matlab codes consider beams subjected to several constraints
and loads. A section is devoted to beams subjected to concentrated
forces. The weak form for the related equation is introduced and Matlab
applications are illustrated in this new situation.

6.1 Finite Element Method


6.1.1 Euler–Bernoulli Beam Equation
Consider the lateral vibrations U (x, t) of a beam subjected to the dis-
tributed load F (x, t) (Fig. 6.1.1). The governing equation is derived in the
framework of the Euler–Bernoull beam theory, where it is assumed that
plain cross-sections remain plain and deformed beam angles (slopes) are
small. Consider Newton’s Second Law
 x2  x2
∂2U
ρ dx = F dx + V (x1 , t) − V (x2 , t), (6.1.1)
x1 ∂t2 x1

 x2  
∂V ∂2U
F− − ρ 2 dx = 0, (6.1.2)
x1 ∂x ∂t
where ρ is the density per unit length and V is the shear force. Consider
the moment equation about x1 (neglecting rotary effects)

267
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 268

268 Matlab for Engineering

U(x, t) F(x, t)
F M(x1, t) M(x2, t)

x1 x2 x V(x1, t) V(x2, t)
Δx

Figure 6.1.1. Lateral vibrations of a beam.

 x2
(x − x1 )(F − ρUtt )dx − M (x1 , t) + M (x2 , t) − (x2 − x1 )V (x2 , t) = 0.
x1
(6.1.3)
 x2  
∂M ∂(x − x1 )V
(x − x1 )(F − ρUtt ) + − dx = 0. (6.1.4)
x1 ∂x ∂x
Equation (6.1.2) holds for any Δx = x2 − x1 . Therefore, it implies

ρUtt = F − Vx . (6.1.5)

Similarly, from (6.1.4), it follows that

(x − x1 )(F − ρUtt − Vx ) + Mx − V = 0,

that simplifies to

V = Mx . (6.1.6)

because of Eq. (6.1.5). Consider the relationship between the bending


moment M and (approximate) curvature

M = EIUxx , (6.1.7)

where E is the Young modulus and I is the beam’s second moment of area.
Substitute (6.1.7) into (6.1.6) and, then, into (6.1.5) to obtain the equation
for the lateral motions of an elastic beam

ρUtt + (EIUxx )xx = F. (6.1.8)

Often, the product EI is constant and we have

ρUtt + EIUxxxx = F. (6.1.9)

Equation (6.1.8) is named the Euler–Lagrange equation. In Statics, Eq.


(6.1.8) simplifies to the Euler–Bernoulli equation.

(EIUxx )xx = F, (6.1.10)


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 269

The Euler–Bernoulli Beam 269

A B A B x

Figure 6.1.2. Pinned-pinned beam (left), fixed-free beam (right).

Two initial conditions are prescribed to Eq. (6.1.8)


U (x, 0) = ϕ(x), Ut (x, 0) = ψ(x), (6.1.11)
corresponding to the beam initial position and velocity. In addtion, four
boundary conditions must be assigned. For example, for the pinned-pinned
beam in Fig. 6.1.2 (left), we have
U (xA , t) = 0, Uxx (xA , t) = 0, U (xB , t) = 0, Uxx (xB , t) = 0, (6.1.12)
where the second and fourth conditions are equivalent to M (xA , t) =
M (xB , t) = 0, because of (6.1.7). For the fixed end-free end beam in Fig.
6.1.2 (right), we have
U (xA , t) = 0, Ux (xA , t) = 0, Uxx (xB , t) = 0, Uxxx (xB , t) = 0, (6.1.13)
where the last two conditions are equivalent to M (xB , t) = V (xB , t) = 0,
because of (6.1.6) and (6.1.7).
The external forces and moments at the ends FA , MA , FB and MB can
be known loads or unknown reactive forces. In both cases, the relationships
between external forces and shear force and the bending moment (internal
forces) are expressed as (Fig. 6.1.3)
FA = V (xA , t), FB = −V (xB , t), (6.1.14)
MA = −M (xA , t), MB = M (xB , t). (6.1.15)
These relationships are clear from Fig. 6.1.3. However, they can be
rigorously derived from Eqs. (6.1.1) and (6.1.3). Indeed, consider Eq. (6.1.1)
on (xA , xA + Δx) (Fig. 6.1.4, left)
 xA +Δx
(F − ρUtt ) dx + FA − V (xA + Δx, t) = 0.
xA

For Δx → 0, this equation gives (6.1.14)1 . Next, consider Eq. (6.1.3) on


(xB − Δx, xB ) (Fig. 6.1.4, right)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 270

270 Matlab for Engineering

V(xA,t) V(xB,t) M(xA,t) M(xB,t)


A B A B x

FA FB MA MB

Figure 6.1.3. Forces and moments at the ends.

MA F(x,t) M(xA+Δ x,t) M(xB-Δ x,t) F(x,t) MB

A B

FA V(xA+Δ x,t) V(xB-Δ x,t) FB


xA xA+Δ x xB-Δ x xB

Figure 6.1.4. Relationships between external and internal forces at the ends.

 xA
(x−xB )(ρUtt −F ) dx+MB −M (xB −Δx, t)−ΔxV (xB −Δx, t) = 0.
xB −Δx

For Δx → 0, we get (6.1.15)2 . Similar reasonings lead to Formulas (6.1.14)2


and (6.1.15)1 .

6.1.2 Shape Functions


Consider the grid of punts x1 , . . . , xn , with step h, in Fig. 6.1.5 (right),
and refer to the interval e1 = [x1 , x2 ] = [0, h], the named element in this
context. The shape functions are defined as (Fig. 6.1.5, left)

3 2 2 3 N1 (x) = −6x/h2 + 6x2 /h3 ,


N1 (x) = 1 − x + x , ⇒ (6.1.16)
h2 h3 N1 (x) = −6/h2 + 12x/h3 ,

2 2 1 N2 (x) = 1 − 4x/h + 3x2 /h2 ,


N2 (x) = x − x + 2 x3 , ⇒ (6.1.17)
h h N2 (x) = −4/h + 6x/h2 ,

3 2 2 3 N3 (x) = 6x/h2 − 6x2 /h3 ,


N3 (x) = x − x , ⇒ (6.1.18)
h2 h3 N3 (x) = 6/h2 − 12x/h3 ,

1 2 1 3 N4 (x) = −2x/h + 3x2 /h2 ,


N4 (x) = − x + 2 x , ⇒ (6.1.19)
h h N4 (x) = −2/h + 6x/h2 .
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 271

The Euler–Bernoulli Beam 271

N1 N2 N3
x x
x1 x2 x1 x2 xi xi+1
N4
h

Figure 6.1.5. Hermitian functions.

These cubic polynomials are known as hermitian functions or hermitian


polynomials. The expression of the hermitian functions on the element ei =
[xi , xi+1 ] is obtained by replacing x with x − xi in Formulas (6.1.16) to
(6.1.19).

Remark 6.1.1 The hermitian functions have an interesting meaning in


Statics that is outlined below. Consider the Euler–Bernoulli equation

EIU iv = F (x). (6.1.20)

Apply Eq. (6.1.20) to discuss the fixed end-fixed end beam in Fig. 6.1.6 (left).
Assume that the beam is subjected to no external load and the constraint
in A is subjected to a vertical displacement UA = 1 (Fig. 6.1.6, right). The
deformed configuration is obtained by solving Eq. (6.1.20), where F = 0,
with the following boundary conditions

U (0) = 1, U  (0) = 0, U (h) = 0, U  (h) = 0, (6.1.21)

where h is the length of the beam. A simple calculation shows that U = N1


and it explains the physical meaning of N1 defined in (6.1.16). Now, suppose
that the constraint in A is subjected to the vertical displacement UA . After
replacing the first condition in (6.1.21) with U (0) = UA , an easy calculation
leads to U = UA N1 . Very often, it is UA < 0. The shear force and bending
moment are derived from Formulas (6.1.6) and (6.1.7), (6.1.16). Then, the
reactive forces follow from Formulas (6.1.14) and (6.1.15). See Exercise 6.4.1
for the physical meaning of N2 , N3 and N4 .
The importance of the hermitian polynomials is due to the fact that
any cubic polynomial defined on the element can be expressed as a
linear combination of those polynomials. Indeed, consider the generic cubic
polynomial

u(x) = a1 x3 + a2 x2 + a3 x + a4 , x ∈ e1 , (6.1.22)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 272

272 Matlab for Engineering

A
A B B
x

Figure 6.1.6. Meaning of N1 in Statics.

and introduce the notations

u1 = u(0), u2 = u (0), u3 = u(h), u4 = u (h). (6.1.23)

Use (6.1.23) in (6.1.22)





a4 = u 1 ,

⎨a = u ,
3 2
(6.1.24)

⎪ 1a h 3
+ a2 h2 + a3 h + a4 = u 3 ,


3a1 h2 + 2a2 h + a3 = u4 .
Solving System (6.1.24) with respect to ai and the result substituted into
(6.1.22) yields

u(x) = u1 N1 (x) + u2 N2 (x) + u3 N3 (x) + u4 N4 (x), (6.1.25)

that is the desired result. See Exercise 6.4.2. In addition, the functions
{Ni , i = 1, . . . , 4} are a linearly independent system, a basis, for the cubic
polynomials defined on the element. It must be proved that

0 = u1 N1 (x) + u2 N2 (x) + u3 N3 (x) + u4 N4 (x), x ∈ [0, h], (6.1.26)

if and only if
ui = 0, i = 1, . . . , 4. (6.1.27)

It is enough to show that (6.1.26) implies (6.1.27), since the opposite


statement is apparent. For x = 0 and x = h, from Formulas (6.1.16) to
(6.1.19), it follows that

0 = u1 N1 (0) = u1 , 0 = u3 N3 (h) = u3 .

In addition, let us differentiate (6.1.26)

0 = u1 N1 (x) + u2 N2 (x) + u3 N3 (x) + u4 N4 (x), x ∈ [0, h].

For x = 0 and x = h, from Formulas (6.1.16) to (6.1.19), it follows that

0 = u2 N2 (0) = u2 , 0 = u4 N4 (h) = u4 ,


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 273

The Euler–Bernoulli Beam 273

that completes the proof.


Let U (x, t) be a function defined on e1 ∪ e2 , e1 = [x1 , x2 ], e2 = [x2 , x3 ],
differentiable with respect to x. Introduce the notations
u1 (t) = U (x1 , t), u3 (t) = U (x2 , t), u5 (t) = U (x3 , t),
u2 (t) = Ux (x1 , t), u4 (t) = Ux (x2 , t), u6 (t) = Ux (x3 , t),
and consider the function
u(x, t) = u1 (t)N1,1 (x) + u2 (t)N2,1 (x) + u3 (t)N3,1 (x)
(6.1.28)
+ u4 (t)N4,1 (x), x ∈ e1 .
This function is named the hermitian approximating of U in e1 . Therefore,
the function
u(x, t) = u3 (t)N1,2 (x) + u4 (t)N2,2 (x) + u5 (t)N3,2 (x)
(6.1.29)
+ u6 (t)N4,2 (x), x ∈ e2 ,
is the hermitian approximating of U in e2 . A second subscript, related to
the element, was added to the hermitian functions, as their expressions
depend on the element. As it is immediately deduced that
u(x1 , t) = u1 (t), u(x2 , t) = u3 (t), u(x3 , t) = u5 (t),
ux (x1 , t) = u2 (t), ux (x2 , t) = u4 (t), ux (x3 , t) = u6 (t),
we realize that the values of U and Ux on the nodes are the same as
hermitian approximating. Combine (6.1.28) and (6.1.29) in one formula
u1 N1,1 + u2 N2,1 + u3 N3,1 + u4 N4,1 , x ∈ e1 ,
u(x, t) = (6.1.30)
u3 N1,2 + u4 N2,2 + u5 N3,2 + u6 N4,2 , x ∈ e2 ,
that is the piecewise hermitian approximating of U in e1 ∪ e2 . Lastly, if the
following definitions
N3,1 , x ∈ e1 ,
Φ1 = N1,1 , Φ2 = N2,1 , Φ3 =
N1,2 , x ∈ e2 ,
(6.1.31)
N4,1 , x ∈ e1 ,
Φ4 = Φ5 = N3,2 , Φ6 = N4,2 ,
N2,2 , x ∈ e2 ,

are used, Formula (6.1.30) is rewritten as (Fig. 6.1.7)


u = u 1 Φ1 + u 2 Φ2 + u 3 Φ3 + u 4 Φ4 + u 5 Φ5 + u 6 Φ6 . (6.1.32)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 274

274 Matlab for Engineering

e1 e1 e2 e2

Figure 6.1.7. Functions Φ1 , Φ2 (left), Φ3 , Φ4 (center), and Φ5 , Φ6 (right).

This formula is related to two elements and it suggests the generalization


to any number of elements.

6.1.3 Weak Form


Consider the Euler–Lagrange equation

ρUtt + (EIUxx )xx = F (x, t), 0 < x < L, t > 0. (6.1.33)

Multiply Eq. (6.1.33) by the smooth test function v = v(x) and integrate
over [0, L]
 L  L  L
ρUtt v dx + (EIUxx )xx v dx = F v dx.
0 0 0

Integrate the second integral by parts twice to obtain


 L  L  L
L
ρUtt v dx + EIUxx v  dx = F v dx + [v  M − vV ]0 , (6.1.34)
0 0 0

where Formulas (6.1.6) and (6.1.7) related to moment and shear were
considered in the finite term. Equation (6.1.34) is the weak form of the
beam equation (6.1.33) that is named the strong form by contrast. Since
integrating by parts lowered the order of the derivative with respect to x,
the set of the solutions is wider. Solutions can exist to the weak form and
do not have the necessary regularity to be solutions to the strong form.
The FEM considers the weak form and the piecewise hermitian
approximating functions on [0, L]. Let us begin to discuss the first element.
The hermitian approximating u of U on e1 is given by

u(x, t) = u1 (t)N1 (x) + u2 (t)N2 (x)u3 (t)N3 (x) + u4 (t)N4 (x), (6.1.35)

where ui (t), i = 1, . . . , 4, are unknown functions. Substituting u into Eq.


(6.1.34) and assuming v = Ni i = 1, . . . , 4, we arrive at the following system
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 275

The Euler–Bernoulli Beam 275

of second-order ordinary differential equations


4  h 4  h  h
üj ρNi Nj dx + uj EINi Nj dx = F Ni dx
j=1 0 j=1 0 0

h
+ [Ni M − Ni V ]0 , i = 1, . . . , 4. (6.1.36)

Hence,
4 4
Mij üj + kij uj = fi , i = 1, . . . , 4, (6.1.37)
j=1 j=1

where
 h  h
Mij = ρNi Nj dx, Kij = EINi Nj dx, i, j = 1, . . . , 4, (6.1.38)
0 0
 h
h
fi = F Ni dx + [Ni M − Ni V ]0 , i = 1, . . . , 4, (6.1.39)
0

In compact matrix form, Formula (6.1.37) is written as

M ü + ku = f . (6.1.40)

The matrix k is the stiffness matrix. The matrix M is named the mass
matrix, or more precisely, consistent mass matrix, as there is another mass
matrix that will be introduced later. The vector f is the load vector. For
constant EI, k is given by
⎡ ⎤
12 6h −12 6h
EI ⎢ 6h 4h2 −6h 2h2 ⎥
k= 3 ⎢ ⎥. (6.1.41)
h ⎣ −12 −6h 12 −6h ⎦
6h 2h2 −6h 4h2
For constant ρ, M is expressed as
⎡ ⎤
156 22h 54 −13h
ρh ⎢
⎢ 22h 4h
2
13h −3h2 ⎥
⎥.
M= ⎣ (6.1.42)
420 54 13h 156 −22h ⎦
−13h −3h2 −22h 4h2

Example 6.1.1 A function is presented that returns the stiffness matrix.


A way to call the function is illustrated in the comment lines of the listing.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 276

276 Matlab for Engineering

function K = stiffness(h, EI)


% This is the function file stiffness.m.
% The function returns the stiffness matrix. The input variables are:
% length of the element h and the product E*I. For example, the function
% can be called with the commands: h = 1; EI = 1; K = stiffness(h,EI).
K = zeros(4,4);
for i=1:4
for j=i:4
K(i,j) = EI*integral(@(x)Nxx(i, x, h).*Nxx(j, x, h), 0, h);
K(j,i) = K(i,j);
end
end
end
% Local function
function f = Nxx(i, x, h)
switch i
case 1
f = -6/h^2 + 12*x/h^3;
case 2
f = -4/h + 6*x/h^2;
case 3
f = 6/h^2 - 12*x/h^3;
case 4
f = -2/h + 6*x/h^2;
end
end
Another application is suggested in Exercise 6.4.3.

Example 6.1.2 A function is presented that returns the mass matrix.


A simple way to call the function is illustrated in the comment lines of the
listing.

function M = mass(h, rho)


% This is the function file massa.m.
% The function returns the mass matrix. The input variables are:
% length of the element h and the density rho. For example, the function
% can be called with the commands: h = 1; rho = 1; M = mass(h, rho).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 277

The Euler–Bernoulli Beam 277

M = zeros(4,4);
for i=1:4
for j=i:4
M(i,j) = rho*integral(@(x)N(i, x, h).*N(j, x, h), 0, h);
M(j,i) = M(i,j);
end
end
end
% Local function
function f = N(i, x, h)
switch i
case 1
f = 1 - 3*x.^2/h^2 + 2*x.^3/h^3;
case 2
f = x - 2*x.^2/h + x.^3/h^2;
case 3
f = 3*x.^2/h^2 - 2*x.^3/h^3;
case 4
f = -x.^2/h + x.^3/h^2;
end
end
Another application is suggested in Exercise 6.4.4.
Lastly, let us provide another expression of the mass matrix that is
much more used in the programs for beams in Dynamics,
⎡ ⎤
1000
ρh ⎢ ⎥
⎢0 0 0 0⎥.
M= (6.1.43)

2 0 0 1 0⎦
0000
This expression is derived by discretizing the distributed inertial forces
−ρÜ (x, t).

6.2 Statics
In Statics, the inertial forces are zero and the Euler–Lagrange equation
simplifies to the Euler–Bernoulli equation
(EIU  ) = F (x), 0 < x < L, (6.2.1)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 278

278 Matlab for Engineering

where the unknown displacement depends on x only: U = U (x). The weak


form is derived from (6.1.34) as a special case
 L  L
  L
EIU v dx = F v dx + [v  M − vV ]0 . (6.2.2)
0 0

The FEM solution on e1 is expressed as


u(x) = u1 N1 (x) + u2 N2 (x) + u3 N3 (x) + u4 N4 (x), (6.2.3)
where, now, ui are unknown coefficients that must satisfy the algebraic
system
ku = f , (6.2.4)
that follows from (6.1.40) when the inertial forces are zero. Consider the
load vector (6.1.39), composed by two terms
f = fd + fb . (6.2.5)
The first vector depends on the special distributed load F (x, t) on the beam.
The second vector refers to the boundary conditions on the element, and it
has the following interesting expression
⎡  h⎤ ⎡ ⎤
[N1 M − N1 V ]0 V (0)
⎢  ⎥ ⎢ ⎥
⎢ [N2 M − N2 V ]h0 ⎥ ⎢ −M (0) ⎥
fb = ⎢
⎢  h⎥
⎥=⎢

⎥,
⎥ (6.2.6)
⎣ [N3 M − N3 V ]0 ⎦ ⎣ −V (h) ⎦
h
[N4 M − N4 V ]0 M (h)

where the last equality follows from the properties of the shape functions.

Example 6.2.1 Consider a fixed end-free end beam subjected to the tip
load p (Fig. 6.2.1). The following boundary conditions hold

p
x
1 2
M1 L
F1

Figure 6.2.1. Fixed end-free end beam subjected to a tip load.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 279

The Euler–Bernoulli Beam 279

U (0) = 0, U  (0) = 0,
(6.2.7)
M (L) = 0 ⇔ U  (L) = 0, V (L) = p ⇔ EIU  (L) = p.

Consider one element e1 = [0, L]. The approximating Solution (6.2.3)


simplifies to

u(x) = u3 N3 (x) + u4 N4 (x), (6.2.8)

because of boundary Conditions (6.2.7). In addition, the vector f in System


(6.2.4) simplifies to fb , as F = 0. The expression of fb is derived from the
general formula

fb = [V (0) − M (0) − V (L) M (L)]T = [F1 M1 F2 M2 ]T , (6.2.9)

by considering boundary Conditions (6.2.7),

fb = [F1 M1 − p 0]T , (6.2.10)

where F1 and M1 are the unknown reactive force and moment. Considering
all that, System(6.2.4) gives the following four equations

k13 u3 + k14 u4 = F1 , k23 u3 + k24 u4 = M1 , (6.2.11)

k33 u3 + k34 u4 = −p, k43 u3 + k44 u4 = 0. (6.2.12)

The last two equations are a simple algebraic system with unknowns u3
and u4 . Solving it yields the unknowns

u3 = −pL3 /(3EI), u4 = −pL2 /(2EI). (6.2.13)

Insert these values into (6.2.11) to find the reactive force and moment

F1 = p, M1 = pL.

In addition, substitute (6.2.13) into (6.2.8) to obtain u. Using Definitions


(6.1.16) to (6.1.19) of the shape functions, we find that u is the cubic
polynomial

u(x) = p(x3 − 3Lx2 )/(6EI). (6.2.14)

An easy calculation shows that this solution is the same as the analytical
solution U of the Euler–Bernoulli equation. This result is due to the
fact that the hermitian approximating of a cubic polynomial is the same
polynomial. See Exercise 6.4.5.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 280

280 Matlab for Engineering

The situation of the previous example is very rare, although interesting.


Generally, accurate results need many elements. Therefore, let us begin to
discuss two elements: [0, L] = e1 ∪ e2 = [x1 , x2 ] ∪ [x2 , x3 ]. From Formula
(6.1.32), we know that the approximate solution in this case is expressed
as

u = u 1 Φ1 + u 2 Φ2 + u 3 Φ3 + u 4 Φ4 + u 5 Φ5 + u 6 Φ6 , (6.2.15)

with Φi , i = 1, ..., 6, defined in (6.1.31). Substituting (6.2.15) into Eq. (6.2.2)


and assuming v = Φi , i = 1, . . . , 6, we arrive at a system of six equations
that can be obtained more easily by considering the two elements separately.
For the first element, we get
x
k11 u1 + k12 u2 + k13 u3 + k14 u4 = x12 F N1,1 dx + V (x1 ),
x
k21 u1 + k22 u2 + k23 u3 + k24 u4 = x12 F N2,1 dx − M (x1 ),
x (6.2.16)
k31 u1 + k32 u2 + k33 u3 + k34 u4 = x12 F N3,1 dx − V (x2 ),
x
k41 u1 + k42 u2 + k43 u3 + k44 u4 = x12 F N4,1 dx + M (x2 ),

and for the second element we get


 x3
k11 u3 + k12 u4 + k13 u5 + k14 u6 = x2
F N1,2 dx + V (x2 ),
 x3
k21 u3 + k22 u4 + k23 u5 + k24 u6 = x2
F N2,2 dx − M (x2 ),
 x3 (6.2.17)
k31 u3 + k32 u4 + k33 u5 + k34 u6 = x2
F N3,2 dx − V (x3 ),
 x3
k41 u3 + k42 u4 + k43 u5 + k44 u6 = x2 F N4,2 dx + M (x3 ).

Summing Eqs. (6.2.16)3,4 to (6.2.17)1,2 , related to the same nodes, yields


the desired system
x
k11 u1 + k12 u2 + k13 u3 + k14 u4 = x12 F Φ1 dx + V (x1 ),
x
k21 u1 + k22 u2 + k23 u3 + k24 u4 = x12 F Φ2 dx − M (x1 ),
k31 u1 + k32 u2 + (k33 + k11 )u3 + (k34 + k12 )u4
x
+ k13 u5 + k14 u6 = x13 F Φ3 dx,
(6.2.18)
k41 u1 + k42 u2 + (k43 + k21 )u3 + (k44 + k22 )u4
x
+ k23 u5 + k24 u6 = x13 F Φ4 dx,
x
k31 u3 + k32 u4 + k33 u5 + k34 u6 = x23 F Φ5 dx − V (x3 ),
x
k41 u3 + k42 u4 + k43 u5 + k44 u6 = x23 F Φ6 dx + M (x3 ).
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 281

The Euler–Bernoulli Beam 281

Now, we can be more rigorous. The first equation of System (6.2.18) is


derived by assuming v = N1,1 = Φ1 in (6.2.2), noting that the support
of Φ1 is e1 and using Definitions (6.1.16) to (6.1.19). The third equation is
found by assuming v = Φ3 in (6.2.2), noting that the support of Φ3 is e1 ∪e2
(Fig. 6.1.7) and using Definitions (6.1.16) to (6.1.19). The other equations
are derived with similar reasonings. The matrix K of the unknowns of
System (6.2.18) is given by (Fig. 6.2.2, left)
⎡ ⎤
k11 k12 k13 k14 0 0
⎢k k 0 0 ⎥
⎢ 21 22 k23 k24 ⎥
⎢ ⎥
⎢ k31 k32 k33 + k11 k34 + k12 k13 k14 ⎥
K=⎢ ⎥.
⎢ k41 k42 k43 + k21 k44 + k22 k23 k24 ⎥
⎢ ⎥
⎣ 0 0 k31 k32 k33 k34 ⎦
0 0 k41 k42 k43 k44

The generalization of matrix K to more elements is illustrated in Fig. 6.2.2


(right). Thus, we arrive at the algebraic system

Ku = f , (6.2.19)

that allows us to find beam displacements and rotations on the nodes.


Subsequently, shear forces and bending moments are derived from the
equations of each element. For example, for i = 1, 2, these equations follow
from (6.2.16)1,2 and (6.2.17)1,2
x
V (xi ) = k11 u2i−1 + k12 u2i + k13 u2i+1 + k14 u2i+2 − xii+1 F N1,i dx,
x
M (xi ) = xii+1 F N2,i dx − k11 u2i−1 − k12 u2i − k13 u2i+1 − k14 u2i+2 .
(6.2.20)

K= k K=
k

Figure 6.2.2. Matrix K for 2 and 4 elements.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 282

282 Matlab for Engineering

Example 6.2.2 The following listing presents a function that applies the
FEM to solve a fixed end-free end beam subjected to a distributed load.
The boundary conditions are:

U (0) = 0, U  (0) = 0, M (L) = 0, V (L) = 0 ⇔ M  (L) = 0. (6.2.21)

function [U, V, M, F1, M1] = beam fx fr(L, EI, F, n)


% This is the function file beam fx fr.m.
% The FEM is applied to solve the fixed end-free end beam. The input variables
% are: length of the beam L, product E*I, distributed load F and number of
% elements n. The function returns the displacement U, the shear force V,
% the bending moment M and the reactive force and moment F1, M1.
% Initialization
h = L/n;
N1 = @(xi, xn) 1 - 3*(xi - xn).2 /h2 + 2*(xi - xn).3 /h3 ;
N2 = @(xi, xn) (xi - xn) - 2*(xi - xn).2 /h + (xi - xn).3 /h2 ;
N3 = @(xi, xn) 3*(xi - xn).2 /h2 - 2*(xi - xn).3 /h3 ;
N4 = @(xi, xn) -(xi - xn).2 /h + (xi - xn).3 /h2 ;
x = linspace(0,L,n+1); V = zeros(n+1,1); M = zeros(n+1,1);
k = stiffness(h,EI);
fd = zeros(n*4,1);
for i=1:n
fd((i-1)*4+1) = integral(@(xi)F(xi).*N1(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+2) = integral(@(xi)F(xi).*N2(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+3) = integral(@(xi)F(xi).*N3(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+4) = integral(@(xi)F(xi).*N4(xi,x(i)), x(i), x(i+1));
end
nn = n*2 + 2;
K = zeros(nn,nn);% Global matrix
for i=1:2:nn-3
K(i:i+3,i:i+3) = K(i:i+3,i:i+3)+k;
end
K = K(3:nn,3:nn);
f = zeros(n*2+2,1);
f(1) = fd(1); f(2) = fd(2);
for i=2:2:2*(n-1)
f(i+1) = fd(2*i-1) +fd(2*i +1);
f(i+2) = fd(2*i) +fd(2*i+2);
end
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 283

The Euler–Bernoulli Beam 283

f(2*n+1) = fd(n*4-1); f(2*n+2) = fd(n*4);


f = f(3:2*n+2,1);
% FEM
ur(3:nn,1) = K\f;% Displacements and rotations.
U = ur(1:2:nn,1); % Displacements.
for i=1:n
V(i) = k(1,1)*ur(2*i-1) + k(1,2)*ur(2*i) + k(1,3)*ur(2*i+1)...
+ k(1,4)*ur(2*i+2) - fd((i-1)*4+1);
M(i) = -(k(2,1)*ur(2*i-1) + k(2,2)*ur(2*i) + k(2,3)*ur(2*i + 1)...
+ k(2,4)*ur(2*i + 2)) + fd((i-1)*4+2);
end
F1 = V(1); M1 = -M(1); % Reactive force and moment.
end

Example 6.2.3 The following listing illustrates a way to call the


beam fx fr function and solves the fixed end-free end beam in Fig. 6.2.3,
subjected to the triangular load

F = −q0 (L − x)/L, q0 > 0, 0 ≤ x ≤ L. (6.2.22)

The graphs of the displacement, shear force and bending moment are shown
in Fig. 6.2.4.

function beam fx fr ex
% This is the function file beam fx fr ex.m.
% Beam fx fr function is called to solve the fixed end-free end beam subjected
% to a triangular load. The lengths are in [mm] and the forces are in [N].
L = 3*103; E = 3*104; I = 9*108; EI = E*I;
q0 = 100; n = 10;

q0

M1 F1

Figure 6.2.3. Fixed end-free end beam subjected to a triangular load.


September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 284

284 Matlab for Engineering

Displacement
0
FEM
Exact

U -5

-10
0 1000 x 2000 3000

4 Shear force 7 Bending moment


10 10
15 0
FEM
Exact
10 -5
FEM
V M Exact
5 -10

0 -15
0 1000 2000 3000 0 1000 2000 3000
x x

Figure 6.2.4. Deformed beam, shear force and the bending moment.

F = @(xi) -q0*(L - xi)/L; % Triangular load.


[u, v, m, F1, M1] = trave m(L, EI, F, n);
x = linspace(0,L,n+1);
% Exact displacement, bending moment and shear force.
U = q0/120/L*(x’.5 - 5*L*x’.4 + 10*L2 *x’.3 - 10*L3*x’.2 )/EI;
M = q0/6/L*(x’ - L).3 ;
V = q0/2/L*(x’ - L).2 ;
subplot (2,2,1:2)
plot(x,u,’k’,x,U,’r*’,’LineWidth’,1);
xlabel(’x’); ylabel(’U’); title(’Displacement’); legend(’FEM’,’Exact’);
subplot (2,2,3)
plot(x,v,’k’,x,V,’r*’,’LineWidth’,1);
xlabel(’x’); ylabel(’V’); title(’Shear force’); legend(’FEM’,’Exact’);
subplot (2,2,4)
plot(x,m,’k’,x,M,’r*’,’LineWidth’,1);
xlabel(’x’); ylabel(’M’); title(’Bending moment’);
legend(’FEM’,’Exact’,’Location’,’Best’);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 285

The Euler–Bernoulli Beam 285

fprintf(’Reactive force F1 = %g\n’, F1)


fprintf(’Reactive moment M1 = %g\n’, M1)
fprintf( ’Maximum error U = %g\n’,max(abs(U-u)))
fprintf( ’Maximum error V = %g\n’,max(abs(V-v)))
fprintf( ’Maximum error M = %g\n’,max(abs(M-m)))
end
Another application is suggested in Exercise 6.4.6.

Example 6.2.4 A function is presented that applies the FEM to solve


a pinned-pinned beam subjected to a distributed load. The boundary
conditions are:

U (0) = 0, M (0) = 0, U (L) = 0, M (L) = 0. (6.2.23)

function [U, V, M, F1, F2] = beam pn pn(L, EI, F, n)


% This is the function file beam pn pn.m.
% The FEM is applied to solve the pinned-pinned beam. The input variables
% are: length of the beam L, product E*I, distributed load F, the number
% of elements n. The function returns the displacement U, the shear
% force V, the bending moment M and the reactive forces F1, F2.
% Initialization
h = L/n; x = linspace(0,L,n+1);
N1 = @(xi, xn) 1 - 3*(xi - xn).2 /h2 + 2*(xi - xn).3 /h3 ;
N2 = @(xi, xn) (xi - xn) - 2*(xi - xn).2 /h + (xi - xn).3 /h2 ;
N3 = @(xi, xn) 3*(xi - xn).2 /h2 - 2*(xi - xn).3 /h3 ;
N4 = @(xi, xn) -(xi - xn).2 /h + (xi - xn).3 /h2 ;
U = zeros(n+1,1); V = zeros(n+1,1); M = zeros(n+1,1);
k = stiffness(h,EI);
fd = zeros(n*4,1);
for i=1:n
fd((i-1)*4+1) = integral(@(xi)F(xi).*N1(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+2) = integral(@(xi)F(xi).*N2(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+3) = integral(@(xi)F(xi).*N3(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+4) = integral(@(xi)F(xi).*N4(xi,x(i)), x(i), x(i+1));
end
nn = n*2 + 2;
K = zeros(nn,nn);
for i=1:2:nn-3
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 286

286 Matlab for Engineering

K(i:i+3,i:i+3) = K(i:i+3,i:i+3)+k;
end
K=K([2:nn-2 nn],[2:nn-2 nn]);
f = zeros(n*2+2,1);
f(1) = fd(1); f(2) = fd(2);
for i=2:2:2*(n-1)
f(i+1) = fd(2*i-1) +fd(2*i +1);
f(i+2) = fd(2*i) +fd(2*i+2);
end
f(2*n+1) = fd(n*4-1); f(2*n+2) = fd(n*4);
f = f([2:nn-2 nn],1);
% FEM
ur = K\f;% Displacements and rotations.
for i=1:n-1
U(i+1) = ur(2*i);% Displacements.
end
ur = [0; ur(1:n*2-1); 0;ur(n*2)];
V(1) = k(1,1)*ur(1) + k(1,2)*ur(2) + k(1,3)*ur(3) + k(1,4)*ur(4) - fd(1);
for i=2:n
V(i) = k(1,1)*ur(2*i-1) + k(1,2)*ur(2*i) + k(1,3)*ur(2*i+1)...
+ k(1,4)*ur(2*i+2) - fd((i-1)*4+1);
M(i) = -(k(2,1)*ur(2*i-1) + k(2,2)*ur(2*i) + k(2,3)*ur(2*i + 1)...
+ k(2,4)*ur(2*i + 2))+ fd((i-1)*4+2);
end
V(n+1) = -(k(3,1)*ur(2*n-1) + k(3,2)*ur(2*n) + k(3,3)*ur(2*n+1)...
+ k(3,4)*ur(2*n+2))+fd(4*n-1);
F1 = V(1); F2 = -V(n+1); % Reactive forces.
end

Example 6.2.5 The following listing illustrates a way to call the


beam pn pn function and solves the pinned-pinned beam in Fig. 6.2.5,
subjected to the trapezoidal load
F (x) = −(q2 − q1 )x/L − q1 = −q3 x/L − q1 , q2 > q1 ≥ 0, 0 ≤ x ≤ L.
(6.2.24)

For q1 = 0, F simplifies to a triangular load and for q3 = 0 to a uniform


load. The graphs of the displacement, shear force and bending moment are
shown in Fig. 6.2.6.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 287

The Euler–Bernoulli Beam 287

q1 q2
x

F1 F2
Figure 6.2.5. Pinned-pinned beam subjected to a trapezoidal load.

Displacement
0
FEM
Exact
-2

U
-4

-6
0 1000 2000 3000 4000 5000
x
4 Shear force 7 Bending moment
10 10
5 6
FEM
Exact
4
V 0 M
2
FEM
Exact
-5
0
0 2000 4000 6000 0 2000 4000 6000
x x

Figure 6.2.6. Graphs of displacement, shear force and the bending moment.

function beam pn pn ex
% This is the function file beam pn pn ex.m.
% The beam pn pn function is called to solve the pinned-pinned beam sub-
% jected to a trapezoidal load. The lengths are in [mm] and the forces are in
% [N].
L = 5*103; E = 3*104; I = 9*108; EI = E*I;
n = 8; q2 = 30; q1 = 10; q3 = q2-q1;
F = @(xi) -q3*xi/L-q1; % Trapezoidal load.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 288

288 Matlab for Engineering

[u, v, m, F1, F2] = beam pn pn(L, EI, F, n);


x = linspace(0,L,n+1);
% Exact displacement, bending moment and shear force.
U =(-q3*x’.5 /120/L - q1*x’.4 /24 + (q3/36+q1/12)*L*x’.3-...
(7*q3/360 + q1/24)*L3*x’)/EI;
M = -q3*x’.3 /6/L - q1*x’.2 /2 + (q3/6 + q1/2)*L*x’;
V = -q3*x’.2 /2/L - q1*x’ + q3*L/6 + q1*L/2;
subplot (2,2,1:2)
plot(x,u,’k’,x,U,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’U’);
title(’Displacement’); legend(’FEM’,’Exact’,’Location’,’North’);
subplot (2,2,3)
plot(x,v,’k’,x,V,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’V’);
title(’Shear force’); legend(’FEM’,’Exact’);
subplot (2,2,4)
plot(x,m,’k’,x,M,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’M’);
title(’Bending moment’); legend(’FEM’,’Exact’,’Location’,’South’);
fprintf(’Reactive force F1 = %g\n’, F1)
fprintf(’Reactive force F2 = %g\n’, F2)
fprintf( ’Maximum error U = %g\n’,max(abs(U-u)))
fprintf( ’Maximum error V = %g\n’,max(abs(V-v)))
fprintf( ’Maximum error M = %g\n’,max(abs(M-m)))
end
Another application is suggested in Exercise 6.4.7.

Example 6.2.6 A function is presented that applies the FEM to solve the
fixed end-pinned end beam, subjected to a distributed load. The boundary
conditions are:

U (0) = 0, U  (0) = 0, U (L) = 0, M (L) = 0. (6.2.25)

function [U, V, M, F1, F2, M1] = beam fx pn(L, EI, F, n)


% This is the function file beam fx pn.m.
% The FEM is applied to solve the fixed end-pinned end beam. The input
% variables are: length of the beam L, product E*I, distributed load F,
% number of elements n. The function returns the displacement U, the
% shear force V, the bending moment M and the reactive forces and
% moment F1, F2, M1.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 289

The Euler–Bernoulli Beam 289

% Initialization
h = L/n;
N1 = @(xi, xn) 1 - 3*(xi - xn).2 /h2 + 2*(xi - xn).3 /h3 ;
N2 = @(xi, xn) (xi - xn) - 2*(xi - xn).2 /h + (xi - xn).3 /h2 ;
N3 = @(xi, xn) 3*(xi - xn).2 /h2 - 2*(xi - xn).3 /h3 ;
N4 = @(xi, xn) -(xi - xn).2 /h + (xi - xn).3 /h2 ;
x = linspace(0,L,n+1); V = zeros(n+1,1); M = zeros(n+1,1);
k = stiffness(h,EI);
fd = zeros(n*4,1);
for i=1:n
fd((i-1)*4+1) = integral(@(xi)F(xi).*N1(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+2) = integral(@(xi)F(xi).*N2(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+3) = integral(@(xi)F(xi).*N3(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+4) = integral(@(xi)F(xi).*N4(xi,x(i)), x(i), x(i+1));
end
nn = n*2 + 2;
K = zeros(nn,nn);
for i=1:2:nn-3
K(i:i+3,i:i+3) = K(i:i+3,i:i+3)+k;
end
K = K([3:nn-2 nn],[3:nn-2 nn]);
f = zeros(n*2+2,1);
f(1) = fd(1); f(2) = fd(2);
for i=2:2:2*(n-1)
f(i+1) = fd(2*i-1) +fd(2*i +1);
f(i+2) = fd(2*i) +fd(2*i+2);
end
f(2*n+1) = fd(n*4-1); f(2*n+2) = fd(n*4);
f = f([3:nn-2 nn],1);
% FEM
ur = K\f;
ur = [0; 0; ur(1:end-1,1); 0; ur(end,1)];
U = ur(1:2:nn,1);
for i=1:n
V(i) = k(1,1)*ur(2*i-1) + k(1,2)*ur(2*i) + k(1,3)*ur(2*i+1)...
+ k(1,4)*ur(2*i+2) - fd((i-1)*4+1);
M(i) = -(k(2,1)*ur(2*i-1) + k(2,2)*ur(2*i) + k(2,3)*ur(2*i + 1)...
+ k(2,4)*ur(2*i + 2))+ fd((i-1)*4+2);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 290

290 Matlab for Engineering

qM
M1 x

F1 F2
Figure 6.2.7. Fixed end-pinned end beam subjected to a parabolic load.

end
V(n+1) = -(k(3,1)*ur(2*n-1)+k(3,2)*ur(2*n)+k(3,3)*ur(2*n+1)+...
k(3,4)*ur(2*n+2))+fd(4*n-1);
M(n+1) = k(4,1)*ur(2*n-1)+k(4,2)*ur(2*n)+k(4,3)*ur(2*n+1)+...
k(4,4)*ur(2*n+2)-fd(4*n);
F1 = V(1); F2 = -V(n+1); M1 = -M(1);
end

Example 6.2.7 The following listing illustrates a way to call the


beam fx pn function and solves the fixed end-pinned end beam in Fig. 6.2.7,
subjected to the parabolic load

F = −4qM (Lx − x2 )/L2 , qM > 0. (6.2.26)

The graphs of the displacement, shear force and bending moment are shown
in Fig. 6.2.8.

function beam fx pn ex
% This is the function file beam fx pn ex.m.
% The beam fx pn function is called to solve the fixed end-pinned end beam
% subjected to a parabolic load. The lengths are in [mm] and the forces are in
% [N].
L = 6*103; E = 3*104; I = 9*108; EI = E*I;
qM = 10; n = 10;
F = @(xi) -4*qM*(L*xi-xi.2)/L2 ;
[u, v, m, F1, F2, M1] = beam fx pn(L, EI, F, n);
x = linspace(0,L,n+1);
U = qM/180/L2*(2*x’.6 -6*L*x’.5 +13*L3*x’.3 -9*L4 *x’.2 )/EI;
M = qM/30/L2*(10*x’.4 - 20*L*x’.3 + 13*L3 *x’ - 3*L4 );
V = qM*(40*x’.3-60*L*x’.2+13*L3 )/30/L2;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 291

The Euler–Bernoulli Beam 291

Displacement
0
FEM
-0.5 Exact

U -1

-1.5

-2
0 2000 4000 6000
x
4 Shear force 7 Bending moment
10 10
2
2 FEM
Exact
1 0
V M
0
-2
-1 FEM
Exact
-2 -4
0 2000 4000 6000 0 2000 4000 6000
x x

Figure 6.2.8. Graphs of displacement, shear force and the bending moment.

subplot (2,2,1:2)
plot(x,u,’k’,x,U,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’U’);
title(’Displacement’); legend(’FEM’,’Exact’,’Location’,’North’);
subplot (2,2,3)
plot(x,v,’k’,x,V,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’V’);
title(’Shear force’); legend(’FEM’,’Exact’);
subplot (2,2,4)
plot(x,m,’k’,x,M,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’M’);
title(’Bending moment’); legend(’FEM’,’Exact’,’Location’,’South’);
fprintf(’Reactive force F1 = %g\n’, F1)
fprintf(’Reactive force F2 = %g\n’, F2)
fprintf(’Reactive moment M1 = %g\n’, M1)
fprintf( ’Maximum error U = %g\n’,max(abs(U-u)))
fprintf( ’Maximum error V = %g\n’,max(abs(V-v)))
fprintf( ’Maximum error M = %g\n’,max(abs(M-m)))
end
Another application is suggested in Exercise 6.4.8.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 292

292 Matlab for Engineering

q MB

x
A B
FB
MA FA
Figure 6.2.9. Fixed end-fixed end beam with the constraint in A subjected to a vertical
displacement.

Consider the fixed end-fixed end beam of length L in Fig. 6.2.9 (left),
subjected to the uniform load F = −q. Suppose that the constraint in A is
subjected to the vertical displacement UA (Fig. 6.2.9, right). The statical
problem is governed by the Euler–Bernoulli Eq. (6.2.1) with the following
boundary conditions

U (0) = UA , U  (0) = 0, U (L) = 0, U  (L) = 0. (6.2.27)

Straightforward calculations show that the displacement is given by


F UA
U (x) = (x4 − 2Lx3 + L2 x2 ) + 3 (2x3 − 3x2 L + L3 ). (6.2.28)
24EI L
Hence, the bending moment, shear force and reactive forces are
F EIUA
M (x) = (6x2 − 6Lx + L2 ) + (12x − 6L), (6.2.29)
12 L3
V (x) = F (2x − L)/2 + 12EIUA /L3 , (6.2.30)
FA = −F L/2 + 12EIUA /L3 , MA = −F L2 /12 + 6EIUA /L2 ,
(6.2.31)
FB = −F L/2 − 12EIUA /L3 , MB = F L2 /12 + 6EIUA /L2 .
Consider the FEM. Since boundary Conditions (6.2.27) are inhomogeneous,
the form of the algebraic system that solves the problem is different
from (6.2.19) and similar to that considered in Sec. 4.2.4, where other
inhomogeneous problems were discussed. Therefore, the algebraic system
is

ku = f − g, (6.2.32)

where

gi = ki1 UA , i = 1, 2, 3, 4, gi = 0, i > 4. (6.2.33)

Formulas (6.2.32) and (6.2.33) will be used in the FEM applications below.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 293

The Euler–Bernoulli Beam 293

Example 6.2.8 A function is presented that applies the FEM to solve


the beam in Fig. 6.2.9.
function [U, V, M, F1, F2, M1 M2] = beam fx fx uA(L, EI, F, n, uA)
% This is the function file beam fx fx uA.m.
% The FEM is applied to solve the fixed end-fixed end beam subjected to a
% vertical displacement of the first constraint.
% Initialization
h = L/n;
N1 = @(xi, xn) 1 - 3*(xi - xn).2 /h2 + 2*(xi - xn).3 /h3 ;
N2 = @(xi, xn) (xi - xn) - 2*(xi - xn).2 /h + (xi - xn).3 /h2 ;
N3 = @(xi, xn) 3*(xi - xn).2 /h2 - 2*(xi - xn).3 /h3 ;
N4 = @(xi, xn) -(xi - xn).2 /h + (xi - xn).3 /h2 ;
x = linspace(0,L,n+1); V = zeros(n+1,1); M = zeros(n+1,1);
k = stiffness(h,EI);
fd = zeros(n*4,1);
for i=1:n
fd((i-1)*4+1) = integral(@(xi)F(xi).*N1(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+2) = integral(@(xi)F(xi).*N2(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+3) = integral(@(xi)F(xi).*N3(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+4) = integral(@(xi)F(xi).*N4(xi,x(i)), x(i), x(i+1));
end
nn = n*2 + 2; K = zeros(nn,nn);
for i=1:2:nn-3
K(i:i+3,i:i+3) = K(i:i+3,i:i+3)+k;
end
K = K(3:2*n,3:2*n);
f = zeros(n*2+2,1);
f(1) = fd(1); f(2) = fd(2);
for i=2:2:2*(n-1)
f(i+1) = fd(2*i-1) +fd(2*i +1);
f(i+2) = fd(2*i) +fd(2*i+2);
end
f(2*n+1) = fd(n*4-1); f(2*n+2) = fd(n*4);
f(1:4,1)= f(1:4,1)- k(1:4,1)*uA; % Inhomogeneous boundary conditions.
f = f(3:2*n,1);
% FEM
ur(3:n*2,1) = K\f;
ur = [uA; 0; ur(3:n*2,1); 0; 0]; U = ur(1:2:nn,1);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 294

294 Matlab for Engineering

for i=1:n
V(i) = k(1,1)*ur(2*i-1) + k(1,2)*ur(2*i) + k(1,3)*ur(2*i+1)...
+ k(1,4)*ur(2*i+2) - fd((i-1)*4+1);
M(i) = -(k(2,1)*ur(2*i-1) + k(2,2)*ur(2*i) + k(2,3)*ur(2*i + 1)...
+ k(2,4)*ur(2*i + 2)) + fd((i-1)*4+2);
end
V(n+1) = -(k(3,1)*ur(2*n-1)+k(3,2)*ur(2*n)+k(3,3)*ur(2*n+1)+...
k(3,4)*ur(2*n+2))+fd(4*n-1);
M(n+1) = k(4,1)*ur(2*n-1)+k(4,2)*ur(2*n)+k(4,3)*ur(2*n+1)+...
k(4,4)*ur(2*n+2)-fd(4*n);
F1 = V(1); F2 = -V(n+1); M1 = -M(1); M2 = M(n+1);
end

Example 6.2.9 The following listing illustrates a way to call the


beam fx fx uA function. It solves the fixed end-fixed end beam in Fig. 6.2.9,
subjected to the uniform load, with the first constrain subjected to the
vertical displacement UA . The graphs of the displacement, shear force and
bending moment are shown in Fig. 6.2.10.

function beam fx fx uA ex
% This is the function file beam fx fx uA ex.m.
% The beam fx fx uA function is called to solve the fixed end-fixed end beam
% subjected to a uniform load and to a displacement of the first
% constraint. The lengths are in [mm] and the forces are in [N].
L = 4*10^3; E = 3*10^4; I = 9*10^8; EI = E*I; uA = -.1;
n = 18; q = 10;
F = @(xi) -q;
[u, v, m, F1, F2, M1, M2] = beam fx fx uA(L, EI, F, n, uA);
x = linspace(0,L,n+1);
% Exact displacement, bending moment and shear force.
U = -q/24*(x’.^4 - 2*L*x’.^3 + L^2*x’.^2)/EI...
+ uA*(2*x’.^3/L^3 - 3*x’.^2/L^2 + 1);
M = -q/12*(6*x’.^2 - 6*L*x’ + L^2)+EI*uA*(x’*12/L^3 - 6/L^2);
V = -q/2*(2*x’ - L)+12*uA*EI/L^3;
subplot (2,2,1:2);
plot(x,u,’k’,x,U,’r*’); xlabel(’x’); ylabel(’U’); title(’Displacement’);
legend(’FEM’,’Exact’,’Location’,’North’);
subplot (2,2,3);
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 295

The Euler–Bernoulli Beam 295

Displacement
0
FEM
Exact
-0.1
U
-0.2

-0.3
0 500 1000 1500 2000 2500 3000 3500 4000
x
x 10
4 Shear force x 10
7 Bending moment
2 1

1 0.5

0 0
V M
-1 -0.5

-2 FEM -1 FEM
Exact Exact
-3 -1.5
0 1000 2000 3000 4000 0 1000 2000 3000 4000
x x

Figure 6.2.10. Graphs of displacement, shear force and the bending moment.

plot(x,v,’k’,x,V,’r*’); xlabel(’x’); ylabel(’V’);


title(’Shear force’); legend(’FEM’,’Exact’,’Location’,’South’);
subplot (2,2,4);
plot(x,m,’k’,x,M,’r*’);
xlabel(’x’); ylabel(’M’); title(’Bending moment’);
legend(’FEM’,’Exact’,’Location’,’South’);
fprintf(’Reactive force F1 = %g\n’, F1)
fprintf(’Reactive force F2 = %g\n’, F2)
fprintf(’Reactive moment M1 = %g\n’, M1)
fprintf(’Reactive moment M2 = %g\n’, M2)
fprintf( ’Maximum error U = %g\n’,max(abs(U-u)))
fprintf( ’Maximum error V = %g\n’,max(abs(V-v)))
fprintf( ’Maximum error M = %g\n’,max(abs(M-m)))
end
Another application is suggested in Exercise 6.4.9.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 296

296 Matlab for Engineering

6.3 Beam Subjected to Concentrated Forces


Consider a beam subjected to a distributed load and concentrated forces

{(xh , Fh ), h = 1, . . . , N }.

Equation (6.1.1) is easily modified to include the new forces


 x2 n2
(F − ρUtt ) dx + V (x1 , t) − V (x2 , t) + Fi = 0, (6.3.1)
x1 i=n1

where Fi , i = n1 , . . . , n2 are the concentrated forces within the inter-


val [x1 , x2 ]. Now, the discussion is delicate. Indeed, consider a single
concentrated force, say (xh , Fh ), and consider the interval [x1 , x2 ] with
x1 = xh − Δx and x2 = xh + Δx. If Δx is sufficiently small, only Fh
is in [x1 , x2 ] and Eq. (6.3.1) is written as
 xh +Δx
(F − ρUtt ) dx + V (xh − Δx, t) − V (xh + Δx, t) + Fh = 0.
xh −Δx

When Δx → 0, the previous equation gives

V (x+ −
h , t) − V (xh , t) = Fh , (6.3.2)

proving that V is discontinuous on x = xh . Consequently, V cannot be


differentiated for x = xh and U cannot be a (classical) solution to the
Euler–Lagrange Eq. (6.1.8). Let us show that U is a weak solution with
a suitable forcing term that depends on Fh . For simplicity, suppose that
the beam is subjected to a single concentrated force (xh , Fh ) and consider
the weak form (6.1.34) on the intervals, 0 < x < xh and xh < x < L,
respectively,
 xh  xh  xh
x
ρUtt v dx + EIUxx v  dx = F v dx + [v  M − vV ]0 h , (6.3.3)
0 0 0

 L  L  L
L
ρUtt v dx + EIUxx v  dx = F v dx + [v  M − vV ]xh . (6.3.4)
xh xh xh

Note that
L x L
[v  M − vV ]xh +[v  M − vV ]0 h = v(xh )(V (x+ − 
h , t)−V (xh , t))+[v M − vV ]0 .
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 297

The Euler–Bernoulli Beam 297

Therefore, summing (6.3.3) and (6.3.4) yields


 L  L  L
 L
ρUtt v dx + EIUxx v dx = F v dx + v(xh )Fh + [v  M − vV ]0 ,
0 0 0
(6.3.5)
where the step Relationship (6.3.2) was used. Equation (6.3.5) shows that U
is a weak solution of the Euler–Lagrange, with a new forcing term depending
on Fh and the Dirac δ function

ρUtt + (EIUxx )xx = F + Fh δ(x − xh ). (6.3.6)

In the general case, where the beam is subjected to many concentrated


forces, the last term in Eq. (6.3.6) is replaced by a sum. In Statics, Eq.
(6.3.6) simplifies to the Euler–Bernoulli equation

EIU iv = F + Fh δ(x − xh ). (6.3.7)

Let us discuss the previous situation with the FEM. Since this method
considers the weak equation, it is able to provide an approximating solution
to the problem. It remains to understand the role of the new forcing term
in FEM equations. Let us refer to h = 2, as the equations related to e1
and e2 are explicitly written in Formulas (6.2.16) and (6.2.17). When the
third equation in (6.2.16) is added to the first in (6.2.17), the following new
forcing term appears

V (x+ −
2 ) − V (x2 ) = F2 , (6.3.8)

due to the discontinuity of V . All the other equations remain unchanged.


In the general case of a concentrated force (xh , Fh ), only the forcing term
f2h−1 (related to the distributed load) has to be modified and the new term
added
f2h−1 + Fh . (6.3.9)

See Exercise 6.4.10.

Example 6.3.1 The following listing presents a function that applies the
FEM to solve the pinned end-pinned end beam subjected to a distributed
load and a concentrated force.

function [U, V, M, F1, F2] = beam c pn pn(L, EI, F, n, ih, Fh)


% This is the function file beam c pn pn.m.
% The FEM is applied to solve the pinned end-pinned end beam subjected to
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 298

298 Matlab for Engineering

% a distributed load and a concentrated force. The input variables are: length
% of the beam L, product E*I, distributed load F, number of elements n, node
% ih, and concentrated force Fh. The function returns the displacement U,
% the shear force V, the bending moment M, and the reactive forces F1, F2.

% Initialization
h = L/n;
N1 = @(xi, xn) 1 - 3*(xi - xn).2 /h2 + 2*(xi - xn).3 /h3 ;
N2 = @(xi, xn) (xi - xn) - 2*(xi - xn).2 /h + (xi - xn).3 /h2 ;
N3 = @(xi, xn) 3*(xi - xn).2 /h2 - 2*(xi - xn).3 /h3 ;
N4 = @(xi, xn) -(xi - xn).2 /h + (xi - xn).3 /h2 ;
x = linspace(0,L,n+1);
U = zeros(n+1,1); V = zeros(n+1,1); M = zeros(n+1,1);
k = stiffness(h,EI);
fd = zeros(n*4,1);
for i=1:n
fd((i-1)*4+1) = integral(@(xi)F(xi).*N1(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+2) = integral(@(xi)F(xi).*N2(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+3) = integral(@(xi)F(xi).*N3(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+4) = integral(@(xi)F(xi).*N4(xi,x(i)), x(i), x(i+1));
end
nn = n*2 + 2;
K = zeros(nn,nn);
for i=1:2:nn-3
K(i:i+3,i:i+3) = K(i:i+3,i:i+3)+k;
end
K = K([2:nn-2 nn],[2:nn-2 nn]);
f = zeros(n*2+2,1);
f(1) = fd(1); f(2) = fd(2);
for i=2:2:2*(n-1)
f(i+1) = fd(2*i-1) +fd(2*i +1);
f(i+2) = fd(2*i) +fd(2*i+2);
end
f(2*n+1) = fd(n*4-1); f(2*n+2) = fd(n*4);
f(ih*2-1) = f(ih*2-1) + Fh;% Concentrated force.
f = f([2:nn-2 nn],1);
% FEM
ur = K\f;
for i=1:n-1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 299

The Euler–Bernoulli Beam 299

U(i+1) = ur(2*i);
end
ur = [0; ur(1:n*2-1); 0; ur(n*2)];
V(1) = k(1,1)*ur(1) + k(1,2)*ur(2) + k(1,3)*ur(3) + k(1,4)*ur(4) - fd(1);
for i=2:n
V(i) = k(1,1)*ur(2*i-1) + k(1,2)*ur(2*i) + k(1,3)*ur(2*i+1)...
+ k(1,4)*ur(2*i+2) - fd((i-1)*4+1);
M(i) = -(k(2,1)*ur(2*i-1) + k(2,2)*ur(2*i) + k(2,3)*ur(2*i + 1)...
+ k(2,4)*ur(2*i + 2))+ fd((i-1)*4+2);
end
V(n+1) = -(k(3,1)*ur(2*n-1) + k(3,2)*ur(2*n) + k(3,3)*ur(2*n+1)...
+ k(3,4)*ur(2*n+2))+fd(4*n-1);
F1 = V(1); F2 = -V(n+1); % Reactive forces.
end

Example 6.3.2 The following listing illustrates a way to call the


beam c pn pn function to solve the pinned end-pinned end beam in Fig.
6.3.1, subjected to a concentrated force and the uniform load

F (x) = −q, 0 ≤ x ≤ L.

The graphs of the displacement, shear force and bending moment are shown
in Fig. 6.3.2.
function beam c pn pn ex
% This is the function file beam c pn pn ex.m.
% Beam c pn pn function is called to solve the pinned end-pinned end beam
% subjected to a uniform load and a concentrated force. The lengths are in
% [mm] and the forces are in [N].
L = 8*103; E = 3*104; I = 9*108; EI = E*I;
n = 40; q = 1;

q
x
Fh
F1 F2
Figure 6.3.1. Pinned end-pinned end beam subjected to a concentrated force.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 300

300 Matlab for Engineering

Displacement
0
FEM
-0.5 Exact

-1
U
-1.5

-2

0 2000 4000 6000 8000


x
Shear force 6 Bending moment
10
5000 10
FEM
Exact

V 0 M 5

FEM
Exact
-5000 0
0 4000 8000 0 4000 8000
x x

Figure 6.3.2. Graphs of displacement, shear force and bending moment.

F = @(xi) 0*xi-q; % Uniform load.


ih = 16; % 2 < ih < n.
Fh = -1000; % Concentrated Force.
[u, v, m, F1, F2] = trave c cc(L, EI, F, n, ih, Fh);
x = linspace(0,L,n+1); xh = x(ih);
% Displacement, bending moment and shear force related to uniform load.
U = -q*(x’.4 - 2*L*x’.3 + L3 *x’)/24/EI;
M = -q*(x’.2 - L*x’)/2; V = -q*(2*x’ - L)/2;
C1 = Fh*(xh - L)/6/L/EI; C2 = Fh*xh*(xh - L)*(xh - 2*L)/6/L/EI;
C3 = Fh*xh/6/L/EI; C4 = Fh*xh*(xh2 - L2 )/6/L/EI;
% Displacement, moment and shear force related to concentrated force.
Uh(1:ih-1,1) = C1*x(1:ih-1)’.3 + C2*x(1:ih-1)’;
Uh(ih:n+1,1) = C3*(x(ih:n+1)’- L).3 + C4*(x(ih:n+1)’- L);
Mh(1:ih-1,1) = EI*6*C1*x(1:ih-1)’; Mh(ih:n+1,1) = EI*6*C3*(x(ih:n+1)’-L);
Vh(1:ih-1,1) = EI*6*C1; Vh(ih:n+1,1) = EI*6*C3;
U = U + Uh; M = Mh + M; V = V + Vh;
subplot (2,2,1:2)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 301

The Euler–Bernoulli Beam 301

plot(x,u,’k’,x,U,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’U’);


title(’Displacement’); legend(’FEM’,’Exact’,’Location’,’North’);
subplot (2,2,3)
plot(x,v,’k’,x,V,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’V’);
title(’Shear force’); legend(’FEM’,’Exact’);
subplot (2,2,4)
plot(x,m,’k’,x,M,’r*’,’LineWidth’,1); xlabel(’x’); ylabel(’M’);
title(’Bending moment’); legend(’FEM’,’Exact’,’Location’,’South’);
fprintf(’Reactive force F1 = %g\n’, F1)
fprintf(’Reactive force F2 = %g\n’, F2)
fprintf( ’Maximum error U = %g\n’,max(abs(U-u)))
fprintf( ’Maximum error V = %g\n’,max(abs(V-v)))
fprintf( ’Maximum error M = %g\n’,max(abs(M-m)))
end
Another application is suggested in Exercise 6.4.11.

Example 6.3.3 Consider the overhanging beam of length L in Fig. 6.3.3,


subjected to the tip load p. The boundary conditions are:
U (0) = 0, U  (0) = 0, M (L) = 0, V (L) = p. (6.3.10)
The roller support is located at xh < L, and it is cinematically modeled by
the equation
U (xh ) = 0. (6.3.11)
In this problem, the concentrated force is the unknown reactive force F2 .
Of course, the step Relationship (6.3.2) holds
V (x+ −
h ) − V (xh ) = Fh = F2 , (6.3.12)

p
x

M1 1 h1 = xh 2 h2 3
F1 F2
Figure 6.3.3. Overhanging beam.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 302

302 Matlab for Engineering

but both the left and right sides are unknown. The solution U to Problem
(6.3.10) and (6.3.12) is found by solving the homogenous Euler–Bernoulli
equation on the two intervals x < xh and x > xh . An easy calculation gives

−C1 (x3 − xh x2 ), x ∈ e1 ,
U= (6.3.13)
C2 (x − xh )3 − C3 (x − xh )2 − C4 (x − xh ), x ∈ e2 ,

where
p(L − xh ) p p(L − xh ) pxh (L − xh )
C1 = , C2 = , C3 = , C4 = .
4xh EI 6EI 2EI 4EI
Shear force and the bending moment are deduced by deriving (6.3.13) on
the related intervals. Hence, the reactive forces and moments are

F1 = −3p(L − xh )/2xh , M1 = −p(L − xh )/2, F2 = p(3L − xh )/2xh .

Let us apply the FEM. Consider two elements of lengths h1 = xh and


h2 = L − xh , respectively. The approximating solution is

u4 N4,1 , x ∈ e1 ,
u= (6.3.14)
u4 N2,2 + u5 N3,2 + u6 N4,2 , x ∈ e2 ,

since

u1 = U (0) = 0, u2 = U  (0) = 0, u3 = U (x2 ) = 0. (6.3.15)

Refer to Formulas (6.2.16) and (6.2.17), where the equations for two
elements are explicitly written. Adding the last two equations in (6.2.16)
to the first two in (6.2.17) and considering (6.3.12) and (6.3.15), we arrive
at the following system
⎧ 1


k14 u4 = V (x1 ),



⎪ 1
u4 = −M (x1 ),

⎪ k24



⎨ (k34
1 2 2 2
+ k12 )u4 + k13 u5 + k14 u6 = F2 ,
(6.3.16)

⎪ 1 2 2 2
(k44 + k22 )u4 + k23 u5 + k24 u6 = 0,





⎪ 2 2 2
⎪ k32 u4 + k33 u5 + k34 u6 = −p,


⎩ 2 2 2 2
k42 u4 + k43 u5 + k44 u6 = 0,

where subscript 1, or 2, indicates that k must be evaluated for h = h1 , or


h = h2 . Solve the system formed by the last three equations in (6.3.16). We
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 303

The Euler–Bernoulli Beam 303

get the unknown coefficients



⎪ u4 = −pxh (L − xh )/4EI,


u5 = −p(L − xh )3 /3EI − pxh (L − xh )2 /4EI, (6.3.17)



u6 = −p(L − xh )2 /2EI − pxh (L − xh )/4EI,

that, inserted in (6.3.14), give u. A rapid calculation shows that u = U , as


expected. Substituting u4 , u5 and u6 into the first three equations of System
(6.3.16) yields the shear force, bending moment and reactive forces.
The following listing presents a function that applies the FEM to solve
the overhanging beam.

function [U, V, M, F1, M1, F2] = beam ov(L, EI, F, n, ih, Fp)
% This is the function file beam ov.m.
% The FEM is applied to solve an overhanging beam with a roller support
% located at xh (< L). The input variables are: length of the beam L,
% product E*I, distributed load F, number of elements n, node ih where
% the roller support is located, and tip load Fp. The function returns the
% displacement U, shear force V, bending moment M, and reactive forces
% and moment F1, F2, M1.
% Initialization
h = L/n;
N1 = @(xi, xn) 1 - 3*(xi - xn).2 /h2 + 2*(xi - xn).3 /h3 ;
N2 = @(xi, xn) (xi - xn) - 2*(xi - xn).2 /h + (xi - xn).3 /h2 ;
N3 = @(xi, xn) 3*(xi - xn).2 /h2 - 2*(xi - xn).3 /h3 ;
N4 = @(xi, xn) -(xi - xn).2 /h + (xi - xn).3 /h2 ;
x = linspace(0,L,n+1); V = zeros(n+1,1); M = zeros(n+1,1);
k = stiffness(h,EI);
fd = zeros(n*4,1);
for i=1:n
fd((i-1)*4+1) = integral(@(xi)F(xi).*N1(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+2) = integral(@(xi)F(xi).*N2(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+3) = integral(@(xi)F(xi).*N3(xi,x(i)), x(i), x(i+1));
fd((i-1)*4+4) = integral(@(xi)F(xi).*N4(xi,x(i)), x(i), x(i+1));
end
nn = n*2 + 2;
K = zeros(nn,nn);
for i=1:2:nn-3
K(i:i+3,i:i+3) = K(i:i+3,i:i+3)+k;
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 304

304 Matlab for Engineering

end
K = K([3:2*ih-2 2*ih:nn],[3:2*ih-2 2*ih:nn]);
f = zeros(n*2+2,1);
f(1) = fd(1);
f(2) = fd(2);
for i=2:2:2*(n-1)
f(i+1) = fd(2*i-1) +fd(2*i +1);
f(i+2) = fd(2*i) +fd(2*i+2);
end
f(2*n+1) = fd(n*4-1)+ Fp;
f(2*n+2) = fd(n*4);
f = f([3:2*ih-2 2*ih:nn]);
% FEM
ur = K\f;
ur = [0; 0; ur];
ur = [ur(1:2*ih-2,1); 0; ur(2*ih-1:nn-1,1)];
U = ur(1:2:nn,1);
for i=1:n
V(i) = k(1,1)*ur(2*i-1) + k(1,2)*ur(2*i) + k(1,3)*ur(2*i+1)...
+ k(1,4)*ur(2*i+2) - fd((i-1)*4+1);
M(i) = -(k(2,1)*ur(2*i-1) + k(2,2)*ur(2*i) + k(2,3)*ur(2*i + 1)...
+ k(2,4)*ur(2*i + 2)) + fd((i-1)*4+2);
end
V(n+1) = -(k(3,1)*ur(2*n-1)+k(3,2)*ur(2*n)+k(3,3)*ur(2*n+1)+...
k(3,4)*ur(2*n+2))+fd(4*n-1);
M(n+1) = k(4,1)*ur(2*n-1)+k(4,2)*ur(2*n)+k(4,3)*ur(2*n+1)+...
k(4,4)*ur(2*n+2)-fd(4*n);
F1 = V(1); % Reactive force.
M1 = -M(1); % Reactive moment.
F2 = V(ih) - V(ih-1);% Reactive force.
end

See Exercise 6.4.12. If the following data


L = 3 ∗ 103 , E = 3 ∗ 104 , I = 9 ∗ 108 ,
(6.3.18)
F = 0, p = 10, n = 15, ih = 11, F p = −p,
are used, Fig. 6.3.4 should be produced.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 305

The Euler–Bernoulli Beam 305

-4 Displacement
10

-1
U

-2
FEM
Exact
-3
0 1000 2000 3000
x
Shear force Bending moment
10 5000
FEM FEM
5 Exact Exact
0

V 0 M
-5000
-5

-10 -10000
0 1000 2000 3000 0 1000 2000 3000
x x

Figure 6.3.4. Overhanging beam: displacement, shear force and bending moment.

6.4 Exercises
Exercise 6.4.1 Explain the physical meaning of N2 , N3 and N4 .
Hint. If the constraint in A is subjected to the rotary displacement U  (0) =
1, it is
EIU iv = 0, U (0) = 0, U  (0) = 1, U (h) = 0, U  (h) = 0, ⇒ U = N2 .

Exercise 6.4.2 Solve the algebraic System (6.1.24) and find Formula
(6.1.25).

Exercise 6.4.3 Write a function that returns the symbolic stiffness


matrix.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 306

306 Matlab for Engineering

Answer.
function K = stiffness s
% This is the function file stiffness s.m.
% Symbolic stiffness matrix is returned.
syms x h EI;
K(4,4) = h;
for i=1:4
for j=i:4
K(i,j) = EI*int(Nxx(i, x, h).*Nxx(j, x, h), x, 0, h);
K(j,i) = K(i,j);
end
end
end
% Local function
function f = Nxx(i, x, h)
switch i
case 1
f = -6/h^2 + 12*x/h^3;
case 2
f = -4/h + 6*x/h^2;
case 3
f = 6/h^2 - 12*x/h^3;
case 4
f = -2/h + 6*x/h^2;
end
end

Exercise 6.4.4 Write a function, say mass s, that returns the symbolic
mass matrix.
Hint. See the stiffness s function.

Exercise 6.4.5 Consider the pinned-pinned beam subjected to the


external moments M1 and M2 at the ends, shown in Fig. 6.4.1 (left). The
following boundary conditions hold
U (0) = 0, U  (0) = −M1 /EI, U (L) = 0, U  (L) = M2 /EI. (6.4.1)
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 307

The Euler–Bernoulli Beam 307

M1 M2 M1 M2
1 2 1 2 x

F1 F2

Figure 6.4.1. Pinned-pinned beam subjected to external moments at the ends.

Find the approximating Solution by considering one element e1 = [0, L].


Answer. The approximating Solution (6.2.3) simplifies to
u(x) = u2 N2 (x) + u4 N4 (x), (6.4.2)
because of boundary Conditions (6.4.1). Moreover, the vector f in System
(6.2.4) simplifies to fb , as F = 0. The expression of fb is derived from general
Formula (6.2.6) by considering boundary Conditions (6.4.1),
fb = [F1 M1 F2 M2 ]T , (6.4.3)
where F1 and F2 are the unknown reactive forces. Considering all that,
System(6.2.4) gives the following four equations
k12 u2 + k14 u4 = F1 , k32 u2 + k34 u4 = F2 , (6.4.4)

k22 u2 + k24 u4 = M1 , k42 u2 + k44 u4 = M2 . (6.4.5)


The last two equations are a simple algebraic system with unknowns u2
and u4 . Solving it yields the unknowns
M1 L M2 L M2 L M1 L
u2 = − , u4 = − . (6.4.6)
3EI 6EI 3EI 6EI
Insert these values into (6.4.4) to find the reactive forces
F1 = (M1 + M2 )/L, F2 = −(M1 + M2 )/L.
In addition, substitute (6.4.6) into (6.4.2) to obtain u. Using Definitions
(6.1.16) to (6.1.19) of the shape functions, we find that u is the cubic
polynomial (Fig. 6.4.1, right)
M1 + M2 3 M1 2 2M1 − M2
u(x) = x − x + Lx. (6.4.7)
6LEI 2EI 6EI
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 308

308 Matlab for Engineering

qM
x

F1 F2

Figure 6.4.2. Pinned-pinned beam subjected to a parabolic load.

Figure 6.4.3. Fixed end-fixed end beam.

An easy calculation shows that this solution is the same as the analytical
solution U of the Euler–Bernoulli equation, as expected.

Exercise 6.4.6 Write a listing that calls the beam fx fr function to solve
the fixed end-free end beam subjected to the uniform load F (x) = −q.

Exercise 6.4.7 Write a listing that calls the beam pn pn function to solve
the pinned-pinned beam in Fig. 6.4.2, subjected to a parabolic load

F = −4qM (Lx − x2 )/L2 , qM > 0.

Exercise 6.4.8 Write a function, say beam fx fx, to solve the fixed end-
fixed end beam in Fig. 6.4.3.

Exercise 6.4.9 Consider the fixed end-fixed end beam of length L in Fig.
6.4.4 (left), subjected to a uniform load. Suppose that the constraint in A
is subjected to a rotational displacement (Fig. 6.4.4, right). The statical
problem is governed by the Euler–Bernoulli Eq. (6.2.1) with the following
boundary conditions

U (0) = 0, U  (0) = rA , U (L) = 0, U  (L) = 0.

Write a function, say beam fx fx rA, that applies the FEM to solve the
problem above.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 309

The Euler–Bernoulli Beam 309

MB
q

x
A B MA
FA FB
Figure 6.4.4. Fixed end-fixed end beam with the constraint in A subjected to a
rotational displacement.

Fh
x
xh
F1 F2
Figure 6.4.5. Pinned end-pinned end beam subjected to concentrated force.

Exercise 6.4.10 Consider the pinned end-pinned end beam in Fig. 6.4.5,
subjected to the concentrated force (xh , Fh ). The boundary conditions are:

U (0) = 0, M (0) = 0, U (L) = 0, M (L) = 0. (6.4.8)

Find the displacement, shear force and bending moment.


Answer. The function U can be found by solving the homogeneous Euler–
Bernoulli equation on the two intervals x < xh and x > xh with boundary
Conditions (6.4.8) and two more conditions

U (x+ −
h ) = U (xh ), V (x+ −
h ) − V (xh ) = Fh . (6.4.9)

These conditions follow from the beam continuity on xh and step Relation-
ship (6.3.2). An easy calculation gives

C1 x3 + C2 x, x < xh ,
U= 3
(6.4.10)
C3 (x − L) + C4 (x − L), x > xh ,

where
C1 = Fh (xh − L)/6LEI, C2 = Fh xh (xh − 2L)(xh − L)/6LEI,
C3 = Fh xh /6LEI, C4 = Fh xh (x2h − L2 )/6LEI.
The analytical expressions of the shear force and bending moment are
obtained by differentiating (6.4.10) on the related intervals.
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch06 page 310

310 Matlab for Engineering

Exercise 6.4.11 Write a function that applies the FEM to solve a fixed
end-pinned end beam subjected to a distributed load and a concentrated
force.

Exercise 6.4.12 Write a listing to call the beam ov. Use Data (6.3.18)
and obtain Fig. 6.3.4. Moreover, introduce the distributed load, which was
assumed to be zero in (6.3.18).
September 8, 2021 11:35 Matlab for Engineering 9in x 6in b4335-Bib page 311

Bibliography

Cannon, J. R. (1984). The One-Dimensional Heat Equation (Addison-Wesley,


London, UK).
Carslaw, H. S. and Jager, J. C. (1959). Conduction of Heat in Solids (Clarendon
Press, London, England).
Clough, R. W. (1960). The finite element method in plane stress analysis, Second
ASCE Conference on Electronic Computation, Pittsburg, USA, September
8–9, pp. 345–378.
Collatz, L. (1966). The Numerical Treatment of Differential Equations (Springer-
Verlag, New York, NY, USA).
Cooper, J. M. (1998). Introduction to Partial Differential Equations with Matlab
(Birkhauser, Boston, Ma, USA).
Courant, R., Friedrichs, K. and Lewy, H. (1928). Uber die partiellen Differenzen-
gleichunghen der Mathematischen Physik, Math. Ann., Vol. 100, pp. 32–74.
Crank, J. (1979). The Mathematics of Diffusion (Clarendon Press, Oxford, UK).
Crank, J. (1984). Free and Moving Boundary Problems (Oxford Science Publica-
tions, Clarendon Press, Oxford, UK).
D’Acunto, B. (2004). Computational Methods for PDE in Mechanics (World
Scientific, Singapore).
D’Acunto, B. and Massarotti, P. (2016). Meccanica Razionale per Ingegneria
(Maggioli Editore, Santarcangelo di Romagna, Italy).
de Vahl Davis, G. (1986). Numerical Methods in Engineering & Science (Chap-
man & Hall, London, UK).
Fenner, R. T. (2005). Finite Element Method for Engineers (Imperial College
Press, London, UK).
Forsythe, G. E. and Wasov, W. R. (1960). Finite Difference Methods for Partial
Differential Equations (Wiley, New York, NY, USA).
Hutton, D. V. (2004). Fundamentals of Finite Element Analysis (McGraw-Hill,
New York, NY, USA).
Kharab, A. and Guenther, R. B. (2002). Introduction to Numerical Methods. A
Matlab Approach (Chapman & Hall/CRC, Boca Raton, Fl, USA).

311
September 8, 2021 11:35 Matlab for Engineering 9in x 6in b4335-Bib page 312

312 Matlab for Engineering

Knabner, P. and Angermann, L. (2003). Numerical Methods for Elliptic and


Parabolic Partial Differential Equations (Springer, New York, NY, USA).
Kwon, Y. W. and Bang, H. (2000). The Finite Element Method Using Matlab
(CRC Press, Boca Raton, Fl, USA).
Lapidus, L. and Pinter, G. F. (1982). Numerical Solutions of Partial Differential
Equations in Science and Engineering (J. Wiley & Sons, New York, NY,
USA).
Mitchell, A. R. and Griffiths, D. F. (1995). The Finite Difference Method in
Partial Differential Equations (J. Wiley & Sons, New York, NY, USA).
Moler, C. (2011). Experiments with MATLAB (E-book: The MathWorks, Inc.,
Natick, MA, USA).
Necati Ozisik, M. (1994). Finite Difference Methods in Heat Transfer (CRC Press,
London, UK).
Rao, S. S. (2005). The Finite Element Method in Engineering (Elsevier, Oxford,
UK).
Richtmyer, R. D. and Morton, K. W. (1967). Difference Methods for Initial-value
Problems (J. Wiley & Sons, New York, NY, USA).
Rubinstein, L. I. (1971). The Stefan Problem (Translation of Mathematical
Monographs, American Mathematical Society, Providence, RI, USA).
Schwartz, L. (1950). Theorie des Distributions (Hermann, Paris, France).
September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-index page 313

Index

< (less), Matalab operator, 20 consolidation2, function file, 125


<= (less or equal), Matalab operator, crank, function file, 86
20 Crank, historical note, 85
>= (greater or equal), Matalab
operator, 20 dam, function file, 235
>= (greater), Matalab operator, 20 dam 5p, function file, 254
== (equal), Matalab operator, 20 Darcy, historical note, 49
˜= (not equal), Matalab operator, 20 diag, Matlab command, 5
diffusion d, function file, 192
abs, Matlab command, 42 diffusion n, function file, 195
all, logical function, Matlab, 30 Dirac δ function, 184
anonymous function, Matlab, 26 Dirac, historical note, 184
any, logical function, Matlab, 31 Dirichlet boundary condition, 52
axis, Matlab command, 13 Dirichlet, historical note, 52
disp, Matlab command, 13
backward, function file, 93
beam c pn pn, function file, 297 error, Matlab command, 20
beam fx fr, function file, 282 errordlg, Matlab command, 20
beam fx fx uA, function file, 293 Euler, historical note, 39
beam fx pn, function file, 288 Euler–Bernoulli equation, 268
beam ov, function file, 303 Euler–Lagrange equation, 268
beam pn pn, function file, 285 euler e, function file, 60
Bessel, historical note, 158 euler edn, function file, 72
besselj, Matlab command, 158 euler el, function file, 92
break, Matlab command, 24 euler em, function file, 62
burgers, function file, 128 euler en, function file, 70
Burgers, historical note, 127 euler enm, function file, 71
euler i, function file, 81
central, function file, 43 eye, Matlab command, 4
central implicit, function file, 108
clc, Matlab command, 12 fem dd, function file, 174
consolidation1, function file, 121 fem dn, function file, 177

313
September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-index page 314

314 Matlab for Engineering

feval, Matlab command, 26 Lagrange, historical note, 169


Fick’s law, 55 laplace, function file, 229
Fick, historical note, 49 Laplace, historical note, 52
find, logical function, Matlab, 31 layers, function file, 77
five point, function file, 243 left division, Matlab, 10
five point f1, function file, 246 legend, Matlab command, 13, 21
for, Matlab structure, 10 lines c d 1, function file, 137
forward, function file, 41 lines c d 2, function file, 138
Fourier’s law, 49 lines heat1, function file, 123
Fourier, historical note, 49 linspace, Matlab command, 3
fplot, Matlab command, 27 load, Matlab command, 142
fprintf, Matlab command, 42 local function, Matlab, 25
ftbs, function file, 113 logical operators, Matlab, 27
ftfs, function file, 115
full, Matlab command, 7 mass, function file, 276
function file, Matlab, 15 max, Matlab command, 42

Gauss, historical note, 51 Neumann boundary condition, 52


gradient, Matlab command, 17 Neumann, historical note, 52
Green’s identities, 214 Newton, historical note, 53
Green, historical note, 214 Nicolson, historical note, 85
nonlinear, function file, 131
hermitian functions, 271 norm, Matlab command, 96
hold off, Matlab command, 122 num2str, Matlab command, 13
hold on, Matlab command, 121
homogeneous boundary condition, 53 ode15s, Matlab command, 132
Hooke, historical note, 165 ode45, Matlab command, 121
ones, Matlab command, 4
if-elseif-else, Matlab structure, 19
int, Matlab command, 155, 160 Péclet, historical note, 100
integral, Matlab command, 155 pause(s), Matlab command, 13
integral2, Matlab command, 160 pause, Matlab command, 15
integral 1, function file, 155 plot, Matlab command, 13
integral 2, function file, 156 Poisson, historical note, 214
integral 2D 1, function file, 160 pure Neumann problem, 179
integral 2D 2, function file, 205 pyramid, function file, 223
integral 2D 3, function file, 163 pyramid phi, function file, 225
integral 2D 4, function file, 163
integral 3, function file, 157 quiver, Matlab command, 18
integral 4, function file, 158
ismember, logical function, Matlab, Rectangle Rule, 153
32 rectangle, Matlab command, 18
isspace, Matlab command, 25 relational operators, Matlab, 20
repmat, Matlab command, 7
Kronecker δ function, 169 reshape, Matlab command, 5
Kronecker, historical note, 169 right division, Matlab, 9
September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-index page 315

Index 315

Robin boundary condition, 53 tic, Matlab command, 123


Robin, historical note, 53 title, Matlab command, 13
toc, Matlab command, 124
save, Matlab command, 141 transpose matrix, Matlab, 1
sheet pile wall, function file, 262 Trapezium Rule, 154
Simpson Rule, 154 triangulation plot1, function file, 217
Simpson, historical note, 154 triangulation plot2, function file, 218
simpson, function file, 156 type, Matlab command, 142
simpson2d, function file, 161
size, Matlab command, 29 upper, Matlab command, 23
spdiags, Matlab command, 6 upwind, function file, 101
spy, Matlab command, 7
sqr, function file, 15 variable1, function file, 134
Stefan condition, 57 Von Neumann criterium, 89
Stefan problem, 58 Von Neumann, historical note, 89
Stefan, historical note, 57
stiffness, function file, 276 wave d, function file, 200
stiffness s, function file, 306 wave n, function file, 203
strcat, Matlab command, 23 while, Matlab structure, 24
stress 1, function file, 181 whos, Matlab command, 7
stress 2, function file, 210
surf, Matlab command, 15 xlabel, Matlab command, 13
switch, Matlab structure, 22
sym, Matlab command, 156 ylabel, Matlab command, 13
symbol O, 40 Young, historical note, 165

Taylor, nota storica, 40 zeros, Matlab command, 4


test function, 167 zlabel, Matlab command, 15

You might also like