D'Acunto - Matlab For Engineering [2021]
D'Acunto - Matlab For Engineering [2021]
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.
Printed in Singapore
Preface
v
B1948 Governing Asia
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
vii
September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-FM page viii
Contents ix
Bibliography 311
Index 313
B1948 Governing Asia
Chapter 1
Function Files
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
1
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 2
rv = [10 11 12 13]
[4 − 5 16],
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 3
Function Files 3
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
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.
Function Files 5
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.
Function Files 7
produce
5 5 5 5
A= ,u = 5 5 5 , v = ,
5 5 5 5
22 2 22 67
, .
67 20 2 20
Of course, it results in
(A ∗ B) = B ∗ A ,
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
3.0000 −2.0000
.
2.0000 −1.0000
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch01 page 10
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.
Function Files 11
For loop
for variable = expression
code lines
end
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
∂ 2u ∂ 2 u
− 2 = 0, (1.2.2)
∂t2 ∂x
and verifies the initial-boundary conditions
[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
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
Example 1.2.2 The following script file produces 2D and 3D plots of the
function (Fig. 1.2.2),
% 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’);
Function
function [output] = name of function (input)
% comments
code lines
end
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.
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.
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
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.
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.
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.
As the first application, the heat flux function is suitably modified and
the problem outlined in Remark 1.3.2 is eliminated.
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.
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
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
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
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.
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
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
% 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
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
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
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 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.
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.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.
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
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.14 Type help sqr at the command line and press Enter.
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
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
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
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’);
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
Chapter 2
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.
1 Leonard Euler, a Swiss scientist, 1707–1783. He formulated the laws of solid and fluid
39
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 40
(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)
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
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.
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.
1
Exact
Forward
0.6
0.2
du/dx
-0.2
-0.6
-1
-4 -2 0 x 2 4
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.
1
Exact
Forward
0.6 Backward
Central
0.2
du/dx
-0.2
-0.6
-1
-4 -2 0 x 2 4
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.
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
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.
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
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
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
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.
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
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
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 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
6 Johann Friedrich Carl Gauss, a German scientist, 1777–1855. He was the greatest
u(x, 0) = ϕ(x), 0 ≤ x ≤ L.
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
that is reduced to
ut − αΔu + v · ∇u = f, (2.2.21)
J = −D∇C, (2.2.25)
Ct + ∇ · J = 0, ∀ x. (2.2.27)
Ct = ∇ · (D∇C), (2.2.28)
Ct = DΔC.
September 8, 2021 14:15 Matlab for Engineering 9in x 6in b4335-ch02 page 56
Ct = DΔC − ∇ · (Cv),
Ct − DΔC + v · ∇C + C∇ · v = 0, (2.2.30)
x = s(t).
t
a
x = s(t)
x = s(t)
x
LIQUID SOLID x
where the index s is related to the solid phase. At the liquid-solid interphase,
the temperature assumes the melting temperature value um ,
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
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
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
respectively. Indeed, the Problems (2.2.32), (2.2.40) and (2.2.41) have the
solution
u(s(t), t) = um , t ≥ 0,
As already outlined, Eq. (2.3.1) can be uniquely solved only when the initial-
boundary conditions are assigned
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
0 x L
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
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
% 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.
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
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
% 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
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
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
2e -3e 2e
e j=k
i=h
perturbed by round-off errors at the initial time, the stability analysis leads
to a discussion of the behavior of
uj = Auj−1 , (2.3.14)
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:
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
|uj+1
i | ≤ r(|uji+1 | + |uji−1 |) + |1 − 2r| |uji |
|uj+1
i | ≤ ||uj ||∞ , i = 1, . . . , n − 1,
max |uj+1
i | ≤ ||uj ||∞ ,
i
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
Since (Ut − αUxx )ji = fij , from (2.3.21), it follows the desired result.
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)
uj+1
0 = uj+1
1 + g1j+1 Δx, uj+1
n = uj+1 j+1
n−1 + g2 Δx. (2.3.27)
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)
uj+1
n = (1 − 2r)ujn + 2r(ujn−1 + g2j Δx) + Δtfnj . (2.3.32)
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
% 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
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
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
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
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
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
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)
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)
% 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
a1 x2 + c1 x + V, x < xh ,
U (x, 0) = (2.3.55)
a2 (x − L)2 + c2 (x − L), x > xh ,
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
0 x L
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
Buj+1 = uj + bj+1 ,
uj+1 = Auj + aj+1 , (A = B −1 , aj+1 = B −1 bj+1 ). (2.3.63)
t=1
0.114
Implicit
Exact
0.11
0.106
0.102
0 0.2 0.4 x 0.6 0.8 1
Consider the Robin problem for Eq. (2.3.57) with initial Condition
(2.3.58) and Robin boundary conditions
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
[1 + 2r(1 + h1 Δx)]uj+1
0 − 2ruj+1
1 = uj0 + 2rΔxg1j+1 + f0j+1 Δt. (2.3.68)
−2ruj+1 j+1
n−1 + [1 + 2r(1 + h2 Δx)]un = ujn + 2rΔxg2j+1 + fnj+1 Δt. (2.3.69)
where
Buj+1 = uj + bj+1 ,
0 x L
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
⎡ ⎤ ⎡
⎤⎡ ⎤
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 ,
Consider the first condition and apply the central approximation for t = tj
and t = tj+1
(1 + r + rΔxh1 )uj+1
0 − ruj+1
1
(1 + r + rΔxh2 )uj+1
n − ruj+1
n−1
(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.
⎡ ⎤⎡ 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
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
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
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
Finally, let us recall the Euler formulas that are frequently applied in
the following examples
uj+1
i = r(uji+1 + uji−1 ) + (1 − 2r)uji .
−1 ≤ 1 − 4r sin2 (βΔx/2) ≤ 1.
2r sin2 (βΔx/2) ≤ 1,
that is satisfied if
r ≤ 1/2.
−ruj+1 j+1
i−1 + (1 + 2r)ui − ruj+1 j
i+1 = ui .
ξ[1 + 2r − 2r cos(βΔx)] = 1,
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.
−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 .
The Von Neumann condition |ξ| ≤ 1 is satisfied for any value of r. The
Crank–Nicolson Method is unconditionally stable.
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 + |λ|Δt.
Wt − αWxx = 0. (2.3.95)
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.
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
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.
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.
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
u(x1 , x2 , x3 ) = x3 . (2.4.3)
Explain why the previous result does not imply sin(x) = 0 on [0, 2π].
∂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
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)
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.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
uj+1
i = r(uji+1 + uji−1 ) + (1 − 2r)uji , i = 1, . . . , n − 1, (2.4.7)
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
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)
Chapter 3
99
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch03 page 100
Ut + vUx = 0.
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
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
i = rαuji−1 + (1 − 2rα + sv)uji + (rα − sv)uji+1 . (3.1.5)
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
Indeed,
|uj+1
i | ≤ p||uj ||∞ + (1 − p − q)||uj ||∞ + q||uj ||∞ ≤ ||uj ||∞ ,
and therefore,
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
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
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
legend(’Upwind’,’Exact’,’Location’,’Best’);
fprintf( ’Maximum error = %g\n’,max(abs(U - u)))
end
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
−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
Hence,
−rαuj+1 j+1
i−1 + (2 + 2rα − sv)ui + (vs − rα)uj+1
i+1
where
r = Δt/(Δx)2 , s = Δt/Δx. (3.1.21)
|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
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
−puj+1 j+1
i−1 + (1 + p + q)ui − quj+1 j
i+1 = ui ,
Buj+1 = uj + bj+1 ,
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
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
−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 ,
−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 ).
t t
x x
Figure 3.1.4. Upwind Method for v > 0 (left) and v < 0 (right).
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)
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
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).
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
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
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)
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
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)
s|vij | ≤ 1, ∀ i, j, (3.1.45)
as it is
|uj+1
i | ≤ (1 − svij )|uji | + svij |uji−1 | ≤ ||uj ||∞ ⇒ ||uj+1 ||∞ ≤ ||uj ||∞ .
uj+1
i = (1 + svij )uji − svij uji+1 , (3.1.46)
The variable x is discretized and the variable t is not. Using the central
approximation for Uxx yields
0 L
x
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)
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
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.
H sand
z draining
boundary
L
clay
8
t = 0 years
t = 3 years
7
5
z
4
0
0 5 10 15 20 25 30 35 40
u
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
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
u̇ = Au + a. (3.2.16)
Equation (3.2.16) will be applied in the example below. The Dirichlet–
Neumann problem
q
sand
H
z draining
boundary
L clay
impervious
boundary
U (z, 0) = q, 0 ≤ z ≤ L, (3.2.19)
z 4
1 t = 0 years
t = 3 years
0
0 5 10 15 20 25 30 35 40
U
% 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
Ut + U Ux = αUxx , (3.2.22)
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
where p = α/(Δx)2 .
For the Burgers Eq. (3.2.22), System (3.2.24) is written as
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
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
Dw(n−1) = p∗(w(n−2)−2∗w(n−1)+g2(t))−q∗w(n−1)∗(g2(t)−w(n−2)).
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
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
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 )
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
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
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
1 − M Δt − 2rα ≥ 0, rα − M1 s/2 ≥ 0,
where
where
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
% 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)
2rαji ≤ 1, ∀ i, j. (3.2.43)
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
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
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
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
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
W = U exp(λt), (3.2.57)
Finally, note that the result (3.2.58) holds even when λ = λ(t), as shown
in Exercise 3.4.28.
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.
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.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
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
|ξ|2 = 1/{[1 + 4rα sin2 (βΔx/2) + 2vs sin2 (βΔx/2)]2 + v 2 s2 sin2 (βΔx)} < 1.
−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.
t
x < vt x > vt
(x, t) (x, t)
vt
t - x/v
=
x
x - vt x
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.
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 ],
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
t x < vt + L x > vt + L
(x, t) (x, t)
x
t + (L - x)/v
=
vt
+
L
x - vt x
Exercise 3.4.12 Write a listing, say ftbs ex3, that calls the fbfs function
and solves the following initial-boundary value problem
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
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).
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
Hint. See Example 2.3.14. Another way to introduce the initial condition
could be
phi = @(z) 0*z + q;
Exercise 3.4.16 Write a listing, say lines heat2, that applies the Method
of Lines to solve the following problem
U (x, 0) = x2 , 0 ≤ x ≤ L, (3.4.12)
U (x, 0) = x2 , 0 ≤ x ≤ L, (3.4.15)
U (x, t) = x2 + 2t + t2 .
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 ,
U (x, 0) = x2 , 0 ≤ x ≤ L, (3.4.21)
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.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.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.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
Chapter 4
153
September 8, 2021 14:34 Matlab for Engineering 9in x 6in b4335-ch04 page 154
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
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
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
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.
% 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
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.
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
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
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
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)
with gi given by
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
0.6
0.2
simpson2d
integral2
-0.2
0 1 2 3 4 5
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
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
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
{(Pi , Fi ), i = 1, . . . , N }.
Aσ(x+ −
h , t) − Aσ(xh , t) = −Fh , (4.2.7)
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
FB
A B A B
U (xA ) = 0, U (xB ) = 0.
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
provided by the FEM is in agreement with the weak form of such a principle.
The Principle of Virtual Work was introduced by Lagrange5.
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
Φi (x) = ai x + bi , (4.2.24)
Φ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
Φ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
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
n
u(x) = uj Φj (x), x ∈ [0, L], (4.2.28)
j=0
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
u(0) = u0 , u(L) = un ,
u 0 = U0 , u n = UL ,
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
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
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
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
L
+ F (x)Φi (x) dx, ∀Φi , i = 1, . . . , n. (4.2.45)
0
as
Φi (0) = 0, i = 1, . . . , n.
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
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
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.
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
Integrate the differential equation over (0, L) and use the boundary
conditions
L
U0 − UL = F (x)dx. (4.2.52)
0
U2 = U1 + c, (4.2.53)
F0 q FL
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
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 = ui Φi + ui+1 Φi+1 .
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
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
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
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
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.
7 Paul Adrien Maurice Dirac, a British scientist, 1902–1984. He was Professor at the
R0 Fh RL
xh
Figure 4.2.13. Fixed end-fixed end bar subjected to an axial concentrated force.
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
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
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
where
L
Fh Fh
fi = δ(x − xh )Φi (x) dx = Φi (xh ).
AE 0 AE
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
(4.2.70) simplifies to
2u1 /h − u2 /h = Fh /(AE),
−u1 /h + 2u2 /h = 0.
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
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
Fh
0 1 2 3
h h xh = 2h
L = 3h
Figure 4.2.16. Simple grid with xh = x2 .
and
⎧
⎨ 2u1 /h − u2 /h = 0,
⎪
−u1 /h + 2u2 /h − u3 /h = Fh /(AE),
⎪
⎩
−u2 /h + u3 /h = 0.
Solving the previous system yields
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 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
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,
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
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
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
gi (t) = 0, i = 2, . . . , n − 2. (4.3.15)
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
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
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
where
L L L
Mij = Φj Φi dx, Kij = αΦj Φi dx, fi (t) = F Φi )dx, i, j = 0, . . . , n,
0 0 0
xi+1
fi (t) = F Φi dx, i = 1, . . . , n − 1,
xi−1
Example 4.3.2 The following listing presents a function that applies the
FEM to solve the Neumann problem
U (x, 0) = 0, 0 ≤ x ≤ L, (4.3.31)
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
t =1
0
FEM
Exact
-0.2
-0.4
-0.6
0 0.5 x 1 1.5
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
n−1
u(x, t) = uj (t)Φj (x) + G1 (t)Φ0 (x) + G2 (t)Φn (x), (4.3.37)
j=1
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
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
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
gi (t) = 0, i = 2, . . . , n − 2. (4.3.46)
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
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
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
L
= F (x, t)Φi (x)dx + c2 G1 (t)Φi (0) + c2 G2 (t)Φi (L), ∀Φi , i = 0, . . . , n.
0
where
L L
Mij = Φj (x)Φi (x)dx, Kij = c2 Φj (x)Φi (x)dx, i, j = 0, . . . , n,
0 L
0
w = u̇,
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 .
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
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).
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.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
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
Assume that
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
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
q(x) = qA (L − x)/L.
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
R0 Fh RL
xh
Figure 4.4.4. Fixed end-fixed end bar subjected to an axial concentrated force.
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 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
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
Exercise 4.4.12 Write a function, say fem nd, that solves the Neumann–
Dirichlet problem
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
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 ).
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
The supports of Φ1 and Φ3 are the intervals [x0 , x2 ] and [x2 , x4 ], respec-
tively. Their intersection is void and M1,3 = 0.
U (x, 0) = x2 , 0 ≤ x ≤ L,
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,
Chapter 5
213
September 8, 2021 11:25 Matlab for Engineering 9in x 6in b4335-ch05 page 214
V = U1 − U2 .
Hence,
Therefore,
V = U1 − U2 = c, on Ω,
V = U1 − U2 = c, on Ω.
V = 0, on ∂Ω, (5.1.12)
and
V = U1 − U2 = c, on Ω,
where the constant c must be zero because of (5.1.12). The Robin problem
has a unique solution.
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
1
3
y
0
2
-1 1
4
3 3
2 2
y 1 1 x 0
0 0 0 1 x 2 3
29 30 31 32
25 28
21 24
17 20
13 16
9 35
5 34
1 2 3 4 33
3 (x3, y3)
y
T
functions on triangle T
The gradient of Φi
∂Φi ∂Φi
∇Φi (x, y) = , , (x, y) ∈ T, (5.2.7)
∂x ∂y
if and only if
ui = 0, i = 1, 2, 3. (5.2.10)
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
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
3 2
T6 1 T7 T3
T5 T1 T2 8
15
16 7
T1 T3
T2
% 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
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
0.8
0.6
Φ
0.4
0.2
0
4
3 3
2 2
y 1 1 x
0 0
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
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.
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 .
(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
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
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
% 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
∂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.
x/h + y/h − 2, on T3 ,
Φ3 =
x/h − y/h, on T4 .
y6 h
4h Φ2 (2h, y)dy = 4h (1 − y/h)dy = 2h2 , (5.2.38)
y2 0
y6 h 2h
4h Φ3 (2h, y)dy = 4h y/h dy + 4h (2 − y/h)dy = 4h2 , (5.2.39)
y2 0 h
u1 = h2 , u2 = 4h2 , u3 = 3h2 .
H2
H3 H1
L x
L1
L2
where
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
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
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
% 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
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
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
The previous finite-difference equation holds for all points within the
domain. Moreover, setting
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.
k=m
ui,k+1
k=0
i=0 x i=n
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
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
⎡ ⎤ ⎡ ⎤⎡ ⎤
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
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)
2
U
1
-1
1
2
0.5 1.5
1
y 0.5 x
0 0
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
where
⎡ ⎤
f1,k
⎢ f2,k ⎥
fk = (Δy)2 ⎢ ⎥
⎣ · ⎦ , k = 1, ..., m − 1.
fn−1,k
0
U
-1
-2
-3
6
10
4 8
6
y 2 4
2 x
0 0
H2
H3 H1
L x
L1
L2
where
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
k = m+1
k1
k=1
i=1 i1 i2 i = n+1
B1 h2 + h3 = 0. (5.3.25)
⎡ ⎤ ⎡ ⎤⎡ ⎤ ⎡ ⎤
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
⎡ ⎤ ⎡ ⎤
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
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
⎡ ⎤ ⎡ ⎤
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
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,
Ah + a = 0. (5.3.31)
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
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
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.
⎧
⎡ ⎤ ⎡ ⎤ ⎪ 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
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 Φ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.
3
14 T11 T6 1
15 T5
16
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
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
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
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
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
% 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
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);
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.
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
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
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
U(x, t) F(x, t)
F M(x1, t) M(x2, t)
x1 x2 x V(x1, t) V(x2, t)
Δx
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)
(x − x1 )(F − ρUtt − Vx ) + Mx − V = 0,
that simplifies to
V = Mx . (6.1.6)
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
A B A B x
FA FB MA MB
A B
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
N1 N2 N3
x x
x1 x2 x1 x2 xi xi+1
N4
h
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(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
A
A B B
x
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
if and only if
ui = 0, i = 1, . . . , 4. (6.1.27)
0 = u1 N1 (0) = u1 , 0 = u3 N3 (h) = u3 .
0 = u1 N1 (x) + u2 N2 (x) + u3 N3 (x) + u4 N4 (x), x ∈ [0, h].
e1 e1 e2 e2
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
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)
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
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
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
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
U (0) = 0, U (0) = 0,
(6.2.7)
M (L) = 0 ⇔ U (L) = 0, V (L) = p ⇔ EIU (L) = p.
where F1 and M1 are the unknown reactive force and moment. Considering
all that, System(6.2.4) gives the following four equations
The last two equations are a simple algebraic system with unknowns u3
and u4 . Solving it yields the unknowns
Insert these values into (6.2.11) to find the reactive force and moment
F1 = p, M1 = pL.
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
u = u 1 Φ1 + u 2 Φ2 + u 3 Φ3 + u 4 Φ4 + u 5 Φ5 + u 6 Φ6 , (6.2.15)
Ku = f , (6.2.19)
K= k K=
k
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:
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
Displacement
0
FEM
Exact
U -5
-10
0 1000 x 2000 3000
0 -15
0 1000 2000 3000 0 1000 2000 3000
x x
Figure 6.2.4. Deformed beam, shear force and the bending moment.
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
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
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:
% 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
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
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
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
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
ku = f − g, (6.2.32)
where
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
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
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
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.
{(xh , Fh ), h = 1, . . . , N }.
V (x+ −
h , t) − V (xh , t) = Fh , (6.3.2)
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
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)
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.
% 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
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
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
Displacement
0
FEM
-0.5 Exact
-1
U
-1.5
-2
V 0 M 5
FEM
Exact
-5000 0
0 4000 8000 0 4000 8000
x x
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
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
u4 N4,1 , x ∈ e1 ,
u= (6.3.14)
u4 N2,2 + u5 N3,2 + u6 N4,2 , x ∈ e2 ,
since
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,
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
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
-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).
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.
M1 M2 M1 M2
1 2 1 2 x
F1 F2
qM
x
F1 F2
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
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
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
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 (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
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
311
September 8, 2021 11:35 Matlab for Engineering 9in x 6in b4335-Bib page 312
Index
313
September 8, 2021 11:37 Matlab for Engineering 9in x 6in b4335-index page 314
Index 315