You are on page 1of 61

MATLAB

odes (v.2017.2)

139
Chapter 8

The elasti string model


8.1 Finite dieren es
8.1.1 A simple s ript

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.

Listing 8.1: Preliminary variables denition

L=2; %String length


mu=1; %Shear modulus
fforce=@(x)sin(2*pi/L*x); %Force density
N=100; %Number of internal nodes in the grid

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

Listing 8.2: Computational grid generation

h=L/(N+1); %Calculate the discretization step


x=linspace(0,L,N+2); %and the N+2 nodes of the computational grid
x=x.'; %Take the transpose of x to obtain a column vector

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

Listing 8.3: Matrix onstru tion; example 1

A=2*eye(N); %Create the main diagonal,


A=A−diag(ones(N−1,1),+1); %sum the diagonal above the main one
A=A−diag(ones(N−1,1),−1); %and the diagonal below the main one;
A=mu/(h*h)*A; %finally rescale by mu/h^2

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.

Listing 8.4: Matrix onstru tion; example 2

C=mu/(h*h); %Multiplication coefficient.


v=C*ones(N,1); %Vector to be placed on the diagonals
A=2*diag(v,0); %Place 2*v on the main diagonal,
A=A−diag(v(1:N−1),+1); %sum the diagonal above the main one
A=A−diag(v(1:N−1,1),−1); %and the diagonal below the main one;

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.

Listing 8.5: Matrix onstru tion; example 3

C=mu/(h*h); %Multiplication coefficient


v=C*ones(N,1) %Vector to place on the diagonals
A=diag(v,0); %Place v on the main diagonal
A=A−diag(v(1:N−1),+1); %and sum diagonal +1
A=A+A.'; %Sum A with itself transposed

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

in positions 1:N+1:N*N, while elements on diagonals +1 and −1 an be a essed using the


indi es N+1:N+1:N*N−1 and 2:N+1:N*N−N, respe tively.

Listing 8.6: Matrix onstru tion; example 4

c=mu/(h*h); %Scalign factor


A=zeros(N,N); %Matrix preallocation
A(1:N+1:N*N)=2*c; %Place 2*c on the main diagonal
A(N+1:N+1:N*N−1)=−c; %c on diagonal +1
A(2:N+1:N*N−N)=−c; %and on diagonal +1

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.

Listing 8.7: Matrix onstru tion; example 5

C=mu/(h*h); %Multiplication coefficient


e=C*ones(N,1); %N elements column vector, to simplify the
%construction of B
B=[2*e, −e, −e]; %Nx3 matrix; the 1st column will be placed on
%the main diagonal (0) and the other two columns
%on diagonals +1 and −1
A=spdiags(B,[0,+1,−1],N,N) %matrix construction: the 2nd argument indicates
%the indices of the diagonals where the
%corresponding columns of B must be placed;
%the 3rd and 4th arguments indicates the
%number of rows and columns respectively

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

Listing 8.8: Example of spdiags result

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

Listing 8.9: For ing term ve tor generation

xUnknown=x(2:N+1); %new vector containing the unknown nodes


f=fforce(xUnknown); %samples of the force in those nodes

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.

Listing 8.10: Linear system solution

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.

Listing 8.11: Solution representation

u=[0; u; 0]; %We append the homogeneous Dirichlet B.C. in the


%first and last nodes
plot(x,u,'o'); %We finally plot the solution, adding the x− and
%y− labels and the grid
xlabel('Position [A.U.]'); %labels on the axes
ylabel('Displacement [A.U.]');
grid('on'); %display the axes grid

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

Listing 8.12: Solution to exer ise 8.1.1

1 %% Input variables definition


2 L=2;
3 mu=1;
8.1. FINITE DIFFERENCES 145

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');

8.1.2 A simple fun tion

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.

Listing 8.13: Solution to exer ise 8.1.2

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];

8.1.3 Example of appli ation

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.

L=0.1; %Length [m]


r=5e−4; %Radius [m]
g=9.81; %Gravity [m/s^2]
fforce=@(x)−0.1*g/(2*pi*r^2*L)*ones(size(x)); %Force density [N/m^3]
mu=6e5; %Shear modulus [Pa]
[x,u]=ElStringDirichletHomo(L, 101, mu, fforce);
umax=max(abs(u)); %Maximum modulus [m]

The resulting maximum displa ement umax is approximately 1.3 m.

8.1.4 Variable shear stress with mixed B.C.s

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.

Listing 8.14: Solution to exer ise 8.1.4

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

16 %% Calculate the discretization step and the computational grid


17 h=L/(N+1); %discretization step
18 x=linspace(0,L,N+2).' %Integer indices grid for displacement and force
19 xFrac=linspace(h/2,L−h/2,N+1).'; %Semi−integer indices grid for sheer stress
20 mu=fmu(xFrac);
21 %% Build the sparse tridiagonal, symmetric, positive definite square matrix A
22 MainDiagonal=[mu(1:end−1)+mu(2:end); mu(end)]/h^2;
23 UpperDiagonal=mu/h^2;
24 LowerDiagonal=mu(2:end)/h^2;
25 A=spdiags([MainDiagonal,LowerDiagonal,UpperDiagonal],[0−1,1],N+1,N+1);
26 %% Evaluate the force density on the nodes which are degrees of freedom
27 f=fforce(x(2:end)); %sample force in the unknown nodes
28 f(1)=f(1)+mu(1)/h/h*g0; %first unknown node
29 f(end)=0.5*f(end)+1/h*mu(end)/fmu(L)*flux; %last unknown node
30 %% Solve the linear system
31 u=A\f;
32 %% Include the homogeneous Dirichlet B.C. in the first node
33 u=[g0; u];

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 .

Listing 8.15: Semi-integer indi es grid

xFrac=linspace(h/2,L−h/2,N+1).';
mu=fmu(xFrac);

µj±1/2 map into ve tor mu; in parti ular µ1/2 →mu(1),


It's important to observe how the terms
µj+1/2 →mu(j+1), µj−1/2 →mu(j−1), µN +1/2 →mu(N+1)=mu(end). It's then lear that the main
diagonal of matrix A is (mu(1:end−1)+mu(2:end))/h^2.

Listing 8.16: Matrix onstru tion

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)

8.1.5 Finite dieren es, method onvergen e

Solve the se ond order dierential equation

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.

Listing 8.18: S ript solving Problem 8.1.5

L=1; %domain length


mu=1; %shear modulus
fforce=@(x)−8*exp(−4*x).*(2*x−1); %force density
NIntervals=[10,100,1000,10000]; %vector of intervals numbers
I=zeros(size(NIntervals)); %preallocation of integral errors vector
for k=1:length(NIntervals) %for each interval
N=NIntervals(k); %number of intervals
fprintf('Number of intervals: %g\n',N); %print a message
NIntervalNodes=N−1; %number of internal nodes
[x,u]=ElStringFinDiffDirichletHomo(L,NIntervalNodes,mu,fforce);
ue=x.*(exp(−4*x)−exp(−4)); %exact solution in the grid nodes
delta=abs(u−ue); %local error
I(k)=(delta(1)+2*sum(delta(2:end−1))+delta(end))/N; %integral error
end

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.

Listing 8.19: Eigenvalues al ulation and representation

L=1; %string length


mu=1; %shear modulus
rho=1; %density
N=250−1; %number of internal nodes
h=L/(N+1); %discretization step
x=linspace(0,L,N+2); %computational grid
m=3; %number of required eigenvectors
e=ones(N,1); %column vector with N unitary elements
A=mu/rho/h^2*spdiags([2*e,−e,−e],[0,+1,−1],N,N); %matrix construction
[V,D]=eigs(A,m,'sa'); %eigenvalues and eigenvectotors calulations
figure; %open a new window
hold on; %keep previous plots
for p=1:m, %for each computed eigenvalue
eigenvector=[0;V(:,p);0]; %isolate eigenvector p, pad with Dirichlet B.C.s
plot(x,eigenvector); %plot the eigenvector
152 CHAPTER 8. THE ELASTIC STRING MODEL

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

diagonal. A ording to (4.23), the eigenvalues of the onsidered problem are λp = π 2 p 2 , p =


1, 2, . .p
. , m,; the al ulated values returned by diag(D) onrm the orre tness of the al ulation
sin e λp /π 2 is lose to the sequen e 1, 2, . . . , m.
>> sqrt(diag(D)'/pi^2)
ans =
0.999993420276310 1.999947362525209 2.999822350277063

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)

8.1.7 Condition number

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.

Listing 8.20: Funtion to al ulate the ondition number

1 function [h,cMath, cEst]=ElStringFinDiffCondition


2 %Condition number of matrix A calculated with Finite Difference
3 %assuming homogeneous Dirichlet B.C.s
4 %Input:
5 % none
6 %Output:
7 % h : vector of discretization steps
8 % cMath : vector of condition numbers calculated from mathematical
considerations
9 % cEst : vector of condition numbers calculated calling condest function
10 L=1; %string length
11 mu=1; %shear modulus
12 Nv=100:250:2500; %number of internal nodes
13 h=L./(Nv+1); %vector of steps
14 cMath=(1−cos(Nv*pi./(Nv+1)))./(1−cos(pi./(Nv+1)));
15 cEst=zeros(size(Nv)); %preallocation of the result vector
16 for k=1:length(Nv) %iterate over each step
17 N=Nv(k); %current number of intervals
18 e=ones(N,1); %column vector with N unitary elements
19 A=mu/h(k)^2*spdiags([2*e,−e,−e],[0,+1,−1],N,N); %matrix construction
20 cEst(k)=condest(A); %condition number
21 end

Listing 8.21: Condition number al ulation

[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

ylabel('Condition number'); %y−axes label

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)

8.2 Finite elements


8.2.1 Variable shear stress with Diri hlet B.C.s

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:

XFrac=x(1:end−1)+h/2; %semi−integer nodes


%vector of shear modulus divided by the corresponding interval widths
muh=fmu(XFrac)./h;
%vector of force densities multiplied by the corresponding interval widths
fh=fx(XFrac).*h;

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

f=0.5*(fh(1:end−1)+fh(2:end)); %avarage of adjacent fh elements

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

f=conv(fh,[.5 .5],'valid'); %sliding average of fh

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

f(1)=f(1)+muh(1)*g0; %Dirichlet B.C. in x=0


f(end)=f(end)+muh(end)*gL; %Dirichlet B.C. in x=L

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.

fx=@(x)−ones(size(x)); %force density, same size as x


mux=@(x)ones(size(x)); %shear modulus, same size as x
u0=1; %Dirichlet B.C. in x=0
uL=2; %Dirichlet B.C. in x=L
u=ElStringFinElemDirichlet(x,mux,fx,u0,uL);
plot(x,u); %plot the displacement

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)

8.2.2 Variable shear stress with Robin B.C.

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 .

Using the midpoint formula, the for e density f ∈ RN +1,1 an be written as


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.

Listing 8.22: Solution to exer ise 8.2.2


8.2. FINITE ELEMENTS 157

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

N=length(x)−2; %Internal nodes


h=diff(x); %calculate the steps

XFrac=x(1:end−1)+h/2; %fractionary index nodes


muh=fmu(XFrac)./h; %vector of elastic coefficients
muh=[muh;muh(end)];
fh=fx(XFrac).*h; %vector of forces multiplied by vector h
f=0.5*(fh(1:end−1)+fh(2:end)); %calculate the average
f(1)=f(1)+muh(1)*g0; %Dirichlet B.C. in x=0
f=[f;fh(end)/2+alpha*uInf]; %Robin B.C. in x=L (part 1)

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.

h1=1e−3; %step for x<0.15 and x>0.25


h2=1e−4; %step for 0.15<=x<=0.25
x=[0:h1:0.15−h1, ... %0<=x<0.15
0.15:h2:0.25, ... %0.15<=x<=0.25
0.25+h1:h1:0.40].'; %0.25<x<=0.40 −−> 1301 total nodes
u0=0.01; %Non homogeneous Dirichlet B.C. in x=0
alpha=4e6; %Robin B.C in x=L
uInf=0; %Robin B.C in x=L

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

and in this ase it is possible to al ulate the displa ement as

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:

fforce=@(x)−0.05*9.81/(2*pi*1e−3^2*0.40)*(x>=0.175 & x<=0.225);


fmu=@(x)4e5*ones(size(x));
u=ElStringFinElemRobin(x,fmu,fforce,u0,alpha,uInf)

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

Methods for the solution of linear


systems: MATLAB ode
9.1 Non iterative methods
9.1.1 Ba kward method for upper triangular matri es

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

Issue an error if matrix A is singular.

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:

N=size(b,1); %Number of rows of b


x=zeros(N,1); %Preallocation of solution vector x

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.

if A(N,N)==0 %Singular matrix


error('matrix A is singular');
end
x(N)=b(N)/A(N,N); %Start from the last element of x

161
162CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE

for k=N−1:−1:1 %Loop over the rows, with step −1


if A(k,k)==0 %Singular matrix
error('matrix A is singular');
end
tot=0; %Accumulator used to store the summation result
for m=k+1:N
tot=tot+A(k,m)*x(m); %Calculate the summation
end
x(k)=(b(k)−tot)/A(k,k); %Calculate x(k)
end

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.

Listing 9.1: Ba kward substitution method

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.

Listing 9.2: Forward substitution method

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.

9.1.2 PA=LU fa torization

Write a MATLAB fun tion to fa torize matrix A ∈ RN ×N as an upper triangular matrix U, a


lower triangular matrix L, and a permutation matrix P , su h that LU = P A.

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:

An,p = An,p − Ak,p /Ak,k , p = k + 1, . . . , 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.

Listing 9.3: Gauss elimination with partial pivoting

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

10 Swap=1:N; %Preallocate vector 1...N


11 for k=1:N−1 %Loop over columns
12 [MaxValue, MaxPos]=max(abs(A(k:N,k))); %search for pivot element in column k
13 if MaxValue==0 %check singularity
14 error('Matrix A is singular'); %notify that A is singular
15 end
16 MaxPos=MaxPos+(k−1); %maxPos is the position of the maximum in
17 %subvector A(k:N,k). To obtain the index
18 %in the overall column I add (k−1)
19 if k~=MaxPos %Is a swap required?
20 A([k,MaxPos],:)=A([MaxPos,k],:); %Yes, swap A(k,:) and A(MaxPos,:)
21 Swap([k,MaxPos])=Swap([MaxPos,k]); %Keep track of the swap
22 end
23 A(k+1:N,k)=A(k+1:N,k)/A(k,k); %Multipliers (below the diagonal element)
24 A(k+1:N,k+1:N)=... %Linear combination of rows k+1:N and row k
25 A(k+1:N,k+1:N)−A(k+1:N,k)*A(k,k+1:N);
26 end
27 if A(N,N)==0 %Is A singular?
28 error('Matrix A is singular'); %Yes, stop execution
29 end
30 L=tril(A,−1)+eye(N,N); %L is the lower triangular part of A, with 1s
31 %on the main diag.
32 U=triu(A); %U data is stored in the upper triangular part of A
33 P=zeros(N,N); %P matrix is square
34 P(Swap+(0:N−1)*N)=1; %Create P transposed
35 P=P'; %and finally P

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.

9.1.3 Linear system solution with P A = LU fa torization

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

Sin e Ax = b, multiplying both term by matrix P we obtain P Ax = P b, and therefore LU x = P b.


We an then al ulate the intermediate solution of the system Ly = P b using forward fun tion
and nally used backward to solve the system U x = y.
A=magic(5); %matrix A definition
b=(1:5).'; %column vector b definition
[L,U,P]=LuFactor(A); %PA=LU decomposition
y=Forward(L,P*b); %intermediate solution, PAx=L(Ux)=Ly=Pb
x=Backward(U,y); %solution of Ux=y
9.1. NON ITERATIVE METHODS 165

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.

9.1.4 Thomas algorithm

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

Listing 9.4: Thomas method for the solution of a linear system

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.

9.1.5 Cholesky fa torization

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.

Listing 9.5: Cholesky fa torization

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.

9.1.6 MATLAB ba kslash ( \) operator

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 triangular, a triangular solver (similar to the forward/ba kward substitution method)


is used.

ˆ If A is obtained as permutation of rows or olumns of a triangular matrix, then a permuted


triangular solver is used. This algorithm is an e ient extension to the forward/ba kward
methods whi h an handle permuted triangular matri es.

ˆ 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

 if A is an upper Hessenberg matrix (i.e., upper triangular, with 1 on diagonal below


the main one), the Hessenberg solver is used, whi h an be seen as an extension of
the Thomas method to upper Hessenberg matri es;

 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

Aj,i = 0 ifj < i − k1


Aj,i = 0 ifj > i + k2 .

For an upper triangular matrix, k1 = 0, while for a lower triangular matrix, k2 = 0.


The matrix bandwidth k is the smallest non negative integer k su h that

Aj,i = 0 if|i − j| > k.

It's straightforward to obtain that k = max(k1 , k2 ). If A is diagonal then k = 0; for a


pentadiagonal matrix k = 2.
Finally, the band density is dened as k/N .
The algorithm applied to solve a linear system with sparse oe ient matrix is hosen a ording
to the de ision ow presented in Fig. 9.2.
The hoi e of the optimal algorithm to apply to solve the linear system an be time onsuming.
While it is possible to expli itly all the spe i fun tions (su h as Thomas, lu, chol, ...), it is also
possible to inform MATLAB about the matrix properties and leave to the program the hoi e
of the best algorithm. This an be a hieved using fun tion linsolve(A,b,options), where
options is a stru ture indi ating if the matrix A is triangular, Hessemberg, symmetri and/or
positive denite.

9.2 Iterative methods


Iterative methods rely on loops to progressively onverge from an initial guess to a solution
respe ting the imposed toleran es, if any exist. In general, the algorithms main loop requires
two tests. The rst test is on the maximum number of iterations k, whi h has to be ontrolled
to avoid innite loops when the method is not onverging. The se ond test is used to verify if
the solution is su iently a urate, e.g. if the residual rk = Axk − b at iteration k is smaller
than a predened quantity, su h as toll|b|2 , with toll relative toleran e.

9.2.1 Gradient des ent method

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

Does Is A main LU solver


Cholesky
Cholesky diagonal
solver yes yes
su eed? positive?

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

Diagonal is A no k1 = 0 or yes Triangular


solver yes diagonal? k2 = 0? solver

no

Banded k/N > is A tri- yes Tridiagonal


solver yes kthr ? no diagonal? solver

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.

Listing 9.6: Gradient des ent method

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

9.2.2 Conjugate gradient method

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.

Listing 9.7: Conjugate gradient method

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.

9.2.3 Bi- onjugate gradient method

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.

Listing 9.8: Bi- 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

9.2.4 Linear system solution with iterative methods - 1

Consider the linear system Ax = b, with A=gallery('lehmer',100) symmetri positive denite


matrix ∈ R100x100 and b=sum(A,2) olumn ve tor. The solution of the system is the unitary
ve tor.

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

x0 is the initial guess. If it is missing, an all-zero ve tor is used.

The meaning of the output arguments is the following:

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.

Listing 9.9: S ript to estimate the number of iterations vs. toleran e

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

9.2.5 Linear system solution with iterative methods - 2

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

ve tor initial guess.

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.

9.2.6 Linear system solution with iterative methods - 3

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.

9.2.7 In omplete LU fa torization

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.

 rout returns the so alled ILUC U are always onserved,


fa torization. The diagonal elements of
while the other nonzero entries of U
abs(U(i,j))>= droptol*norm(A(:,j)). The
satisfy
entries L(i,j) of matrix L are onserved only if abs(L(i,j))>= droptol*norm(A(:,j))/U
(j,j). A complete fa torization is performed when droptol=0; the larger this parameter,
178CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE

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

N=50; %number of rows and columns


A=sprand(N,N,0.1); %10% filling with uniformly distributed number in (0,1)
A(A>0)=1; %set all the non null elements to 1
A=A+A'+2*speye(N); %sum A to its transposed and add twice the identity

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.

9.2.8 In omplete LU de omposition and onjugate gradient method

Solve the linear system Ax = b, with A = gallery('neumann', 1600)+ speye(1600); and b


=cos(1:1600), using the pre onditioned onjugate gradient method with a residual toleran e
tol=1e−6, starting from a null initial guess. Provide to pcg fun tion also the matri es L and U
obtained from the in omplete LU fa torization of A, with drop toleran e dt=0.1.
The problem solution involves 3 steps: the input matrix A and the olumn ve tor b are on-
stru ted, then the ILUC fa torization of A is evaluated, and nally the onjugate gradient method
is used to solve the proposed linear system:

Solution

A = gallery('neumann', 1600) + speye(1600); %matrix A


b=cos(1:1600)'; %column constant terms vector
type=struct('type','crout','droptol',0.1); %ilu options
[L,U]=ilu(A,type); %ilu factorization
tol=1e−6; %relative tolerance for pcg
maxit=1000; %maximum number of iterations
9.2. ITERATIVE METHODS 179

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

[x, flag,relres,iter]=pcg(A,b,tol,maxit,L,U,zeros(size(b))); %linear system


solution

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

[x, flag,relres,iter]=pcg(A,b,tol,maxit,[],[],zeros(size(b))); %linear system


solution

requires 17 iterations.

9.2.9 In omplete Cholesky fa torization

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.

At ea h fa torization step j, a lo al drop toleran e norm(A(j:end,j),1)*droptol is de-


ned: elements whi h are smaller in magnitude than this quantity are dropped from the
resulting matrix; the diagonal elements are always kept.

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

N = 50; %number of rows and columns


density=0.2; %the number of non−zero elements is approximately density*N*N
overcond=0.1; %reciprocal condition number
kind=1; %positive definite
rng(0); %re−initialize random generator to always obtain the same matrix
A=sprandsym(N,density,overcond, kind);
9.2. ITERATIVE METHODS 181

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)

9.2.10 Linear system solution with pre onditioning matri es - 1

Consider the positive denite matrix A returned by the ode

N = 1000; %number of rows and columns


density=0.2; %the number of non−zero elements is approximately density*N*N
overcond=0.1; %reciprocal condition number
kind=1; %positive definite
rng(0); %re−initialize random generator to always obtain the same matrix
A = sprandsym(N,density,overcond, kind);

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)

1 normb=norm(b); %norm of vector b


2 [~,~,~,~,resvec]=pcg(A,b,1e−8,100); %no prenconditioners
3 semilogy(resvec/normb,'o−−','displayname','no precond.');%plot the residual
4 hold('on'); %retain current plot
5 L=ichol(A,struct('type','nofill')); %'nofill' factorization
6 [~,~,~,~,resvec]=pcg(A,b,1e−8,100,L,L');
7 semilogy(resvec/normb,'*−−','displayname','nofill');%plot the residual
8
9 droptol=[5e−2, 1e−2, 1e−3]; %considered drop tolerances
10 for k=1:length(droptol) %loop over the drop tolerances
11 L=ichol(A,struct('type','ict','droptol',droptol(k))); %ict factorization
12 [~,~,~,~,resvec]=pcg(A,b,1e−8,100,L,L');
13 semilogy(resvec/normb,'+−−','displayname',num2str(droptol(k)));
14 end
15 xlabel('Drop tolerance');
16 ylabel('Relative residual');
17 %show legend using the string passed in the 'displayname' property in semilogy
calls
18 legend('show');
19 grid ('on');

9.2.11 Linear system solution with pre onditioning matri es - 2

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.

droptol=logspace(−5,−1,9); %drop tolerances to use


t_ichol=zeros(size(droptol)); %allocate space to save time required by ichol
t_pcg=zeros(size(droptol)); %allocate space to save time required by pcg
iter=zeros(size(droptol)); %allocate space to save iterations number
nnnz=zeros(size(droptol)); %allocate space to save number of non null elem.

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.

MaxRep=10; %number of times to repeat the test to average time

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.

for k=1:length(droptol) %loop over the drop tolerances


tic %start timer
for Nrep=1:MaxRep %repeat Maxrep times
L=ichol(A,struct('type','ict','droptol',droptol(k)));
end
t_ichol(k)=toc/MaxRep; %save average time required by each ichol call
nnnz(k)=nnz(L); %save the number of non zero elements of L
tic %restart the timer
for Nrep=1:MaxRep %repeat MaxRep times
[~,flag,~,iter(k)]=pcg(A,b,1e−10,100,L,L');
end
t_pcg(k)=toc/MaxRep; %save average time required by each ichol call
end

The total time is then given by the sum of the time required to fa torize matrix A and to solve
the linear system

t_tot=t_ichol+t_pcg; %total time is ichol time + pcg time

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

Number of pcg iterations


80 total 25 2.5

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.

9.2.12 Conjugate gradient method with pre onditioning matri es

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.

Listing 9.11: Conjugate gradient method with pre onditioning

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

5 % A,b: matrix and constant terms vector of the linear system


6 % M1,M2: preconditioning matrices; M1 lower triangular, M2 upper triangular
7 % toll: relative tolerance
8 % maxiter: maximum number of iterations
9 % x: initial guess for the linear system solution
10 %Output:
11 % x: last approximation of the solution
12 % flag: 0 is the required tolerance was reached, 1 otherwise
13 % k: number of performed iterations
14 % relres: relative residual evaluated at the last performed iteration
15 normb=norm(b); %norm of b vector
16 r=b−A*x; %initial residual
17 normr=norm(r); %residual norm as sqrt(scalar product)
18 k=0; %iteration counter
19 if normr>toll*normb %is the initial guess sufficiently good?
20 ytmp=Forward(M1,r); %solve M1*(M2*y)=r using Forward to solve M1*
ytmp=r
21 y=Backward(M2,ytmp); %and then Backward to solve M2*y=ytmp
22 ry=(r.'*y);
23 p=y;
24 while k<maxiter %algorithm loop
25 k=k+1; %increment the iteration number
26 z=A*p;
27 alpha=ry/(p.'*z); %descent direction
28 x=x+alpha*p; %new guess
29 rnew=r−alpha*z; %new residual
30 ytmp=Forward(M1,rnew);
31 y=Backward(M2,ytmp);
32 rynew=(rnew.'*y); %scalar product <r,r>
33 rrnew=(rnew'*rnew); %scalar product <r,r>
34 normr=sqrt(rrnew); %norm of the new residual
35 if normr<toll*normb %is the new residual small enough?
36 break; %if so, exit
37 end
38 beta=rynew/ry;
39 p = y + beta*p; %new value of p
40 r=rnew; %save the new value of r
41 ry=rynew; %and the just calculated scalar product
42 end
43 end
44 relres=normr/normb; %relative residual norm
45 if normr<toll*normb %converge reached?
46 flag=0; %yes, flag=0
47 else
48 flag=1; %no, flag=1
49 end
186CHAPTER 9. METHODS FOR THE SOLUTION OF LINEAR SYSTEMS: MATLAB CODE
Chapter 10

Solution of Poisson equation with


Finite Dieren es
10.1 Nodes numbering
10.1.1 Numbering internal nodes of a squared domain

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:

N=11; %number internal nodes on each side


Z=zeros(N,N); %matrix allocation
for m=2:N−1 %loop over columns (internal nodes only)
for l=2:N−1 %loop over rows (internal nodes only)
Z(l,m)=(l−1)+(m−2)*N; %assign the index

187
188 CHAPTER 10. SOLUTION OF POISSON EQUATION WITH FINITE DIFFERENCES

end
end

As an alternative to the nested loops, we an generate a square matrix B ∈ RN xN , assign to it


all the integer numbers between 1 and N 2 , and nally opy B in the  enter of Z. Using fun tion
reshape to onvert a ve tor into a matrix, we take here advantage of the fa t that MATLAB
matri es are internally stored per olumns.
Listing 10.1: Solution with auxiliary matrix

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

Numbering internal nodes of a ir ular domain


Generalize the solution proposed in Problem 10.1.1 to onsider a ir ular domain C with enter
in (L/2, L/2) and radius r ≤ L/2; a 2D Cartesian grid is dened on the region [0, L]x[0, L] with
N nodes in both x and y dire tions. Generate a matrix Z ∈ RN,N , with N = L/h + 1, whose
element Zi,j ontains the unique number of the node lexi ographi ordering if the node is internal
to C and 0 otherwise.
10.1. NODES NUMBERING 189

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.

N=11; %number internal nodes on each side


h=L/(N−1); %discretization step
Z=zeros(N,N); %matrix allocation
NodeIndex=1; %index of the internal node
for m=2:N−1 %loop over columns (internal nodes only)
for l=2:N−1 %loop over rows (internal nodes only)
x=m*h; %node y coordinate
y=l*h; %node x coordinate
if (x−L/2)^2+(y−L/2)^2<r^2
Z(l,m)=NodeIndex; %assign the index
NodeIndex=NodeIndex+1; %index of the next internal node
end
end
end

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

N=11; %number internal nodes on each side


r=L/2; %limit radius
h=L/(N−1); %discretization step
Z=zeros(N,N); %matrix allocation
Nm1Div2=(N−1)/2;
rOverhSq=(r/h)^2; %normalized radius squared
NodeIndex=1; %index of the internal node
for m=2:N−1 %loop over columns (internal nodes only)
xnormSq=(m−Nm1Div2)^2; %squared norm. y coord. for nodes on col m
for l=2:N−1 %loop over rows (internal nodes only)
ynormSq=(l−Nm1Div2)^2; %squared normalized y coordinate of the node
if xnormSq+ynormSq<rOverhSq
Z(l,m)=NodeIndex; %assign the index
NodeIndex=NodeIndex+1; %index of the next internal node
end
end
end

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);

The ode an be visually validated for small values of N, su h as

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

Numbering internal nodes of a generi domain


Generalize the solution proposed in Problem 10.1.1 to onsider a generi domain G in the region
[0, L]x[0, L].

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.

Listing 10.5: Solution to Problem 10.1.1

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

Examples of possible fun tion provided as se ond argument to GeneralDomain are

ˆ 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

ˆ Cir ular domain: assuming side L and ir le radius r<L we have:


f=@(xx,yy)xx.^2+yy.^2<(r/L)^2).

ˆ 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

10.2 Solution with Finite Dieren es s heme


10.2.1 Poisson equation on a square domain

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.

N2=N*N; %number of elements of A


h=L/(N+1); %step size
d=ones(NN,1); %helper vector for the construction of A
A=spdiags([2*d,−d,−d], [0,1,N], NN,NN); %build upper triangular A
for k=1:(N−1) %the elements A(k*N,k*N+1) are 0, for k=1 to N−1
A(k*N,k*N+1)=0;
end
A=mu/h^2*(A+A'); %use symmetry to generate the lower part of A

The onstant terms ve tor an be generated using two loops, over the x and y dire tion,
respe tively:

x=h:h:L−h; %unknown nodes in the x axis (columns)


y=x; %unknown nodes in the y axis (rows)
b=zeros(NN,1); %preallocate
for r=1:N %loop over rows (y direction)
for c=1:N %loop over columns (x direction)
b((c−1)*N+r)=f(x(c),y(r));
end
end

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);

or simply use the : operator as in

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.

Listing 10.6: Solution to Exer ise 10.2.1

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

10.2.2 Poisson equation on a generi domain

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

14 A(rowIndex,rowIndex)=4; %place 4 on the main diagonal


15 if Z(r−1,c)~=0 %is the node 'above' a d.o.f.?
16 A(rowIndex,Z(r−1,c))=−1; %if so, place −1 in A
17 end
18 if Z(r,c+1)~=0 %is the node at the 'right' a d.o.f.?
19 A(rowIndex,Z(r,c+1))=−1; %if so, place −1 in A
20 end
21 if Z(r+1,c)~=0 %is the node 'below' a d.o.f.?
22 A(rowIndex),Z(r+1,c))=−1; %if so, place −1 in A
23 end
24 if Z(r,c−1)~=0 %is the node at the 'left' a d.o.f.?
25 A(rowIndex,Z(r,c−1))=−1; %if so, place −1 in A
26 end
27 end
28 end
29 end

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

Instead of he king ea h node to observe if it is a degree of freedom (d.o.f.) or not, we an


repla e the rst test if Z(k)=0 using fun tion find to lo ate the indi es of all the nodes whi h
are degrees of freedom in the onsidered problem. We an also use a for loop to repla e the four
196 CHAPTER 10. SOLUTION OF POISSON EQUATION WITH FINITE DIFFERENCES

separated tests in dire tion +1,+N,−1,−N.

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

A nal improvement is possible: instead of working on ea h element k, we an work dire tly


on the whole ve tor Indices in a ve torized way.

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

and the matrix ff evaluated in the nodes of the region are

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

b=zeros(nnz(Z),1); %preallocate b as column vector


for k=1:numel(Z), %for each element in Z
if Z(k)>0 %is it a degree of freedom?
b(Z(k))=ff(k); %if so, set the related b value
end
end

whi h an be ve torized testing all the elements of Z at the same time

tmp=Z>0;
b(Z(tmp))=ff(tmp); %row vector
b=b.'; %column vector

and nally ompa ted in

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.

Listing 10.8: Solution to Exer ise 10.2.1

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.

Listing 10.9: Solution to Exer ise 10.2.2

1 N=101; %number of nodes per side


2 fAnnulus=@(xx,yy)(xx.^2+yy.^2<0.5^2) & (xx.^2+yy.^2>0.25^2); %annular domain
3 mu=1; %shear modulus
4 L=1; %region side
5 fxy=@(x,y) ones(size(x)); %applied force density
6 Z=GeneralDomain(N, fAnnulus); %d.o.f.s indices
7 [xx,yy,uu,ff]=PoissonGeneral(L,Z,mu,fxy); %solution of the Poisson problem
8 figure;surf(xx,yy,uu); %plot in a new figure
9 figure;contour(xx,yy,uu); %plot in a new figure
10.2. SOLUTION WITH FINITE DIFFERENCES SCHEME 199

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.

You might also like