Beruflich Dokumente
Kultur Dokumente
Lecture8
Whats the big deal? ....cost
Look at 1D:
3 equations
3 unknowns
each unknown coupled to its neighbor
2x
1
x
2
= 5.8
x
1
2x
2
x
3
= 13.9
x
2
2x
3
= 0.03
or
2 1 0
1 2 1
0 1 2
x
1
x
2
x
3
5.8
13.9
0.03
Easy in 1d
2 1
.
.
.
1 2 1
.
.
.
1 2
8 1 1
.
.
.
1 1 8 1 1
.
.
.
1 1 8
Hand Calculations
Cartoon Version
The Algorithm
Gaussian Elimination with Pivoting
Implementation
Solving Systems with the Backslash Operator
Solving Diagonal Systems
The system dened by
A =
1 0 0
0 3 0
0 0 5
b =
1
6
15
is equivalent to
x
1
= 1
3x
2
= 6
5x
3
= 15
The solution is
x
1
= 1 x
2
=
6
3
= 2 x
3
=
15
5
= 3
Solving Diagonal Systems
The system dened by
A =
1 0 0
0 3 0
0 0 5
b =
1
6
15
is equivalent to
x
1
= 1
3x
2
= 6
5x
3
= 15
The solution is
x
1
= 1 x
2
=
6
3
= 2 x
3
=
15
5
= 3
Solving Diagonal Systems
The system dened by
A =
1 0 0
0 3 0
0 0 5
b =
1
6
15
is equivalent to
x
1
= 1
3x
2
= 6
5x
3
= 15
The solution is
x
1
= 1 x
2
=
6
3
= 2 x
3
=
15
5
= 3
Solving Diagonal Systems (1)
Listing 1: Diagonal System Solution
1 given A, b
2 for i = 1 . . . n
3 x
i
= b
i
/a
i,i
4 end
In M:
1 >> A = ... % A is a diagonal matrix
2 >> b = ...
3 >> x = b./diag(A)
This is the only place where element-by-element division (.*) has anything to
do with solving linear systems of equations.
Operations?
Try...
Sketch out an operation count to solve a diagonal system of equations...
cheap!
one division n times O(n) FLOPS
Operations?
Try...
Sketch out an operation count to solve a diagonal system of equations...
cheap!
one division n times O(n) FLOPS
Triangular Systems (1)
The generic lower and upper triangular matrices are
L =
l
11
0 0
l
21
l
22
0
.
.
.
.
.
.
.
.
.
l
n1
l
nn
and
U =
u
11
u
12
u
1n
0 u
22
u
2n
.
.
.
.
.
.
.
.
.
0 u
nn
2 1 2
0 3 2
0 0 4
b =
9
1
8
is equivalent to
2x
1
+ x
2
+ 2x
3
= 9
3x
2
+ 2x
3
= 1
4x
3
= 8
Solve in backward order (last equation is solved rst)
x
3
=
8
4
= 2 x
2
=
1
3
(1 + 2x
3
) =
3
3
= 1
x
1
=
1
2
(9 x
2
2x
3
) =
4
2
= 2
Solving Triangular Systems (3)
A =
2 1 2
0 3 2
0 0 4
b =
9
1
8
is equivalent to
2x
1
+ x
2
+ 2x
3
= 9
3x
2
+ 2x
3
= 1
4x
3
= 8
Solve in backward order (last equation is solved rst)
x
3
=
8
4
= 2 x
2
=
1
3
(1 + 2x
3
) =
3
3
= 1
x
1
=
1
2
(9 x
2
2x
3
) =
4
2
= 2
Solving Triangular Systems (4)
A =
2 1 2
0 3 2
0 0 4
b =
9
1
8
is equivalent to
2x
1
+ x
2
+ 2x
3
= 9
3x
2
+ 2x
3
= 1
4x
3
= 8
Solve in backward order (last equation is solved rst)
x
3
=
8
4
= 2 x
2
=
1
3
(1 + 2x
3
) =
3
3
= 1
x
1
=
1
2
(9 x
2
2x
3
) =
4
2
= 2
Solving Triangular Systems (5)
Solving for x
1
, x
2
, . . . , x
n
for a lower triangular system is called forward
substitution.
1 given L, b
2 x
1
= b
1
/
11
3 for i = 2 . . . n
4 s = b
i
5 for j = 1 . . . i 1
6 s = s
i,j
x
j
7 end
8 x
i
= s/
i,i
9 end
Using forward or backward substitution is sometimes referred to as performing
a triangular solve.
Solving Triangular Systems (6)
Solving for x
1
, x
2
, . . . , x
n
for a lower triangular system is called forward
substitution.
1 given L, b
2 x
1
= b
1
/
11
3 for i = 2 . . . n
4 s = b
i
5 for j = 1 . . . i 1
6 s = s
i,j
x
j
7 end
8 x
i
= s/
i,i
9 end
Using forward or backward substitution is sometimes referred to as performing
a triangular solve.
Operations?
Try...
Sketch out an operation count to solve a triangular system of equations...
cheap!
begin in the bottom corner: 1 div
row -2: 1 mult, 1 add, 1 div, or 3 FLOPS
row -3: 2 mult, 2 add, 1 div, or 5 FLOPS
row -4: 3 mult, 3 add, 1 div, or 7 FLOPS
.
.
.
row -j: about 2j FLOPS
Total FLOPS?
n
j=1
2j = 2
n(n+1)
2
or O(n
2
) FLOPS
Operations?
Try...
Sketch out an operation count to solve a triangular system of equations...
cheap!
begin in the bottom corner: 1 div
row -2: 1 mult, 1 add, 1 div, or 3 FLOPS
row -3: 2 mult, 2 add, 1 div, or 5 FLOPS
row -4: 3 mult, 3 add, 1 div, or 7 FLOPS
.
.
.
row -j: about 2j FLOPS
Total FLOPS?
n
j=1
2j = 2
n(n+1)
2
or O(n
2
) FLOPS
Gaussian Elimination
Goal is to transform an arbitrary, square system into the equivalent upper
triangular system so that it may be easily solved with backward substitution.
The formal solution to Ax = b, where A is an n n matrix is
x = A
1
b
In M:
1 >> A = ...
2 >> b = ...
3 >> x = A\b
Gaussian Elimination Hand Calculations (1)
Solve
x
1
+ 3x
2
= 5
2x
1
+ 4x
2
= 6
Subtract 2 times the rst equation from the second equation
x
1
+ 3x
2
= 5
2x
2
= 4
This equation is now in triangular form, and can be solved by backward
substitution.
Gaussian Elimination Hand Calculations (2)
The elimination phase transforms the matrix and right hand side to an
equivalent system
x
1
+ 3x
2
= 5
2x
1
+ 4x
2
= 6
x
1
+ 3x
2
= 5
2x
2
= 4
The two systems have the same solution. The right hand system is upper
triangular.
Solve the second equation for x
2
x
2
=
4
2
= 2
Substitute the newly found value of x
2
into the rst equation and solve for x
1
.
x
1
= 5 (3)(2) = 1
Gaussian Elimination Hand Calculations (3)
When performing Gaussian Elimination by hand, we can avoid copying the x
i
by using a shorthand notation.
For example, to solve:
A =
3 2 1
6 6 7
3 4 4
b =
1
7
6
A = [A b] =
3 2 1
6 6 7
3 4 4
1
7
6
The vertical bar inside the augmented matrix is just a reminder that the last
column is the b vector.
Gaussian Elimination Hand Calculations (4)
Add 2 times row 1 to row 2, and add (1 times) row 1 to row 3
A
(1)
=
3 2 1
0 2 5
0 2 3
1
9
7
A
(2)
=
3 2 1
0 2 5
0 0 2
1
9
2
A
(2)
=
3 2 1
0 2 5
0 0 2
1
9
2
x x x x x
x x x x x
x x x x x
x x x x x
The xs represent numbers, they are not necessarily the same values.
Begin elimination using the rst row as the pivot row and the rst element of
the rst row as the pivot element
x x x x x
x x x x x
x x x x x
x x x x x
indicates a
value that has been changed once.
x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
0 x
x x x x x
x x x x x
x x x x x
0 x
0 x
x x x x x
x x x x x
0 x
0 x
0 x
indicates a value
that has been changed twice.
x x x x x
0 x
0 x
0 x
x x x x x
0 x
0 0 x
0 x
x x x x x
0 x
0 0 x
0 0 x
indicates a value
that has been changed three times.
x x x x x
0 x
0 0 x
0 0 x
x x x x x
0 x
0 0 x
0 0 0 x
x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
0 x
0 0 x
0 0 0 x
2 4 2 2
1 2 4 3
3 3 8 2
1 1 6 3
b =
4
5
7
7
Note that there is nothing wrong with this system. A is full rank. The solution
exists and is unique.
Form the augmented system.
2 4 2 2
1 2 4 3
3 3 8 2
1 1 6 3
4
5
7
7
2 4 2 2
0 0 5 2
0 3 5 5
0 3 5 4
4
7
1
5
The next stage of Gaussian elimination will not work because there is a zero
in the pivot location, a
22
.
The Need for Pivoting (3)
Swap second and fourth rows of the augmented matrix.
2 4 2 2
0 3 5 4
0 3 5 5
0 0 5 2
4
5
1
7
2 4 2 2
0 3 5 4
0 0 0 1
0 0 5 2
4
5
4
7
2 4 2 2
0 3 5 4
0 0 5 2
0 0 0 1
4
5
7
4