Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13 Simultaneous Linear Equations – Gaussian Elimination (2) Partial Pivoting Dr .Qi Ying.

Similar presentations


Presentation on theme: "Lecture 13 Simultaneous Linear Equations – Gaussian Elimination (2) Partial Pivoting Dr .Qi Ying."— Presentation transcript:

1 Lecture 13 Simultaneous Linear Equations – Gaussian Elimination (2) Partial Pivoting
Dr .Qi Ying

2 Objectives Understanding and implementing partial pivoting
Understanding the solution technique for the tridiagonal system

3 Example Consider the following system
Using Gaussian elimination, the multiplying factor is 1/ε: Solution is:

4 Try it with our simple Gauss-elimination solver.
>> A=[1e-17 1 ; 1 1] A = >> b=[2 1]’ b = 2 1 >> gauss_simple(A,b) ans =

5 What happened? If ε is small, for example 1e-17, then 1/ε=1e17, and 1- 1/ε=-1/ε (when using double precision) becomes and the solution is which is obviously wrong

6 Gauss Elimination – Improvement using pivoting
Examine the factor used in the forward elimination Fac=A(m,i)/A(i,i); A(i,i) = leads to “divide by zero” error Solution: rearrange the rows so that the ith diagonal element is none zero. 2. A(i,i) is close to zero – leads to roundoff error. Solution: rearrange the rows so that the ith diagonal element is as large as possible.

7 Pivoting (1) Fundamentals
Pivot element: in the ith step of the forward elimination process, A(i,i) is called the pivot element

8 Pivoting (2) Fundamentals
Row switching does not change to solution of the linear system and the order of the unknowns.

9 Pivoting (3) Fundamentals
Switching columns does not change the solution of the system but change the order of the unknowns

10 Pivoting (4) Partial pivoting finds the coefficient with the largest absolute value in the column below the pivot element. Row swap operations are used to make the coefficient the pivoting element. Full pivoting finds the coefficient with the largest absolute value in the columns and rows below the pivot element. Both row and column swaps are used to make the coefficient the pivoting element.

11 Partial Pivoting row i row m Find the largest in these coefficients

12 Example Partial pivoting: Forward elimination:
This always generates the correct solution:

13 Partial pivoting code row i row m
[max_num,ir]=max(abs(A(i:n,i))) % ir is the row that has the % largest coefficient irow=ir+i-1; % row index in the complete matrix for m=i:n % now swap the coeffients between temp=A(i,m); % row (i) and row (irow) A(i,m)=A(irow,m); A(irow,m)=temp; end temp=b(i); % and the RHS coefficients b(i)=b(irow); b(irow)=temp; row i row m

14 Exercise Implement the partial pivoting code in the gauss_simple code and call the new function gauss_partial_pivoting. Test the Guass elimination with partial pivoting using the example. Could you get the correct result? >> A=[1e-17 1 ; 1 1] A = >> b=[2 1]’ b = 2 1 >> gauss_partial_pivoting(A,b)

15 Tri-diagonal Systems

16 Tri-diagonal Systems Gaussian elimination for tri-diagonal system – can be very efficient because there are many zeros in the coefficients Equations for forward elimination:

17 Tri-diagonal Systems Back substitution is also easier

18 Example Code function x=tridiag(e,f,g,r)
% TRIDIAG solves a tridiagonal linear equation system. % Inputs: % e,f,g: coefficients in the diagonals % r: right hand size vector % Output: % x: solution vector n=length(f); % forward elimination for i=2:n fac=e(i)/f(i-1); f(i)=f(i)-fac*g(i-1); r(i)=r(i)-fac*r(i-1); end % back substitution x(n)=r(n)/f(n) for i=n-1:-1:1 x(i)=(r(i)-g(i)*x(i+1))/f(i)

19 Non-singular condition
For a square coefficient matrix A, the product of the main diagonal elements after Gaussian elimination process is not zero. Otherwise, there could be more than 1 solution, or no solution at all, as we have seen previously.

20 Computation time analysis
A multiplication/division operation usually takes more time than add/subtraction. Order of magnitude analysis, no need for exact results.

21 (n-1)(n+1) (n-2)(n) (n-i)(n-i+2)
1: For each row, 1 division + n multiplication, and there are n-1 rows. Thus, total number of multiplication/division is: (n-1)(n+1) 2: For each row, 1 division + n-1 multiplication, and there are n-2 rows. Thus, total number of multiplication/division is: (n-2)(n) i: For each row, 1 division + n-i+1 multiplication, and there are n-i rows. Thus, total number of multiplication/division is: (n-i)(n-i+2) Total number of multiplication/division is:

22 Now consider a tridiagonal system
Total number of multiplication/division (including the right hand side) is: 4(n-1), or O(n)

23 Key points Forward elimination: row operation that converts the coefficient matrix into an upper triangular matrix Back substitution: solves the unknowns from the last to the first (Partial) pivoting: reduce round-off error and avoid divide by zero error during forward elimination Tridiagonal systems: fast equations Computation time estimation: order of magnitude analysis.


Download ppt "Lecture 13 Simultaneous Linear Equations – Gaussian Elimination (2) Partial Pivoting Dr .Qi Ying."

Similar presentations


Ads by Google