Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

ENG 1060

Computing for Engineers


Lecturer: Dr Christopher Hutchinson
Lecture 17:
MATLAB Solutions to Linear Algebraic Eqns
Last Lecture
• Last lecture

• 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

a21 x2  a22 x2      a2 n xn  b2 a11 a12 . . a1n x1  b1 


• Special
    
a21 a22 . . . x 2  b2 
matrices
.......
• Lecture
 . . . . .  .   . 
summary an1 xn  an 2 x2      ann xn  bn     
• Next lecture  . . . . .  .   . 

an1 . . . ann 

x n 
 
bn 


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’

Of course this is not really division,


since division is not a valid operator
in matrix math. So what is MATLAB doing?
5
Last Lecture
• Last lecture Mixing oil……
• Today’s Lecture

• 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

• Gaussian  Q13 (Q34  Q35 ) x 3  0 


elimination     
 Q24 Q34 Q44 x 4  0 
• Special
matrices 
 Q35 Q55 

x 5 
 
0 

• Lecture
summary

• Next lecture
Qx   F 


 Q1Qx  Q1F 
I x  Q1 F 
x  Q1F 

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

Note the problem!

This ‘ means transpose.


Change a row into a column

8
Last Lecture
• Last lecture

• Today’s Lecture

• Elimination of
unknowns
Mixing oil……
• Gaussian
elimination

• Special
matrices

• Lecture
summary

• Next lecture

Here are the concentrations in each


reactor: x1…..x5

9
Last Lecture
• Last lecture

• Today’s Lecture

• Elimination of
unknowns
Mixing oil……
• Gaussian
elimination

• Special
matrices

• Lecture
summary

• Next lecture

Pretty easy!

But how is MATLAB calculating


this?

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’

What is really being done here?

12
Elimination of Unknowns
• Last lecture
Consider we had only two equations in two unknowns (x1, x2):
• Today’s Lecture

• Elimination of Eq 1. a11  x1  a12  x2  b1


unknowns

• Gaussian Eq 2. a21  x1  a22  x2  b2


elimination

• Special If we multiply Eq. 1 by a21/a11 we obtain:


matrices
a 21 a a a21 a
• Lecture
Eq 1’. a11  x1  21 a12  x 2  21 b1 a21  x1  a12  x2  21 b1
summary a11 a11 a11 a11 a11
• Next lecture

If we then subtract Eq. 1’ from Eq. 2:


Eq. 2 - Eq. 1’
Eq 2. a21  x1  a22  x2  b2
a21 a
a21 a (a22  a12 )  x2  b 2  21 b1
a21  x1  a12  x2  21 b1 a11 a11
Eq 1’. a11 a11
This is only a function of x2

13
Elimination of Unknowns
• Last lecture
Consider we had only two equations in two unknowns (x1, x2):
• Today’s Lecture

• Elimination of Eq 1. a11  x1  a12  x2  b1


unknowns

• Gaussian Eq 2. a21  x1  a22  x2  b2


elimination
a21 a
• Special Multiply Eq. 1 by a21/a11 we obtain: Eq 1’. a21  x1  a12  x2  21 b1
matrices a11 a11
• Lecture
summary
Eq. 2 - Eq. 1’
• Next lecture a21 a
(a22  a12 )  x2  b 2  21 b1
a11 a11
Substitute for x2 back
This is only a function of x2
into Eq. 1 or 2 to find x1.

The simultaneous linear algebraic equations can be solved in two steps:


1. Elimination of the unknowns
2. Back substitution
14
Gaussian Elimination
• Last lecture The simultaneous linear algebraic equations can be solved in two steps:
• Today’s Lecture1. Elimination of the unknowns
2. Back substitution
• Elimination of
unknowns
This process can be readily programmed into a computer to solve n
• Gaussian
elimination simultaneous linear algebraic equations.
• Special
matrices It is called Gaussian Elimination (you have seen this in L4 ENG1091)
• Lecture
summary Gaussian elimination is one of the main methods MATLAB uses to solve
sets of equations of the form:
• Next lecture

a11 x1  a12 x2      a1n xn  b1


a21 x2  a22 x2      a2 n xn  b2
.......
an1 xn  an 2 x2      ann xn  bn

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

0 a22 a2n b2


16
Gaussian Elimination
• Last lecture a11 a12  a1n b1 
 
• Today’s Lecture
 a22  a2n b2
• Elimination of       
unknowns
  repeat for
• Gaussian an1 an 2  ann bn  remaining
elimination
rows
• Special
matrices
a11 a12  a1n b1 
 

• Lecture
 a22  a b 
2n 2 
summary (n-1) rows
      {<=> (n-1)
• Next lecture
  unknowns}
 an 2  a b 
nn n 

a11 a12  a1n b1 


 
  a22  a2n b2
get rid of x2 systematically
     (n-2) rows
 
  ann bn
 {<=> (n-2) unknowns}
17
Gaussian Elimination
a11 a11 x1  a12 x 2                  a1(n1) x n1  a1n x n  b1
• Last lecture a12  a1n b1 
  a22 x 2                  a2(n1) x n1  a2n x n  b2
• Today’s Lecture  a22  a2n b2

• Elimination of     
unknowns   (n1) x n1  a(n1)n
a(n1)(n1) (n1) x n  bn1
(n1)
 annn bnn 
• Gaussian annn x n  bnn
elimination
ELIMINATION COMPLETED:
• Special
matrices upper triangular matrix of aij’s
 
• Lecture
summary
bn
• Next lecture xn  n
annn
(n1)  a(n1)n
(bn1 (n1) x n )
x n1  BACK
(n1)
a(n1)(n1)
SUBSTITUTE
  
  

x1 
b1  a12 x 2                  a1(n1) x n1  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

• Today’s Lecture nb = n+1; a11 a12 . .  b1 


Aug = [A b];    
• Elimination of
a21 . . . 
unknowns A b   
• Gaussian
 . . . .   
elimination    
 . . . ann  b n 
• Special
matrices

• Lecture
summary

• Next lecture a11 a12 . . b1 


  
a21
 . . .  n
Aug 
 . . . . 
 
 . . . ann bn 
nb=n+1

 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 i2 factor  a21 a
summary 11
• Next lecture

  a2(1:n 1)  a2(1:n 1)  factor * a1(1:n 1)



    
  This subtracts factor*row 1 from
   
Aug   row 2 right across the augmented
     matrix, getting rid of the first
  element of row 2
    
23
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 1 i3 a31
factor 
• Lecture
summary a11
• Next lecture

 a3(1:n 1)  a3(1:n 1)  factor * a1(1:n 1)

    
     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 in an1
factor 
• Lecture
summary a11
• Next lecture

 an (1:n 1)  an (1:n 1)  factor * a1(1:n 1)

    
     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 i3 a32
factor 
• Lecture
summary a22
• Next lecture

a3( 2:n 1)  a3( 2:n 1)  factor * a2 ( 2:n 1)

    
     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 i4 factor  a42
• Lecture
summary a22
• Next lecture

a4 ( 2:n 1)  a4 ( 2:n 1)  factor * a2( 2:n 1)

    
     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)
in factor 
• Lecture
summary
a( n 1)( n 1)
• Next lecture

an ( n 1)  an ( n 1:n 1)  factor * a( n 1)( n 1:n 1)

    
    
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 i4 factor  a42
• Lecture
summary a22
• Next lecture

a4 ( 2:n 1)  a4 ( 2:n 1)  factor * a2( 2:n 1)

    
     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 en1 f n1 gn1 x n1 r
summary    n1

 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
 n1 e
 n1 gn1

 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

• Next lecture MATLAB uses a complex algorithm to:

1. Check the nature of the matrices (e.g. sparsely distributed,


tridiagonal, etc.)
2. If MATLAB can use a fast technique because the matrix has
special properties it will.
3. Otherwise MATLAB uses Gaussian Elimination

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

What about the iterative root finding


• Today’s Lecture

• Elimination of
unknowns
techniques we learnt in lecture 13?
• Gaussian
elimination

• Special
(e.g. Newton-Raphson)
matrices

These techniques can also be modified


• Lecture
summary

• Next lecture
to solve sets of equations:

These ‘Iterative methods’ are the topic


of L18.

35

You might also like