Lecture5 Lenguajes de Programacion v3
Lecture5 Lenguajes de Programacion v3
APPLICATIONS - MATLAB
• 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.
• 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:
• Example
>> rank(A)
• 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.
𝑳𝑼𝒙 = 𝒃
Progressive
𝑳𝒚 = 𝒃 lower triangular system
substitution for L
𝑼𝒙 = 𝒚 upper triangular system matrix
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
• Comparing the left- and right-hand side entries of the above block matrix equation we see that:
• 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.
• If A is a triangular matrix, use the backward or forward substitution method to solve the system.
• If A is not symmetric or the Cholesky decomposition fails, a Gaussian algorithm with partial pivot is
used.
• Iterative Methods
• They provide a sequence of approximate solutions that converge as the number of
iterations, tends to infinity, that is:
𝑥 0 →𝑥 1 →𝑥 2 →⋯→𝑥 𝑘
• 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
𝑛 for 𝑖 = 1,2, … , 𝑛
Jacobi Method (𝒌+𝟏) (𝒌)
𝑥𝑖 = − 𝑎𝑖𝑗 𝑥𝑗 + 𝑏𝑖 /𝑎𝑖𝑖 𝑘 = 0,1,2, …
initial solution 𝑥0 𝑗=1,𝑗≠𝑖
Sebastian Valencia – Luis Angeles 20
Solution of Equation Systems
• Initial value
• Error : 1𝑒 − 4
• 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, …
• Initial value
• Error : 1𝑒 − 4
𝑖−1 𝑛 𝒌
(𝒌+𝟏) 𝒌+1 𝒌 𝒌 𝒂𝒊𝒊 𝒙𝒊
𝑥𝑖 = − 𝑎𝑖𝑗 𝑥𝑗 − 𝑎𝑖𝑗 𝑥𝑗 − 𝒂𝒊𝒊 𝒙𝒊 + 𝑏𝑖 /𝑎𝑖𝑖 +
𝑎𝑖𝑖
𝑗=1, 𝑗=𝑖+1
• For certain ill-conditioned problems, the convergence rate of direct methods can be
very slow.
• 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
• Make a figure of number of iterations versus ω for SOR. Which value of ω is giving the
lowest number of iterations?
[email protected] , [email protected].