Linear Systems - Introduction To Computers & Engineering
Linear Systems - Introduction To Computers & Engineering
00 Lecture 31
3 1 -2 x0 5
2 4 3 x1 = 35
1 -3 0 x2 -5
A x = b
3 x 3 3 x 1 3 x 1
1
Algorithm to Solve Linear System
x0 b0
Create matrix =aij
x1 = b1
x2 b2
A x b
x0 b0 =aij
Forward solve 0 x1 = b1
0 0 x2 b2
A x b
x0 b0 x0
Back solve x1 b1
0 = x1
0 0 x2 b2 x2
A x b
2
Gaussian Elimination: Back Solve
3 1 -2 5
0 10/3 13/3 95/3
0 0 15/3 75/3 (15/3)x2=(75/3) x2 = 5
3 1 -2 5
0 10/3 13/3 95/3 (10/3)x1 + (13/3)*5= (95/3) x1 = 3
0 0 15/3 75/3
A Complication
0 1 -2 5
2 4 3 35 Row 1= row 1 - (2/0) row 0
1 -3 0 -5
2 4 3 35
0 1 -2 5
1 -3 0 -5
3
Gaussian Elimination
// In class Matrix, add:
Forward Solve
private static void forward_solve(Matrix q) {
int n = q.data.length;
4
Back Solve
private static void back_solve(Matrix q) {
int n = q.data.length;
Variations
Multiple right hand sides: augment Q, solve all eqns at once
3 1 -2 5 7 87
2 4 3 35 75 -1
1 -3 0 -5 38 52
Matrix inversion:
3 1 -2 1 0 0 # # # @ @ @
2 4 3 0 1 0 0 # # @ @ @
1 -3 0 0 0 1 0 0 # @ @ @
A I A-1
A•?=I
Q
? = A-1
5
Exercise
• Download GElim and Matrix
• Compile and run GElim:
Replace row
with result
© Oracle. All rights reserved. This content is excluded from our Creative
Commons license. For more information, see http://ocw.mit.edu/fairuse.
Exercise
• Experiment with the following 3 systems:
– Use pivot, back subst, divide on selection, etc. not solve
System 2:
4 6 -3 10
2 5 9 12
8 8 -27 6
System 3:
12 -13.5 3 0.5 2.75
8 -9 4 2.5 3.5
3 6 1.5 2 4.25
2 1.5 4 12 6
6
Using Linear Systems
• A common pattern in engineering, scientific
and other analytical software:
– Problem generator (model, assemble matrix)
• Customized to specific application (e.g. heat transfer)
• Use matrix multiplication, addition, etc.
– Problem solution (system of simultaneous linear
equations)
• Usually canned: either from library or written by you for
a library
– Output generator (present result in understandable
format)
• Customized to specific application (often with graphics,
etc.)
• We did a pattern earlier: model-view-controller
x0 x1 x2 x3 4 by 4 grid of points
x x5 x6 x7 on the plate produces
80 4 40
x8 x9 x10 x11 16 unknown temperatures
x12 x13 x14 x15 x0 through x15
20
7
Heat Transfer Equations
• Node 0:
x0= (80 + x1 + 60 + x4)/4 4x0- x1 –x4= 140
• Node 6:
x6= (x5 + x7 + x2 + x10)/4 4x6 –x5 –x7 –x2 –x10= 0
• Interior node:
xi= (xi-1 + xi+1 + xi-n + xi+n )/4 4xi – xi-1 –xi+1 –xi-n –xi+n= 0
Node 0 1 2 3 4 5 6 7
0 4 -1 0 0 -1 0 0 0
1 -1 4 -1 0 0 -1 0 0
2 0 -1 4 -1 0 0 -1 0
3 0 0 -1 4 0 0 0 -1
4 -1 0 0 0 4 -1 0 0
5 0 -1 0 0 -1 4 -1 0
6 0 0 -1 0 0 -1 4 -1
7 0 0 0 -1 0 0 -1 4
Ax= b
j
A x b
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 x0 140
1 -1 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0 0 x1 60
2 0 -1 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0 x2 60
3 0 0 -1 4 0 0 0 -1 0 0 0 0 0 0 0 0 x3 100
4 -1 0 0 0 4 -1 0 0 -1 0 0 0 0 0 0 0 x4 80
5 0 -1 0 0 -1 4 -1 0 0 -1 0 0 0 0 0 0 x5 0
6 0 0 -1 0 0 -1 4 -1 0 0 -1 0 0 0 0 0 x6 0
i 7 0 0 0 -1 0 0 -1 4 0 0 0 -1 0 0 0 0 x7 40
8 0 0 0 0 -1 0 0 0 4 -1 0 0 -1 0 0 0 x8 80
9 0 0 0 0 0 -1 0 0 -1 4 -1 0 0 -1 0 0 x9 0
10 0 0 0 0 0 0 -1 0 0 -1 4 -1 0 0 -1 0 x10 0
11 0 0 0 0 0 0 0 -1 0 0 -1 4 0 0 0 -1 x11 40
12 0 0 0 0 0 0 0 0 -1 0 0 0 4 -1 0 0 x12 100
13 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4 -1 0 x13 20
14 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4 -1 x14 20
15 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4 x15 60
8
Heat Transfer System
16
a00 a01 a02 a0,15 x0 b0
a10 a11 a12 a1,15 x1 b1
16 a20 a21 a22 a2,15 x2 = b2
a15,0 a15,1 a15,15 x15 b15
A x b
66 59 55 50
66 56 50 45
80 40
62 50 44 41
50 38 34 34
20
9
Exercise: Ax= b: Fill in green, orange
i=j j=i+1 j= ___
_______ j
A x b
j=i-1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 x0 140
1 -1 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0 0 x1 60 N
j=___ 2 0 -1 4 -1 0 0 -1 0 0 0 0 0 0 0 0 0 x2 60
3 0 0 -1 4 0 0 0 -1 0 0 0 0 0 0 0 0 x3 100
____ 4 -1 0 0 0 4 -1 0 0 -1 0 0 0 0 0 0 0 x4 80
5 0 -1 0 0 -1 4 -1 0 0 -1 0 0 0 0 0 0 x5 0
____ 6 0 0 -1 0 0 -1 4 -1 0 0 -1 0 0 0 0 0 x6 0 W
i 7 0 0 0 -1 0 0 -1 4 0 0 0 -1 0 0 0 0 x7 40
8 0 0 0 0 -1 0 0 0 4 -1 0 0 -1 0 0 0 x8 80
9 0 0 0 0 0 -1 0 0 -1 4 -1 0 0 -1 0 0 x9 0 E
10 0 0 0 0 0 0 -1 0 0 -1 4 -1 0 0 -1 0 x10 0
11 0 0 0 0 0 0 0 -1 0 0 -1 4 0 0 0 -1 x11 40
12 0 0 0 0 0 0 0 0 -1 0 0 0 4 -1 0 0 x12 100
13 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4 -1 0 x13 20 S
14 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4 -1 x14 20
15 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1 4 x15 60
10
Heat Transfer Exercise, p.2
Matrix b= new Matrix(n, 1); // Known temps
for (int i=0; i < n; i++) {
if (i < col) // Next to north edge
b.incrElement(i, 0, Tn); // incrElement, not setElement
if (…) // Step 2
// Complete this code for the other edges; no elses
// Add edge temperature to b; you may add more than one
// Look at the Ax=b example slide to find the pattern
// Use i, col, row to determine cells at the edge
}
Matrix x= Matrix.gaussian(a, b); // Problem solution
Linear Systems
a00x0 + a01x1 + a02x2 + + a0,n-1xn-1 = b0
a10x0 + a11x1 + a12x2 + + a1,n-1xn-1 = b1
11
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.