You are on page 1of 17

CE 206: Engineering Computation Sessional p System of Linear Equations

Gauss Elimination
Forward elimination
Starting with the first row, add or subtract multiples of that row to b li l f h 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 th next hi h t row. t highest Because of the upper-triangular nature of the matrix, each row will contain only one more unknown.

CE 206: Engg. Computation sessional

Dr. Tanvir Ahmed

Gauss Elimination code


function x = G f ti GaussNaive(A,b) N i (A b) % 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 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
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Partial Pivoting
Problems with nave Gauss elimination: a coefficient along the diagonal is 0 or close to 0 Solution: determine the coefficient with the largest absolute value in the column below the pivot element. Switch the rows so that the largest element is the pivot element. Class Exercise: Implement partial pivoting in your GaussNaive m function GaussNaive.m file Also modify the code in a way so that you can display the matrix after each operation. t i ft h ti
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Tridiagonal systems
A tridiagonal system is a banded system with a bandwidth of 3:
f1 e2 g1 f2 e3 g2 f3 x r x 1 r1 2 2 x 3 r3 = gn1 x n1 rn1 f n x n rn

g3

en1

f n1 en

Tridiagonal systems can be solved using the same method as Gauss elimination, but with much less effort.
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Tridiagonal solver
function x = T idi ( f f ti Tridiag(e,f,g,r) ) % input: % e = subdiagonal vector % f = diagonal vector % g = superdiagonal vector % r = right hand side vector % output: % x = solution vector n=length(f); % forward elimination for k = 2:n factor = e(k)/f(k-1); f(k) = f(k) - factor*g(k-1); r(k) = r(k) - factor*r(k-1); end % back substitution x(n) = r(n)/f(n); for k = n-1:-1:1 x(k) = (r(k)-g(k)*x(k+1))/f(k); / end
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

LU Factorization

The main advantage is once [ ] is decomposed, the same [ ] g [A] p [L] and [U] can be used for multiple {b} vectors.
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Built-in function lu()and / operator


To solve [A]{x}={b}, first decompose [A] to get [L][U]{x}={b} [L, [L U] = lu(A) Set up and solve [L]{d}={b}, where {d} can be found using [L]{d} {b}, forward substitution. d = L\b Set up and solve [U]{x}={d}, where {x} can be found using backward substitution substitution. x = U\d

CE 206: Engg. Computation sessional

Dr. Tanvir Ahmed

Decomposition vs. Inverse

Inverting a matrix is computationally expensive


CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Exercise 1: Truss Analysis


Find out all the bar forces and the external reactions of the following truss:
1000lb 1

F1 H2
2 30

90 60

F3
3

Node 1:

F1 cos 30 + F3 cos 60 = 0 F1 sin 30 F3 sin 60 = 1000

F2 V2 V3

Node 2:

Node 3:

F2 + F1 cos 30 + H 2 = 0 F1 sin 30 + V2 = 0
CE 206: Engg. Computation sessional

F2 + F3 cos 60 = 0 F3 sin 60 + V3 = 0
Dr. Tanvir Ahmed

Exercise 1: Truss Analysis


Unknowns External External forcing

0.5 0 0 0 0 F1 0 0.866 0.5 0 0.866 0 0 0 F2 1000 0.866 1 0 1 0 0 F3 0 = 0.5 0 0 0 1 0 H 2 0 0 1 0.5 0 0 0 V2 0 0 0.866 0 0 1 V3 0 0


F1 = 500 F2 = 433 F3 = 866 H 2 = 0 V2 = 250 V3 = 750
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Exercise 1: Truss Analysis


External f E t l forces h have no effect on LU decomposition, th f ff t d iti therefore it need d not be implemented over and over again for a different external force Example: Analyze the same truss with two horizontal 1000 lb forces due to wind Just change the forcing matrix and solve
1000 0 1000 0 0 0
CE 206: Engg. Computation sessional

1000lb

F1 H2
2 30

90 60 60

F3
3

1000lb

F2 V2 V3

F2 = 250 F3 = 500 H 2 = 2000 V2 = 433 V3 = 433


Dr. Tanvir Ahmed

F1 = 866

Exercise 2: Body in motion


Three bl k are connected b a weightless cord and rest on an Th blocks t d by i htl d d t inclined plane. Develop the set of three simultaneous equations and solve for acceleration and tensions in the cable.

Frictionfactor=0.375

45

Frictionfactor=0.25

[Use your free body diagram concept from analytic mechanics and apply Newtons second law] Newton s
CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Other engineering applications


Electrical Circuits
3 10 2 10 5 V6=0V 4 5 1 V1=200V 3 i32 5 4 i43 i54 15 20 6 5 i52 i65 6 2 i12 1

1 0 0 0 i12 0 1 1 0 1 0 1 1 0 i52 0 0 0 1 0 0 1 i32 0 = 0 0 0 0 1 1 i65 0 0 10 10 0 15 5 i54 0 0 i43 200 5 10 0 20 0


CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Other engineering applications


Mass-spring system ( p g y (Steady-state) y )

Stiffness matrix

CE 206: Engg. Computation sessional

Dr. Tanvir Ahmed

Matrix condition number


The matrix condition number can be used to estimate the precision of solutions of linear algebraic equations. Cond[A] is obtained by calculating Cond[A]=||A||||A-1||
column - sum norm l Frobenius norm row - sum norm spectral norm (2 norm) A 1 = max aij
1 jn n

A f =

a
i=1 j=1 n 1in j=1 1/2

i=1 n n

2 ij

A = max aijj A 2 = ( max )

Matlab built-in function: cond(A, p)


CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Matrix condition number


An ill-conditioned matrix will have very high condition number. It can be shown that
X A Cond[A] X A

Relative error of the norm of the computed solution will be very sensitive to the relative error in norm of coeff. of [A] If the coefficients of [A] are known to t digit precision, the solution [X] may be valid to only t - log10(Cond[A]) digits. Example:
1 1 / 2 1 / 3 A = 1 / 2 1 / 3 1 / 4 1 / 3 1 / 4 1 / 5

cond ( A) = 451.2 >> 1

CE 206: Engg. Computation sessional

Dr. Tanvir Ahmed

You might also like