ce206_system of linear equations

17
CE 206: Engineering CE 206: Engineering Computation Sessional System of Linear Equations

Upload: imdadul-shohag

Post on 30-Oct-2014

13 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CE206_System of Linear Equations

CE 206: Engineering CE 206: Engineering Computation Sessionalp

System of Linear Equations

Page 2: CE206_System of Linear Equations

Gauss Elimination

Forward elimination•Starting with the first row, add or

b l i l f h 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 th t hi h t the next highest row.•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

Page 3: CE206_System of Linear Equations

Gauss Elimination codef ti G N i (A b)function x = GaussNaive(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'); endnb = n+1;Aug = [A b];% forward elimination% forward eliminationfor k = 1:n-1for i = k+1:nfactor = Aug(i,k)/Aug(k,k);Aug(i,k:nb) = Aug(i,k:nb)-factor*Aug(k,k:nb);

endend% back substitution% back substitutionx = zeros(n,1);x(n) = Aug(n,nb)/Aug(n,n);for i = n-1:-1:1

/

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

x(i) = (Aug(i,nb)-Aug(i,i+1:n)*x(i+1:n))/Aug(i,i);end

Page 4: CE206_System of Linear Equations

Partial PivotingProblems with naïve 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 Implement partial pivoting in your GaussNaive.m function fileAlso modify the code in a way so that you can display the

t i ft h ti

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

matrix after each operation.

Page 5: CE206_System of Linear Equations

Tridiagonal systems

A tridiagonal system is a banded system with a bandwidth of 3:

f1 g1e2 f2 g2

f

⎡ ⎢ ⎢

⎤ ⎥ ⎥

x1x2

⎧ ⎪ ⎪

⎫ ⎪ ⎪

r1r2

⎧ ⎪ ⎪

⎫ ⎪ ⎪ e3 f3 g3

⋅ ⋅ ⋅⋅ ⋅ ⋅

⋅ ⋅ ⋅

⎢ ⎢ ⎢ ⎢ ⎢

⎥ ⎥ ⎥ ⎥ ⎥

x3⋅⋅⋅

⎪ ⎪

⎪ ⎪

⎪ ⎪

⎪ ⎪

=r3⋅⋅⋅

⎪ ⎪

⎪ ⎪

⎪ ⎪

⎪ ⎪

en−1 fn−1 gn−1en fn⎣

⎢ ⎢ ⎢ ⎦

⎥ ⎥ ⎥

xn−1xn⎩

⎪ ⎪

⎪ ⎪ rn−1

rn⎩

⎪ ⎪

⎪ ⎪

Tridiagonal systems can be solved using the same method as Gauss elimination, but with much less

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

effort.

Page 6: CE206_System of Linear Equations

Tridiagonal solverf ti T idi ( f )function x = Tridiag(e,f,g,r)% input:% e = subdiagonal vector% f = diagonal vector% g = superdiagonal vector% r = right hand side vector% output:% x = solution vector% x = solution vectorn=length(f);% forward eliminationfor k = 2:nfactor = e(k)/f(k-1);f(k) = f(k) - factor*g(k-1);r(k) = r(k) - factor*r(k-1);

endend% back substitutionx(n) = r(n)/f(n);for k = n-1:-1:1

/

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

x(k) = (r(k)-g(k)*x(k+1))/f(k);end

Page 7: CE206_System of Linear Equations

LU Factorization

The main advantage is once [A] is decomposed, the same [L]

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

g [ ] p [ ]and [U] can be used for multiple {b} vectors.

Page 8: CE206_System of Linear Equations

Built-in function lu()and / operator

To solve [A]{x}={b}, first decompose [A] to get [L][U]{x}={b}[L U] = lu(A)[L, U] = lu(A)

Set up and solve [L]{d}={b}, where {d} can be found using Set up and solve [L]{d} {b}, where {d} can be found using forward substitution.

d = L\b

Set up and solve [U]{x}={d}, where {x} can be found using backward substitutionbackward substitution.

x = U\d

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Page 9: CE206_System of Linear Equations

Decomposition vs. Inverse

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Inverting a matrix is computationally expensive

Page 10: CE206_System of Linear Equations

Exercise 1: Truss AnalysisFind out all the bar forces and the external reactions of the following truss:

1000 lb

F1 F390°

1

Node 1:

F

3

H2 30° 60°2 3 100060sin30sin

060cos30cos

31

31

=°−°−=°+°−

FFFF

F2V2 V3

030cos 212 =+°+ HFF 060cos32 =°+− FF

Node 2: Node 3:

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

030sin 21 =+° VF 060sin 33 =+° VF

Page 11: CE206_System of Linear Equations

Exercise 1: Truss AnalysisExternal

UnknownsExternal forcing

⎥⎥⎥⎤

⎢⎢⎢⎡−

⎥⎥⎥⎤

⎢⎢⎢⎡

⎥⎥⎥⎤

⎢⎢⎢⎡ −

010000

001018660000866.005.00005.00866.0

2

1

FFF

⎥⎥⎥⎥

⎢⎢⎢⎢

=

⎥⎥⎥⎥

⎢⎢⎢⎢

⎥⎥⎥⎥

⎢⎢⎢⎢

−−−−−

000

0005010010005.000101866.0

2

3

VHF

⎥⎥⎥

⎦⎢⎢⎢

⎣⎥⎥⎥

⎦⎢⎢⎢

⎣⎥⎥⎥

⎦⎢⎢⎢

⎣ −− 00

100866.0000005.010

3

2

VV

7502500866433500

322

321

===−==−=

VVHFFF

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

322

Page 12: CE206_System of Linear Equations

Exercise 1: Truss AnalysisE t l f h ff t LU d iti th f it d External forces have no effect on LU decomposition, therefore it need not be implemented over and over again for a different external force

Example: Analyze the same truss

1000 lb 1

Example: Analyze the same truss with two horizontal 1000 lb forces due to wind

F1 F3

30°

90°

60°

Just change the forcing matrix and solve

F2

H2

V V

30° 602 31000 lb

⎥⎥⎥⎤

⎢⎢⎢⎡−

10000

1000

V2 V3

⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢−

00

1000

4334332000500250866

322

321

=−=−=−===

VVHFFF

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

⎥⎥⎦⎢

⎢⎣ 0

322

Page 13: CE206_System of Linear Equations

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

Friction factor = 0.375

45° Friction factor = 0.25

[Use your free body diagram concept from analytic mechanics and apply Newton’s second law]

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

and apply Newton s second law]

Page 14: CE206_System of Linear Equations

Other engineering applications

123 5Ω10ΩV =200V

123

Electrical Circuits

10Ω5Ω

V1=200V

i12

ii52

ii43

i32

4 5 615Ω 20ΩV6=0V 4 5 6

i65i54

⎥⎥⎥⎤

⎢⎢⎢⎡

⎥⎥⎥⎤

⎢⎢⎢⎡

⎥⎥⎥⎤

⎢⎢⎢⎡

−−000

100100011010000111

52

12

iii

⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢

=

⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢

⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢

−−−−

000

515010100110000

100100

54

65

32

iii

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

⎥⎥⎦⎢

⎢⎣⎥

⎥⎦⎢

⎢⎣⎥

⎥⎦⎢

⎢⎣ −− 20000200105 43i

Page 15: CE206_System of Linear Equations

Other engineering applications

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

Stiffness matrixStiffness matrix

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

Page 16: CE206_System of Linear Equations

Matrix condition numberThe 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||

l An

∑column - sum norm A 1 =1≤ j≤nmax aij

i=1∑

Frobenius norm A = a2n

∑n

∑Frobenius norm A f aijj=1∑

i=1∑

row - sum norm A∞

= max aij

n

∑∞ 1≤i≤nj

j=1∑

spectral norm (2 norm) A2

= μmax( )1/2

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

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

Page 17: CE206_System of Linear Equations

Matrix condition numberAn ill-conditioned matrix will have very high condition number.

ΔX ΔAIt can be shown that ΔXX

≤ Cond A[ ]ΔAA

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:

⎥⎥⎥⎤

⎢⎢⎢⎡

= 4/13/12/13/12/11

A 12.451)( >>=Acond

CE 206: Engg. Computation sessional Dr. Tanvir Ahmed

⎥⎦⎢⎣ 5/14/13/1