lecture 14 - mathematicsmath.mit.edu/~stoopn/18.086/lecture14.pdflecture 14 18.086. matrices in fd...

12
Lecture 14 18.086

Upload: others

Post on 19-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Lecture 1418.086

Page 2: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Matrices in FD and Matlab

• In discretized problems, we often end up solving Ku=f

• Need fast algorithms to invert K

• Require some knowledge about K (symmetry, Eigenvalues, etc.)

Page 3: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Gauss elimination• Consider Ax=b (with A invertible). Rewrite as

• Bring A of Ax=b into upper triangular form by adding/subtracting multiples of lines with each other:

• Solution is

This is an upper triangular matrix U. One can write A=LU with L a lower triangular matrix: LU decomposition

Page 4: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Symmetric A, Cholesky factorization

• What if A is symmetric?

• What if A is symmetric and positive definite?

Page 5: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Performance• If A is tridiagonal n x n matrix, U and L are bi-diagonal and

LU-factorization requires O(n) operations

• If A is a full matrix:

• A symmetric: O(1/3 n3) operations

• general A: O(2/3 n3) operations

• Band matrices: A has bandwidth w if it’s “farthest” nonzero diagonal has offset w from the diagonal.

• Takes O(w2n) operations

Page 6: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Condition number

Page 7: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

K, K2D and K3D: FD for heat equation

• K: FD matrix for -uxx, for 3 nodes:

• K2D: FD matrix for -uxx -uyy

• K3D: FD matrix for -uxx -uyy -uzz

1.2 Differences, Derivatives, Boundary Conditions 19

Solution

Solution

= 1

= ones

Figure 1.3: Finite differences give an exact match of u(x) and ui for the special case -u" = 1 with uo = un+l = 0. The discrete values lie right on the parabola.

set x = ih in the true solution u(x) = ?(x - x2) to find the correct ui.

1 . 2 2 1 Finite difference solution u = -(rh - i h ) has un+l = - (1 - 12) = 0. " 2

It is unusual to have this perfect agreement between ui and the exact u(ih). It is . . also unusual that no matrices were displayed. When 4h = 1 and f = 1, the matrix is

3 4 3 . K3/h2 = 16K3. Then ih = a, i, leads to ui = b, H, E.

The -1's in columns 0 and 4 were safely chopped off because uo = u4 = 0.

A Different Boundary Condition

Chapter 2 will give a host of physical examples leading to these differential and difference equations. Right now we stay focused on the boundary condition at x = 0, changing from zero height to zero slope:

d2u -- du = f ( x ) with - (0) = 0 (free end) and u(1) = 0. dx2 dx

With this new boundary condition, the difference equation no longer wants uo = 0. Instead we could set the first dzjj'erence to zero: ul - uo = 0 means zero slope in the first small interval. With uo = ul, the second difference -uo + 2ul - u2 in row 1 reduces to ul - u2. The new boundary condition changes Kn t o Tn.

Example 2 Solve the differential and difference equations starting from zero slope:

Free-f ixed d2u -- du dx2

= 1 with -(0) = 0 and u(1) = 0 dx

+ 2ui - Ui-1 = 1 with u1 - uo

h2 h = 0 and un+l = 0 (26 )

Let’s write them down and see what happens when we do Gauss elimination (or LU factorization)

K3=

Page 8: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

spy(Matrix)• Matlab: spy(M) shows nonzeros in a matrix M:

K K2D K3D

most elements are zero => sparsestore (and operate on) only non-zero elements! (sparse(A) in matlab turns A into sparse representation)

Page 9: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Useful Matlab commandsdescription

eye(5) 5x5 identity matrix

speye(5) 5x5 sparse identity matrix

toeplitz([2, -1, zeros(1,2)]) creates K4

toeplitz(sparse([2,-1,zeros(1,2)])) creates sparse K4

[L,U]=lu(K) L-U factorization

kron(A,B) Kronecker product of matrices A and B

sparse(A) makes A sparse

Page 10: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Kronecker product• The Kronecker product for the 2x2 matrix A and 3x2 matrix B is

=

• Can write K2D = kron(I,K) + kron(K,I) with I identity matrix (NxN), K the NxN FD matrix

• Can write K3D = kron(I2D,K) + kron(K2D,I) with I2D=kron(I,I)

Page 11: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Sparse matrices and elimination

K K2D K3D

The sparsity could save us a lot of operations during elimination: Only few off-diagonal are nonzero and need to be eliminated using pivot rows!

Really? Unfortunately: Fill-ins occur!

Page 12: Lecture 14 - Mathematicsmath.mit.edu/~stoopn/18.086/Lecture14.pdfLecture 14 18.086. Matrices in FD and Matlab ... heat equation • K: FD matrix for ... Chapter 2 will give a host

Elimination with reordering