Lecture 14 18.086

advertisement
Lecture 14
18.086
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.)
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:
This is an upper triangular matrix U. One can write
A=LU
with L a lower triangular matrix: LU
decomposition
•
Solution is
Symmetric A, Cholesky
factorization
•
What if A is symmetric?
•
What if A is symmetric and positive definite?
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
Condition number
set x = ih in the true solution u ( x ) = ? ( x - x
"
Finite difference
K, K2D and
K3D:solution
FD for
It isequation
unusual to have this perfect agreement
heat
also unusual that no matrices were displayed.
1 .
2
u = -(rh - i
K3/h2= 16K3. Then ih =
a, i,
leads to ui
K3=
•
K: FD matrix for -uxx, for 3 nodes:
•
K2D: FD matrix forThe
-uxx-1's
-uyyin columns 0 and 4 were safely chop
•
K3D: FD matrix for -uxx -uyy -uzz
A
Chapter
2 will
givehappens
a host of when
physical exam
Let’s write them down
and see
what
difference
Right now we stay focus
we do Gauss elimination
(orequations.
LU factorization)
changing from zero height to z e r o slope:
spy(Matrix)
•
Matlab: spy(M) shows nonzeros in a matrix M:
K
K2D
K3D
most elements are zero => sparse
store (and operate on) only non-zero elements!
(sparse(A) in matlab turns A into sparse representation)
Useful Matlab commands
description
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
Kronecker product
•
The Kronecker product
for the 2x2 matrix A and 3x2 matrix B is
=
•
Can write K2D = kron(I,K) + kron(K,I)
NxN FD matrix
with I identity matrix (NxN), K the
•
Can write K3D = kron(I2D,K) + kron(K2D,I)
with I2D=kron(I,I)
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!
Elimination with reordering
Download