You are on page 1of 36

Matrix Algebra Using Matlab

Dr. Arpan Gupta


Associate Professor
Department of Mechanical Engineering
Graphic Era University
1
Solution to System of Linear Equations
Different methods to solve system of linear
equation
1. Graphical Method (for 2x2 system)
2. Cramer's rule
3. Using Matrix inverse
4. Elimination of variables

n n nn n n
n
n
n n
b x a x a x a
b x a x a x a
b x a x a x a
= + + +
= + + +
= + + +
......
......
......
2 2 1 1
2 2 2 22 1 21
1 1 2 12 1 11

System of n equations with n


variables.
2
Matrix Inverse
x
1
+ 2x
2
+ 3x
3
= 1
2x
1
+ 6x
2
+ 10x
3
= 0
3x
1
+ 14x
2
+28x
3
= -8

(
(
(

8
0
1
3
2
1
28 14 3
10 6 2
3 2 1
x
x
x
| |{ } { } b x A =
Matlab inbuilt function : inv

a = [1 2 3 ; 2 6 10 ; 3 14 28];
b = [1 0 -8];
x = inv(a)*b

Output :
x =

2.0000
1.0000
-1.0000

Problems with matrix inverse:
Computationally very expensive,
as the number of variable (n)
increases.
Takes lot of computational
memory and computational
time, as n becomes large.
{ } | | { } b A x
1
=
3
Gauss Elimination
Step by step eliminates variables in successive
equations by performing row operation on
whole equation.
Performing operation on equation means
operating on the Augmented Matrix.
Finally it converts the matrix into an upper
triangular matrix for which the solution
becomes straight forward.
The solution is obtained by back substitution.
4
Gauss Elimination
(
(
(

=
8
0
1
28 14 3
10 6 2
3 2 1
Aug
(
(
(

=
11
2
1
19 8 0
4 2 0
3 2 1
Aug
(
(
(

=
3
2
1
3 0 0
4 2 0
3 2 1
Aug
x
1
+ 2x
2
+ 3x
3
= 1 E1
2x
1
+ 6x
2
+ 10x
3
= 0 E2
3x
1
+ 14x
2
+28x
3
= -8 E3
x
1
+ 2x
2
+ 3x
3
= 1
2x
2
+ 4x
3
= -2
3x
3
= -3
x
1
+ 2x
2
+ 3x
3
= 1
2x
2
+ 4x
3
= -2
8x
2
+ 19x
3
= -11
E2 -> E2-2*E1
E3->E3-3*E1
E3->E3-4*E2
Back substitution is
used to obtain the
solution: x
3
, x
2
, x
1

5
Gauss Elimination
Forward elimination
Starting with the first row, add or subtract
multiples of that row to eliminate the first
coefficient from the second row and
beyond.
Continue this process with the second
row to remove the second coefficient
from the third row and beyond.
Stop when an upper triangular matrix
remains.
Back substitution
Starting with the last row, solve for the
unknown, then substitute that value into
the next highest row.
Because of the upper-triangular nature of
the matrix, each row will contain only one
more unknown.
Code for Gauss Elimination
A = [ 1 2 3; 2 6 10; 3 14 28];
b = [1 0 -8]';
[n,n]=size(A); % A should be square matrix
nb = n+1;
Aug = [A b]; % Augmented matrix
% forward elimination
for col = 1:n-1
for row = (col+1):n
factor = Aug(row,col)/Aug(col,col);
Aug(row,:) = Aug(row,:)-factor*Aug(col,:);
end
end
% back substitution
x = zeros(n,1);
x(n) = Aug(n,nb)/Aug(n,n);
for i = n-1:-1:1
x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);
end
x

7
LU factorization
Many times the solution of [A]{x}={b} is
required for a series of {b} vectors.
For examples, {b} can represent force vector
which is a function of time.
For such case, Gauss elimination becomes
inefficient.
A more efficient method is LU factorization or
LU decomposition.
8
LU Factorization
It converts a given matrix A into a product of a
lower and upper triangular matrix.
Matlab command : lu
Syntax: [l,u]=lu(A);


a = [3 -.1 -.2; 0.1 7 -.3;.3 -.2 10];
[l,u]=lu(a)
l =
1.00000 0.00000 0.00000
0.03333 1.00000 0.00000
0.10000 -0.02713 1.00000

u =
3.00000 -0.10000 -0.20000
0.00000 7.00333 -0.29333
0.00000 0.00000 10.01204
a =

3.00000 -0.10000 -0.20000
0.10000 7.00000 -0.30000
0.30000 -0.20000 10.00000
9
How LU factorization is used
[A]{x}={b}
[A] = [L][U]
[L][U]{x}={b}
Let us say [U]{x} = {y}
[L]{y}={b} or we can solve for {y} either by
back substitution or L\b
Then we can solve for {x}
[U]{x} = {y}

(
(
(

3
2
1
3
2
1
3 0 0
4 2 0
3 2 1
y
y
y
10
Cholesky Factorization for symmetric
matrices
Most of the matrices encountered in
engineering systems are symmetric matrices.
Symmetric matrix means [A]= [A]
T

a
ij
= a
ji

Such matrices give computational advantage
as only half space is required to store the
matrices and half computational time is
required.
11
The symmetric matrix can be factorized as


Matlab command is: chol
Matlab syntax: u = chol(a)


Cholesky Factorization for symmetric
matrices
| | ] [ ] [ U U A
T
=
a =

1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70

>> u = chol(a)
u =

1 1 1 1 1
0 1 2 3 4
0 0 1 3 6
0 0 0 1 4
0 0 0 0 1
octave:24> u'*u
ans =

1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70
12
More about Matlab solver \
Matlab checks for the elements of Matrix A and
depending on the type of matrix, it uses optimal
method/algorithm to solve for system of linear
equations.
Symmetric matrix
Triangular matrix
Banded matrix
Tridiagonal matrix etc.
Finally Gauss Elimination!
13
Some properties of Matrices
Determinant : Det(a)
If det(a) = 0 -> Matrix a cannot be inverted. It is called as a
singular matrix.
inv(a) will give problem.
Norm : norm(a) gives an idea of size of elements in
matrix.
Different type of norms
norm(a, 1)

norm(a,inf)


norm(a,2) same as norm(a)

=
s s
=
n
i
ij
n j
a A
1
1
1
max

=
s s

=
n
j
ij
n i
a A
1
1
max
14
Some properties of Matrices
Conditioning number: cond(a)

Higher the conditioning number, more difficult
it is to obtain the accurate inverse of matrix.
It is difficult to obtain solution to linear
equation from the ill-conditioned system.
Or the obtained solution is highly prone to
errors.
3 2
1 2
= +
= +
y x
y x
No solution
3 2 0001 . 1
1 2
= +
= +
y x
y x
How about this equation?
| |
1
= A A A Cond
15
a1 =

1.0000 2.0000
1.0001 2.0000

octave:49> cond(a1)
ans = 5.0001e+004

octave:52> inv(a1)
ans =

-10000.0 10000.0
5000.5 -5000.0
format long
inv(a1)*a1
ans =
0.999999999999724 0.000000000000000
0.000000000000138 1.000000000000000

octave:58> inv(a)
warning: inverse: matrix singular to machine precision, rcond = 0
ans =

Inf Inf
Inf Inf
octave:59> a
a =

1 2
1 2
16
Example of ill conditioned matrix : Hilbert matrix.

17
octave:70> hilb(3)
ans =

1.00000 0.50000 0.33333
0.50000 0.33333 0.25000
0.33333 0.25000 0.20000
octave:71> cond(ans)
ans = 524.06
Eigenvalue and Eigenvector
System of linear equation
For a non-zero vector {b} and for det(A)!=0, we
can get a solution of x.
What if the right hand side {b} = 0 ?
Such equations are called homogenous equation.
One solution is trivial solution {x} = {0}
There can be other solution also where det(A) = 0
This will lead to eigenvalues and eigenvectors.

18
| |{ } { } b x A =
| |{ } 0 = x A
Eigenvalue and Eigenvector
General form of eigenvalue problem.

where lamda is the eigenvalue and {x} is the
eigenvector.


Rather then setting {x} as zero, the eigenvalue
lamda is determined such that
19
| |{ } { } x x A =
| | | | | |{ } 0 = x I A
| | 0 det = I A
(a
11
) x
1
+ a
12
x
2
+ a
13
x
3
= 0

a
21
x
1
+ (a
22
) x
2
+ a
23
x
3
= 0

a
31
x
1
+ a
32
x
2
+ (a
33
) x
3
= 0

Eigenvalue and Eigenvector
Matlab function : eig
Syntax:
lamda = eig(A);
[v, lamda] = eig(A);
20
A vibration problem

21
m
2
d
2
x
2
dt
2

= k(x
2
x
1
) kx
2

m
1
d
2
x
1
dt
2

= k x
1
+ k(x
2
x
1
)
m
2
d
2
x
2
dt
2

k (x
1
2x
2
) = 0
m
1
d
2
x
1
dt
2

k ( 2x
1
+ x
2
) = 0
Collect terms:
Model With Force Balances
(F = ma)
x
i
= X
i
sin (e t) where
e =
2t
T
p

Differentiate twice:
x
i

= X
i
e
2
sin (e t)
Substitute back into system and collect
terms
Assume a Sinusoidal Solution
e
2
X
1

2k
m
1
X
2
= 0
k
m
1
e
2
X
2
= 0
2k
m
2
X
1
+
k
m
2
Given: m
1
= m
2
= 40 kg; k = 200 N/m
(10 e
2
) X
1
5 X
2
= 0

5 X
1
+ (10 e
2
) X
2
= 0
This is now a homogeneous system where the
eigenvalue represents the square of the fundamental
frequency.
Evaluate the determinant to yield a polynomial
Solution: The Polynomial Method
10 e
2
5 X
1


0
5 10 e
2
X
2


0
=
= (e
2
)
2
20e
2
+ 75
10 e
2
5
5 10 e
2

The two roots of this "characteristic polynomial"
are the system's eigenvalues:
e
2
=
15
5
e =
or
3.873 rad/s
2.36 rad/s
Calculations
e
2
= 5

e = 2.236 rad/s
T
p
= 2t/2.236 = 2.81 s
(10 e
2
) X
1
5 X
2
= 0
5 X
1
+ (10 e
2
) X
2
= 0
(10 5) X
1
5 X
2
= 0
5 X
1
+ (10 5) X
2
= 0
5 X
1
5 X
2
= 0
5 X
1
+ 5 X
2
= 0
X
1
= X
2

V =
0.7071
0.7071
(10 15) X
1
5 X
2
= 0
5 X
1
+ (10 15) X
2
= 0
5 X
1
5 X
2
= 0
5 X
1
5 X
2
= 0
X
1
= X
2
V =
0.7071
0.7071
e
2
= 15

e = 3.873 rad/s
T
p
= 2t/3.373 = 1.62 s
Matlab method
27
octave:80> a = [10 -5;-5 10]
a =

10 -5
-5 10

octave:81> [v,d]=eig(a)
v =

-0.70711 -0.70711
-0.70711 0.70711

d =

Diagonal Matrix

5 0
0 15
Principle Modes of Vibration

T
p
= 1.62 s
e = 3.873
rad/s


T
p
= 2.81 s
e = 2.236 rad/s

Some points about eigenvalue
problem
The eigen value are associated with the
frequency of vibration.
Eigen vectors are associated with the vibration
pattern or mode shape of the system.
Eigen vectors dont have absolute values.
Their relative values are important. However,
they are generally normalized to certain
values.
29
A Simple Truss Problem : system of
linear equation.
Calculate the
forces in
members of
truss and
reaction forces?
30
2000 N
F1
F3
F2
1
2
3
30
0
60
0

Force Equilibrium at nodes
Assume all members are in
Tension.
Eq at node 1




Similarly for node 2 and 3


31
2000 N
F1
F3
F2
1
2
3
30
0
60
0

F1
30
0

Node 1
F3
60
0

2000 N
0 ) 3 / cos( 3 ) 6 / cos( 1
0
= +
=

pi F pi F
F
x
0 2000 ) 3 / sin( 3 ) 6 / sin( 1
0
=
=

pi F pi F
F
y
0 2 ) 6 / sin( 1
0 2 ) 6 / cos( 1 2
= +
= + +
V pi F
H pi F F
H2
V2 V3
0 3 ) 3 / sin( 3
0 ) 3 / cos( 3 2
= +
=
V pi F
pi F F
Forming the system of linear equation
32
0 ) 3 / cos( 3 ) 6 / cos( 1 = + pi F pi F
0 2000 ) 3 / sin( 3 ) 6 / sin( 1 = pi F pi F
0 2 ) 6 / sin( 1
0 2 ) 6 / cos( 1 2
= +
= + +
V pi F
H pi F F
0 3 ) 3 / sin( 3
0 ) 3 / cos( 3 2
= +
=
V pi F
pi F F
Six equations six variables!
(F1, F2 F3, H2, V2, V3)
Our aim is to transform this set of
equation into a matrix form
[A]{x} = {b}

(
(
(
(
(
(
(
(

0
0
0
0
2000
0
3
2
2
3
2
1
... ... ... ... ... ...
... ... ... ... ... ...
... ... ... ... ... ...
... ... ... ... ... ...
... ... ... ... ... ...
... ... ... ... ... ...
V
V
H
F
F
F

(
(
(
(
(
(
(
(

0
0
0
0
2000
0
3
2
2
3
2
1
1 0 0 ) 3 / sin( 0 0
0 0 0 ) 3 / cos( 1 0
0 1 0 0 0 ) 6 / sin(
0 0 1 0 1 ) 6 / cos(
0 0 0 ) 3 / sin( 0 ) 6 / sin(
0 0 0 ) 3 / cos( 0 ) 6 / cos(
V
V
H
F
F
F
pi
pi
pi
pi
pi pi
pi pi
Coding the above equations
33
% code to solve a simple truss problem
a= zeros(6,6);
b = zeros(6,1);
b(2) = 2000;

c3 = cos(pi/3);
c6 = cos(pi/6);
s3 = sin(pi/3);
s6 = sin(pi/6);

a = [-c6 0 c3 0 0 0; -s6 0 -s3 0 0 0; c6 1 0 1 0 0;
s6 0 0 0 1 0; 0 -1 -c3 0 0 0; 0 0 s3 0 0 1];

f = a\b

(
(
(
(
(
(
(
(

0
0
0
0
2000
0
3
2
2
3
2
1
1 0 0 ) 3 / sin( 0 0
0 0 0 ) 3 / cos( 1 0
0 1 0 0 0 ) 6 / sin(
0 0 1 0 1 ) 6 / cos(
0 0 0 ) 3 / sin( 0 ) 6 / sin(
0 0 0 ) 3 / cos( 0 ) 6 / cos(
V
V
H
F
F
F
pi
pi
pi
pi
pi pi
pi pi
Results and verification
34
Output
octave:10> truss
f =

F1 -1.0000e+003
F2 8.6603e+002
F3 -1.7321e+003
H2 0.0000e+000
V2 5.0000e+002
V3 1.5000e+003

2000 N
F1
F3
F2
1
2
3
30
0
60
0

H2
V2 V3
Verification of results
Newtons Second Law for whole system

H2 = 0
V2 + V3 = 2000

F1 and F3 are negative > in compression.
F2 is in tension.

=
=
0
0
Fy
Fx
Verification or validation of
numerical results is very important!
Things to think about!
35
What if the truss has hundreds of members??
Imagine the size of the problem and
computation required?
How about calculating displacements?
Entry point to Finite Element Method!!
Thank you!
36

You might also like