Professional Documents
Culture Documents
odes (v.2017.2)
139
Chapter 8
Using the equations derived in Se
tion 1.2, write a MATLAB s
ript to
al
ulate and plot the
displa
ement of an elasti
string having length L = 2 and longitudinally
onstant shear modulus
µ = 1, when a sinusoidal for
e density f (x) = sin(2πx/L) is applied. Assume homogeneous
Diri
hlet boundary
onditions (B.C.s) at the two end points.
Solution
It is possible to organize the numeri
al
ode in logi
ally distin
t 5 parts:
Problem variables denition In order to write a s
ript, it is su
ient to de
lare the variables
for the length (L) and the shear modulus (mu) in a le with extension .m . The for
e density
is not
onstant in this exer
ise but it is represented by a fun
tion: in order to keep the
problem parameters separated from the
ode, we dene fforce as an anonymous fun
tion:
fforce=@(x)sin(2*pi*x/L).
The number of dis
retization points in the
omputational grid is not provided in the text,
and we assume N=100 internal nodes (see (1.15)). The total number of nodes is therefore
N+2, and the number of intervals is N+1.
Computational grid generation The step between two adja
ent nodes of the grid is h=L/(
N+1). The
omputational grid is a ve
tor of N+2 linearly spa
ed values ranging from 0 to
L, whi
h
an be obtained using the linspace fun
tion, as in x=linspace(0,L,N+2), or the
olon operator, as in x=0:h:L. Both the approa
hes return a row ve
tor. The displa
ement
u will be a
olumn ve
tor, sin
e it will be
al
ulated as solution of a linear system; it is
therefore advisable to immediately take the transpose of the
omputational grid in order
to have both the ve
tors x and u as
olumn ve
tors.
141
142 CHAPTER 8. THE ELASTIC STRING MODEL
Matrix
onstru
tion The matrix indi
ated in equation (1.25) has N rows and N
olumns,
sin
e we are imposing Diri
hlet B.C.s in the two end nodes, therefore redu
ing the degrees
of freedom (d.o.f.) from N +2 to N.
MATLAB does not provide any spe
i
fun
tion for the dire
t
onstru
tion of tridiagonal
matri
es; however, it's very simple to obtain su
h a matrix using available fun
tions of
ommon use. A simple solution
onsists in a
ombination of the results returned by fun
-
tions eye and diag. Fun
tion eye
an be used to generate the main diagonal of A (whi
h
is indi
ated in MATLAB as diagonal 0). Fun
tion diag is then used twi
e to pla
e the
unitary elements on the two diagonals with indi
es −1 and +1, respe
tively. It has to be
noted that in a N xN matrix the number of elements on diagonals ±1 isN − 1 and for
this reason the rst argument supplied to diag is a
olumn ve
tor with N − 1 elements:
ones(N−1,1).
The nal multupli
ation by mu/(h*h) is not
omputationally e
ient, sin
e it also multi-
plies the null elements of A: it's mu
h more
onvenient to pre-multiply the ve
tors passed
to diag.
Sin
e the matrix is square and symmetri
, it is possible to generate only its upper (or
lower) triangular part and sum the obtained matrix to its transpose.
It is also possible to play with the indi
es of the elements of matrix A, in parti
ular using
the single index notation. The elements on the main diagonal of a N xN matrix are lo
ated
8.1. FINITE DIFFERENCES 143
The proposed approa
hes all involve the use of full matri
es: memory is allo
ated also
for the zero elements. For the elasti
string problem, the non-zero elements of A are just
N + 2(N − 1) and they s
ale linearly with the size of the
omputational grid, whereas
the memory required to store the full matrix A grows as N 2. The re
ommended approa
h
is therefore to use a sparse data stru
ture, where only non-zero elements are stored in
memory. MATLAB fun
tion spdiags pla
es the
olumns of the matrix provided as rst
argument on the diagonals spe
ied as se
ond input argument. Finally, the third and forth
input arguments indi
ate the number of rows and
olumns of the matrix. In this exer
ise
the se
ond argument will be [0, −1, 1]; the rst argument is therefore a matrix with 3
olumns: the rst one is a
olumn with values 2c, while the se
ond and third
olumns only
ontain c.
Fun
tion spdiags will try to pla
e B(:,1) on the main diagonal of the N ×N matrix A,
and will generate an error if the sizes are not
onsistent. It will then pla
e B(2:end,2) on
diagonal +1 of A and B(1:end−1,3) on diagonal −1 of A, again generating errors is sizes
are not
onsistent. In general, then, if the diagonal index k is positive, spdiags will
opy
the last N−k elements of the
orresponding input
olumn; on the
ontrary if k is negative,
spdiags will
opy the rst N−k elements of the
orresponding input
olumn. To further
larify this aspe
t, whi
h is very important e.g., when a non uniform shear modulus is
onsidered (see Problem 8.1.4),
onsider the example presented in Listing 8.8, where the
ve
tors of integers from 1 to 5 are pla
ed on diagonals -2, 0, 1 and 4 of the 5×5 matrix A.
On diagonal -2, the last 2 elements of v are dropped, while the rst elements are removed
when
onstru
ting diagonals above the main one. On diagonal 4, there is nally pla
e only
for the last element of v.
144 CHAPTER 8. THE ELASTIC STRING MODEL
>> v=(1:5).';
>> A=spdiags([v,v,v,v],[−2,0,1,4],5,5);» full(A)ans =1 2 0 0 50 2 3 0 01 0 3
4 00 2 0 4 50 0 3 0 5
For
ing terms ve
tor generation A
ording to equation (1.24), the applied for
e density
must be sampled only in the unknown nodes of the
omputational grid, whi
h
orrespond
in this
ase to the elements of ve
tor x with indi
es between 2 and N+1: it's not required
to evaluate the for
e in the node in position 1 (
orresponding to x = 0) and in the node in
position N+2 (
orresponding to x = L). For simpli
ity, we
an isolate the unknown nodes
in a new row ve
tor, as xUnknown=x(2:N+1) or xUnknown=x(2:end−1). With the latter
ommand we take advantage of the keyword end to indi
ate the index of the last element
of the ve
tor, thus in
luding all the elements from the se
ond to the se
ond to last.
Linear system solution Many numeri
al te
hniques
an be used to solve the linear system
Au = f , in this
ase we simply use the MATLAB ba
kslash operator to obtain a ve
tor u
with N elements, indi
ating the displa
ement of the elasti
string in the N unknown nodes.
u=A\f; %Solve the linear system Au=f, using MATLAB backslash operator
Solution representation To plot the displa
ement over the whole interval [0, L], it is ne
essary
to
omplete ve
tor u with the two displa
ements in x=0 and x = L, whi
h are both null
be
ause of the homogeneous Diri
hlet B.C.s. On
e the two elements have been added, both
x and u have the same size and
an be plotted using MATLAB plot fun
tion.
A
omplete version of the s
ript using a sparse matrix is reported in the Listing (8.12), while a
plot of the displa
ement is presented in Fig.8.1
0.15
0.1
Displacement [A.U.]
0.05
-0.05
-0.1
-0.15
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Position [A.U.]
Figure 8.1: Cal ulated displa ement of the elasti string (Exer ise 8.1.1)
4 fforce=@(x)sin(2*pi/L*x);
5 N=100;
6 %% Computational grid generation
7 h=L/(N+1);
8 x=linspace(0,L,N+2).';
9 %% Matrix construction
10 C=mu/(h*h);
11 e=C*ones(N,1);
12 B=[2*e, −e, −e];
13 A=spdiags(B,[0,+1,−1],N,N)
14 %% Forcing term vector generation
15 f=fforce(x(2:N+1));
16 %% Linear system solution
17 u=A\f;
18 %% Solution representation
19 u=[0; u; 0];
20 plot(x,u,'o');
21 xlabel('Position [A.U.]');
22 ylabel('Displacement [A.U.]');
23 grid('on');
Convert the s
ript presented in Listing (8.12) into a fun
tion taking as input the string length,
the value of the shear modulus, the for
e density and the number of internal nodes. The fun
tion
must return the ve
tor with the position of the equally spa
ed nodes of the
omputational grid
and the
orresponding
al
ulated displa
ements of the elasti
string.
146 CHAPTER 8. THE ELASTIC STRING MODEL
Solution
A MATLAB fun
tion represents a more exible solution with respe
t to the s
ript presented in
Listing (8.12), sin
e the problem parameters (in this
ase, the string length, the shear stress, the
for
e density, and the number of points in the grid) are provided as input to the fun
tion and
are not hard-
oded in the le anymore. Moreover, lo
al variables
reated in the fun
tion do not
overwrite variables with the same name already dened by other s
ript previously exe
uted or
dire
tly in the MATLAB prompt and their memory is automati
ally freed after the fun
tion is
exe
uted.
A
riti
al point in this exer
ise is represented by the for
e density
al
ulation. The string
length and the number of internal points of the
omputational grid are provided as input; the
new fun
tion will be responsible for the allo
ation and
al
ulation of the
omputational grid
ve
tor. As a side result, it's not possible to provide as input a ve
tor with the for
e density
evaluated on the grid, but a fun
tion handle must be passed to allow for sampling the for
e
density on the (internally generated)
omputational grid nodes.
The
reated fun
tion returns two
olumn ve
tors, the rst being the
oordinates of the nodes
of the
omputational grid, the se
ond being the
orresponding
omputed displa
ements.
1 function [x,u]=ElStringFinDiffDirichletHomo(L,mu,fforce,N)
2 %1D Poisson equation solved with Finite Differences Method
3 %assuming homogeneous Dirichlet B.C.s on the two end points.
4 %Input:
5 % L: string length
6 % N: number of discretization internal nodes
7 % mu: constant shear stress
8 % fforce: handle to a function returning the applied force density
9 %Output:
10 % x: vector of position of the nodes of the discretization grid
11 % u: vector of displacements evaluated on the discretization grid
12
13 %% Computational grid generation
14 h=L/(N+1);
15 x=linspace(0,L,N+2).';
16 %% Matrix construction
17 A=mu/(h*h)*(2*eye(N)−diag(ones(N−1,1),−1)−diag(ones(N−1,1),+1));
18 %% Forcing term vector generation
19 f=fforce(x(2:N+1));
20 %% Linear system solution
21 u=A\f;
22 %% B.C.s in x=0 and x=L
23 u=[0; u; 0];
Using the fun
tion des
ribed in Se
tion 8.1.2,
al
ulate the maximum displa
ement of a 10
m
long rubber string, with a 1 mm diameter, when a 100 g load is uniformly applied.
8.1. FINITE DIFFERENCES 147
Solution
The typi
al shear modulus for rubber is 6 × 105 Pa; expressing all the quantities in SI units the
string length is L = 0.1 m, the radius is r = 5×10
−4 m whereas the for
e density per unit volume,
2 3 2
pointing toward the −x dire
tion, is f = 0.1g/(2πr L) N/m , being g = 9.81 m/s the gravity
a
eleration.
Using the equations derived in Se
tion 1.6.1, write a MATLAB fun
tion to
al
ulate the displa
e-
ment of an elasti
string with length L
onsidering non uniform shear modulus µ(x) and for
e
density f (x). Assume non homogeneous Diri
hlet B.C. u(0) = g0 in x = 0 and non homogeneous
du
Neumann B.C. µ (L) = ψL in x = L.
dx
Using this fun
tion,
al
ulate and plot the displa
ement of an elasti
string with L = 2,
µ(x) = 2 + cos(2πx/L), f (x) = exp(−x), g0 = 1 and ψL = −4, dis
retizing the string with 101
uniform intervals.
Solution
The linear system obtained with the nite dieren
es approximation is dened in Eq.s (1.68)
and (1.69); the
orresponding MATLAB implementation is reported in Listing (8.14), whi
h also
explains the role of the input and output arguments.
1 function [x,u]=ElStringDiffFinNeumann(L,N,fmu,fforce,g0,flux)
2 %1D Poisson equation with Neumann B.C. solved with Finite Differences Method
3 %Input:
4 % L: string length
5 % N: number of discretization internal nodes
6 % fmu: handle to a function returning the shear stress as a function of
7 % the position
8 % fforce: handle to a function returning the applied force density as a
9 % function of the position
10 % g0: displacement in x=0 (Non−homogeneous Dirichlet B.C.)
11 % flux: flux in x=L (Non−homogeneous Neumann B.C.)
12 %Output:
13 % x: vector of position of the nodes of the discretization grid
14 % u: vector of displacements evaluated on the discretization grid
15
148 CHAPTER 8. THE ELASTIC STRING MODEL
With respe
t to the
ase with uniform shear stress, an additional ve
tor xFrac is here required,
with the position of the nodes of the semi-integer indi
es grid. We may then evaluate the fun
tion
handle fmu on the nodes of that grid to obtain the values of the terms µj±1/2 .
xFrac=linspace(h/2,L−h/2,N+1).';
mu=fmu(xFrac);
MainDiagonal=[mu(1:end−1)+mu(2:end); mu(end)]/h^2;
UpperDiagonal=mu/h^2;
LowerDiagonal=mu(2:end)/h^2;
A=spdiags([MainDiagonal,LowerDiagonal,UpperDiagonal], [0,−1,1],N+1,N+1);
The matrix is symmetri
, but the two ve
tors UpperDiagonal LowerDiagonal provided to
and
spdiags are not the same. This is due to the way the fun
tion spdiags operates, in parti
ular
when building the diagonal in position +1, only the elements of UpperDiagonal from from 2 to
N−1 are used (whi
h
orrespond to the elements from mu(2)/h^2 to mu(N+1)/h^2). In this way,
the element A(1, 2) is mu(2)/h^2 and A(N − 1, N ) is mu(N+1)/h^2, as expe
ted.
In order to solve the se
ond part of the exer
ise, we dene the elasti
string parameters and
all
ElStringFinDiffNeumann; the plot of the obtained displa
ement is reported in Fig.8.2. While
it's straightforward to
he
k that the solution satises the B.C. in x = 0, in order to verify
the
orre
t implementation of the Neumann B.C. we need to numeri
ally evaluate τ = µ du
dt as
des
ribed in (1.16); the MATLAB
ode is des
ribed in the se
ond part of Listing 8.17.
8.1. FINITE DIFFERENCES 149
Listing 8.17: Elasti
string displa
ement,
al
ulated with non homogeneous Diri
hlet B.C. in
x=0 and non homogeneous Neumann B.C. in x=L
L=1; %String length
N=100; %101 intervals−>100 internal nodes
fmu=@(x)2+cos(2*pi*x/L); %Shear modulus
fforce=@(x)exp(−x) %Force density
g0=1; %Non−homogeneous Dirichlet condition in x=0
psiL=−4; %Non−homogeneous Neumann condition in x=L
[x,u]=ElStringFinDiffNeumann(L,N,fmu,fforce,g0,psiL);
%% Solution representation
figure; %Open a new window
plot(x,u,'o'); %Plot displacement vs. position of the grid nodes
%% plot mu du/dt to check the Neumann B.C.
h=x(2)−x(1); %Spatial step used by ElStringFinDiffNeumann
xInterm=x(1:end−1)+h/2; %Semi−integer nodes for the shear modulus
tau=diff(u)/h.*fmu(xInterm);%1st order derivative, multiplied by shear stress
figure; %Open a new window
plot(xInterm,tau,'o'); %Plot tau vs. position of the semi−integer points
1 -3.3
-3.4
0.5
-3.5
Displacement [A.U.]
µ du\dt [A.U.]
0
-3.6
-3.7
-0.5
-3.8
-1
-3.9
-1.5 -4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Position [A.U.] Position [A.U.]
Figure 8.2: Cal
ulated displa
ement of the Figure 8.3: Cal
ulated derivative of the
elasti
string (Exer
ise 8.1.4) elasti
string displa
ement (Exer
ise 8.1.4)
2
d u
= −8e−4x (2x − 1), x ∈ [0, 1] (8.1)
dx2
with Homogeneous Diri
hlet boundary
onditions using the Finite Dieren
e method dis
retizing
the interval [0, 1] in N = [10, 100, 1000, 10000] intervals. Compare the numeri
al results un with
the exa
t solution
ue = x(exp−4x − exp−4 )
al
ulating the lo
al error δk = |un,k − ue,k | asso
iated to the node xk , k ∈ [0, N ] of the grid.
150 CHAPTER 8. THE ELASTIC STRING MODEL
Dis
uss the behavior of the integral error estimated using the trapezoidal form I = (δ0 +
P N −1
2 k=1 δk + δN )/N .
Solution
To solve the proposed problem we
an use the function des
ribed in Se
tion 8.1.2, paying
attention to the fa
t that the length is unitary, and so is the shear stress. The external for
e
density is fforce=@(x)−8*exp(−4*x).*(2*x−1).
A for N
an be
loop over the elements of ve
tor used to
all the fun
tion 4 times, evaluating
then the exa
t solution in the grid nodes and nally
al
ulating the lo
al and integral errors.
Attention must be paid to the fa
t that the fun
tion ElStringFinDiffDirichletHomo a
epts
as se
ond input the number of internal nodes, whi
h is the number of intervals minus 1.
The
omprehensive s
ript used to solve the problem is reported in Listing (8.18), while a
graphi
omparison of the analyti
and numeri
al solution obtained with 10 intervals is presented
in Fig.8.4.
After the exe
ution of the s
ript, ve
tor I
ontains the values listed in Table 8.1. In
reasing
by an order of magnitude the number of intervals, the error in the numeri
al solution redu
es by
two orders of magnitude, thus
onrming that the error behaves as O(h2 ), as dis
ussed in 1.5.
Table 8.1: Integral error as a fun tion of the number of intervals for Problem 8.1.5
N I log10 I
10 4.6546e-03 -2.3321
100 4.7850e-05 -4.3201
1000 4.7863e-07 -6.3200
10000 4.7847e-09 -8.3201
8.1.6 Eigensolutions
Plot the eigenve
tors asso
iated to the m smallest eigenvalues of an elasti
string with unitary
length L, shear modulus µ and density ρ,
al
ulated with the 1D Finite dieren
es method over
8.1. FINITE DIFFERENCES 151
0.09
Numerical solution
0.08 Analytic solution
0.07
0.06
Displacement [A.U]
0.05
0.04
0.03
0.02
0.01
0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Position [A.U]
Figure 8.4: Solution of the dierential problem 8.1 with homogeneous Diri
hlet boundary
ondi-
tions. Analyti
solution (red
urve) and numeri
al solution obtained using the Finite Dieren
e
method on a uniform grid with N = 10 intervals (blue
urve) (Problem 8.1.5)
a grid with 250 uniform intervals. Assume homogeneous Diri hlet B.C.s at the end points.
Solution
A
ording to (1.29), the exer
ise
an be
ompleted
al
ulating the eigenve
tors of the algebrai
eigenvalue problem (1.28), where A is the matrix built in Listing 8.7. In MATLAB, the fun
tion
eig returns all the eigenvalues and eigenve
tors of matrix A supplied as input; in this
ase
it's preferable to use fun
tion eigs, whi
h
al
ulates only the required eigenvalues. The latter
fun
tion a
epts as se
ond argument the number of eigenvalues and eigenve
tors to
al
ulate
and as third argument the type of eigenvalues: the string sa asks for the smallest algebrai
eigenvalues. Attention must be paid to the fa
t that, while the eigenvalues returned by eigs are
sorted a
ording to the type argument, the results returned by eig are not guaranteed to be
sorted in any parti
ular order.
end
grid on; %show the axes grid
xlabel('Position [A.U.]'); %add x axes title
ylabel('Displacement [A.U.]'); %and y axes title
In Listing 8.19, matrix D ∈ Rm,m is diagonal and
ontains on the main diagonal the
al
ulated
eigenvalues; the
olumns of V ∈ R
N,m are the eigenve
tors asso
iated to the eigenvalues on D
The eigenve tors orresponding to the 3 smallest eigenvalues are presented in Fig.8.5.
0.1
0.08
0.06
0.04
Displacement [A.U.]
0.02
-0.02
-0.04
-0.06
-0.08
-0.1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Position [A.U.]
Figure 8.5: Cal
ulated eigenve
tors asso
iated to the 3 smallest eigenvalues of an elasti
string
(Exer
ise 8.1.6)
Cal
ulate the 2-norm
ondition number of the matrix obtained from the dis
retization of the
elasti
string problem using Finite Dieren
es, rst as ratio λh,N /λh,1 of the exa
t eigenvalues
obtained in Eq. (1.32) and
ompare the results with the estimate of the 1-norm
ondition
number returned by MATLAB fun
tion condest. Plot the results versus the re
ipro
al of the
dis
retization step.
Consider a string with unitary length and shear stress, with homogeneous Diri
hlet B.C.s and
a number of internal nodes ranging from 200 to 2000 with step 250.
Solution
Sin
e the shear modulus is
onstant, it is simpler to start from the
ode presented in Listing
8.12 and adapt it in order to evaluate the
ondition number of A using fun
tions condest. After
the generation of the ve
tor Nv with the requested number of internal nodes, it's possible to
8.1. FINITE DIFFERENCES 153
al
ulate the
orresponding dis
retization steps using the ve
torized expression h=L./(Nv+1).
The mathemati
ally exa
t
ondition number
an be
al
ulated as c2Math=(1−cos(Nv*pi./(Nv
+1)))./(1−cos(pi./(Nv+1))). A for loop is then used to build matrix A having the proper size
and estimate the
orresponding 1-norm
ondition number. The result is stored in ve
tor c2Norm
whi
h is preallo
ated outside the for loop: while MATLAB
an dynami
ally resize ve
tors
and matri
es, it's generally a good programming pra
ti
e to preallo
ate them (e.g., using zeros
fun
tion) sin
e this operation
an signi
antly improve performan
es in
omputationally
riti
al
appli
ations. Remark that, sin
e matrix A is sparse, fun
tioncond
annot be used in this
ase,
and only the 1-norm
ondition number
an be
al
ulated using condest.
The resulting
ode is presented in Listing 8.20; the fun
tion is
alled in Listing 8.21 and the
results are used to generate the plot presented in Fig. 8.6. The
ondition number returned by
condest is always larger than the exa
tly
al
ulated 2-norm.
[h,cMath, cEst]=ElStringFinDiffCondition;
figure; %open a new figure (window)
Nv=1./h−1; %vector with the numbers of internal nodes
plot(Nv,cMath,'r*'); %plot cMath in red, with * as marker
hold on; %keep current plot
plot(Nv,cEst,'bo'); %plot cEst in blue, with o as marker
grid; %add a grid
legend('cMath (2−norm value)','cEst (1−norm estimate)'); %insert the legend
xlabel('Number of internal nodes'); %x−axes label
154 CHAPTER 8. THE ELASTIC STRING MODEL
106
3.5
c2Math (2-norm value)
c1Est (1-norm estimate)
3
2.5
Condition number
2
1.5
0.5
0
0 500 1000 1500 2000 2500
Number of internal nodes
Figure 8.6: Condition number of the matrix obtained applying the Finite Dieren
es method to
the elasti
string problem, with Homogeneous Diri
hlet B.C.s (Exer
ise 8.1.7)
Following the equations derived in Se
tion 1.4, write a MATLAB fun
tion to
al
ulate the dis-
pla
ement of an elasti
string with shear modulus µ(x), when a for
e f (x) is applied. The
omputational grid is provided in input as a ve
tor x. Assume non homogeneous Diri
hlet B.C.s
g0 in x=0 and gL = in x = L.
Solution
A
ording to the exer
ise text, the function re
eives 5 input arguments and returns one output
argument
1 function u=ElStringFinElemDirichlet(x,fmu,fx,g0,gL)
2 %Calculate the displacement of the elastic string with the finite elements
3 %Non homogeneous Dirichlet B.C.s are assumed in the external nodes
4 %Input :
5 % x : column vector of grid nodes, from 0 to L
6 % fmu: shear modulus as a function of the position
7 % fx : force density as a function of the position
8 % g0 : displacement in x=0 (Dirichlet B.C.)
9 % gL : displacement in x=L (Dirichlet B.C.)
10 %Output:
11 % u : displacement vector.
With respe
t to the Finite Dieren
e method dis
ussed in 8.1.1, in this
ase the dis
retization
step is not ne
essarily
onstant over the whole
onsidered interval: it is a ve
tor and its elements
must be
al
ulated from the provided
omputational grid x as h=diff(x). It's then simple to
8.2. FINITE ELEMENTS 155
implement in a ve
torized way the
al
ulations indi
ated in Se
tion 1.4, paying attention to the
fa
t that the .* and ./ operators must be used every time an element by element produ
t or
division o
urs:
The for
ing terms ve
tor,
al
ulated as indi
ated in 1.59, is the average of adja
ent elements
of ve
tor fh and
an be
al
ulated expli
itly
or, equivalently, as sliding average of fh using a two elements window,
al
ulated taking the
onvolution of fh with a two-elements ve
tor with weight 0.5
The rst and last elements of ve
tor f must be modied in order to take into a
ount the
non-homogeneous Diri
hlet B.C.s
The
onstru
tion of matrix A and the solution of the obtained linear system, with the nal
addition of the Diri
hlet B.C.s, both follow the same logi
dis
ussed in Listing 8.16.
A=spdiags(muh(1:end−1)+muh(2:end),0,N,N)...
−spdiags(muh(2:end−1),−1,N,N)...
−spdiags(muh,1,N,N);
u=[g0; A\f;gL]; %calculate solution and pad with B.C.s
In order to test the
ode, it's possible to
reate a non uniform grid with 100 nodes in the [0, 1]
interval using fun
tion rand to generate a
olumn ve
tor with 98 samples uniformly distributed
in the (0, 1) interval and sorting it. It's then important to append 0 and 1 as rst and last
elements, respe
tively
x=sort([0;rand(98,1);1]);
The solution obtained for a unitary shear modulus, a for
e density set to −1, and assuming
u(0) = 1 and u(L) = 2 is reported in Figure 8.7.
The solution plot is presented in Fig. 8.7; the presen
e of a non-uniform grid is
learly visible
from the
ir
ular markers
156 CHAPTER 8. THE ELASTIC STRING MODEL
1.9
1.8
1.7
Displacement [A.U.]
1.6
1.5
1.4
1.3
1.2
1.1
1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Position [A.U.]
Figure 8.7: Cal ulated displa ement of the elasti string (Exer ise 8.2.1)
Write a MATLAB fun
tion to
al
ulate, with the nite elements te
hnique, the displa
ement of
an elasti
string with shear modulus µ(x), when a for
e f (x) is applied. The
omputational grid
x. Assume a
is provided in input as a ve
tor non homogeneous Diri
hlet B.C g0 in x=0 and a
du
Robin B.C. µ (L) = α(u − uinf ) in x = L.
dx
Solution
With respe
t to exer
ise 8.2.1, we have an additional degree of freedom in x = L; the total
number of unknown nodes is therefore N + 1, with N number of internal nodes.
With the applied B.C.s, the stiness matrix A ∈ RN +1,N +1 is tridiagonal and symmetri
, with
entries: µ
j−1/2 µj+1/2
+ if k=j≤N ,
hj hj+1
µN +1/2
+α if k =j =N +1,
hN +1
µ
ajk = − j−1/2 if k =j−1,
hj
µj+1/2
− if k =j+1,
hj+1
0 otherwise .
1
2 f (x1/2 )h1 + f (x3/2 )h2 + µ1/2 /h1 g0 if j=1,
1
fj = 2 f (xj−1/2 )hj + f (xj+1/2 )hj+1 if j ∈ [2, N ] ,
1
2 f (xN +1/2 )hN +1 + αu∞ if j =N +1.
The function will re
eive as input 6 arguments and return one output argument; the
ode is
presented in Listing 8.22.
function u=ElStringFinElemRobin(x,fmu,fx,g0,alpha,uInf)
%Calculate the displacement of the elastic string with the finite elements
%Non homogeneous Dirichlet B.C is assumed in x=0, while Robin B.C. is imposed in
x=L
%Input :
% x : column vector of grid nodes, from 0 to L
% fmu : shear modulus as a function of the position
% fx : force density as a function of the position
% g0 : solution in x=0
% uInf, alpha: coefficients for the Robin B.C. in x=L
%Output :
% u : displacements vector
A=spdiags(muh(1:end−1)+muh(2:end),0,N+1,N+1)...
−spdiags(muh(2:end−1),−1,N+1,N+1)−spdiags(muh,1,N+1,N+1);
A(end,end)=muh(end)+alpha; %Robin B.C. in x=L (part 2)
u=[g0; A\f]; %homogeneous Dirichlet B.C. in the 1st node
8.2.3 An example
Using the fun
tion des
ribed in Listing 8.22, plot the displa
ement of a 40
m long rubber string,
with a 0.4 MPa shear modulus and a 2 mm diameter when a 75 g load is applied in the interval
[17.5, 22.5℄
m. The displa
ement in x = 0 is 1
m, while on the other end point the B.C.
du
− 10u = 0 is applied. Dis
retize the string length using a
omputational grid with a step
dx
h = 0.1 mm if x ∈ [15, 25]
m and h = 1 mm elsewhere.
Solution
Expressing all the problem quantities in SI units, the string length is L = 0.4 m, the radius is
r = 10−3 m; in x=0 the displa
ement is g0 = 0.01 m. In order to properly identify the input
du
arguments α and u∞ , the B.C. in x = L must be written as µ = 4 106 (u − 0); therefore
dx
α= 4 106 Pa/m and u∞ = 0.
The dis
retization steps are h1 = 10−3 m and h2 = 10−4 m: the
omputational grid
an be
al
ulated using the
olon operator (:) and
on
atenating three ve
tors dis
retizing the intervals
158 CHAPTER 8. THE ELASTIC STRING MODEL
[0, 15)
m, [15, 25]
m and (25, 30]
m; the obtained ve
tor is the transposed to obtain a
olumn
ve
tor.
Finally, the for
e density in the interval [17.5, 22.5]
m is f = 0.05g/(2πr 2 L) N/m3 , being g =
9.81 m/s2 the gravity a
eleration pointing toward the −x dire
tion. The fun
tion representing
the for
e density
an be written in a dierent le, as
function f=force(x)
%Force density applied to the elastic string
%Input:
% x: vector of nodes to compute the force in
%Ouput:
% f: vector of force densities
val=−0.05*9.81/(2*pi*1e−3^2*0.40) %applied force density [n/m^3]
xmin=0.175; %minimum position for applied force
xmax=0.225; %maximum position for applied force
f=zeros(size(x)); %Preallocate f to the same size as x
for k=1:length(x) %for each node
if x(k)>=xmin & x(k)<=xmax %if the node is in the [xmin, xmax] interval
f(k)=val; %assign val to the force. Otherwise f(k) is 0
end
end
fmu=@(x)4e5*ones(size(x));
u=ElStringFinElemRobin(x,fmu,@force,u0,alpha,uInf)
Sin
e force is dened in a .m le, the @ operator is required to obtain the handle to the fun
tion
and pass it as input to ElStringFinElemRobin; on the other hand, fmu is already a handle to
an anonymous fun
tion and
an be dire
tly passed as input argument.
In a mu
h more
ompa
t way, it's possible to dene the for
e density fun
tion as an anonymous
fun
tion:
The boolean operator & applied to the inequalities in the new denition of anonymous fun
tion
fforce returns true/1 if a node is lying in the spe
ied interval, and false/0 otherwise; they are
a simple and ee
tive repla
ement for the for loop with if statement used in the .m fun
tion. The
for
e density and the
orresponding displa
ement are reported in Fig. 8.8 and 8.9, respe
tively.
8.2. FINITE ELEMENTS 159
105
1
0
0.9
-0.5 0.8
0.7
Force density [N/m 3 ]
Displacement [cm]
-1
0.6
-1.5 0.5
0.4
-2
0.3
-2.5 0.2
0.1
-3
0
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4
Position [m] Position [m]
Figure 8.8: For
e density applied to the Figure 8.9: Cal
ulated displa
ement of the
elasti
string (Exer
ise 8.2.3) elasti
string (Exer
ise 8.2.3)
160 CHAPTER 8. THE ELASTIC STRING MODEL
Chapter 9
Write a MATLAB fun
tion to solve the linear system Ax = b, with A ∈ RN,N upper triangular
matrix, b∈ RN,1 using the ba
kward substitution algorithm:
bN
xN =
AN,N
N (9.1)
!
1 X
xk = bk − Ak,m xm , k = N − 1, . . . , 1.
Ak,k
k=i+1
Solution
The problem requires to implement a simple fun
tion with two inputs (A and b), returning a
single output x.
function x=Backward(A,b)
It may be
onvenient to save in a variable (N) the number of elements of b (whi
h is equal to the
number of rows and
olumns of A), and to preallo
ate x as
olumn ve
tor with N rows:
A for loop is required to evaluate the solution, starting from element xN . A test is required on
the diagonal elements Ak,k : if 0, then the upper triangular matrix is singular.
161
162CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
The previous
ode
an be dramati
ally simplied and ve
torized. First, the
he
k for the matrix
singularity
an be ve
torized verifying, outside of the loop on k, if any diagonal element is 0.
Then, the inner loop over m
an be repla
ed by the s
alar produ
t between the ve
tors A(k,k+1:N
) and x(k+1:N). The rst
al
ulated value of the solution (x(N)) must in this
ase be
al
ulated
separately, sin
e A(N,N+1:N) and x(N+1:N) would result in an empty matrix and generate an
error during the subsequent
al
ulations. The resulting fun
tion is presented in Listing 9.1.
1 function x=Backward(A,b)
2 N=size(b,1); %Number of rows of b
3 x=zeros(N,1); %Preallocation of solution vector x
4 if any(diag(A))==0 %Singular matrix
5 error('matrix A is singular');
6 end
7 x(N)=b(N)/A(N,N);
8 for k=N−1:−1:1 %Loop over the rows, with step −1
9 tot=A(k,k+1:N)*x(k+1:N);
10 x(k)=(b(k)−tot)/A(k,k);
11 end
A similar algorithm ( forward substitution )
an be used for the solution of linear systems with
lower triangular matri
es: in this
ase, however, the rst element to be evaluated is x(1) and
the loop ranges for k=2:N; the
orresponding fun
tion is presented in Listing 9.2.
1 function x=Forward(A,b)
2 N=size(b,1); %Number of rows of b
3 x=zeros(N,1); %Preallocation of solution vector x
4 if any(diag(A))==0 %Singular matrix
5 error('matrix A is singular');
6 end
7 x(1)=b(1)/A(1,1);
8 for k=2:N %Loop over the rows, with step +1
9 tot=A(k,1:k−1)*x(1:k−1);
10 x(k)=(b(k)−tot)/A(k,k);
11 end
9.1. NON ITERATIVE METHODS 163
The number of arithmeti
operations required by the forward and ba
kward substitution algo-
2
rithms to solve the linear system is approximately N /2.
Solution
The requested de
omposition
an be seen as the matrix formulation of the Gauss elimination
algorithm, involving a sequen
e of elementary row
ombinations to transform the linear system
Ax = b into an equivalent one U x = b̃ with U upper triangular matrix.
The algorithm involves a loop over k = 1, . . . , N − 1. At ea
h iteration, all the elements An,k
with n>k are set to zero trough a linear
ombination of row k and row n:
The resulting upper triangular part A is then saved into matrix U . The main diagonal of L is
omposed by 1s, Ak,p /Ak,k are stored in Lk,p; these values
an be temporarily
while the terms
stored as Ak,p and then moved to L.
The fa
torization algorithm
ould not pro
eed if, at iteration k , Ak,k = 0 be
ause of the ratio
Ak,p /Ak,k . If this
ondition is veried, row k is ex
hanged with another row m > k having
Am,k 6= 0; to in
rease the algorithm stability the row m > k is
hosen for whi
h |Am,k | is
maximum. In the proposed implementation, to additionally in
rease the stability, the partial
pivoting te
hnique is used, whi
h performs the swap if |Am,k | > |Ak,k even if Ak,k 6= 0. Matrix
P keeps tra
k of these
hanges: it is initially the identity matrix, and every time a swap o
urs
between rows k and m of A, the
orresponding rows of P are also swapped.
The algorithm su
essfully produ
es a fa
torization if A is not singular, of if its rank is N − 1;
in the latter
ase the last row of U is identi
ally null.
The implementation proposed in Listing 9.3 overwrites matrix A during the fa
torization pro-
ess. Just before the end of the fun
tion, the multipliers stored in the lower triangular part of
A are
opied into L, and 1s are pla
ed on its main diagonal. In a similar way, the elements in
the upper triangular part of A are
opied into matrix U. In order to avoid swapping the rows of
P(whi
h
ontains only one non zero element per row), we dene a ve
tor
ounting from 1 to N,
and swap its elements: it's then easy to build matrix P after matrix P has been fa
torized. For
simpli
ity, an error is generated as soon as the matrix is dis
overed to be singular.
1 function [L,U,P]=LuFactor(A)
2 %PA=LU factorization of matrix A
3 %Input:
4 % A: square matrix to be factorized
5 %Output:
6 % L: lower triangular matrix with the multipliers and 1 on main diagonal
7 % U: upper triangular matrix obtained from linear combinations of A rows
8 % P: permutation matrix
9 N=size(A,1); %Size of A
164CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
The arithmeti
operations required for the
al
ulation of the P A = LU fa
torization are approx-
3
imately 2N /3. In MATLAB, the P A = LU de
omposition
an be performed using fun
tion
lu.
Solve the linear system Ax = b, with A=magic(5) and b=(1:5).',
al
ulating the P A = LU
fa
torization of matrix A and then
alling the Forward and Backward fun
tions.
Solution
The
orre
tness of the algorithms
an be
he
ked veryng that x is the solution of the linear
system, i.e., that Ax−b is zero: due to the nite pre
ision of the ma
hine arithmeti
, max(abs(A
*x−b)) returns 8.8818e−16. This result is very
lose to the ma
hine epsilon for the numeri
al
values we are dealing with, sin
e eps(1) is 2.2204e−16.
Write a MATLAB fun
tion to solve the linear system Ax = b, with A ∈ RN,N tridiagonal positive
denite matrix, b ∈ RN,1 . The fun
tion will a
ept as inputs the main diagonal of A (d), its
upper diagonal (u), its lower diagonal (l ), the
onstant terms ve
tor (b), and will return the
solution ve
tor (x).
Solution
Sin
e matrix A is tridiagonal, the only element to be updated in loop over A rows is dk . On
e the
matrix has been triangularized in linear time, the ba
kward algorithm is applied, whi
h simplies
with respe
t to Eq. (9.1) and redu
es to
bN
xN =
dN
(9.2)
bk − uk xk+1
xk = , k = N − 1, N − 2, . . . , 1.
dk
1 function x=Thomas(d,u,l,b)
2 %Thomas algorithm for the solution of a tridiagonal linear system Ax=b
3 %Input:
4 % d,u,l: main diagonal, upper diagonal and lower diagonals of matrix A
5 % b : constant terms column vector
6 %Output:
7 % x : column vector with the solution of the linear system
8
9 %% Simplified Gauss elimination
10 N=length(b); %number of rows in b
11 for k=1:N−1 %loop over columns
12 if d(k)==0 %is the matrix singular?
13 error('matrix is singular');
14 end
15 m=−l(k)/d(k); %multiplier
16 d(k+1)=d(k+1)+m*u(k); %combination of rows k+1 and k
17 b(k+1)=b(k+1)+m*b(k); %combination of rows k+1 and k
18 end
19 if d(N)==0
20 error('matrix is singular');
21 end
22 %% Backward substitution algorithm
23 x(N)=b(N)/d(N);
166CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
24 for k=N−1:−1:1
25 x(k)=(b(k)−u(k)*u(k+1))/d(k);
26 end
The arithmeti
operations required for the solution of the linear system with a tridiagonal A
matrix s
ale linearly with the number of rows/
olumns of A. The proposed algorithm
an be
optimized in terms of memory o
upation if A is symmetri
: in this
ase, only ve
tors d and u
are required.
Write a MATLAB fun
tion implementing the Cholesky fa
torization of a symmetri
positive
semi-denite matrix A ∈ RN ×N in the produ
t of the lower triangular matrix L ∈ RN ×N and its
transpose: A= LLt . Use the following relations for k = 1, . . . , N :
v
u
u k−1
X
Lk,k = tA k,k − L2k,n (9.3a)
n=1
k−1
!
1 X
Lm,k = Am,k − Lm,n Lk,n , m > k. (9.3b)
Lk,k
n=1
The fun tion should re eive A as input and return the lower triangular matrix L in output.
Solution
As for the P A = LU fa
torization,
al
ulations are performed dire
tly on matrix A, and the
useful data is
opied into matrix L just at the end. The algorithm only uses the elements in the
lower triangular part of A, and no
he
k is performed in order to verify that A is really symmetri
.
If the argument of the square root in (9.3a) is negative, then the matrix is not positive denite,
and an error is generated.
1 function L=CholFactor(A)
2 %Cholecky factorization of matrix symmetric positive definte matrix A
3 %Input:
4 % A: square matrix, symmteric and positive definite. No check is
5 % perfomed to ensure that the matrix is symmetric; only its lower
6 % triangular part is used in the decomposition
7 %Output:
8 % L: lower triangular matrix satisfying A=L*L.'
9 N=size(A,1); %Number of rows
10 for k=1:N
11 t=A(k,k)−sum(A(k,1:k−1).^2);%This term must be >=0 is A is pos. definite
12 if t<0
13 error('matrix A is not positive definite');
14 end
15 A(k,k)=sqrt(t); %Diagonal element
16 for m=k+1:N %Non diagonal elements
9.1. NON ITERATIVE METHODS 167
17 A(m,k)=(A(m,k)−sum(A(m,1:k−1).*A(k,1:k−1)))/A(k,k);
18 end
19 end
20 L=tril(A); %Extract the lower traingular part of A
The arithmeti
operations required for the
al
ulation of the Cholesky fa
torization is approxi-
mately N 3 /3, i.e., half the
ost of the LU de
omposition. In MATLAB, this de
omposition
an
be performed using fun
tion chol; the
ommand R=chol(A) returns an upper triangular matrix
R equal to L.'; the
ommand L=chol(A,'lower') returns the lower triangular matrix L.
It has to be noted that, if matrix A is symmetri
but not semi-denite positive, then the
Cholesky fa
torization
annot be used. In this
ases, the LDLT de
omposition
an be employed:
A = LDLt , with L unitary lower triangular matrix and D diagonal matrix. The
omputational
ost of this fa
torization is still approximately N 3 /3; this fa
torization
an be obtained using
MATLAB fun
tion ldl.
In MATLAB, the ba
kslash operator provides a
onvenient way to solve the linear system Ax = b
simply using the syntax x=A\b. Des
ribe the algorithms used by the \ operator when A is a
square real matrix.
Solution
MATLAB \ operator maps dire
tly into a
all to fun
tion mldivide, whi
h uses a dierent
de
ision ow is matrix A is full, or sparse. If matrix A is full, then the solution of the linear
system requires the following tests:
If A is symmetri
if all the elements on the main diagonal are positive, MATLAB tries a Cholesky
de
omposition;
if not all the elements on the main diagonal are positive, or the former Cholesky
de
omposition failed, the A = LDLT de
omposition is used, with L lower triangular
matrix and D diagonal matrix.
If A is not symmetri
nally, is all the other options fail, then the P A = LU fa
torization is used.
168CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
The de
ision ow used to
hoi
e the method to apply in order to solve a linear system when A
is full is presented in Fig. 9.1.
If A is sparse, then the algorithm applied depends on the matrix band density.
The lower bandwidth k1 and the upper bandwidth k2 of matrix A ∈ RN,N are the smallest
non negative integers su
h that
Write a fun
tion to solve the linear system Ax = b, with A ∈ RN ×N semi-positive denite matrix,
using the gradient des
ent method des
ribed in (3.20), a
epting in input a maximum number
of iterations itermax, a relative toleran
e toll and as initial guess x0 .
The fun
tion must return the last estimate of the solution xk , a ag indi
ating
onvergen
e
(0) or failure (1), the number of performed iterations, and the norm of the residual Axk − b.
Solution
The MATLAB fun
tion is presented in Listing 9.6. In the proposed implementation, the norm
of ve
tor b is
al
ulated only on
e, and then used at ea
h iteration in order to de
ide if a
su
iently good approximation of the solution has been found. In the for loop, the most
9.2. ITERATIVE METHODS 169
Figure 9.1: Algorithm used by the MATLAB ba
kslash operator to sele
t the best method to
solve the linear system Ax = b depending of the properties of the full matrix A
Start
Is A yes Triangular
triangular? solver Hessemberg
solver
no
yes
Permuted is A Is A upper
no Is A no
triangular permuted Hessem-
yes symmetri
?
solver triangular? berg?
yes
no
no
no
LDL solver
170CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
Figure 9.2: Algorithm used by the MATLAB ba
kslash operator to sele
t the best method to
solve the linear system Ax = b depending of the properties of the sparse matrix A
Start
no
no
Permuted is A
no Is A no
triangular permuted LU solver
yes symmetri
?
solver triangular?
yes
Does Is A main
Cholesky
Cholesky diagonal
solver yes yes
su
eed? positive?
no
no
LDL solver
9.2. ITERATIVE METHODS 171
resour
e intensive operations are the matrix-ve
tor produ
ts A*r, requiring n2 operations, with
n number of elements of b. The s
alar produ
t between two
olumn ve
tors (e.g., r with z) is
al
ulated as r.'*z, whi
h is a
ommon te
hnique in MATLAB. The exe
ution of the for loop
terminates when the maximum number of iterations is rea
hed, or when the residual norm normr
be
omes less than tool*normb. In the former
ase, flag is set to 1, indi
ating a failure; in the
latter flag is set to 0 indi
ating su
ess.
1 function [x,flag,k,relres]=GradientDescent(A,b,toll,maxiter,x0)
2 %Solution of the linear system Ax=b using the gradient descent method
3 %Input:
4 % A,b: matrix and constant terms vector of the linear system
5 % toll: relative tolerance
6 % maxiter: maximum number of iterations
7 % x0: initial guess for the linear system solution
8 %Output:
9 % x: last approximation of the solution
10 % flag: 0 is the required tolerance was reached, 1 otherwise
11 % k: number of performed iterations
12 % relres: relative residual evaluated at the last performed iteration
13 normb=norm(b); %norm of b vector
14 r = b−A*x0; %initial residual
15 normr=norm(r); %residual norm
16 k=0; %iterations counter
17 while k<maxiter && normr>toll*normb %algorithm loop
18 k=k+1; %increment the iteration number
19 z=A*r;
20 alpha=(r.'*r)/(r.'*z); %descent direction
21 x=x+alpha*r; %new guess
22 r=r−alpha*z; %new residual, faster than r=b−A*x
23 normr=norm(r); %norm of the residual
24 end
25 relres=normr/normb; %relative residual norm
26 if normr<toll*normb %converge reached?
27 flag=0; %yes, flag=0
28 else
29 flag=1; %no, flag=1
30 end
Write a fun
tion to solve the linear system Ax = b , with A ∈ RN ×N semi-positive denite
matrix, using the gradient des
ent method des
ribed in (3.28), a
epting in input a maximum
number of iterations itermax, a relative toleran
e toll and as initial guess x0 .
The fun
tion must return the last estimate of the solution xk , a ag indi
ating
onvergen
e
(0) or failure (1), the number of performed iterations, and the norm of the residual Axk − b.
172CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
Solution
The MATLAB fun
tion is presented in Listing 9.7. In the
ode we take advantage of the fa
t
that the 2-norm of a ve
tor is the square root of the s
alar produ
t of the ve
tor with itself.
1 function [x,flag,k,relres]=ConjugateGradient(A,b,toll,maxiter,x)
2 %Solution of the linear system Ax=b using the conjugate gradient method
3 normb=norm(b); %norm of b vector
4 r=b−A*x; %initial residual
5 rr=r'*r; %scalar product <r,r>
6 normr=sqrt(rr); %residual norm as sqrt(scalar product)
7 k=0; %iterations counter
8 if normr>toll*normb %is the initial guess sufficiently distant?
9 p=r;
10 while k<maxiter %algorithm loop
11 k=k+1; %increment the iteration number
12 z=A*p;
13 alpha=rr/(p.'*z); %descent direction
14 x=x+alpha*p; %new guess
15 rnew=r−alpha*z; %new residual
16 rrnew=(rnew'*rnew); %scalar product <r,r>
17 normr=sqrt(rrnew); %norm of the new residual
18 if normr<toll*normb %is the new residual small enough?
19 break; %if so, exit
20 end
21 beta=rrnew/rr;
22 p = rnew + beta*p; %new value of p
23 r=rnew; %save the new value of r
24 rr=rrnew; %and the just calculated scalar product
25 end
26 end
27 relres=normr/normb; %relative residual norm
28 if normr<toll*normb %converge reached?
29 flag=0; %yes, flag=0
30 else
31 flag=1; %no, flag=1
32 end
The onjugate gradient method is available in MATLAB trough the fun tion pcg.
Write a fun
tion to solve the linear system Ax = b with the gradient des
ent method des
ribed
in (3.33), a
epting in input a maximum number of iterations itermax, a relative toleran
e toll
and as initial guess x0 .
The fun
tion must return the last estimate of the solution xk , a ag indi
ating
onvergen
e
(0) or failure (1), the number of performed iterations, and the norm of the residual Axk − b.
9.2. ITERATIVE METHODS 173
Solution
The MATLAB fun
tion is presented in Listing 9.8. The method requires two matrix-ve
tor
produ
ts at ea
h iteration, and presents an overall
omputational
ost per iteration whi
h is
doubled with respe
t to the
onjugate gradient method.
1 function [x,flag,k,relres]=BiConjugateGradientStab(A,b,toll,maxiter,x)
2 %Solution of the linear system Ax=b by stabilized biconjugate gradient method
3 normb=norm(b); %norm of b vector
4 r=b−A*x; %initial residual
5 normr=norm(r); %residual norm as sqrt(scalar product)
6 k=0; %iteration counter
7 if normr>toll*normb %is the initial guess sufficiently good?
8 rtilde=r;
9 while k<maxiter %algorithm loop
10 k=k+1; %increment the iteration number
11 rrtildenew=r.'*rtilde;
12 if k==1 %first iteration: do not calculate beta
13 p=r;
14 ptilde=rtilde;
15 else %all the other iterations
16 beta=rrtildenew/rrtilde;
17 p=r+beta*p;
18 ptilde=rtilde+beta*ptilde; %new guess
19 end
20 q=A*p;
21 qtilde=A.'*ptilde;
22 alpha=rrtildenew/(ptilde.'*q); %new direction
23 x=x+alpha*p; %new solution guess
24 r=r−alpha*q; %and new residual
25 normr=norm(r); %norm of the residual
26 if normr<toll*normb %is the new residual small enough?
27 break; %if so, exit
28 end
29 rtilde=rtilde−alpha*qtilde;
30 rrtilde=rrtildenew;
31 end
32 end
33 relres=normr/normb; %relative residual norm
34 if normr<toll*normb %convergence reached?
35 flag=0; %yes, flag=0
36 else
37 flag=1; %no, flag=1
38 end
The bi-
onjugate gradient method is available in MATLAB trough the fun
tion bicg; a sta-
bilized version of the algorithm is implemented in the fun
tion bicgstab.
174CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
Solve the linear system using the MATLAB fun
tions implementing the
onjugate gradient
method (pcg) requesting a 10−10 relative toleran
e. Use the null ve
tor as initial guess.
How many iterations are required to obtain a solution respe
ting the imposed toleran
e?
For both the methods, generate a plot of the iteration number for a relative toleran
e
ranging from 10−2 to 10−10 .
What is the residual of the solution returned after 10 iterations by the two methods?
Solution
MATLAB fun
tions implementing iterative methods for the solution of linear systems (e.g.,
pcg for the
onjugate gradient, bicg for the bi-
onjugate gradient, bigcstab for the stabilized
onjugate gradient, gmres for the generalized minimum residual method) all share the same
denition:
[x,flag,relres,iter,resvec]=function_name(A,b,tol,maxit,M1,M2,x0)
Only A and b, matrix and
onstant terms ve
tor of the linear system to solve, are required in
input. The other input arguments are
tol is the relative toleran
e on the residual norm; if this argument is missing of empty, the
default value 10−6 is used.
maxit is the maximum number of iterations; if this argument is missing of empty, the default
value min(size(A, 1), 20) is used.
M1,M2 are the pre
onditioning matri
es. The syntax function_name(A,b,tol,maxit,M) is also
a
epted, with M=M1*M2. If they are empty, no pre
onditioner is applied.
x is a
olumn ve
tor with the best approximation of the solution of the linear system Ax=b.
Remark that the returned ve
tor is not ne
essarily the last
omputed value of the guess,
but it is the one asso
iated to the smallest residual norm that the method en
ountered.
ag is a variable des
ribing the
onvergen
e: if 0, the method
onverged to the desired toleran
e
within the spe
ied maximum number of iterations, otherwise the returned x ve
tor does
not respe
t the required toleran
es. It has to be noted that, when these fun
tions are
alled
simply as x=function_name(...), a message is printed indi
ating su
ess or failure, whi
h
an be
ome annoying is the fun
tion is
alled many times in a for loop. This message is
suppressed if at least two output arguments are required, as in [x,flag]=pcg(A,b).
relres is the relative residual norm norm(b−A*x)/norm(b) asso
iated to the returned solution.
9.2. ITERATIVE METHODS 175
iter is the number of the iteration at whi
h the approximation returned in x was
omputed. If an
approximation is found, satisfying the required residual, then the routine exits immediately,
and the iter is the index of the last performed iteration. However, if the maximum number
of iterations is rea
hed and the
onvergen
e
riteria are not satised, the returned solution
is not ne
essarily the last one, but the one for whi
h the residual (whi
h is, in general, not
a monotoni
fun
tion) was minimum.
resve
is a ve
tor of the residual norms norm(b−A*x) at ea
h iteration. Note that this is an
absolute residual; to obtain the relative one just
al
ulate resvec/norm(b). resvec also
in
lude the residual of the initial guess x0; k is stored in
therefore the residual at iteration
position k+1.
For this exer
ise, rst we
reate A=gallery('lehmer',100)b=sum(A,2). Sin
e the max-
and
imum number of iterations is not indi
ated in the text, we try using maxit=20:
[x,flag,relres,iter]=pcg(A,b,1e−10,20,[],[],zeros(size(b)));
Unfortunately, flag is 1, indi
ating that pcg iterated 20 times but did not
onverge. We try
in
reasing the maximum number of iterations up to 100:
[x,flag,relres,iter]=pcg(A,b,1e−10,100,[],[],zeros(size(b)));
In this way, flag is 0, indi
ating that a solution satisfying the required toleran
es was found;
the number of iterations, stored in iter, is 70.
In order to generate a plot of the iterations number for a relative toleran
e ranging from 10−2
to 10
−10 , we use the
ode presented in Listing 9.9; results are shown in Fig. 9.3.
1 A=gallery('lehmer',100);
2 b=sum(A,2);
3 N=21; %number of considered tolerances
4 tol=logspace(−2,−10,N); %vector of tolerances to test
5 iterpcg=zeros(N,1); %vector to save the iterations number
6 for k=1:length(tol) %loop over the tolerances
7 [~,flag,~,iter]=pcg(A,b,tol(k),100,[],[],zeros(size(b)));
8 if flag~=0 %if flag is not zero, notify the problem and
exit
9 error('tolerance not reached');
10 end
11 iterpcg(k)=iter; %save the iteration number in iterpcg
12 end
13 semilogx(tol,iterpcg,'+'); %plot with logarithmic x axes
14 xlabel('Tolerance') %x axes label
15 ylabel('Iterations'); %y axes label
16 grid('on'); %show the grid
Finally, in order to obtain the residual of the solution at the 10th iteration we
an look at
the 11th
omponent of ve
tor resvec, spe
ifying maxit=10 and relative residual small enough to
ensure that at least 10 iterations are performed.
Using the
onjugate method we have:
[x,flag,relres,iter,resvec]=pcg(A,b,1e−10,10,[],[],zeros(size(b)));
and resvec(11)/norm(b)≈2.4861e-05. The best approximation returned in x was
al
ulated at
176CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
70
60
50
Iterations
40
30
20
10
0
-10 -8 -6 -4 -2
10 10 10 10 10
Tolerance
Figure 9.3: Number of iterations vs. toleran
e requested to the
onjugate gradient method
(Exer
ise 9.2.4)
the previous iteration, sin
e iter is 9 and, a
ordingly, the minimum value of resvec is lo
ated
in position 10:
>> resvec.'
resvec =
522.1063 25.7381 7.6265 1.8771 0.6246 0.2548 0.0819 0.0307 0.0116 0.0047 0.0130
Apply the fun
tions pcg and bicg to the linear system Ax = b with A=gallery('chebvand',10)
non symmetri
matrix and b=sum(A,2). Use a relative toleran
e 10
−4 and start from a null
Solution
After
onstru
ting A=gallery('chebvand',10); and b=sum(A,2);, we try to solve the proposed
linear system with the
onjugate gradient method using
[x,flag,relres,iter,resvec]=pcg(A,b,1e−4,1000,[],[],zeros(size(b)));
In this
ase flag is 4, indi
ating that a s
alar quantity be
ame too small or too large to
ontinue
omputing. Indeed, matrix A is not symmetri
: the
onjugate gradient method should
only be used if A is symmetri
positive denite.
bicg fun
tion
On the
ontrary, using the
[x,flag,relres,iter,resvec]=bicg(A,b,1e−4,1000,[],[],zeros(size(b)));
we obtain a solution satisfying the requirement in 7 iterations.
Solve the linear system Ax = b with A positive dened matrix returned by the
ommands A =
gallery('moler',101) and b=sum(A,2), using the fun
tion pcg, with relative toleran
e 10−5
starting from a null ve
tor initial guess. Dis
uss the results.
9.2. ITERATIVE METHODS 177
Solution
The elements of A are dened as Ai,j = min(i, j) − 2, and Ai,i = i, i.e., the elements in the last
rows and
olumns are mu
h larger that the others. The matrix is square, symmetri
positive
denite; it has one eigenvalue whi
h is small (5.2436e-15), while all the others fall in the range
(2.25, 162.6). The solution of the proposed linear system
an be obtained with:
A=gallery('moler',101);
b=sum(A,2);
[x,flag,relres,iter]=pcg(A,b,1e−5,1000,[],[],zeros(size(b)));
Sin
e flag is 0, the fun
tion pcg su
essfully returns, in iter=11 iterations, a solution satis-
fying the requested toleran
e: the nal result saved in x presents a relative error relres=9.1926
e−06.
Sin
e in this
ase the exa
t solution is the unitary ve
tor, it's simple to evaluate the relative
errors asso
iated to ea
h
omponent of x: ǫk = (xk − 1)/1. While the elements stored in
x(12:101) are asso
iated with error smaller than 1%, the rst 11 elements exhibit a larger
error, whi
h is maximum (≈ 48%) for the rst element. In fa
t, when
al
ulating Ax, the rst
elements of x are multiplied by small elements of A, while elements toward the end of ve
tor x
are multiplied by larger elements of A: as a result, the relative toleran
e
an be rea
hed even if
the error on the upper elements of x is still very large.
In order to improve the solution a
ura
y, it's not su
ient to redu
e the relative toler-
an
e: the
ommand [x,flag,relres,iter]=pcg(A,b,1e−15,1000,[],[],zeros(size(b)));
still
onverges (after 48 iterations), but x(1)=−0.5.
A possible workaround is to modify the test
ondition in Listing 9.7 in order to test not only
for the norm of the residual to be low enough, but also to verify that the absolute variation of
ea
h
omponent of the solution between ea
h iteration is smaller than a provided threshold.
MATLAB fun
tion ilu implements the in
omplete LU fa
torization of an assigned matrix. Com-
pare the results returned by this fun
tion with the ones returned by fun
tion lu implementing
the
omplete P A = LU fa
torization.
Solution
The fun
tion [L,U,P]=ilu(A,opt) performs three dierent types of in
omplete fa
torization,
depending on the se
ond parameter passed to the fun
tion. In parti
ular, opt must be a stru
ture,
and its eld
alled type determines the algorithm applied during the fa
torization:
noll returns the so
alled ILU(0) fa
torization: the non zero elements of the upper triangular
matrix U and the lower triangular matrix L o
upy the same position of non zero elements
of A. The sparsity patterns of U+L and A are the same. No pivoting is performed, and
therefore P is the identity matrix (see Fig.9.4b). The relation A=L*U is satised only for
the elements of A whi
h are not zero.
the larger is the number of non zero element in U and L. The droptol parameter is spe
ied
as an additional eld in stru
ture opt. In this
ase as well, no pivoting is performed, and
P is the identity matrix.
ilutp is similar to the crout fa
torization, but the pivoting may be performed, depending on
the parameter thresh. The rows swap is performed only if the diagonal item for the
onsidered
olumn is not thresh times larger than the pivot one. Therefore the swap is
always performed when thresh=1.
In order to test the dierent types of de
omposition, we
onsider a 50x50 symmetri
, sparse
matrix with 488 non zero entries, built with
With the
omplete LU fa
torization, obtained with [L,U,P]=lu(A), matri
es L and U are
substantially full, having 849 and 788 elements, respe
tively, as indi
ated in Fig. 9.4a, where
fun
tion spy is used to represent the non-null elements of ea
h matrix with a blue marker.
The in
omplete fa
torization with nofill option,
al
ulated with the
ommand [L,U,P]=ilu
(A,struct('type','nofill')), allo
ates for L and U 273 elements ea
h (Fig. 9.4b): in this
ase,
(LU )k,j = Ak,j only if Ak,j 6= 0. Sin
e this option does not perform pivoting, matrix P is the
identity matrix.
The in
omplete fa
torization with crout option,
al
ulated with the
ommand [L,U,P]=ilu
(A,struct('type','crout','droptol',0.2)), keeps additional elements in L and U, sin
e the
droptol paremeter is not 0 (Fig. 9.4
). This option too does not perform any pivoting.
Finally, in Fig. 9.4d, the in
omplete fa
torization is
al
ulated with the
ommand [L,U,P
]=ilu(A,struct('type','ilutp','thresh',0.5,'droptol',0.2)) and the partial pivotig
an
be performed when required.
Solution
A P A P
0 0 0 0
20 20 20 20
40 40 40 40
0 50 0 50 0 50 0 50
nz = 498 nz = 50 nz = 498 nz = 50
L U L U
0 0 0 0
20 20 20 20
40 40 40 40
0 50 0 50 0 50 0 50
nz = 849 nz = 788 nz = 273 nz = 273
(a) [L,U,P℄=lu(A) (b) [L,U,P℄=ilu(A,stru
t('type','noll'))
A P
A P 0 0
0 0
20 20
20 20
40 40
40 40
0 50 0 50
0 50 0 50
nz = 498 nz = 50
nz = 498 nz = 50
L U
L U 0 0
0 0
20 20
20 20
40 40
40 40
0 50 0 50
0 50 0 50
nz = 325 nz = 399
nz = 366 nz = 366
(d) [L,U,P℄=
(
) [L,U,P℄=ilu(A,stru
t('type','
rout','droptol',0.2));
ilu(A,stru
t('type','ilutp','thresh',0.5,'droptol',0.2));
Figure 9.4: Examples of LU de
omposition on a 50x50 sparse matrix with 448 non zero entries
(Exer
ise 9.2.7)
180CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
Che
king the value of output argument iter, we observe that a solution satisfying the require-
ment on the residual relative error is found with 6 iterations.
Without using pre
onditioners, the
ommand
requires 17 iterations.
MATLAB fun
tion ichol implements the in
omplete Cholesky fa
torization of an assigned ma-
trix. Compare the results returned by this fun
tion with the ones returned by L=chol(A,'lower
') implementing the
omplete A = LLt fa
torization with L lower triangular matrix.
Solution
The fun
tion L=ichol(A,opt) performs two dierent types of in
omplete fa
torization, whi
h
an be sele
ted setting the eld opt.type:
noll has a similar meaning to the nofill type used by the ilu fun
tion: the non zero elements of
lower triangular matrix L o
upy the same position of non zero elements of A (see Fig.9.5b).
The relation A=L*L' is satised for the elements of A whi
h are not zero.
i
t implements the in
omplete fa
torization wity threshold dropping ( ICT ),
on
eptually sim-
ilar to the ILUC fa
torization.
The droptol parameter is spe
ied as an additional eld in stru
ture opt. In this
ase as
well, no pivoting is performed, and P is the identity matrix.
The in
omplete Cholesky fa
torization of positive denite matri
es is not guaranteed to ex-
ist. If the errorEncountered non−positive pivot. is returned during the fa
torization, as a
workaround it's possible to apply ichol to the matrix A + αI instead; the parameter α
an be
passed in the eld opt.diagcomp, whi
h is 0 by default.
In order to test the dierent types of de
omposition and the role of the droptol parameter,
we
onsider a 50x50 symmetri
, sparse matrix with 484 non zero entries in the range [−1, 1],
built with
A L A L
0 0 0 0
20 20 20 20
40 40 40 40
0 20 40 0 20 40 0 20 40 0 20 40
nz = 486 nz = 455 nz = 486 nz = 268
(a) L=
hol(A); (b) L=i
hol(A,stru
t('type','noll'));
A L A L
0 0 0 0
20 20 20 20
40 40 40 40
0 20 40 0 20 40 0 20 40 0 20 40
nz = 486 nz = 153 nz = 486 nz = 59
(
) L=i
hol(A,stru
t('type','i
t','droptol',0.01)); (d) L=i
hol(A,stru
t('type','i
t','droptol',0.2));
Figure 9.5: Examples of LU de
omposition on a 50x50 sparse matrix with 448 non zero entries
(Exer
ise 9.2.9)
and the
olumn ve
tor b=sum(A,2). Solve the linear system Ax=b using the
onjugate gradient
method with toleran
e 10−8 . Compare the behavior of the residual at ea
h iteration when the
method is applied without pre
onditioners and when in
omplete Cholesky fa
torizations are used,
al
ulated using type no fill and ict with droptol=[5e−2, 1e−2, 1e−3].
Solution
The
ode is presented in Listing 9.10, and the resulting plot is shown in Fig. 9.6. Without
pre
onditioners, the required toleran
e is obtained in 29 iterations, while the use of the 'noll'
type Cholesky pre
onditioner redu
es the number of iterations to 10. The use of a 'i
t' type
pre
onditioner with 0.05, 0.01, 0.001 drop toleran
es redu
es the number of 24, 14, 7 iterations,
respe
tively.
Listing 9.10: S
ript for the
omparison of the solution of the linear system Ax=b (Exer
ise
9.2.10)
182CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
100
no precond.
nofill
-2 0.05
10
0.01
0.001
Relative residual
-4
10
-6
10
-8
10
-10
10
0 5 10 15 20 25 30
Drop tolerance
Figure 9.6: Residual vs. iteration number for dierent solutions of the linear system Ax = b
based on the
onjugate gradient method (Exer
ise 9.2.4)
Consider the positive denite matrix A and the
olumn ve
tor b
al
ulated in Exer
ise 9.2.10.
Estimate the time required to solve the linear system Ax=b using the methods implemented in
Listing 9.10, using 10−10 toleran
e.
9.2. ITERATIVE METHODS 183
Solution
The use of pre
onditioners may signi
antly redu
e the number of iterations needed by iterative
methods in order to rea
h the required toleran
es. However, their use
omes to a
ost, sin
e
the generation of the in
omplete fa
torization
an be time
onsuming, in parti
ular if the drop
toleran
e is very small.
To estimate this time, we rst allo
ate the ve
tors used to save the
omputational time required
by the ichol and pcg fun
tions, the number of iterations required by pcg to rea
h the required
toleran
e and the number of non zero elements in matrix L.
While fun
tion timeit
an be used for a more rigorous measurement of fun
tion exe
ution
time, the simplest way to estimate the time required to
omplete a series of
ommand is to take
advantage of the tic/toc fun
tions: toc will return the time elapsed sin
e the last
all to tic,
in se
onds. To obtain a more pre
ise estimate, we repeat ea
h
al
ulation MaxRep times.
We
an then start the main loop, over the elements of droptol ve
tor. At ea
h iteration we
fa
torize matrix A with the spe
ied drop toleran
e, and use the generated matrix L as pre
on-
ditioner in the
all to pcg.
The total time is then given by the sum of the time required to fa
torize matrix A and to solve
the linear system
Redu
ing the drop toleran
e, the time required by the ichol fun
tion in
reases, (Fig. 9.7a);
the number of non zero elements stored in matrix L also in
reases (Fig. 9.7b, right axis).
When the drop toleran
e is around 10−1 , pcg is the most expensive fun
tion in terms of
omputational time , the ichol fun
tion returning a small number of non zero items (Fig. 9.7b,
right axes). In this
ondition, the number of iterations is quite large (Fig. 9.7b, left axes).
184CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
105
35 3.5
100
30 3
ichol
pcg
20 2
nnz(L)
60
Time [ms]
15 1.5
40 10 1
5 0.5
20
0 0
10-5 10-4 10-3 10-2 10-1
0
10-5 10-4 10-3 10-2 10-1
Drop tolerance
Drop tolerance
(b) Number of iterations of the
onjugate gra-
(a) Computational times on a referen
e
om- dient method when a in
omplete Cholesky pre-
puter for the exe
ution of i
hol and p
g fun
-
onditioner is used, as a fun
tion of the drop
tions vs. the drop toleran
e used in the in
om- toleran
e (left axes) and number of non zero
plete fa
torization (Exer
ise 9.2.11) elements in the 1000 × 1000 lower triangular
matrix L (Exer
ise 9.2.11)
Sin
e the fa
torization tends to the
omplete Cholesky de
omposition redu
ing the drop tol-
eran
e, the number non zero elements in L drasti
ally in
reases (Fig. 9.7b, right axes).
As a result, on a referen
e
omputer, the total time required to solve the system is ≈18 ms when
droptol=1e−1 (35 iterations) and it de
reases thanks to the better pre
ondition used redu
ing
droptol, until droptol=1e−4, when only ≈8 ms are required to solve the linear system.
Write a fun
tion to solve the linear system Ax = b with the gradient des
ent method des
ribed
in (3.33) a lower triangular matrix M 1, an upper triangular matrix M 2, and a
epting in input
a maximum number of iterations itermax, a relative toleran
e toll and as initial guess x0 .
The fun
tion must return the last estimate of the solution xk , a ag indi
ating
onvergen
e
(0) or failure (1), the number of performed iterations, and the norm of the residual Axk − b.
Solution
The MATLAB fun
tion is presented in Listing 9.11. The method re
eives as input two matri
es
M1 and M2, obtained from the in
omplete fa
torization of matrix A. With respe
t to the Conjugate
Gradient method des
ribed in 9.7, the main dieren
e is represented by the solution, at ea
h
iteration, of the linear system M1*M2*y=r, the result being used in the determination of the new
beta
o
ient. Sin
e both M1 and M2 are supposed to be triangular matri
es, Forward (9.2)
and Backward (9.1) algorithms
an be used to
al
ulate the solution with a quadrati
number of
operations.
1 function [x,flag,k,relres]=PrecondConjugateGradient(A,b,M1,M2,toll,maxiter,x)
2 %Solution of the linear system Ax=b using the conjugate gradient method
3 %with preconditioning
4 %Input:
9.2. ITERATIVE METHODS 185
Consider a squared two-dimensional region R(x, y) with side length L. A Cartesian grid is
dened over this region, with step h in both dire
tions: the position of a grid node (xl , ym )
an
be therefore expressed as ((l − 1)h, (m − 1)h), with 1 ≥ l, m ≥ N . A total of N 2 nodes are then
present in the grid.
Write a MATLAB s
ript to generate a matrix Z ∈ RN,N whose element Zl,m are 0 if the node
(xl , ym ) is at the border of the domain, and
ontain the unique node index j = (l − 1) + (m − 2)N
otherwise.
Solution
The nodes numbering proposed in this problem is similar to the lexi
ographi
ordering introdu
ed
in Se
tion 2.2, but it pro
eeds per
olumns instead of per rows. This approa
h is simpler to
implement in MATLAB sin
e the program stores matri
es data by
olumns; the lexi
ographi
order proposed in Se
tion 2.2
an be obtained simply taking the transpose of matrix Z. Attention
must be paid, however, to the fa
t that in this exer
ises N is the total number of nodes per side,
while in Se
tion 2.2 it indi
ates the number of unknown nodes per side. This
hange has been
introdu
ed to a
ommodate to the fa
t that MATLAB indi
es start from 1 and to simplify
therefore the notation.
Sin
e the rst proposed solution is using a
ouple of for loops to set the elements of z, for
performan
e reasons it is important to allo
ate the whole memory required by Z before inserting
the elements; using fun
tion zeros we also assign the
orre
t value to all the elements at the
border of the domain. We
an then use two nested for loops, one iterating over the rows and
the other over the
olumns of the matrix to assign the node indi
es in the
orre
t position:
187
188 CHAPTER 10. SOLUTION OF POISSON EQUATION WITH FINITE DIFFERENCES
end
end
Z=zeros(N,N);
B=reshape(1:(N−2)^2,N−2,N−2);
Z(2:N−1,2:N−1)=B;
As a nal step, we
an further optimize the
ode working dire
tly on Z, obtaining the nal
version presented in Listing 10.2, where the s
ript has also been transformed in a fun
tion
a
epting in input the number of total nodes per side N and returning matrix Z.
Listing 10.2: Fun tion to assign a progressive index to the internal nodes of a squared domain.
function Z=SquaredDomain(N)
%Lexicographic numbering of the nodes in a squared domain in a squared region
Z=zeros(N,N); %matrix allocation
M=N−2; %number of internal noded on each side
Z(2:N−1,2:N−1)=reshape(1:M*M,M,M); %final matrix with internal nodes indices
The
ode
an be easily validated visually for small values of the input argument, as shown in
Listing 10.3.
Listing 10.3: Example with 11x11 matrix. The nodes indi es range from 1 to 81
>> Z=SquaredDomain(11);
>> Z
0 0 0 0 0 0 0 0 0 0 0
0 1 10 19 28 37 46 55 64 73 0
0 2 11 20 29 38 47 56 65 74 0
0 3 12 21 30 39 48 57 66 75 0
0 4 13 22 31 40 49 58 67 76 0
0 5 14 23 32 41 50 59 68 77 0
0 6 15 24 33 42 51 60 69 78 0
0 7 16 25 34 43 52 61 70 79 0
0 8 17 26 35 44 53 62 71 80 0
0 9 18 27 36 45 54 63 72 81 0
0 0 0 0 0 0 0 0 0 0 0
Solution
It is
onvenient to split the problem in two parts: rst we need to identify the nodes of the region
whi
h lie inside C, and then we must assign a progressive index to them.
For the rst step, a simple approa
h is, again, represented by the two nested for loops.
The value of y does not depend on the m index, and
an be moved outside of the inner for
loop and the squared term
an be
al
ulated only on
e per
olumn. The inequality (x−L/2)
^2+(y−L/2)^2<r^2
an be simplied dividing the left and right sides by h^2 and obtaining the
version
A more
ompa
t and easy to read solution
an be obtained if we generate two ve
tors x and y
of N equally spa
ed points in the [0, 1] interval, then we take advantage of the meshgrid fun
tion
to
reate two matri
es xx and yy
overing all the possible
ombinations of the elements of x and
y ve
tors. We
an then use a simple ve
torized expression to nd whi
h nodes lie in C:
x=linspace(0,1,N);
190 CHAPTER 10. SOLUTION OF POISSON EQUATION WITH FINITE DIFFERENCES
y=x;
[xx,yy]=meshgrid(x,y);
Tmp=(xx−1/2).^2+(yy−1/2).^2<(r/L)^2;
Matrix Tmp is now a boolean matrix
ontaining 1 for the internal nodes, and 0 otherwise. The
total number of internal nodes Ni
an be
al
ulated as Ni=nnz(Tmp), or Ni=sum(Tmp(:)). The
ommand Tmp=(xx−1/2).^2+(yy−1/2).^2<(r/L)^2 returns a boolean matrix whose elements
indi
ate if the
orresponding node lies inside the
ir
ular domain or not.
We nally need to initialize a matrix Z of all zeros with suitable size and repla
e the elements
asso
iated to internal nodes with values ranging from 1 to Ni:
Z=zeros(size(Tmp));
Z(Tmp==true)=1:Ni;
Sin
e Tmp is already a boolean matrix, the last
ommand
an be simply written as Z(Tmp)=1:
Ni; The
ode
an be easily pa
ked in a fun
tion, re
eiving the number of internal nodes N, and
returning matrix Z; su
h a fun
tion is presented in Listing 10.4, where we also take advantage of
the fa
t that the
omputational time of the previous examples
an be optimized simply generating
ve
tors x and y in the interval [−1/2, 1/2] instead of [0, 1].
Listing 10.4: Fun tion to assign a progressive index to the internal nodes of a ir ular domain.
1 function Z=CircularDomain(L,r,N)
2 %Lexicographic numbering of the nodes lying in a circular domain
3 %Input:
4 % L: length of the squared region
5 % r: radius of the circular domain
6 % N: number of discretization nodes per side
7 %Output:
8 % Z: N x N matrix
9 x=linspace(−0.5,0.5,N);
10 y=x;
11 [xx,yy]=meshgrid(x,y);
12 Tmp=xx.^2+yy.^2<(r/L)^2;
13 Z=zeros(size(Tmp));
14 Z(Tmp)=1:nnz(Tmp);
>>CircularDomain(1,0.5,11)
0 0 0 0 0 0 0 0 0 0 0
0 0 0 13 22 31 40 49 0 0 0
0 0 6 14 23 32 41 50 58 0 0
0 1 7 15 24 33 42 51 59 65 0
0 2 8 16 25 34 43 52 60 66 0
0 3 9 17 26 35 44 53 61 67 0
0 4 10 18 27 36 45 54 62 68 0
0 5 11 19 28 37 46 55 63 69 0
0 0 12 20 29 38 47 56 64 0 0
0 0 0 21 30 39 48 57 0 0 0
10.1. NODES NUMBERING 191
0 0 0 0 0 0 0 0 0 0 0
Solution
The approa
h presented in Listing 10.4
an be easily generalized to any domain
ontained in the
squared region R(x, y); it is su
ient to modify the test on the nodes position used to generate
Tmp in the previous problem. The fun
tion presented in Listing 10.5 re
eives in input a fun
tion
handle func expe
ting matri
es XX and yy in input and returning a boolean matrix indi
ating
for ea
h node of the grid if it is internal or not to the
onsidered domain.
1 function Z=GeneralDomain(N,func)
2 %Lexicographic numbering of the nodes lying in a generic domain
3 %Input:
4 % N: number of discretization nodes per side
5 % func: function handle expecting matrices XX and yy in input and returning a
boolean matrix indicating for each node of the grid if it is internal or not
to the considered domain.
6 %Output:
7 % Z: N x N matrix
8 x=linspace(−0.5,0.5, N); %x axes coordinates
9 y=x; %y axes coordinates
10 [xx,yy]=meshgrid(x,y); %xx and yy matrices generation
11 Tmp=func(xx,yy); %test on the nodes
12 Z=zeros(N,N); %allocate matrix Z
13 Z(Tmp)=1:nnz(Tmp); %nodes indices are finally assigned
Square domain: the fun
tion must ex
lude only the nodes in the outern border:
f=@(xx,yy)abs(xx)<0.5 & abs(yy)<0.5
L-shaped domain: it
an be obtained from the square domain removing the quarter
orre-
sponding to nodes with positive x and y
oordinates. Sin
e the remaining nodes have at
least one negative
oordinate the fun
tion be
omes
f=@(xx,yy)(abs(xx)<0.5 & abs(yy)<0.5)& (xx<0 |yy<0).
Fun
tion GeneralDomain is similar to MATLAB fun
tion numgrid, but provides more exibility.
192 CHAPTER 10. SOLUTION OF POISSON EQUATION WITH FINITE DIFFERENCES
Write a s
ript for the solution of the Poisson equation over a squared domain with length L,
dis
retized with N unknown nodes per side, and with a
orresponding step h = L/(N − 1), using
the 5 nodes
omputational mole
ule presented in Fig. 2.2.
Assume a generi
for
e density f (x, y) is applied. Homogeneous Diri
hlet Boundary Conditions
are applied on the four edges of the domain. Generate a 3D plot of the
al
ulated displa
ements.
Solution
The solution requires the
onstru
tion of matrix A following the result presented in Eq. (2.21),
with the same denition of N introdu
ed in that Se
tion. The matrix is
learly sparse and
an be
reated very simply using fun
tion spdiags; it is a symmetri
matrix and therefore it is su
ient
to only generate the upper triangular part of A and
al
ulate A+A.', multiplying the result by
mu/h^2 to obtain the nal matrix. One every N elements on the diagonals +1 and −1 are set to
0: this
an be done with a for loop.
The
onstant terms ve
tor
an be generated using two loops, over the x and y dire
tion,
respe
tively:
In a mu
h more e
ient way it is possible to evaluate the for
e over the 2D domain as ff=fxy
(xx,yy), with xx and yy matri
es returned by meshgrid. We need then to
onvert ff to the
olumn ve
tor f, following the de
ided lexi
ographi
ordering. A
ording to the
onsiderations
reported in 10.1.1, if a squared domain is
onsidered and using the verti
al lexi
ographi
order
for the nodes, we
an use the fun
tion reshape, as in
b=reshape(ff,N*N,1);
b=ff(:);
10.2. SOLUTION WITH FINITE DIFFERENCES SCHEME 193
The resulting linear system Au=b
an be solved in order to
al
ulate the solution of the Poisson
equation. In order to plot it, we must
onvert the
olumn ve
tor solution into a matrix, using a
pro
ess whi
h is the opposite as the one used for the
reation of b:
xx=reshape(x,N,N);
The
omplete fun
tion is presented in Listing 10.6. An example of solution, obtained using
L=1, N=101, mu=1e−3 and a fxy=@(x,y)sin(4*pi*x).*sin(2*pi*y) is presented in Fig. 10.1,
where the
ommands surf(xx,yy,uu) and contour(xx,yy,uu) have been used to generate the
plot of the displa
ement.
1 function [xx,yy,uu,ff]=PoissonQuad(L,N,mu,fxy)
2 %Poisson problem with Homogeneous Dirichlet B.C. on a uniform grid
3 %Input:
4 % L : Square size.
5 % N : Nmber of unknown nodes on each side.
6 % mu : Shear modulus.
7 % fxy : Force applied over the domain
8 %Output:
9 % xx : Matrix containing the x coordinates
10 % yy : Matrix containing the y coordinates
11 % uu : Calculated displacements
12 % ff : Matrix of forces evaluated over the domain
13 NN=N*N; %number of d.o.f.s
14 h=L/(N+1); %step size
15 y=h*(1:N); %vector of coordinates in y
16 x=h*(1:N); %vector of coordinates in x
17 d=ones(NN,1); %helper vector for the construction of A
18 A=spdiags([2*d −d −d], [0 1 N], NN,NN); %build upper triangular A
19 for k=1:(N−1) %the elements A(k*N,k*N+1) are 0, for k=1 to N−1
20 A(k*N,k*N+1)=0;
21 end
22 A=mu/h^2*(A+A'); %use symmetry to generate the complete matrix A
23 [xx,yy]=meshgrid(x,y); %generate the 2D grid
24 ff=fxy(xx,yy); %evaluate the force
25 f=ff(:); %transform the matrix into column vector
26 u=A\f; %solve the linear system
27 uu=reshape(u,N,N); %tranform the result from vector into matrix
Write a fun
tion for the solution of the Poisson equation over a squared region with side L,
dis
retized with a step h = L/(N − 1), assuming a for
e density f (x, y) is applied. The domain
where the problem is dened is provided trough a matrix Z ∈ N, N
onstru
ted with the fun
tion
presented in Listing 10.5. Homogeneous Diri
hlet Boundary Conditions are applied at the borders
of the domain.
Plot the obtained solution using MATLAB surf and contour fun
tions.
194 CHAPTER 10. SOLUTION OF POISSON EQUATION WITH FINITE DIFFERENCES
0.9
6 0.8
4 0.7
Solution u [A.U.]
y position [A.U.]
2
0.6
0
0.5 0
-2
0.4
-4
0.3
-6
1 0.2
1
0.5 0.1
0.5
-5
y position [A.U.] 0 0 0.2 0.4 0.6 0.8
x position [A.U.]
x position [A.U.]
(a) (b)
Figure 10.1: Solution of the Poisson equation on a squared domain with unitary side, shear
modulus µ = 10−3 and for
e density f = sin(4πx) sin(2πy). (Exer
ise 10.2.1), plotted using (a)
fun
tion surf and (b) fun
tion
ontour.
Solution
The solution relies on the generalization of the results summarized in Se
tion 2.2.
The number of degrees of freedom Dof is represented by the largest (integer) number stored
inZ: Dof=max(Z(:)); it
an altenatively be
al
ulated as the number of non zero entries in Z:
Dof=nnz(Z)).
It is preferable to preallo
ate matrix A as a sparse matrix with Dof rows and
olumns, and with
an upper bound to the number of entries equal set 5*Dof whi
h
an be approximately obtain
supposing that ea
h node has 4 for internal neighbors.
For ea
h positive element Z(l,m) in Z having value k, we assign to A(k,k) the value 4. We
then observe the elements above (Z(l−1,m)), at right (Z(l,m+1)), below (Z(l+1,m)), or at the
left (Z(l,m−1)) of the
onsidered one. If any of these elements is positive with value r, we set
A(k,r)=−1.
Listing 10.7: Constru
tion of the linear system matrix for the 2D Poisson problem over a generi
domain (Exer
ise 10.2.2))
1 function A=BuildPoissonGeneric(Z)
2 %Finite Differences approximation of the Poisson problem on a generic domain
3 %Input:
4 % Z: square matrix with the nodes numbering
5 %Output:
6 % A: matrix with the Finite Differences approx. build with a 5 nodes scheme
7 N=size(Z,1); %rows/columns of square matrix Z
8 Dof=nnz(Z); %number of unknown nodes
9 A=spalloc(Dof,Dof,5*N); %A matrix preallocation
10 for r=2:N−1 %loop over rows. The first and last rows are empty
11 for c=2:N−1 %loop over columns. The first and last columns are
empty
12 if Z(r,c)~=0 %is node (r,c) unknown?
13 rowIndex=Z(r,c); %save the (positive) node index
10.2. SOLUTION WITH FINITE DIFFERENCES SCHEME 195
Finally, the non null entries of the returned matrix A have to be multiplied by mu/h/h.
The
ode proposed in Listing 10.7
an be simplied using the single index notation to examine
all the elements of A: in this
ase the positions of the elements surrounding element k are lo
ated
in position k −1 (above), k+N (right), k+1 (below) and k−N (left).
1 function A=BuildPoissonGeneric(Z)
2 N=size(Z,1); %rows/columns of square matrix Z
3 Dof=nnz(Z); %number of unknown nodes (d.o.f.s)
4 A=spalloc(Dof,Dof,5*N); %A matrix preallocation
5 for k=1:N*N %loop over all the entries of Z
6 if Z(k)~=0 %is node (k) unknown?
7 rowIndex=Z(k); %save the (positive) node index
8 A(rowIndex,rowIndex)=4; %place 4 on the main diagonal
9 if Z(k−1)~=0 %is the node 'above' a d.o.f.?
10 A(rowIndex,Z(k−1))=−1; %if so, place −1 in A
11 end
12 if Z(k+N)~=0 %is the node on the 'right' a d.o.f.?
13 A(rowIndex,Z(k+N))=−1; %if so, place −1 in A
14 end
15 if Z(k+1)~=0 %is the node 'below' a d.o.f.?
16 A(rowIndex,Z(k+1))=−1; %if so, place −1 in A
17 end
18 if Z(r,c−1)~=0 %is the node 'left' a d.o.f.?
19 A(rowIndex,Z(k−N))=−1; %if so, place −1 in A
20 end
21 end
22 end
1 function A=BuildPoissonGeneric(Z)
2 N=size(Z,1);
3 Dof=nnz(Z); %number of unknown nodes (d.o.f.s)
4 A=spalloc(Dof,Dof,5*N); %A matrix preallocation
5 Indices=find(Z); %find the indices of the d.o.f.s
6 for k=Indices %loop over all the d.o.f.s
7 rowIndex=Z(k); %save the index of d.o.f.s
8 A(rowIndex,rowIndex)=4; %main diagonal
9 %loop above (+1), right (+N), below (−1) and left (−N)
10 for pos=[+1,+N,−1,−N]
11 if Z(k+pos)~=0 %is this node 'below' a d.o.f.?
12 A(rowIndex,Z(k+pos))=−1;%if so, place −1 in A
13 end
14 end
15 end
1 function A=BuildPoissonGenericVectorized(Z)
2 N=size(Z,1); %rows/columns of square matrix Z
3 Dof=nnz(Z); %number of unknown nodes (d.o.f.s)
4 A=4*speye(Dof,Dof); %4 times the identity matrix to create the main diagonal
5 %where are the d.o.f.s located in Z (using the single index notation)?
6 Indices=find(Z);
7 %which are the corresponding values indicating the indices of the unknown nodes?
8 rowIndices=Z(Indices);
9 %loop above (+1), right (+N), below (−1) and left (−N)
10 for pos=[+1,+N,−1,−N]
11 neighbors=Indices+pos; %indices of the nodes above/below/left/right
12 colIndices=Z(neighbors); %corresponding lexicographic values
13 valid=colIndices>0; %located the positive ones, i.e. dofs
14 numvalid=nnz(valid); %number of non zero elements
15 v=−ones(1,numvalid); %vector of −1 of suitable length
16 %place −1 in position (rowIndices,colIndices)
17 A=A+sparse(rowIndices(valid),colIndices(valid),v,Dof,Dof);
18 end
The approa
h proposed in Listing 10.2.2 is similar to the used by MATLAB fun
tion delsq.
For the for
e density, we
al
ulate ff=fxy(xx,yy), with xx and yy matri
es returned by
meshgrid. Then, for ea
h node in Z with value k >0, we assign to the
orresponding element in
b the value of the
al
ulated for
e.
As an example,
onsider the following
ase, where Z is a 4×4 matrix with entries
>> Z=[0,0,0,0;0,1,4,0;0,3,2,0;0,0,0,0]
Z =
0 0 0 0
10.2. SOLUTION WITH FINITE DIFFERENCES SCHEME 197
0 1 4 0
0 3 2 0
0 0 0 0
>> ff=[0,0,0,0;0,0.1,0.3,0;0,0.5,0.7,0;0,0,0,0]
ff =
0 0 0 0
0 0.1 0.3 0
0 0.5 0.7 0
0 0 0 0
The linear system is built in order to return in the solution ve
tor u the displa
ement of
the nodes 1,2,3,4, in this order, and therefore the
onstant terms ve
tor b must be obtained
rearranging the values in ff to form the
olumn ve
tor [0.1, 0.7, 0.5, 0.3], i.e., pla
ing in
b(1) the value of the element of ff
orresponding to Z=1. This operation
an be easily written
using a for loop
tmp=Z>0;
b(Z(tmp))=ff(tmp); %row vector
b=b.'; %column vector
b(Z(Z>0))=ff(Z>0).';
The last instru
tion
an be read as take the elements of ff whi
h
orrespond to positives entries
of Z, and pla
e them in the elements of ve
tor b following the positions indi
ated by the same
entries of Z.
The so build linear system Ax = b
an be easily solved using dire
t u=A\b; or iterative
methods u=pcg(A,b,1e−6,100,[],[],zeros(size(b))); In the last
ommand, the
onjugate
method has been used sin
e matrix A is symmetri
and positve denite; the required relative
toleran
e is 10−6 and a maximum number is 100 iterations has been set. No pre
onditioning
matri
es are provided and the initial guess has been set to a null ve
tor having the same size as
b.
To plot the solution, we must map the ve
tor u into a matrix having the same size as Z, using
a pro
ess whi
h is the opposite of the one used to map from matrix ff into ve
tor b:
U=zeros(size(Z));
U(Z(Z>0))=u(Z>0);
198 CHAPTER 10. SOLUTION OF POISSON EQUATION WITH FINITE DIFFERENCES
Listing 10.8
ontains the
ode of fun
tion PoissonGeneral, whi
h re
eives in input the matrix
Z, the dis
retization step h, the value of the shear modulus mu and the handle to a fun
tion
returning the for
e, and returns the matri
es xx and yy with the nodes
oordinates, matrix uu
with the problem solution and matrix ff with the applied external for
e. The nal matrix uu
an be plotted using fun
tions su
h as mesh or surf.
1 function [xx,yy,uu,ff]=PoissonGeneral(L,Z,mu,fxy)
2 %Poisson problem with Homogeneous Dirichlet B.C. on a uniform grid
3 %Input:
4 % L : Square size.
5 % Z : Matrix with the indices of the unknown nodes
6 % mu : Shear modulus
7 % fxy : Force applied on the domain
8 %Output:
9 % xx : Matrix containing the x coordinates
10 % yy : Matrix containing the y coordinates
11 % uu : Calculated solutions
12 % ff : Matrix of forces evaluated on the domain
13 x=linspace(0,L,size(Z,1)); %x axes coordinates
14 y=x; %y axes coordinates
15 h=x(2)−x(1); %discretization step
16 A=BuildPoissonGenericVectorized(Z); %A matrix construction
17 A=mu/h/h*A; %multiplication by mu/h^2
18 [xx,yy]=meshgrid(x,y); %xx and yy matrices generation
19 ff=fxy(xx,yy); %force matrix construction
20 b(Z(Z>0))=ff(Z>0).'; %conversion to column vector
21 u=A\b; %linear system solution
22 uu=zeros(size(Z)); %preallocate uu
23 uu(Z>0)=u(Z(Z>0)); %conversion from vector to matrix
As an example, we
onsider an annular domain with external radius rext = 1 and internal
radius rint = 0.5, pla
ed in a squared region with unitary side length and N = 101 nodes per
side. A
onstant unitary for
e is applied, and the shear modulus mu is 1. The problem
an
be studied using the
ode reported in Listing 10.9; a graphi
al representation of the solution is
reported in Fig. 10.2.
0.9
0.8
0.01 0.7
Solution [A.U.]
x direction [A.U.]
0.6
0.005
0.5
0
1 0.4
0.8 0.3
1
0.6 0.8 0.2
0.4 0.6
0.4 0.1
0.2
x direction [A.U.] 0.2 0
0
x direction [A.U.]
0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
x direction [A.U.]
(a) (b)
Figure 10.2: Solution of the Poisson equation on an annular domain with shear modulus µ=1
and for
e density f = 1. (Exer
ise 10.2.2), plotted using (a) fun
tion surf and (b) fun
tion
ontour.