Professional Documents
Culture Documents
Week9 ENG1060 Lecture17
Week9 ENG1060 Lecture17
• Today’s Lecture
• Elimination of
• Introduction to Matrices (Matrix
unknowns
format for linear algebraic eqns and
• Gaussian
elimination
basic matrix math)
• Special
matrices
• Lecture
summary
• Using MATLAB to solve sets of
• Next lecture
linear algebraic eqns
2
Last Lecture
• Last lecture
We need to revisit the basics of ‘Matrix Mathematics’
• Today’s Lecture
• Elimination of
N linear algebraic equations
unknowns in n unknowns
• Gaussian
a11 x1 a12 x2 a1n xn b1 This can be written:
elimination
or
Ax b
3
Last Lecture
• Last lecture We need to revisit the basics of ‘Matrix Mathematics’
• Today’s Lecture
Formally, the matrix equation, Ax=b, can be solved by pre-
• Elimination of
unknowns (or left-) multiplying by A-1
• Gaussian
elimination
A-1Ax = A-1 b
• Special
matrices
• Lecture
summary
Remember A-1 A = I
• Next lecture
and Ix=x
so x = A-1 b
4
Last Lecture
• Last lecture
There are two ways that MATLAB can be used to solve linear
• Today’s Lecture algebraic equations expressed in matrix form :
• Elimination of
unknowns
• Gaussian
elimination • Pre-multiplication by the inverse
• Special • >> x=inv(A)*b =A^-1*b
matrices
• Lecture
summary • Left division MATLAB preferred
• Next lecture • >> x=A\b
Ax = b
A\ Ax = A\ b
I x = A\ b
‘dividing from the front’
• Elimination of
unknowns
• Gaussian Q13
elimination
F Q35
Q55
• Special Q12 Q34
matrices
• Lecture Q44
summary
Q24
• Next lecture
Linear Algebraic Eqns Linear algebraic Eqns expressed in matrix form
(Q12 Q13 )x1 F (Q12 Q13 ) x1 F
Q12 x1 Q24 x 2 0 Q12 Q24 x 2 0
Q13 x1 (Q34 Q35 )x 3 0 Q13 (Q34 Q35 ) x 3 0
Q24 x 2 Q34 x 3 Q44 x 4 0 Q24 Q34 Q44
x 4 0
Q35 x 3 Q55 x 5 0
Q35 Q55
x 5
0
This is now in matrix form; Ax=B,
where A and B are known 6
Last Lecture
• Last lecture Mixing oil……
• Today’s Lecture
(Q12 Q13 ) x1 F
• Elimination of
Q12 Q24 x 2 0
unknowns
• Next lecture
Qx F
Q1Qx Q1F
I x Q1 F
x Q1F
7
Last Lecture
• Last lecture
• Today’s Lecture
• Elimination of
unknowns
Mixing oil…… Input starting values
• Gaussian
elimination
• Special
matrices
• Lecture
summary
• Next lecture
Q-1
8
Last Lecture
• Last lecture
• Today’s Lecture
• Elimination of
unknowns
Mixing oil……
• Gaussian
elimination
• Special
matrices
• Lecture
summary
• Next lecture
9
Last Lecture
• Last lecture
• Today’s Lecture
• Elimination of
unknowns
Mixing oil……
• Gaussian
elimination
• Special
matrices
• Lecture
summary
• Next lecture
Pretty easy!
10
Today’s lecture
• Last lecture
• Today’s Lecture
• How does MATLAB solve these
• Elimination of
unknowns
sets of linear algebraic equations?
• Gaussian
elimination
• Special
matrices
• Lecture
summary
• Next lecture
11
MATLAB Solutions
• Last lecture
There are two ways that MATLAB can be used to solve linear
• Today’s Lecture algebraic equations expressed in matrix form :
• Elimination of
unknowns
You should know
• Gaussian this from ENG1091
elimination • Pre-multiplication by the inverse
• Special • >> x=inv(A)*b =A^-1*b
matrices
• Lecture
summary • Left division MATLAB preferred
• Next lecture • >> x=A\b
Ax = b
A\ Ax = A\ b
I x = A\ b
‘dividing from the front’
12
Elimination of Unknowns
• Last lecture
Consider we had only two equations in two unknowns (x1, x2):
• Today’s Lecture
13
Elimination of Unknowns
• Last lecture
Consider we had only two equations in two unknowns (x1, x2):
• Today’s Lecture
15
Gaussian Elimination
• Last lecture How do we do it in practice? a11 a12 a1n b1
• Today’s Lecture a11x1 a12 x 2 a1n x n b1
augmented a21 a22 a2n b2
• Elimination of a 21x1 a 22 x 2 a 2n x n b2 matrix
unknowns
• Gaussian
an1 an 2 ann bn
elimination
a n1x1 a n 2 x 2 a nn x n bn
pivot equation: a21
• Special multiply row 1 by
matrices a11 the pivot a11
• Lecture element subtract from row 2
summary
• Next lecture
a11 a12 a1n b1
a 21
21 a11 a11
a a22 aa1121 a12 a a 21
a b a 21
2n a11 1n 2 a11 1 b
an1 an 2 ann bn
x1
b1 a12 x 2 a1(n1) x n1 a1n x n
a11
18
Gaussian Elimination
• Last lecture
If only 3 variables:
• Today’s Lecture
• Elimination of
unknowns
• Gaussian
elimination
• Special
matrices
• Lecture
summary
• Next lecture
19
M-file: Gaussian Elimination
function x = GaussNaive(A,b)
% GaussNaive(A,b):
% Gauss elimination without pivoting
% input:
% A: coefficient matrix
% b: right hand side vector
% Output
% x: solution vector
Check on squareness of A
[m,n] = size(A);
if m~=n, error('Matrix A must be square'); end
nb = n+1;
Aug = [A b]; Form augmented matrix
% forward elimination
for k = 1: n-1
for i = k+1:n
factor = Aug(i,k)/Aug(k,k); Elimination steps
Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb);
end
end
% back substitution
c=zeros(n,1);
c(n) = Aug(n,nb)/Aug(n,n);
for i = n-1:-1:1 Back substitution steps
c(i) = (Aug(i,nb)-Aug(i,i+1:n)*c(i+1:n))/Aug(i,i);
end
x=c;
20
M-file: Gaussian Elimination
function x = GaussNaive(A,b)
% GaussNaive(A,b):
% Gauss elimination without pivoting
% input:
% A: coefficient matrix
% b: right hand side vector
% Output
% x: solution vector
[m,n] = size(A);
if m~=n, error('Matrix A must be square'); end
nb = n+1;
Aug = [A b];
% forward elimination
for k = 1: n-1
for i = k+1:n
factor = Aug(i,k)/Aug(k,k);
Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb);
end
end
% back substitution
c=zeros(n,1);
c(n) = Aug(n,nb)/Aug(n,n);
for i = n-1:-1:1
c(i) = (Aug(i,nb)-Aug(i,i+1:n)*c(i+1:n))/Aug(i,i);
end
x=c;
21
M-file: Gaussian Elimination
• Last lecture
• Lecture
summary
22
M-file: Gaussian Elimination
• Last lecture for k = 1:n-1
• Today’s Lecture
for i = k+1:n Two
factor = Aug(i,k)/Aug(k,k);
• Elimination of
Aug(i,k:nb) = Aug(i,k:nb) - factor* Aug(k,k:nb);
nested
unknowns
end loops
• Gaussian
end
elimination
first time
• Special
matrices
• Lecture
k 1 i2 factor a21 a
summary 11
• Next lecture
This subtracts factor*row 1 from
Aug row 3 right across the augmented
matrix, getting rid of the first
element of row 3
24
M-file: Gaussian Elimination
• Last lecture for k = 1:n-1
• Today’s Lecture
for i = k+1:n Two
factor = Aug(i,k)/Aug(k,k);
• Elimination of
Aug(i,k:nb) = Aug(i,k:nb) - factor* Aug(k,k:nb);
nested
unknowns
end loops
• Gaussian
end
elimination
until
• Special
matrices
k 1 in an1
factor
• Lecture
summary a11
• Next lecture
This subtracts factor*row 1 from
Aug row n right across the augmented
matrix, getting rid of the first
element of row n
25
M-file: Gaussian Elimination
• Last lecture for k = 1:n-1
• Today’s Lecture
for i = k+1:n Two
factor = Aug(i,k)/Aug(k,k);
• Elimination of
Aug(i,k:nb) = Aug(i,k:nb) - factor* Aug(k,k:nb);
nested
unknowns
end loops
• Gaussian
elimination
end then
• Special
matrices
k 2 i3 a32
factor
• Lecture
summary a22
• Next lecture
This subtracts factor*row 2 from
Aug row 3 right across the augmented
matrix, getting rid of the first
element of row 3
26
M-file: Gaussian Elimination
• Last lecture for k = 1:n-1
• Today’s Lecture
for i = k+1:n Two
factor = Aug(i,k)/Aug(k,k);
• Elimination of
Aug(i,k:nb) = Aug(i,k:nb) - factor* Aug(k,k:nb);
nested
unknowns
end loops
• Gaussian
end
elimination
next
• Special
matrices
k 2 i4 factor a42
• Lecture
summary a22
• Next lecture
This subtracts factor*row 2 from
Aug row 4 right across the augmented
matrix, getting rid of the first
element of row 4
27
M-file: Gaussian Elimination
• Last lecture for k = 1:n-1
• Today’s Lecture
for i = k+1:n Two
factor = Aug(i,k)/Aug(k,k);
• Elimination of
Aug(i,k:nb) = Aug(i,k:nb) - factor* Aug(k,k:nb);
nested
unknowns
end loops
• Gaussian
end
elimination
until
• Special
matrices
k n 1 an ( n 1)
in factor
• Lecture
summary
a( n 1)( n 1)
• Next lecture
Aug
28
M-file: Gaussian Elimination
Called naïve because in
function x = GaussNaive(A,b)
% GaussNaive(A,b):
calculating the factor it is possible
% Gauss elimination without pivoting we will divide by zero and get an
% input:
% A: coefficient matrix error
% b: right hand side vector
% Output
% x: solution vector
[m,n] = size(A);
if m~=n, error('Matrix A must be square'); end
nb = n+1;
Aug = [A b];
% forward elimination
for k = 1: n-1
for i = k+1:n
factor = Aug(i,k)/Aug(k,k); Elimination steps
Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb);
end
end
% back substitution
c=zeros(n,1);
c(n) = Aug(n,nb)/Aug(n,n);
for i = n-1:-1:1 Back substitution steps
c(i) = (Aug(i,nb)-Aug(i,i+1:n)*c(i+1:n))/Aug(i,i);
end
x=c;
29
M-file: Gaussian Elimination
• Last lecture for k = 1:n-1
• Today’s Lecture
for i = k+1:n Two
factor = Aug(i,k)/Aug(k,k);
• Elimination of
Aug(i,k:nb) = Aug(i,k:nb) - factor* Aug(k,k:nb);
nested
unknowns
end loops
• Gaussian
end
elimination
next
• Special
matrices
k 2 i4 factor a42
• Lecture
summary a22
• Next lecture
This subtracts factor*row 2 from
Aug row 4 right across the augmented
matrix, getting rid of the first
element of row 4
30
Gaussian Elimination: Pivoting
function x = GaussPivot(A,b) To avoid the potential problem of dividing
% GaussPivot(A,b):
% Gauss elimination with pivoting by zero when evaluating the factor, we
% input:
% A: coefficient matrix can check if the pivot element is zero.
% b: right hand side vector
% Output
% x: solution vector If so we just swap that row
with a row beneath it!
[m,n] = size(A);
if m~=n, error('Matrix A must be square'); end
nb = n+1;
The order of the rows does
Aug = [A b]; not matter. This is called
% forward elimination
for k = 1: n-1 ‘partial pivoting’.
% Partial pivoting
[big,i] = max(abs(Aug(k:n,1)));
ipr = i+k-1; Search for biggest
if ipr~=k
%pivot the rows pivot element and
Aug([k,ipr],:)=Aug([ipr,k],:);
end use it in each case
for i = k+1:n
factor = Aug(i,k)/Aug(k,k);
Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb);
Elimination steps
end
end
% back substitution
c=zeros(n,1);
c(n) = Aug(n,nb)/Aug(n,n);
for i = n-1:-1:1 Back substitution steps
c(i) = (Aug(i,nb)-Aug(i,i+1:n)*c(i+1:n))/Aug(i,i);
end
x=c;
31
Special Matrices
• Last lecture
e.g. Tri-diagonal matrix: particular case of sparse banded matrix with
bandwidth of 3
• Today’s Lecture
• Elimination of
f1 g1 x1 r1
unknowns
e2 f2 g2 x 2 r2
• Gaussian e3 f3 g3 x 3 r3
elimination
• Special
matrices
• Lecture en1 f n1 gn1 x n1 r
summary n1
en f n
x n
rn
• Next lecture
f1 g1
f
2 e
2 g2
There are special modifications of the f 3 e3 g3
traditional approaches such as Gaussian
elimination that can be used to solve f e g
these special sets of equations
f
n1 e
n1 gn1
f n
en
32
MATLAB Solutions
• Last lecture
• Today’s Lecture
MATLAB preferred
• Left division
• Elimination of
• >> x=A\b Ax = b
unknowns
A\ Ax = A\ b
• Gaussian I x = A\ b
elimination
‘dividing from the front’
• Special
matrices
What is really being done here?
• Lecture
summary
33
Lecture Summary
• Last lecture
• Today’s Lecture
• Elimination of
• We have looked at what MATLAB
unknowns
is doing when it solves matrix
• Gaussian
elimination
equations.
• Special
matrices
• Lecture
summary
• Considered coding a ‘Gaussian
• Next lecture
elimination’ routine to solve sets of
linear algebraic equations
34
Next lecture
• Last lecture
• Elimination of
unknowns
techniques we learnt in lecture 13?
• Gaussian
elimination
• Special
(e.g. Newton-Raphson)
matrices
• Next lecture
to solve sets of equations:
35