Docsity

Chapter II: Solving Systems of Linear Equations, Slides of Linear Algebra

Partial preview of the text

Download Chapter II: Solving Systems of Linear Equations and more Slides Linear Algebra in PDF only on Docsity!

MATH 350: Introduction to Computational

Mathematics

Chapter II: Solving Systems of Linear Equations

Greg Fasshauer

Department of Applied Mathematics Illinois Institute of Technology

Spring 2011

Outline

(^1) Applications, Motivation and Background Information

(^2) Gaussian Elimination = LU Decomposition

(^3) Forward and Back Substitution in MATLAB

(^4) Partial Pivoting

(^5) MATLAB Implementation of LU-Decomposition

(^6) Roundoff Error and the Condition Number of a Matrix

(^7) Special Matrices

(^8) An Application: Google’s Page Rank

Applications, Motivation and Background Information Representation of Linear Systems Equation form: x 1 + 2 x 2 + 3 x 3 = 1 2 x 1 + x 2 + 4 x 3 = 1 3 x 1 + 4 x 2 + x 3 = 1

Matrix form: A x = b , with

A =

 (^) , x =

x 1 x 2 x 3

 (^) , b =

Note: We will always think of vectors as column vectors. If we need to refer to a row vector we will use the notation x T^.

Remark

Most of the material discussed in this chapter can be found in Chapter 2 of [NCM].

Applications, Motivation and Background Information Avoid Inverses

Never use A−^1 to solve A x = b

In linear algebra we learn that the solution of

A x = b

is given by x = A−^1 b.

This is correct, but inefficient and more prone to roundoff errors.

Always solve linear systems (preferably with some decomposition method such as LU, QR or SVD).

Applications, Motivation and Background Information Matrix Division in MATLAB

How to solve linear systems by “division” in MATLAB

In order to mimic what we do (naturally) for a single equation, MATLAB provides two very sophisticated matrix division operators: For systems AX = B, we have the backslash (or mldivide) operator, i.e., X = A\B,

and XA = B is solved using a forward slash or (mrdivide) operator, i.e., X = B/A.

Remark These operators provide black boxes for the solution of (possibly even non-square or singular) systems of linear equations.

We now want to understand a bit of what goes on “inside” the algorithms.

Applications, Motivation and Background Information A Simple Example

Example

Solve the 3 × 3 linear system

x 1 + 2 x 2 + 3 x 3 = 1 2 x 1 + x 2 + 4 x 3 = 1 3 x 1 + 4 x 2 + x 3 = 1

using a simple (easily programmable) algorithm.

Solution

The main idea is to systematically reduce the system to upper triangular form since triangular systems are easy to solve. Thus, we will have

an elimination phase (resulting in an upper triangular system), and a back substitution phase (during which we solve for the variables).

Applications, Motivation and Background Information A Simple Example

Solution (cont.)

The system x 1 + 2 x 2 + 3 x 3 = 1 (1) − 3 x 2 − 2 x 3 = − 1 (2’)

−

x 3 = −

is upper triangular. Now we do the back substitution:

From (3”): x 3 = (^) −− 204 //^33 = (^15) Insert x 3 into (2’): x 2 = − (^13)

5

Insert x 3 and x 2 into (1): x 1 =

5

5

Remark

In order to have an algorithm it is important to be consistent: in the elimination phase always subtract multiples of the pivot row from rows below it.

Gaussian Elimination = LU Decomposition

The algorithm from the previous example is known as Gaussian elimination (even though it can be found in Chinese writings from around 150 B.C., see “The Story of Maths”, Episode 2) Using matrix notation, the original system, A x = b , is  

 (^) x =

After the elimination phase we end up with the upper triangular matrix

U =

Note that A can be written as A = LU, with U as above, and

L =

where the entries in the lower triangular part of L correspond to the multipliers used in the elimination phase.

Gaussian Elimination = LU Decomposition

An even greater advantage

Consider the problem AX = B with many different right-hand sides associated with the same system matrix. In this case we need to compute the factorization A = LU only once, and then AX = B ⇐⇒ LUX = B. We then proceed as before: (^1) Solve LY = B by many forward substitutions (in parallel). (^2) Solve UX = Y by many back substitutions (in parallel).

Remark In order to appreciate the usefulness of this approach, note that one can show that the operations count for the matrix factorization is O( 23 n^3 ), while that for forward and back substitution is O(n^2 ).

Forward and Back Substitution in MATLAB Forward Substitution

We can easily solve a lower triangular system L x = b by simple forward substitution. In MATLAB this is done with the following function:

function x = forward(L,x) % FORWARD. For lower triangular L, % x = forward(L,b) solves Lx = b. [n,n] = size(L); x(1) = x(1)/L(1,1); for k = 2:n % for each row, from the top down j = 1:k-1; % all columns simultaneously x(k) = (x(k) - L(k,j)x(j))/L(k,k); end

Note that L(k,j)*x(j) calculates a dot product. Also, note that the right-hand side vector, here x, is overwritten with the solution. This function is included in bslashtx.m (see later).

Partial Pivoting

Example

Use Gaussian elimination to solve the linear system

6 x 1 + 2 x 2 + 2 x 3 = − 2 2 x 1 +

x 2 +

x 3 = 1 x 1 + 2 x 2 − x 3 = 0.

Solution

See the Maple worksheet PartialPivoting.mw. Using simulated double-precision and standard Gaussian elimination we get the answer

x = [ 1. 33333333333335 , 0 , − 5. 00000000000003 ]T^.

Swapping rows to obtain a “good” pivot gives the correct solution

x = [ 2. 60000000000002 , − 3. 80000000000001 , − 5. 00000000000003 ]T^.

Partial Pivoting

How to choose “good” pivots

The partial pivoting strategy is a simple one. In order to avoid the tiny multipliers we encountered in the example above, at step k of the elimination algorithm we take that element in rows k through n of the k-th column which is largest in absolute value as the new pivot element. We also swap the k-th row with the new pivot row (or keep track of this swap in a permutation matrix P – see next slide). Example In step k = 2 of the previous example we need to check the elements in the second column of the last two rows to find that the new pivot is the element a 32 , i.e., the larger of |a 22 | and |a 32 |

A =

0 10 −^15 − 0. 3333

 (^2 −→)↔(^3 )

0 10 −^15 − 0. 3333

Partial Pivoting

Pivoting revisited

In the Maple worksheet PartialPivoting.mw we saw that Gaussian elimination (with partial pivoting) for the matrix

A =

leads to

L =

0. 3333 5. 9999 × 10 −^16

 , U =

With the permutation matrix

P =

we can write the LU decomposition as PA = LU.

MATLAB Implementation of LU-Decomposition The Program lutx.m from [NCM]

The Program lutx.m from [NCM]

Most important statements of Gaussian elimination code: for k = 1:n-1 % for each row : : % Compute multipliers i = k+1:n; % for all remaining rows A(i,k) = A(i,k)/A(k,k); % Update the remainder of the matrix j = k+1:n; % and all remaining columns A(i,j) = A(i,j) - A(i,k)A(k,j); end Note: This would be quite a bit more involved in a programming language that does not use matrices and vectors as building blocks. In fact, A(i,j) is a matrix, and A(i,k)A(k,j) is (column vector)×(row vector), i.e., a rank one matrix. Both matrices are of size (n − k) × (n − k).