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

Lecture5 Lenguajes de Programacion v3

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

Lecture5 Lenguajes de Programacion v3

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

Lenguajes de Programación

Diplomatura de Especialización en Dinámica de Fluidos Computacional (CFD)

APPLICATIONS - MATLAB

MSc. Sebastian Valencia


MSc. Luis Angeles
Solution of Equation Systems

• The solution of a system of equations is necessary in most engineering problems. Examples:


• interpolation and curve fitting problems;
• solution of differential equations – simulation of engineering problems.

• Most of the time of a simulation (using finite differences, finite volumes, finite elements or another
numerical method) is invested in the solution of the system of equations obtained with the
discretization.

• Discretization is a mathematical process through which approximate results of the differential equation
of the problem are obtained.

• There is a need for robust and fast methods.

Sebastian Valencia – Luis Angeles 2


Solution of Equation Systems

• System of n equations and n unknows.

𝑎11 𝑥1 + 𝑎12 𝑥2 + ⋯ + 𝑎1𝑛 𝑥𝑛 = 𝑏1


𝑎21 𝑥1 + 𝑎22 𝑥2 + ⋯ + 𝑎2𝑛 𝑥𝑛 = 𝑏2

𝑎𝑛1 𝑥1 + 𝑎𝑛2 𝑥2 + ⋯ + 𝑎𝑛𝑛 𝑥𝑛 = 𝑏𝑛

𝑎11 𝑎12 ⋯ 𝑎1𝑛−1 𝑎1𝑛 𝑥1 𝑏1


𝑎11 𝑎12 ⋯ 𝑎1𝑛−1 𝑎1𝑛 𝑥2 𝑏2
𝑎11 𝑎12 ⋯ 𝑎1𝑛−1 𝑎1𝑛 𝑥3 = 𝑏3 𝑨𝒙 = 𝒃
⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮
𝑎11 𝑎12 ⋯ 𝑎1𝑛−1 𝑎1𝑛 𝑥𝑛 𝑏𝑛

Sebastian Valencia – Luis Angeles 3


Solution of Equation Systems

• Uniqueness and existence of solutions:

• The rank of a matrix (𝑟𝑎𝑛𝑘 𝐴 ) is equal to the number of linearly independent rows or columns.

• if 𝑟𝑎𝑛𝑘 𝐴 = 𝑟𝑎𝑛𝑘 𝐴|𝑏 = 𝑛 (matrix dimension), then the solution of the system 𝑨𝒙 = 𝒃 is unique.
• if 𝑟𝑎𝑛𝑘 𝐴 = 𝑟𝑎𝑛𝑘 𝐴|𝑏 = 𝑟 < 𝑛, then there are infinite solutions.
• if 𝑟𝑎𝑛𝑘 𝐴 ≠ 𝑟𝑎𝑛𝑘 𝐴|𝑏 , then there is solution,

In MATLAB:

rank(A) gives the rank of the matrix A

Sebastian Valencia – Luis Angeles 4


Solution of Equation Systems

• Example
>> rank(A)

>> A = [1 5 7; 4 2 -1; 10 ans =


4 7]
3
A= 𝑟𝑎𝑛𝑘 𝐴 = 𝑟𝑎𝑛𝑘 𝐴|𝑏 = 3, then the
>> rank([A,b]) solution of the system 𝑨𝒙 = 𝒃 is
1 5 7
4 2 -1 ans =
unique.
10 4 7
3
>> b = [3;5;11]
>> [A,b]
b=
ans =
3
5 1 5 7 3
11 4 2 -1 5
Sebastian Valencia – Luis Angeles 5
10 4 7 11
Solution of Equation Systems

• Direct Methods
• The exact solution (if there are no truncation errors* of the
computer) is determined from a finite number of operations. Gaussian elimination
LU factorization
• They require a large storage capacity;
Cholesky
• They are robust;
Band Matrix
• They are fast.

• Iterative Methods
• It provides a sequence of approximate solutions that converge Jacobi
as the number of iterations approaches infinity. Gauss Seidel
• They require less storage capacity than direct methods; SOR
• There may be convergence problems.

Sebastian Valencia – Luis Angeles 6


Solution of Equation Systems

• Direct Methods – LU factorization


• Decomposing A matrix (no singular, 𝐴 ≠ 0, 𝑟𝑎𝑛𝑘 = 𝑛)

𝑳𝑼𝒙 = 𝒃
Progressive
𝑳𝒚 = 𝒃 lower triangular system
substitution for L
𝑼𝒙 = 𝒚 upper triangular system matrix

Sebastian Valencia – Luis Angeles 7


Solution of Equation Systems
Forward substitution algorithm
function x = forward_sub(L, b)
% x = forward_sub(L, b) is the solution
to L x = b
% L must be a lower-triangular matrix
% b must be a vector of the same
leading dimension as L

n = size(L, 1);
x = zeros(n, 1);
for i = 1:n
tmp = b(i);
for j = 1:(i-1)
tmp = tmp - L(i, j) * x(j);
end
x(i) = tmp / L(i, i);
end Sebastian Valencia – Luis Angeles 8

end
Solution of Equation Systems
Back substitution algorithm
function x = back_sub(U, b)
% x = back_sub(U, b) is the solution to
U x = b
% U must be an upper-triangular matrix
% b must be a vector of the same
leading dimension as U

n = size(U, 1);
x = zeros(n, 1);
for i = n:-1:1
tmp = b(i);
for j = i+1:n
tmp = tmp - U(i, j) * x(j);
end
x(i) = tmp / U(i, i);
end Sebastian Valencia – Luis Angeles 9

end
Solution of Equation Systems

• The LU decomposition algorithm


• Given a matrix 𝐴 there are many different algorithms to find the matrices 𝐿 and 𝑈 for the LU
decomposition. Here we will use the recursive leading-row-column LU algorithm. This algorithm is
based on writing 𝐴=𝐿𝑈 in block form as:

• Comparing the left- and right-hand side entries of the above block matrix equation we see that:

Sebastian Valencia – Luis Angeles 10


Solution of Equation Systems

• The LU decomposition algorithm


• These four equations can be rearranged to solve for the components of the 𝐿 and 𝑈 matrices as:

• The first three equations above can be immediately evaluated to give the first row and column
of 𝐿 and 𝑈. The last equation can then have its right-hand-side evaluated, which gives the Schur
complement 𝑆22 of 𝐴. We thus have the equation 𝐿22 𝑈22 = 𝑆22 , which is an (𝑛 − 1) × (𝑛 − 1) LU
decomposition problem which we can recursively solve.

Sebastian Valencia – Luis Angeles 11


Solution of Equation Systems

• The LU decomposition algorithm


function [L, U] = lu_decomp(A) L11 = 1;
% [L, U] = lu_decomp(A) is the LU U11 = A11;
decomposition A = L U
% A is any matrix L12 = zeros(1, n-1);
% L will be a lower-triangular matrix with 1 U12 = A12;
on the diagonal, the same shape as A
% U will be an upper-triangular matrix, the L21 = A21 / U11;
same shape as A U21 = zeros(n-1, 1);
[n, ~] = size(A); S22 = A22 - L21 * U12;
if n == 1 [L22, U22] = lu_decomp(S22);
L = 1;
U = A; L = [L11, L12; L21, L22];
return; U = [U11, U12; U21, U22];
end end
A11 = A(1,1);
A12 = A(1,2:end);
A21 = A(2:end,1);
A22 = A(2:end,2:end);
Sebastian Valencia – Luis Angeles 12
Solution of Equation Systems

• The LU decomposition algorithm


• Python

Sebastian Valencia – Luis Angeles 13


A = [1,2,2;4,4,2;4,6,4];
• The LU decomposition algorithm
b = [10;12;5];
rank(A)
A= 𝑨𝒙 = 𝒃 rank([A,b])
1 2 2
4 4 2 % LU decomposition
4 6 4 [L,U] = lu_decomp(A)
y = forward_sub(L,b)
b= x = back_sub(U,y)
10
12 % Matlab operators
5 y = L\b
x2 = U\y
x=
x3 = A\b
17.0000
-24.5000
21.0000 Sebastian Valencia – Luis Angeles 14
Solution of Equation Systems

• Direct Methods – MATLAB tools


1) If 𝐴 is a square and non-singular matrix (det A ≠ 0) , the solution for the linear equations can be
obtained by:
𝑥 = 𝑖𝑛𝑣(𝐴) × 𝑏
However, it is recommended to use the multiplication by an “inverse”, by the left of a matrix, using the
forward slash “\”:
𝑥 = 𝐴\b
This gives the same result as the command 𝑖𝑛𝑣, but the inverse of 𝐴 is computed differently (using LU
factorization). Also, if 𝐴 has a special structure, such as symmetric, sparse (zero large number of
elements), or triangular, the “\” command provides an efficient algorithm for solving linear equations.
2) Function lu
𝐿, 𝑈 = 𝑙𝑢 𝐴
[𝐿, 𝑈, 𝑃] = 𝑙𝑢(𝐴)

Sebastian Valencia – Luis Angeles 15


Solution of Equation Systems

• Direct Methods – MATLAB tools


The "\" operator has implemented different algorithms such that, when A is square:

• If A is a triangular matrix, use the backward or forward substitution method to solve the system.

• If A is symmetric try to compute a Cholesky decomposition of A.

• If A is not symmetric or the Cholesky decomposition fails, a Gaussian algorithm with partial pivot is
used.

• If A is singular gives an error.

Sebastian Valencia – Luis Angeles 16


Solution of Equation Systems

• Direct Methods – LU factorization – Example


• Consider the following system of linear equations:

• Solve in MATLAB using a) LU factorization and b) Cholesky factorization

Sebastian Valencia – Luis Angeles 17


Solution of Equation Systems

• Direct Methods – LU factorization – Example


• Consider the following system of linear equations:

LU factorization: Cholesky factorization:

>> A = [4 -1 0; -1 4 -1; 0 -1 4] If A is a symmetric and positive definite


matrix:
>> b = [2;8;-6]
𝐴 = 𝐿𝐿𝑇 ; 𝑈 = 𝐿𝑇
>> [L,U,P] = lu(A)
>> U = chol(A)
>> y = L\(P*b)
>> y = U'\b
>> x = U\y
>> x = U\y
x= x=
1.0000 1.0000
2.0000 2.0000 Sebastian Valencia – Luis Angeles 18
-1.0000 -1.0000
Solution of Equation Systems

• Iterative Methods
• They provide a sequence of approximate solutions that converge as the number of
iterations, tends to infinity, that is:

𝑥 0 →𝑥 1 →𝑥 2 →⋯→𝑥 𝑘

Initial value lim 𝑥 (𝑘)


𝑘→∞

• Characteristics:
• It is used for sparse matrices (high-dimensional matrix, in which most of its elements are
zeros).
• Less storage memory required and are usually faster than direct methods.
• It is not "invasive" (Gaussian elimination "invades" the matrix).
• There may be some convergence problems.
Sebastian Valencia – Luis Angeles 19
Solution of Equation Systems

• Iterative Methods – Jacobi Method


• In the system 𝐴𝑥 = 𝑏 , it is true that 𝑎𝑖𝑖 ≠ 0; 𝑖 = 1, 2, ⋯ , 𝑛 of the ith equation,
𝑎11 𝑥1 + 𝑎12 𝑥2 + ⋯ + 𝑎1𝑛 𝑥𝑛 = 𝑏1 𝑛
𝑎21 𝑥1 + 𝑎22 𝑥2 + ⋯ + 𝑎2𝑛 𝑥𝑛 = 𝑏2 for 𝑖 = 1,2, … , 𝑛
⋮ → ෍ 𝑎𝑖𝑗 𝑥𝑗 = 𝑏𝑖
𝑗=1
𝑎𝑛1 𝑥1 + 𝑎𝑛2 𝑥2 + ⋯ + 𝑎𝑛𝑛 𝑥𝑛 = 𝑏𝑛

• Solving for 𝑥𝑖 it is obtained:


𝑛 𝑛

𝑎𝑖𝑖 𝑥𝑖 + ෍ 𝑎𝑖𝑗 𝑥𝑗 = 𝑏𝑖 → 𝑥𝑖 = − ෍ 𝑎𝑖𝑗 𝑥𝑗 + 𝑏𝑖 /𝑎𝑖𝑖 for 𝑖 = 1,2, … , 𝑛


𝑗=1,𝑗≠𝑖 𝑗=1,𝑗≠𝑖

𝑛 for 𝑖 = 1,2, … , 𝑛
Jacobi Method (𝒌+𝟏) (𝒌)
𝑥𝑖 = − ෍ 𝑎𝑖𝑗 𝑥𝑗 + 𝑏𝑖 /𝑎𝑖𝑖 𝑘 = 0,1,2, …
initial solution 𝑥0 𝑗=1,𝑗≠𝑖
Sebastian Valencia – Luis Angeles 20
Solution of Equation Systems

• Iterative Methods – Jacobi Method


• Solve

• Initial value

• Error : 1𝑒 − 4

Sebastian Valencia – Luis Angeles 21


Solution of Equation Systems

• Iterative Methods – Jacobi Method


function [x,X,Actual_error]=jacobiv1(A,b,x_0,error,nm_iter)
% x_0 : x inicial
x = x_0; while actual_error > error && k < nm_iter
n = size(A,1); k = k+1;
actual_error = 1; x_ant = x; % x anterior
Actual_error = []; for i=1:n
X = x; sum = 0;
k = 0; for j=[1:i-1,i+1:n]
sum = sum + A(i,j)*x_ant(j);
% Matriz B de Jacobi end
B_J = -diag(diag(A))\(triu(A,1)+tril(A,-1)); x(i) = (-sum + b(i))/A(i,i);
% spectral radius condition end
rho = max(abs(eig(B_J))); actual_error = norm(b-A*x');
disp('Radio Espectral'); X = [X;x];
disp(rho); Actual_error = [Actual_error;actual_error];
if rho >= 1 end
disp('No converge'); array2table(X)
end end

Sebastian Valencia – Luis Angeles 22


Solution of Equation Systems

• Iterative Methods – Gauss Seidel


• Considerando Jacobi para i=1,...n
𝑛 for 𝑖 = 1,2, … , 𝑛
(𝒌+𝟏) (𝒌)
𝑥𝑖 = − ෍ 𝑎𝑖𝑗 𝑥𝑗 + 𝑏𝑖 /𝑎𝑖𝑖
𝑘 = 0,1,2, …
𝑗=1,𝑗≠𝑖

• To compute 𝑥2𝑘+1 , we use 𝑥1𝑘 , , but 𝑥1𝑘+1 has already been computed. Then, if the
method is convergent, it is better to use 𝑥1𝑘+1 instead of 𝑥1𝑘

𝑖−1 𝑛
(𝒌+𝟏) (𝒌+1) (𝒌) for 𝑖 = 1,2, … , 𝑛
𝑥𝑖 = − ෍ 𝑎𝑖𝑗 𝑥𝑗 − ෍ 𝑎𝑖𝑗 𝑥𝑗 + 𝑏𝑖 /𝑎𝑖𝑖
𝑗=1, 𝑗=𝑖+1 𝑘 = 0,1,2, …

Sebastian Valencia – Luis Angeles 23


Solution of Equation Systems

• Iterative Methods – Gauss Seidel


• Solve

• Initial value

• Error : 1𝑒 − 4

Sebastian Valencia – Luis Angeles 24


Solution of Equation Systems

• Iterative Methods – Gauss Seidel


function [x,X,Actual_error]=gaussv1(A,b,x_0,error,nm_iter)
% x_0 : x inicial
x = x_0;
n = size(A,1); while actual_error > error && k < nm_iter
actual_error = 1; k = k+1;
Actual_error = []; for i=1:n
X = x; sum = 0;
k = 0; for j=[1:i-1,i+1:n]
sum = sum + A(i,j)*x(j);
% Matriz B de Gauss-Seidel end
B_GS = -tril(A)\triu(A,1); x(i) = (-sum + b(i))/A(i,i);
% spectral radius condition end
rho = max(abs(eig(B_GS))); actual_error = norm(b-A*x');
disp('Radio Espectral'); X = [X;x];
disp(rho); Actual_error = [Actual_error;actual_error];
if rho >= 1 end
disp('No converge'); array2table(X)
end end

Sebastian Valencia – Luis Angeles 25


Solution of Equation Systems

• Iterative Methods – SOR Method (succesive over relaxation)


• It is a modification of the Gauss-Seidel Method to speed up convergence:
𝑖−1 𝑛
(𝒌+𝟏) (𝒌+1) (𝒌)
𝑥𝑖 = − ෍ 𝑎𝑖𝑗 𝑥𝑗 − ෍ 𝑎𝑖𝑗 𝑥𝑗 + 𝑏𝑖 /𝑎𝑖𝑖
𝑗=1, 𝑗=𝑖+1

𝑖−1 𝑛 𝒌
(𝒌+𝟏) 𝒌+1 𝒌 𝒌 𝒂𝒊𝒊 𝒙𝒊
𝑥𝑖 = − ෍ 𝑎𝑖𝑗 𝑥𝑗 − ෍ 𝑎𝑖𝑗 𝑥𝑗 − 𝒂𝒊𝒊 𝒙𝒊 + 𝑏𝑖 /𝑎𝑖𝑖 +
𝑎𝑖𝑖
𝑗=1, 𝑗=𝑖+1

(𝒌+𝟏) (𝒌) (𝒌)


𝑥𝑖 = 𝑥𝑖 + 𝑟𝑖 , where:
𝑖−1 𝑛
(𝒌) 𝒌+1 𝒌
𝑟𝑖 = − ෍ 𝑎𝑖𝑗 𝑥𝑗 − ෍ 𝑎𝑖𝑗 𝑥𝑗 + 𝑏𝑖 /𝑎𝑖𝑖
𝑗=1, 𝒋=𝒊
𝟎<𝝎<𝟐
(𝒌+𝟏) (𝒌) (𝒌)
SOR → 𝑥𝑖 = 𝑥𝑖 + 𝝎𝑟𝑖 𝝎: Relaxation parameter Sebastian Valencia – Luis Angeles 26
Solution of Equation Systems

• Iterative Methods – Advantages and Disadvantages


• In general, there are no storage problems with iterative methods.

• For large dimensional systems of equations (1000x1000, or larger), efficient iterative


methods require less computational cost than direct methods.

• They are relatively easy to adapt to parallel calculation processes.

• The number of iterations required in iterative methods is not known a priori.

• For certain ill-conditioned problems, the convergence rate of direct methods can be
very slow.

Sebastian Valencia – Luis Angeles 27


Solution of Equation Systems

• Problem 1 – Solution of a linear system by the iterative method


• Write an algorithm in MatLab for the solution of a linear system using the Jacobi method
and another one using the Gauss-Seidel method.
• A main program must call the respective algorithms (they can be functions) developed for
the solution of the system 𝐴𝑥 = 𝑏; where:
1
• 𝑎𝑖𝑗 = 𝑖+𝑗−1 and 𝑏 is obtained in such a way that the exact solution of the system is 𝑥𝑖 = 1.
• In the main program, the size of the n × n square matrix must be an input.
• Execute the program considering 𝑛 = 2, 10 and 15 for the two iterative methods
implemented.
• Explain what happens.

Sebastian Valencia – Luis Angeles 28


Solution of Equation Systems

• Problem 1 – Solution of a linear system by the iterative method

Sebastian Valencia – Luis Angeles 29


Solution of Equation Systems

• Problem 2 – Temperature Distribution


• The steady state temperature distribution of a flat plate can be approximated at 9
internal points by applying the discretized Laplace equation at each point. If the sides
of a square plate are kept at constant temperatures of 0 °C and 100 °C, the
temperatures at the 9 points can be obtained as the solution of the following system:

• Obtain the temperature at the different points of the plate and make a graph of the
temperature distribution. Sebastian Valencia – Luis Angeles 30
Solution of Equation Systems

• Problem 2 – Temperature Distribution

Sebastian Valencia – Luis Angeles 31


Solution of Equation Systems

• Project 01 – Solving Equation System

• Solve the system 𝑨𝒙 = 𝒃 using the following methods: a) Jacobi; b) Gauss-Seidel; c)


SOR with ω = 0.8 and 1.5. Compare the results in a big plot.

• Make a figure of number of iterations versus ω for SOR. Which value of ω is giving the
lowest number of iterations?

Sebastian Valencia – Luis Angeles 32


MSc. Sebastian Valencia – MSc. Luis Angeles
Mechanical Engineering Section
Department of Engineering
Pontificia Universidad Católica del Perú
Av. Universitaria 1801
San Miguel, Lima 32
Peru

[email protected] , [email protected].

Sebastian Valencia – Luis Angeles 33

You might also like