Вы находитесь на странице: 1из 15

LINEAR

EQUATIONS
Lec. 4.1: Linear Algebra in MATLAB
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4

Review of Linear Algebra

• Related Video: Computational Techniques, Module-3 Lecture-1


http://nptel.ac.in/courses/103106074/3

• A vector…
⎡ 2⎤ 3
• Is an ordered set of scalars x=⎢ ⎥
⎣ 3⎦ 2
• Has “dimension” (# of elements)

• Has direction and “norm” (distance)


Review of Linear Algebra: rank and Solution
!" + 2!% = 1 1 2 1 2
+= +=
!" − !% = 4 2 4 2 4
1 1
1 2 !" 1 ,=
4
,=
2

! =
1 −1 % 4

2x + 4y = 4 2x + 4y = 2
x + 2y = 1

(3, -1) x + 2y = 1 x + 2y = 1

x-y=4

Review of Linear Algebra: Condition Number

x + 2y =1
x = 3; y = −1
2 x + 3.999 y = 2.001

x + 2y =1
x = 1; y = 0
2 x + 3.999 y = 2

⎡1 2 ⎤
A=⎢ ⎥ Eigenvalues λ1 = −2 ×10− 4 ; λ2 = 4.99;
⎣2 3.999⎦
Useful MATLAB functions
command Purpose command Purpose
expm Matrix exponent inv Inverse of a matrix
logm Matrix logarithm rank Rank of a matrix
sqrtm Matrix square root cond Condition number
^ Matrix Power norm Norm of a vector/matrix

eig Eigen values and vectors lu LU factorization


svd Singular value decomposition chol Cholesky factorization
schur Schur decomposition qr QR factorization

End of Lecture 4.1


LINEAR EQUATIONS
Lec. 4.2: Gauss Elimination
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4

Solving Linear Equations

• Example from: Computational Techniques, Module-3 Lecture-2


http://nptel.ac.in/courses/103106074/4
!" + !% + !- = 4
2!" + !% + 3!- = 7
3!" + 4!% − 2!- = 9
• Using MATLAB’s powerful Linear Algebra:
x=inv(A)*b or x=A\b
Gauss Elimination (Algorithm)

• Create matrix A234 = + | ,


• In each step
• + 6, 6 is the pivot element

• Use pivot element to create zeros in pivot column

• 89 = 89 − :;,9 8; where :;,9 = + <, 6 /+ 6, 6

Gauss Elimination (Example)

•+ 1,1 is pivot element


• :%," = + 2,1 /A 1,1 and 8% = 8% − :%," 8"

• Repeat for :-," and 8- (… and so on)

• With + 2,2 as pivot element, repeat the above steps for 8- and below
• … And so on until we obtain upper triangular matrix
Back-Substitution

> -
• !- = ? -,-

> % @? %,- AB
• !% = ? %,%

> " @? ",- AB @? ",% AC


• !" = ? ","

Assignment Problem

• Solve using Gauss Elimination + Back Subsitution:

1 2 2 1 1
2 2 4 2 0
G=
1 3 2 5 2
2 6 5 8 4
End of Lecture 4.2

LINEAR EQUATIONS
Lec. 4.3: LU Decomposition; Partial Pivoting
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4


Gauss Elimination
• Consider example from previous lecture:
!" + !% + !- = 4
2!" + !% + 3!- = 7
3!" + 4!% − 2!- = 9
• We solved it using Gauss Elimination + Back-substitution

• In this lecture
• LU Decomposition

• Partial Pivoting

LU Decomposition

•+ = IJ, where
J is matrix obtained after Gauss Elimination, and

1 0 ⋯ 0
I = :%," 1 ⋯ 0
:-," :-,% ⋱ ⋮
Gauss Elimination + Partial Pivoting

• Idea of Partial Pivoting is to use Row Exchange to ensure the pivot


element + 6, 6 is the largest element in the column

End of Lecture 4.3


LINEAR EQUATIONS
Lec. 4.4: Gauss Siedel
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4

Iterative Methods for Solving Linear Equations

• Jacobi:

,N − ∑9QN +N,9 !9 ;
!N;O" =
+N,N
• Gauss-Siedel:

;O"
,N − ∑N@"
9R" +N,9 !9 + ∑SNO" +N,9 !9 ;
!N;O" =
+N,N
Gauss Siedel
http://nptel.ac.in/courses/103106074/8

• Consider the examples:

!" + 2!% = 1 !" + 2!% = 1


!" − !% = 4 !" − !% = 4

!";O" = 1 − 2!%; !";O" = 4 + !%;


!%;O" = −4 + !";O" !%;O" = "% 1 − !";O"

Gauss Siedel

• Consider example from previous lecture:


!" + !% + !- = 4
2!" + !% + 3!- = 7
3!" + 4!% − 2!- = 9
• Rearrange as Eq. (2), (3), (1) and solve
Assignment Problem

• Solve using Gauss Siedel method for:

1 2 2 1 1
2 2 4 2 0
G=
1 3 2 5 2
2 6 5 8 4

End of Lecture 4.4


LINEAR EQUATIONS
Lec. 4.5: Tri-Diagonal Matrix Algorithm
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras

NPTEL Course: MATLAB Programming for Numerical Computations — Week-4

Tri-Diagonal Matrix

T" U" 0 ⋯ 0 0 ,"


V% T% U% ⋯ 0 0 ,%
+= ⋮ ⋮ ⋱ ⋱ ⋮ ⋮ ,= ⋮
0 0 ⋯ ⋯ TS@" US@" ⋮
0 0 ⋯ ⋯ VS TS ,S
Example
• Example from: Computational Techniques, Module-3 Part-5
http://nptel.ac.in/courses/103106074/7
T %W
= X W − 25 , WY = 100, WY = 25
T! % ARZ AR"

• If we use central difference formula,

T %W W;O" − 2W; + W;@"


[ =
T! % ℎ%
A\

Tri-diagonal System of Equations

Discretizing in 10 intervals, with X = 4, results in following equations:

W" = 100
W" − 2 + : W% + W- = ^ : = 0.04
W% − 2 + : W- + W_ = ^
⋮ ⋮ ^ = −1.0
W"" = 25
Tri-Diagonal Matrix

T" U" 0 ⋯ 0 0 ,"


V% T% U% ⋯ 0 0 ,%
+= ⋮ ⋮ ⋱ ⋱ ⋮ ⋮ ,= ⋮
0 0 ⋯ ⋯ TS@" US@" ⋮
0 0 ⋯ ⋯ VS TS ,S

We will solve the above problem using Thomas Algorithm


(For Theory, see: http://nptel.ac.in/courses/103106074/7)

End of Lecture 4.5