These notes are mainly a record of what we discussed and are not a substitute for attending the lectures and reading books! If anything is unclear/wrong, let me know and I will update the notes.
include("preamble.jl");
✓ file included!
using: Plots, LaTeXStrings, Polynomials, PrettyTables, LinearAlgebra
functions included:
Ch2: simple_iteration, Newton, orderOfConvergence,
Ch3: ChebyshevNodes, ChebyshevInterpolant, Lagrange,
Use @doc <<function>> for help
16.1 Chapter 5: Linear Systems of Equations
NoteWarm-up
How would you write the following system of equations as a matrix equation?
\begin{align*}
3x - y + z &= 1 \\
x-y-z &= 3\\
y + z &= 0
\end{align*}
Then we saw some examples that involve matrices and relate to previous topics you’ve seen:
NotePolynomial interpolation (Chapter 3)
For distinct points X = \{ x_0, \dots, x_n \} and values \{y_0,\dots,y_n\} (which, in our case, were given by y_j = f(x_j) for some function f), consider the Lagrange interpolation problem: find p(x) = a_0 + a_1 x + a_2 x^2 + \dots + a_n x^n such that p(x_j) = y_j for j=0,\dots,n. We saw that the solution can be written as p(x) = \sum_{j=0}^n \ell_j(x) y_j. The problem can also be written as the following matrix equation:
where the matrix V is known as the Vandermonde matrix. Since p exists for any choice of \bm y, we have already proved that V is invertible and \bm a = V^{-1} \bm y.
f = x ->1/ (1+exp(100* x) ) e=-1:.001:1N =10;X =ChebyshevNodes( N )Y =f.(X) V = X.^(0:N)'p =Polynomial( V \ Y )q =ChebyshevInterpolant( f, N )println( maximum( @. abs( f(e)-p(e) ) ) )println( maximum( @. abs( f(e)-q(e) ) ) )plot( f, -1, 1, label=L"f" ) plot!( p, -1, 1, label="p - Vandermonde" ) plot!( q, -1, 1, label="q - Barycentric" ) scatter!( X, Y, primary =false )
0.39419514906422315
0.3941951490642238
NoteDifferential equations (Galerkin method)
Suppose you want to solve the differential equation Lu = f where L is some linear differential operator (e.g. Lu(x) := u''(x) + g(x) u'(x) + h(x) for a general 2nd order linear differential equation). In order to solve this equation numerically, one may discretise it: introduce some finite set of functions \{ \phi_j \}_{j=1}^N and suppose that u \approx \sum_{j=1}^N c_j \phi_j for some coefficients \bm c = (c_j). Then, multiplying by \phi_i and integrating, one obtains
We choose a basis \{ \phi_i \} and expand u(x) = \sum_{i} c_i \phi_i(x). Since we require u(x) = 0 on \{-1,1\}, we shall choose the basis so that \phi(-1) = \phi(1) = 0. Let’s choose Chebyshev points X = \{x_0, \dots, x_n\} and choose the basis \{ \ell_i \}_{i=1}^{n-1} of Lagrange polynomials (ommiting i=0 and i=n so that \phi_i(-1) = \phi_i(1) = 0 for all basis functions).
f = x->-x n =100; X =ChebyshevNodes( n )[2:end-1]A =zeros( n-1, n-1 )S =zeros( n-1, n-1 )F =zeros( n-1 )# Approximate f with a polynomialp =fit(ChebyshevT, X, f.(X))φ= [ Lagrange( 1, n ) ];for i =2:(n-1)push!( φ, Lagrange( i, n ) )endfor i =1:(n-1) F[i] =integrate( p *φ[i], -1, 1 )for j=1:(n-1) A[i,j] =integrate( derivative( φ[i] ) *derivative(φ[j]), -1, 1)endendc = A \ Fu =sum( c .*φ )plot( u, -1, 1, label="approximate solution")
NoteCompanion matrices
The roots function in the Julia package Polynomials.jl finds the roots of a polynomial p(x) = a_0 + a_1 x + \dots + a_n x^n by considering the following Frobenius companion matrix:
It turns out that p is the characteristic polynomial of C: that is, p(x) = \det\big( xI - C \big). Therefore, one may find the roots of p by computing eigenvalues of C (which we will discuss in the following weeks).
where \mathrm{cof}(A) is the transpose of the matrix where the i,j entry is the determinant of the submatrix after removing row i and column j.
The computational effort involved in computing \det A by expanding on the first row (or any row) is O(n!) where A \in \mathbb R^{n\times n}.
If n=100 (which is small in many applications), n! \approx 9 \times 10^{157}, and so, if our computer can run 10^{12} operations per second (which is a lot), then it’ll take n! / 10^{12} seconds $ ^{138}$ years. This is a long time! (Big bang \approx 13 \times 10^9 years ago)
This means we need better methods!
16.3 Gaussian Elimination
Let’s go back to the example we started with:
Note
Solve
\begin{align*}
3x - y + z &= 1 \\
x-y-z &= 3\\
y + z &= 0
\end{align*}
Answer:
Adding the third equation to the second gives x = 3. Adding the third equation to the first gives 3x + 2z = 1 and since x = 3, we have z = -4. The final equation then says that y = 4.
Alternatively, we can see this by using elementary row operations: First write the augmented matrix
This process is called Gaussian elimination (and is equivalent to how you would solve simultaneous equations at school).
NoteExamples
What happens when you apply Gauss elimination to the following systems?
(i)
\begin{align*}
x - y + 2z &= -8 \\
2x - 2y + 3z &= -20 \\
z &= 0
\end{align*}
(ii)
\begin{align*}
x - y + z &=4 \\
2y - z &= -2 \\
3x + 3y &= 6
\end{align*}
Exercises: Burden pg. 372
Notice that, if A is invertible, then by doing row operations we get
\begin{align}
\begin{pmatrix}
A &|& I
\end{pmatrix}
%
\longrightarrow
%
\begin{pmatrix}
I &|& A^{-1}
\end{pmatrix}.
\end{align}
It turns out that this process is O(n^{3}). Again, if n=100, a fast computer will compute the inverse in 100^3 / 10^{12} = 10^{-6} seconds $ $ time since the big bang.
16.4 LU Decomposition
Let L be unit lower triangular (that is, L_{ii} = 1 and L_{ij} = 0 for all i< j) and U be upper triangular (that is, U_{ij} = 0 for all i>j). Then, one can solve
\begin{align}
LU x = b
\end{align}
by solving
\begin{align}
L y = b \tag{forwards substitution} \\
U x = y \tag{backwards substitution}
\end{align}
functionforwardSub(L, b) n =size(L,1) y =zeros(n) y[1] = b[1] / L[1, 1]for i ∈2:n s =sum(L[i, j] * y[j] for j ∈1:i-1) y[i] = (b[i] - s) / L[i, i]endreturn yendfunctionbackwardSub(U, y) n =size(U, 1) x =zeros(n) x[n] = y[n] / U[n, n]for i ∈ n-1:-1:1 s =sum(U[i, j] * x[j] for j ∈ i+1:n) x[i] = (y[i] - s) / U[i, i]endreturn xendL = [1000 ; 3100 ; 4210 ; 0721]U = [4100 ; 0101 ; 0010 ; 0002]A = L*U;b = [1 ; 1 ; 1 ; 1]y =forwardSub(L, b)x =backwardSub(U, y)
4-element Vector{Float64}:
2.375
-8.5
1.0
6.5
A \ B
“Matrix division using a polyalgorithm. For input matrices A and B, the result X is such that AX = B when A is square. The solver that is used depends upon the structure of A. If A is upper or lower triangular (or diagonal), no factorization of A is required and the system is solved with either forward or backward substitution. For non-triangular square matrices, an LU factorization is used.”