Sie sind auf Seite 1von 54

CH 3073: Numerical Methods

CH 5500: Numerical Methods I


Autumn 2014-15

Instructor: Kishalay Mitra


Department of Chemical Engineering
Indian Institute of Technology Hyderabad

(kishalay@iith.ac.in)
Tue / Thu: 11:30 AM 1 PM
Room # 132
Indian Institute of Technology Hyderabad

Overview
Topics

Linear Algebraic Equations (5 lecs)


Nonlinear Algebraic Equations (3 lecs)
Function Approximation (6 lecs)
Ordinary Differential EquationsInitial Value Problems (ODE-IVPs)
(10 lecs)
Programming in MATLAB (10 classes)

Pre-requisites of the Course

BE / BTech in Chemical Engg / Chem Tech


Programming Skills (MATLAB)

Textbook for the Course

S. K. Gupta, Numerical Methods for Engineers, New Age Intl.


Publishers, New Delhi, 2nd Ed., 2009.

Indian Institute of Technology Hyderabad

Examples
Topics

Linear Algebraic Equations

Nonlinear Algebraic Equations

Indian Institute of Technology Hyderabad

Examples (contd.)
Topics

ODE - IVPs

ODE - BVPs

Indian Institute of Technology Hyderabad

Evaluation
No.

Type

Final
Weightage

Tentative
Time

Remarks

1.

Mid Semester Exam

30

End Sept.

Absolute
Grading

2.

End Semester Exam

30

End Nov.

Absolute
Grading

3.

Class Tests, Computer


Programming

15+15

Almost
each
class

Relative
Grading

4.

Attendance & Class


Performance

10

85%
attendance

Total = 100

Indian Institute of Technology Hyderabad

Programming Practices
Problems

Finding sum of a digit and reverse the order


In an array, store all the Fahrenheit values given Celsius values from -100C
to 100C
x3 x5 x7
x 2 n 1
n
... 1
Sum the following series Sum x

Tabulate values of below function for integer values of k = 0 to 15

3!

5!

7!

2n 1!

Sum e a a k / k!
Use of subroutines (preferably)

Solution of a quadratic equation


Write program to evaluate any nth order polynomial
Fitting a straight line through number of given points
Write program to generate Fibonacci sequence
Given a sequence of numbers, sort them in ascending and descending order
Write programs to multiply two matrices taking their sizes, values of elements
as inputs

Indian Institute of Technology Hyderabad

Programming
Programming Review

EVALUATE S = 1 + 2 + 3 + . + n
and COMPARE WITH analytical results
S = n(n + 1)/2

GET FAMILIAR WITH C, FORTRAN,


MATLAB

PLOT S vs. n

GET FAMILIAR WITH MATLAB, MS


Excel

Indian Institute of Technology Hyderabad

Chapter 1
LINEAR ALGEBRIC EQUATIONS

WHEN DO WE HAVE SOLUTIONS

GAUSS ELIMINATION

THOMAS ALGORITHM

LU DECOMP

GAUSS SEIDEL, SOR, ETC.

Indian Institute of Technology Hyderabad

Representing MENU
SET OF M EQUATIONS IN N UNKNOWNS
SIMULTANEOUS LINEAR ALGEBRAIC
EQUATIONS

a11x1 + a12 x2 + ..+ a1N xN = b1


a21x1 + a22 x2 + ..+ a2N xN = b2
.
.
.
.
aM1x1 + aM2 x2 + ..+ aMN xN = bM
e.g. x1 + x2 = 4
2x1 + 3x2 = 11
PDE
ODE

NLE

SLE
9

Indian Institute of Technology Hyderabad

Cramers Rule
Example 1.1
X1 + X2 = 4
2X1 + 3X2 = 11

Unique solution exists in case det(A) is nonsingular


Additionally, if b = 0, |Aj| = 0
all xj = 0 (trivial solution)

In case A is singular

First case: Identical equations


1 equation 2 unknown Infinite solutions
Second case: Incompatible and no solution exist

10

Indian Institute of Technology Hyderabad

Reviewing Rank r

(AUGMENTED A Aug A)
Aug A [ A b]

One highest possible size non zero determinant

1 1 4
2 3 11

1 1
2 3 0

r=2

r=1

11

Indian Institute of Technology Hyderabad

Solution exists
IF and ONLY IF

RANK A = RANK (Aug A)

COMPATABLE EQUATIONS

FOR M = N (FOR CHEMICAL ENGG. PROBLEMS)

IF r (A) = N

UNIQUE SOLUTION

IF r (A) < N

INFINITE SOLNS

12

Assume N-r variables

Indian Institute of Technology Hyderabad

Example
Example 1.3
X1 + X2 = 4
2 X1 + 3 X2 = 11

2 X1 + 3 X2 = 11
4 X1 + 6 X2 = 22

2 X1 + 3 X2 = 11
4 X1 + 6 X2 = 20

13

Indian Institute of Technology Hyderabad

Homogeneous Case
ANxN xNx1 = bNx1
IF
Homogeneous equations

a11x1 + a12x2 = 0
a22x1 + a22x2 = 0; AND r(A) = 2 = N
Unique Solution

Trivial solution

NON-TRIVIAL SOLUTIONS IF r(A) < N

4 2
14

Indian Institute of Technology Hyderabad

Homogeneous Case (contd.)


NON-TRIVIAL SOLUTIONS IF r(A) < N

r(A) = 1
Nr=21=1
ASSUME x1 = 5; THEN x2 = -10
x1 = 10; THEN x2 = -20

15

Indian Institute of Technology Hyderabad

Review

= : FOCUS ON ONLY

SOLUTIONS ONLY IF RANK =

IF RANK = , UNIQUE SOLUTION

IF RANK < , MANY SOLUTIONS

HOMOGENEOUS EQUATIONS (b = 0)

TRIVIAL SOLUTION x = 0, IF RANK =

NONTRIVIAL SOLUTION, IF RANK <

16

Indian Institute of Technology Hyderabad

Direct Methods

()
Ax=b

RANK =

Cramers Rule: Excessive Calculations (N2 N!)


Another route: A-1b
Equally expensive and suffers from additional problem of sensitivity
(round off errors)

17

Indian Institute of Technology Hyderabad

G E Example
Example 1.4

18

Indian Institute of Technology Hyderabad

Direct Methods
PIVOT

()
Ax=b

RANK =

Cramers Rule: Excessive


Calculaitons

19

Indian Institute of Technology Hyderabad

G E (contd.)
PIVOT

20

Indian Institute of Technology Hyderabad

G E (contd.)

Product of diagonal terms gives you the value of determinant


21

Indian Institute of Technology Hyderabad

Computational Complexity

22

Indian Institute of Technology Hyderabad

Pivoting

NOTE

Which one to prefer row or column shift? Which row / column to


shift? One that gives you largest absolute value in the pivot position
Process ends when zero cannot be removed from pivot position and
this occurs when set of equations does not have unique solutions
23

Indian Institute of Technology Hyderabad

G E Example (Pivoting)
Example 1.5

24

Indian Institute of Technology Hyderabad

Pivoting Practice

Get correct result with pivoting and wrong result


without pivoting

25

Indian Institute of Technology Hyderabad

Thomas Algorithm
Finite Difference BVP
Stage operations

26

Indian Institute of Technology Hyderabad

Thomas Algorithm (contd.)

27

Indian Institute of Technology Hyderabad

LU Decomposition (Factorization)
Cases where A does not
change but b changes

1
(1)
a 21
(1)
a11
(1)
a 31
(1)
a11
L
.
.

.
(1)
a N1
(1)
a11

a (322 )
a (222 )

a (NN,N1)1
a (NN11, N) 1

.
.

Multiplying factors are multiplied with the pivot row and added
with the other rows we take ve of multiplying factors
28

Indian Institute of Technology Hyderabad

LU Decomp (contd.)
We can show

A = L U;

Ly=b

A x = L (U x) = b
SOLVE BY FORWARD /DOWNWARD SWEEP

Remember this b is original b, not the modified b as in case of


Gauss Elimination

Ux=y

1
(1)
a 21
(1)
a11
(1)
a 31
(1)
a11
L
.
.

.
(1)
a N1
(1)
a11

THEN SOLVE BY REVERSE SWEEP


0

a (322 )
a (222 )

a (NN,N1)1
a (NN11, N) 1

.
.

29

Indian Institute of Technology Hyderabad

LU Decomp Example

Example 1.8

Ly=b

Ux=y

Solution: [-11 5 4]

30

Indian Institute of Technology Hyderabad

LU Decomp Example
7 1 2 x1 15
1 5 1 x 14

2
2 3 9 x 3 35

1
2 x1
15

1
1
1
1
1 ( )7 5 ( )1 1 ( )2 x 2 14 ( )15
7
7
7
7


2 ( 2 )7 3 ( 2 )1 9 ( 2 )2 x 3 35 ( 2 )15

7
7
7
7

By R2 (1/7) R1 &
R3 (2/7) R1

7 1

34
0

0 19

M21 = (1/7)
M31 = (2/7)

2 x1 15
5 83
x 2

7
7

59 x 3 215
7
7
31

Indian Institute of Technology Hyderabad

LU Decomp Example

7 1

34
0

0 19

2 x1 15
5 83
x 2

7
7

59 x 3 215
7
7

By R3 (19/34) R2

7
0

1
34
7
0

U 0

M21 = (1/7)
M31 = (2/7)
M32 = (19/34)

2
15
x
1

5
83
x 2

7
7

59 19 5 x 3 215 19 83

7 34 7
7 34 7
32

1
34
7
0

2
5

7
1911
238

0
0
1
L 1 / 7
1
0
2 / 7 19 / 34 1

Indian Institute of Technology Hyderabad

LU Decomp Example

1
34
7
0

2 x1 15
5 83
x 2

7
7

1911 x 3 5733
238
238

By GE, x1 = 1, x2 = 2, x3 = 3

Ux=y

Ly=b

0
0 y1 15
1
Ly 1 / 7
1
0 y 2 14
2 / 7 19 / 34 1 y 3 35

By Forward Sweep, y1 =
15, y2 = 83/7, y3 = 5733/238

Ux 0

1
34
7
0

2 x1 15
5 83
x 2

7
7

1911 x 3 5733
238
238

By Backward Sweep, x1 =
1, x2 = 2, x3 = 3
33

Indian Institute of Technology Hyderabad

LU Different b
0
0 y1 45
1
Ly 1 / 7
1
0 y 2 35
2 / 7 19 / 34 1 y 3 77
By Forward Sweep, y1 =
45, y2 = 200/7, y3 =
11466/238

Ux 0

1
34
7
0

2 x1 45
5 200
x 2

7
7

1911 x 3 11466
238
238

By Backward Sweep, x1 =
4, x2 = 5, x3 = 6

34

Indian Institute of Technology Hyderabad

LU Factorization
7 1 2
L11
A 1 5 1 LU L 21
2 3 9
L 31

0
L 22
L 32

0 U11
0 0
L 33 0

U12
U 22
0

U13
U 23
U 33

Doolittles Method

L11 L 22 L 33 1
L11U11 7 U11 7; L 31U11 2; L 31

Row Column
multiplication:
9 equations,
12 unknowns
3 to be assumed

2
7

L11U12 1 U12 1; L 31U12 L 32 U 22 3 L 32

19

34

L11U13 2 U13 2; L 31U13 L 32 U 23 L 33 U 33 9 U 33


L 21U11 1 L 21

1
;
7
34
7
5

7 35

L 21U12 L 22 U 22 5 U 22
L 21U13 L 22 U 23 1 U 23

1911
238

0
0 7
1
2
1

A 1 / 7
1
0 0 34 / 7
5 / 7
2 / 7 19 / 34 1 0
0
1911 / 238

U same as GE
Indian Institute of Technology Hyderabad

LU Factorization (contd.)
7 1 2
L11
A 1 5 1 LU L 21
2 3 9
L 31

0
L 22
L 32

0 U11
0 0
L 33 0

U12
U 22
0

U13
U 23
U 33

Row Column
Multiplication:
9 equations,
12 unknowns
3 to be assumed

Crouts Method

U11 U 22 U 33 1
L11U11 7 L11 7; L 31U11 2; L 31 2
1
19
; L 31U12 L 32 U 22 3 L 32
7
7
2
1911
L11U13 2 U13 ; L 31U13 L 32 U 23 L 33 U 33 9 L 33
7
238
L 21U11 1 L 21 1;

L11U12 1 U12

L 21U12 L 22 U 22 5 L 22
L 21U13 L 22 U 23 1 U 23

0
0
7
1 1 / 7 2 / 7

0 1 5 / 34
A 1 34 / 7
0

2 19 / 7 1911 / 238 0 0
1

34

7
5

34
36

Indian Institute of Technology Hyderabad

LU Factorization (contd.)
Row Column multiplication: 9 equations, 12 unknowns 3 to be assumed

2 1 0
U11
A 1 2 1 LU U12
0 1 2
U13

0
U 22
U 23

0 U11
0 0
U 33 0

U12
U 22
0

U13
U 23 L U T
U 33

Choleskys algorithm for symmetric positive definite A:


Take only the +ve roots
Twice as fast as GE

A 1 / 2
0

0 2

3/ 2
0 0
2 / 3 2 / 3 0
0

37

1/ 2
3/ 2
0

2 / 3
2 / 3
0

Indian Institute of Technology Hyderabad

LU Decomp (contd.)
Benefit of LU Decomposition:
Solving Ax = b when only b is changing as in Finite difference
technique of ODE-BVP
FOR ONE A AND M bs:

# OF MULTIPLICATIONS/ DIVISIONS =

Substantially lower than GE being solved repetitively

WORKS IF A IS DIAGONALLY DOMINANT

||

||
=;

OTHER LU DECOMPS: CHOLESKY PROB. 1.10 &


DOOLITTLE/CROUT PROB. 1.9
38

Indian Institute of Technology Hyderabad

Gauss Jordon

A, b & I (unit diagonal matrix) are written together

ELIMINATE TERMS IN A, BOTH BELOW AND ABOVE THE


DIAGONAL to convert it into I in turn, I is converted into A-1 & b
is converted into solution i.e. the values of x

Twice as time consuming as GE

39

Indian Institute of Technology Hyderabad

GJ Example

Example 1.9
R3/-1

(1/2)R3+R2, (-3/2)R3+R1

R2-R1, R3-2R1
R2/2

R2+R1

40

Indian Institute of Technology Hyderabad

Iterative Techniques
GAUSS JACOBI / GAUSS SIEDEL (G S) / SOR (SUCCESSIVE OVER
RELAXATION)

A x = b: a11x1 + a12x2 + a13x3 + --- + a1NxN = b1


x

( k 1)
1

b1 a12 x (2k ) a13 x (3k ) ......... a1N x (Nk )

a11

( k 1)
2

b 2 a 21 x

( k 1)
1

a 23 x

(k)
3

GUESS x1(1), x2(1), , xN(1)

a 24 ......... a 1N x

(k)
N

For G-Jacobi, k

a 22

For G-Siedel,
k+1
x N( k 1)

bN a N 1 x1( k 1) a N 2 x2( k 1) ......... a N N 1 x N( k11)


a NN
41

Indian Institute of Technology Hyderabad

Iterative Techniques (contd.)

i.e., IF A IS DIAGONALLY DOMINANT (DD), WILL CONVERGE (IF A IS NOT


DD, MAY OR MAY NOT CONVERGE) sufficient, not necessary condition

# MULTIPLICATIONS + DIVISIONS = MN2 (M = # OF ITERATIONS)

CONVERGENCE CRITERION:

MOST COMMON:

TOL: USER SPECIFIED

42

Indian Institute of Technology Hyderabad

GS Example

Example 1.10

43

Indian Institute of Technology Hyderabad

Iterative Techniques (contd.)


SCHEMATICALLY
a
0
X

xi(k)

a/2
xi,GS(k+1) xi,SOR(k+1)

(k )
(k 1)
X
; i 1, 2,...., N ( ALL)
i
i, GS

1)
1)
x(i ,kSOR
x(i k ) w x(i ,kGS
xi(k ) ; i 1, 2,...., N

w > 1 over relaxation,


w < 1 under relaxation,
w = 1 same as GS;
w typically in between 1 & 2, but no rule
Purpose is to speed up the process,
hence we generally go for w > 1

Solved Example: 1.11 Read


Reference: Press, Teukolsky, Vettering & Flannery - NUMERICAL RECIPES
Assignment: 1, 2, 3, 4, 6, 8, 10
Lab Assignment: Write a general program for G-Jacobi, G-Siedel, SOR to solve 13,
15, 18 (Keep a copy of your programs we will use them later in the course)

44

Indian Institute of Technology Hyderabad

SOR Example

Example 1.11

[1 2 1]

45

Indian Institute of Technology Hyderabad

Comparison Iterative schemes

GJ

GS

SOR

1 x 2k 1
x1
2
k 1
k 1
2

x
1
3
x 2k
2
x 3k 4 x 2k 1

1 x 2k 1
x1
2
2 x1k x 3k 1
k
x2
2
x 3k 4 x 2k

k 1
1

x
2
x1k,GS
2
k
k 1
2

x
1,GS
3

x 2k,GS

x 3k,GS
4 x 2k,GS

1.5x

1.5x

x1k x1k 1 1.5 x1k,GS x1k 1


x 2k x 2k 1
x 3k x 3k 1
46

k
2 ,GS

x 2k 1

k
3,GS

x 3k 1

Indian Institute of Technology Hyderabad

MATLAB Exercise
Gauss Elimination
function x=gauss(a,b)
% A = [2 1 0; 1 2 1; 0 1 1]; B = [1 2 4]; p = gauss(A,B);
% a - (n x n) matrix; b - column vector of length n
Steps:
% Step 0: get number of rows in matrix a, get length of b & send
message in case they are not same
% Step 1: form (n,n+1) augmented matrix
% Step 2: Start from row 1 and do for all rows that Pivot row
divided by pivot element
% Step 3: For other rows convert all elements below pivot into 0
% Step 4: begin back substitution
Indian Institute of Technology Hyderabad

MATLAB Exercise (contd.)


Gauss Elimination
function x=gauss(a,b)
m=size(a,1); % get number of rows in matrix a
n=length(b); % get length of b
if (m ~= n)
error('a and b do not have the same number of rows')
end
% Step 1: form (n,n+1) augmented matrix

a(:,n+1)=b;

Indian Institute of Technology Hyderabad

MATLAB Exercise (contd.)


for i=1:n
%Step 2: Start from row 1 and do for all rows
% Pivot row divided by pivot element
a(i, i:n+1) = a(i, i:n+1) / a(i, i);
%Step 3: For other rows convert all elements below pivot into 0
for j=i+1:n
a(j, i:n+1) = a(j, i:n+1) - a(j, i)*a(i, i:n+1);
end
end

Indian Institute of Technology Hyderabad

MATLAB Exercise (contd.)


%Step 4: begin back substitution
for j=n-1:-1:1
a(j,n+1) = a(j,n+1) - a(j,j+1:n)*a(j+1:n,n+1);
end
%return solution
x=a(:,n+1)

Indian Institute of Technology Hyderabad

MATLAB Exercise (contd.)


i = 1 a(1,1:4)=a(1, 1:4)/a(1,1);
j = 2 a(2, 1:4) = a(2, 1:4)- a(2, 1)*a(1,1:4);
j = 3 a(3, 1:4) = a(3, 1:4)- a(3, 1)*a(1,1:4);
i = 2 a(2,2:4)=a(2, 2:4)/a(2,2);

for i=1:n
a(i, i:n+1)=a(i, i:n+1)/a(i, i);
for j=i+1:n
a(j, i:n+1) = a(j, i:n+1)
- a(j, i)*a(i, i:n+1);
end
end

j = 3 a(3, 2:4) = a(3, 2:4)- a(3, 2)*a(2,2:4);


i = 3 a(3,3:4)=a(3, 3:4)/a(3,3);
j = 2 a(2,4) = a(2,4) a(2,3:3)*a(3:3,4);

for j=n-1:-1:1
a(j,n+1) = a(j,n+1)
a(j,j+1:n)*a(j+1:n,n+1);
end

j = 1 a(1,4) = a(1,4) a(1,2:3)*a(2:3,4);

Indian Institute of Technology Hyderabad

MATLAB Exercise (contd.)


i = 1 a(1,1:4)=a(1, 1:4)/a(1,1);
j = 2 a(2, 1:4) = a(2, 1:4)- a(2, 1)*a(1,1:4);
j = 3 a(3, 1:4) = a(3, 1:4)- a(3, 1)*a(1,1:4);
i = 2 a(2,2:4)=a(2, 2:4)/a(2,2);
j = 3 a(3, 2:4) = a(3, 2:4)- a(3, 2)*a(2,2:4);

i = 3 a(3,3:4)=a(3, 3:4)/a(3,3);

2 1 0 1
1 2 1 2

0 1 1 4
1 0 . 5 0 0 .5
0 1 . 5 1 1 .5

0 1 1 4
0
0 .5
1 0 .5
0 1 0.67 1

0 0 0.33 3
0
0 .5
1 0 .5
0 1 0.67 1

0 0
1
9

Indian Institute of Technology Hyderabad

MATLAB Exercise (contd.)


j = 2 a(2,4) = a(2,4) a(2,3:3)*a(3:3,4);
j = 1 a(1,4) = a(1,4) a(1,2:3)*a(2:3,4);

x3 = 9
x2 + 0.67x3 = 1
x1+ 0.5x2 + 0x3 = 0.5

x3 = 9 = a(3,4)
x2 + 0.67 a(3,4) = 1
x2 = 1 - 0.67 a(3,4) = a(2,4)
x1+ 0.5 a(2,4) + 0 a(3,4) = 0.5
x1 = 0.5 - 0.5 a(2,4) - 0 a(3,4) = a(1,4)

0
0 .5
1 0 .5
0 1 0.67 1

0 0
1
9
0
0 .5
1 0 .5
0 1 0.67 5

0 0
1
9
0
3
1 0.5
0 1 0.67 5

0 0
1
9
Indian Institute of Technology Hyderabad

G E Example using MATLAB

2 1 0 1
1 2 1 2

0 1 1 4

1 0 . 5 0 0 . 5
0 1 .5 1 1 .5

0 1 1 4
0
0 .5
1 0.5
0 1 0.67 1

0 0 0.33 3
0
0 .5
1 0.5
0 1 0.67 1

0 0
1
9
0
3
0
0.5 1 0.5
1 0.5
0 1 0.67 5 0 1 0.67 5


0 0
1
9
1
9 0 0
54

Indian Institute of Technology Hyderabad

Das könnte Ihnen auch gefallen