21.04.2015 Views

Lesson 16 Linear Systems - Bruce E. Shapiro

Lesson 16 Linear Systems - Bruce E. Shapiro

Lesson 16 Linear Systems - Bruce E. Shapiro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Lesson</strong> <strong>16</strong><br />

<strong>Linear</strong> <strong>Systems</strong><br />

In this section we will study the solution of a linear system of n equations with n<br />

unknowns. We cover it briefly here because some understanding of the problem will<br />

be necessary in our study of interpolation. However, this subject is normally part of<br />

the Math 481A curriculum and hence will not be covered in any detail here.<br />

Given a square n × n matrix A and n numbers b 1 , . . . , b n , we would like to solve the<br />

linear system<br />

Ax = b (<strong>16</strong>.1)<br />

Since it is generally numerically inefficient to compute an inverse (it generally requires<br />

O(n 3 ) operations we will not solve the system as<br />

x = A −1 b (<strong>16</strong>.2)<br />

although this is technically correct. Instead we will use the process of Gaussian<br />

elimination. We begin by observing that if we can transform equation <strong>16</strong>.1 into a<br />

form<br />

T x = b ′ (<strong>16</strong>.3)<br />

where T is an upper triangular matrix, and b ′ is a modified version of b, then we can<br />

read the solution for x n off the bottom row of the matrix, namely,<br />

x n = b ′ n/T nn (<strong>16</strong>.4)<br />

The matrix T is said to be in Row Echelon Form. The second to the last row of<br />

the system <strong>16</strong>.3 only depends on two variables, x n and x n−1 . Once we read off x n<br />

then we can solve for x n−1 . This process of back substitution moves back up the<br />

matrix one line at a time, solving for one variable at each step.<br />

Gaussian elimination is then summarized as follows:<br />

103


104 LESSON <strong>16</strong>. LINEAR SYSTEMS<br />

1. Convert the system Ax = b into an equivalent form T x = b ′ where T is uppertriangular.<br />

2. Solve for x using back-substitution.<br />

It is possible to take this idea one step further. If we can reduce equation <strong>16</strong>.3 to the<br />

form<br />

Dx = b ′′ (<strong>16</strong>.5)<br />

where D is a diagonal matrix, then it is even easier to read off the solutions, namel,<br />

x i = b ′ i/D ii . In this revised form the matrix D is said to be in Reduced Row<br />

Echelon Form, and the revised algorithm is called Gauss-Jordan Elimination.<br />

The revised algorithm is summarized:<br />

1. Convert the system Ax = b into an equivalent form T x = b ′ , where T is<br />

upper-triangular.<br />

2. Convert the system T x = b ′ into an equivalent form Dx = b ′′ , where D is<br />

diagonal.<br />

3. Solve for the x i .<br />

We will outline the first algorithm (row reduction followed by back-substitution). We<br />

start by writing the linear system<br />

a 11 x 1 + a 12 x 2 + · · · + a 1n x n = b 1 (<strong>16</strong>.6)<br />

a 21 x 1 + a 22 x 2 + · · · + a 2n x n = b 2 (<strong>16</strong>.7)<br />

.<br />

a n11 x 1 + a n2 x 2 + · · · + a nn x n = b n (<strong>16</strong>.8)<br />

From equation <strong>16</strong>.6 we can solve for x 1 in terms of x 2 , . . . , x n ,<br />

x 1 = (b 1 − a 12 x 2 − a 13 x 3 − · · · − a 1n x n )/a 11 (<strong>16</strong>.9)<br />

so if we already know x 2 , . . . , x n we can solve for x 1 immediately. But if we eliminate<br />

x 1 from each of the remaining equations, we have system of n − 1 equations in the<br />

n − 1 variables x 2 , . . . , x n , which is easier to solve than the original system because<br />

it is smaller. We get this system by subtracting an appropriate multiple of the first<br />

equation from each of the remaining equations, namely we subtract<br />

(a i1 /a 11 ) × (a 11 x 1 + a 12 x 2 + · · · + a 1n x n = b 1 ) (<strong>16</strong>.10)<br />

Math 481A<br />

California State University Northridge<br />

2008, B.E.<strong>Shapiro</strong><br />

Last revised: November <strong>16</strong>, 2011


LESSON <strong>16</strong>. LINEAR SYSTEMS 105<br />

from the i th equation. The resulting system for x 2 , . . . , x n is<br />

(a 22 − a 21 a 12 /a 11 )x 2 + · · · + (a 2n − a 21 a 1n /a 11 )x n = b 2 − a 21 b 1 /a 11 (<strong>16</strong>.11)<br />

(a 32 − a 31 a 12 /a 11 )x 2 + · · · + (a 3n − a 31 a 1n /a 11 )x n = b 3 − a 31 b 1 /a 11 (<strong>16</strong>.12)<br />

.<br />

(a n2 − a n1 a 12 /a 11 )x 2 + · · · + (a nn − a n1 a 1n /a 11 )x n = b n − a n1 b 1 /a 11 (<strong>16</strong>.13)<br />

The idea is to keep repeating this process until there is only one equation in the<br />

reduced system. The result is an “upper triangular system.” If the original matrix<br />

system is<br />

⎛<br />

⎜<br />

⎝<br />

⎞<br />

a 11 a 12 a 13 · · · a 1n<br />

a 21 a 22 a 23 · · · a 2n<br />

a 31 a 32 a 33<br />

.<br />

. . .<br />

⎟<br />

. . ⎠<br />

a n1 a n2 a n3 · · · a nn<br />

Then the reduced matrix system is<br />

⎛<br />

⎜<br />

⎝<br />

⎞<br />

x 1<br />

x 2<br />

x 3<br />

⎟<br />

. ⎠<br />

a n1<br />

⎛<br />

=<br />

⎜<br />

⎝<br />

⎞<br />

b 1<br />

b 2<br />

b 3<br />

⎟<br />

. ⎠<br />

x n<br />

⎛<br />

⎞ ⎛ ⎞ ⎛<br />

a 11 a 12 a 13 · · · a ′ 1n x 1<br />

0 a ′ 22 a ′ 23 · · · a ′ 2n<br />

x 2<br />

0 0 a ′ 33 · · · a ′ 3n<br />

x 3<br />

=<br />

⎜<br />

⎝ .<br />

..<br />

⎟ ⎜ ⎟ ⎜<br />

. . ⎠ ⎝ . ⎠ ⎝<br />

0 · · · 0 0 a ′ nn a n1<br />

This process is called Gaussian Reduction. We can then solve the system by<br />

starting on the bottom equation for x n , then the second from the bottom for x n−1 ,<br />

and so forth, until we obtain x 1 . This second step is called back substitution.<br />

Example <strong>16</strong>.1. Solve the system<br />

⎛<br />

1 2<br />

⎞ ⎛<br />

3<br />

⎝ 4 5 2 ⎠ ⎝<br />

2 8 5<br />

x<br />

y<br />

z<br />

⎞<br />

⎛<br />

⎠ = ⎝<br />

using Gaussian Reduction and back substitution.<br />

5<br />

10<br />

15<br />

⎞<br />

⎠<br />

⎞<br />

b ′ 1<br />

b ′ 2<br />

b ′ 3<br />

⎟<br />

. ⎠<br />

b‘ n<br />

Solution. The first step is to subtract multiples of the first row from each of the<br />

remaining two rows to make the coefficients of x zero in each of rows 2 and 3 of the<br />

system. Since the coefficient of x is 1 in the first row, 4 in the second row, and 2 in<br />

the third row, we subtract four times the first row from the second row, and twice<br />

the first row from the third row.<br />

⎛<br />

⎝<br />

1 2 3<br />

4 − 4(1) 5 − 4(2) 2 − 4(3)<br />

2 − 2(1) 8 − 2(2) 5 − 2(3)<br />

⎞ ⎛<br />

⎠ ⎝<br />

x<br />

y<br />

z<br />

⎞<br />

⎛<br />

⎠ = ⎝<br />

5<br />

10 − 4(5)<br />

15 − 2(5)<br />

⎞<br />

⎠<br />

2008, B.E.<strong>Shapiro</strong><br />

Last revised: November <strong>16</strong>, 2011<br />

Math 481A<br />

California State University Northridge


106 LESSON <strong>16</strong>. LINEAR SYSTEMS<br />

⎛<br />

⎝<br />

1 2 3<br />

0 −3 −10<br />

0 4 −1<br />

⎞ ⎛<br />

⎠ ⎝<br />

x<br />

y<br />

z<br />

⎞<br />

⎛<br />

⎠ = ⎝<br />

5<br />

−10<br />

5<br />

Now the first column is all zeroes (except for the first row). The next step is to<br />

subtract a multiple of the second row from the third row to get a zero in the second<br />

entry of the third row. Since the coefficient of y is -3 in the second row and 4 in the<br />

third row, we can add 4/3 times the second row to the third row.<br />

⎛<br />

⎝<br />

1 2 3<br />

0 −3 −10<br />

0 4 + (4/3)(−3) −1 + (4/3)(−10)<br />

⎛<br />

⎝<br />

1 2 3<br />

0 −3 −10<br />

0 0 −43/3<br />

⎞ ⎛<br />

⎠ ⎝<br />

⎞ ⎛<br />

⎠ ⎝<br />

x<br />

y<br />

z<br />

⎞<br />

x<br />

y<br />

z<br />

⎞<br />

⎠ = ⎝<br />

⎛<br />

⎠ = ⎝<br />

⎛<br />

⎞<br />

⎠<br />

5<br />

−10<br />

−25/3<br />

5<br />

−10<br />

5 + (4/3)(−10)<br />

This completes the Gaussian elimination. We can then read off the solution by backsubstitution.<br />

From the third row of the matrix,<br />

From the second row of the matrix,<br />

z = (−25/3)/(−43/3) = 25/43<br />

−3y − 10z = −10<br />

hence<br />

y = − 1 60<br />

(−10 + 10(25/43)) =<br />

3 43<br />

Finally, from the first row, we have<br />

x + 2y + 3z = 5<br />

( ) ( )<br />

60 25<br />

x = 5 − 2 − 3 = 20<br />

43 43 43<br />

We can write a simple recursive algorithm for Gaussian elimination as<br />

Algorithm <strong>Linear</strong>Solve<br />

Input: A, b<br />

If n > 1,<br />

{A ′ , b ′ } = Reduce(A, b)<br />

<strong>Linear</strong>Solve (A ′ , b ′ )<br />

End if<br />

x 1 = (b 1 − a 12 x 2 − a 13 x 3 − · · · − a 1n x n )/a 11<br />

⎞<br />

⎠<br />

⎞<br />

⎠<br />

Math 481A<br />

California State University Northridge<br />

2008, B.E.<strong>Shapiro</strong><br />

Last revised: November <strong>16</strong>, 2011


LESSON <strong>16</strong>. LINEAR SYSTEMS 107<br />

Return {x 1 , x 2 , . . . , x n }<br />

Algorithm Reduce<br />

Input: A, b<br />

n = dimension(b)<br />

For k = 2, . . . , n,<br />

m = a k1 /a 11<br />

For j = 2, . . . , n,<br />

a ′ k−1,j−1 = a kj − ma 1j<br />

End For<br />

b ′ k−1 = b k − mb 1<br />

End For<br />

Return {A ′ , b ′ }<br />

The recurse algorithm can be almost literally translated into Mathematica:<br />

reduce[A_, b_] := Module[{n, j, k, Aprime, bprime, m, row},<br />

n = Length[b];<br />

Aprime = {}; bprime = {};<br />

For[k = 2, k n , k++,<br />

m = A[[k, 1]]/A[[1, 1]];<br />

row = {};<br />

For[j = 2, j n, j++,<br />

AppendTo[row, A[[k, j]] - m* A[[1, j]]];<br />

];<br />

AppendTo[Aprime, row];<br />

AppendTo[bprime, b[[k]] - m*b[[1]]];<br />

];<br />

Return[{Aprime, bprime}];<br />

];<br />

gauss[A_, b_] := Module[{n, x, x1, Aprime, bprime},<br />

n = Length[b];<br />

x = {};<br />

If[n > 1,<br />

{Aprime, bprime} = reduce[A, b];<br />

x = gauss[Aprime, bprime];<br />

];<br />

x1 = b[[1]]/A[[1, 1]];<br />

For[k = 2, k n, k++,<br />

x1 = x1 - A[[1, k]]x[[k - 1]]/A[[1, 1]];<br />

];<br />

2008, B.E.<strong>Shapiro</strong><br />

Last revised: November <strong>16</strong>, 2011<br />

Math 481A<br />

California State University Northridge


108 LESSON <strong>16</strong>. LINEAR SYSTEMS<br />

x = Prepend[x, x1];<br />

Return[x];<br />

]<br />

For example, to solve the system<br />

⎛<br />

⎞ ⎛ ⎞ ⎛ ⎞<br />

0.1<strong>16</strong>093 0.2306<strong>16</strong> 0.34202 x 1 3<br />

⎝0.461232 0.897598 1.28558⎠<br />

⎝x 2<br />

⎠ = ⎝17⎠ (<strong>16</strong>.14)<br />

1.02606 1.92836 2.59808 x 3 5<br />

One could use this function by typing<br />

In:=<br />

Out:=<br />

A={{0.1<strong>16</strong>093, 0.2306<strong>16</strong>, 0.34202},<br />

{0.461232, 0.897598, 1.28558},<br />

{1.02606, 1.92836, 2.59808}};<br />

b={3, 17, 5};<br />

gauss[A, b]<br />

{-33612.9, 27351.9, -7024.58}<br />

In Mathematicawe can also solve the system directly by using the built in function<br />

<strong>Linear</strong>Solve[A,b].<br />

Gaussian elimination can fail if we divide by zero, and is susceptible to large errors or<br />

possible overflow if we divide by a very small number (relative to the other numbers in<br />

the matrix). Division occurs in two places in the algorithm: during the row reduction<br />

phase where we define m = a k1 /a 11 and during the back-substitution step at the end<br />

of the algorithm, where we solve for x 1 (here we also divide by a 11 , but its usually<br />

a different a 11 ). These numbers are called pivots. The solution is to rearrange the<br />

matrix (and the corresponding elements of b): if at any step along the way the pivot<br />

is zero, then the entire row is exchanged with a row that does not have zero in that<br />

column. If all of the remaining elements in that column are zero then the matrix is<br />

singular and there is no unique solution (or no solution at all).<br />

Math 481A<br />

California State University Northridge<br />

2008, B.E.<strong>Shapiro</strong><br />

Last revised: November <strong>16</strong>, 2011

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!