100% Guaranteed Results


18-330 – Exercise 1 : Eigenvalue solvers for a special matrix Solved
$ 20.99
Category:

Description

5/5 – (1 vote)

In this problem we will consider a very special symmetric matrix. Recall that the second-order finite difference scheme for a function ๐‘“(๐‘ฅ) is given by
๐‘“๐‘›โ€ณ = ๐‘“๐‘›+1 โˆ’ 2โ„Ž๐‘“2๐‘› + ๐‘“๐‘›โˆ’1
where ๐‘“๐‘› = ๐‘“(๐‘ฅ0 + ๐‘›โ„Ž) and โ„Ž = ๐‘ฅ๐‘๐‘โˆ’๐‘ฅ0 .
Define the vector f such that f๐‘› = ๐‘“๐‘›. Then the corresponding derivative vector using finite differences can be written as fโ€ณ = ๐ทf. Some care must be taken about the boundary conditions; for simplicity we will assume that ๐‘“0 = ๐‘“๐‘ = 0. Under these assumptions the matrix ๐ท is a tridiagonal matrix:
๐‘“1โ€ฒ โˆ’2 1 ๐‘“1
โŽก ๐‘“2โ€ฒ โŽค โŽก 1 โˆ’2 1 โŽค โŽก ๐‘“2 โŽค
โŽข ๐‘“3โ€ฒ โŽฅ = โŽขโŽข 1 โˆ’2โ‹ฑ โ‹ฑ1 โ‹ฑ โŽฅโŽฅ โŽขโŽข ๐‘“โ‹ฎ3 โŽฅโŽฅ
โŽข โ‹ฎ โŽฅ
โŽข๐‘“๐‘โˆ’2โ€ฒ โŽฅ โŽข 1 โˆ’2 1 โŽฅ โŽข๐‘“๐‘โˆ’2โŽฅ
โŽฃ๐‘“๐‘โˆ’1โ€ฒ โŽฆ โŸโŸโŸโŸโŸโŸโŸโŸโŸโŸโŸโŸโŸโŽฃ 1 โˆ’2โŽฆ โŽฃ๐‘“๐‘โˆ’1โŽฆ
๐ท๐‘
You can construct the matrix ๐ท in Julia using the diagm function. For simplicity keep everything dense in this problem, although there clearly a lot of structure that could be exploited.
1. Consider the matrix ๐ท. Using the ansatz (i.e. hypothesis) v๐‘› = sin(๐œ‹๐‘›๐‘˜/๐‘), where 1 โ‰ค ๐‘˜ โ‰ค ๐‘ โˆ’ 1, show that v is an eigenvector of ๐ท๐‘. What is the corresponding eigenvalue? Remember that the eigenvalues should be real!
2. In class we discussed several ways to find eigenvalues of matrices. The simplest algorithm for finding eigenvalues is the power method. Supppse that a symmetric matrix ๐ด has the eigendecomposition ๐ด =
๐‘‹ฮ›๐‘‹โˆ’1. Recall that for a symmetric matrix the eigenvectors associated with distinct eigenvalues are orthogonal. Starting with a random intitial vector x which can be written as a sum over the eigenvectors,
x = โˆ‘ ๐‘๐‘›v๐‘›
๐‘›
show that
๐ด๐‘˜x = โˆ‘ ๐œ†๐‘˜๐‘›๐‘๐‘›v๐‘›
๐‘›
where we order the eigenvalues by their magnitude, |๐œ†1| > |๐œ†2| > โ‹ฏ > |๐œ†๐‘| and hence
๐ด๐‘˜x = ๐‘1๐œ†๐‘˜1 (v1 + ๐’ช (๐œ†๐œ†๐‘˜1๐‘˜2v2)) .
This shows why the power method converges to the leading eigenvector.
3. The power method gives an approximation for the leading eigenvector v1ฬƒ .
Since it is only an approximation it is not necessarily true that ๐ด ฬƒv1 =
๐œ†1v1ฬƒ exactly.
Instead we will say that the best approximation to ๐œ†1 is given by the ๐œ†
that satifies
2 ๐œ†1 = min ||๐ด ฬƒv1 โˆ’ ๐›ผ ฬƒv1||2 = โˆ‘ (โˆ‘ ๐ด๐‘–๐‘˜(v1ฬƒ )๐‘˜ โˆ’ ๐›ผ( ฬƒv1)๐‘–) .
๐›ผ
๐‘– ๐‘˜
By differentiating this expression with respect to ๐›ผ, show that
ฬƒ โ‹… (๐ด ฬƒv ) ฬƒ
๐œ†1 ฬƒ โ‹… ฬƒ ฬƒ ฬƒ .
This is called the Rayleigh quotient.
4. Implement the power method power_method(A, N, x) for a symmetric matrix ๐ด which iterates for a fixed number of iterations ๐‘ on the intial vector x and returns v1ฬƒ and ๐œ†1 approximated by the Rayleigh quotient above.
5. Run this on the matrix ๐ท10 which is of size (9 ร— 9). Use the true largest magnitude vector from part 1. Plot the relative error between your result and the true value as a function of ๐‘ to get a smooth plot; use the same initial vector each time.
Remember to normalize the vector at each iteration to avoid overflow!
This initial random vector should be complex. (Extra credit: show that the relationship is what you would expect analytically!)
6. A more advanced method that we discussed to find all the eigenvalues was the QR algorithm. We define ๐ด(0) = ๐ด and then
๐‘„(๐‘˜)๐‘…(๐‘˜) = ๐ด(๐‘˜)
๐ด(๐‘˜+1) = ๐‘…(๐‘˜)๐‘„(๐‘˜)
This will normally converge to an upper-triangular matrix. How does this work? We call two matrices ๐ด and ๐ต similar if ๐ต = ๐‘„๐ด๐‘„โŠค where ๐‘„ is orthogonal. Show that if ๐ด has eigenvalue pairs (๐œ†๐‘–, v๐‘–) then ๐ต has eigenpairs (๐œ†๐‘–, ๐‘„v๐‘–).
7. Show that in the QR algorithm ๐ด(๐‘˜+1) is similar to ๐ด(๐‘˜) and hence ๐ด. Therefore if the algorithm converges to an upper-triangular matrix we can read off the eigenvalues from the diagonal and the eigenvectors will be given by the columns of ๐‘„(1)๐‘„(2)๐‘„(3) โ‹ฏ ๐‘„(๐‘).
8. Implement the QR algorithm in a function qr_algorithm(A, N) for a matrix ๐ด with ๐‘ iterations. It should return the resulting matrix ๐ด(๐‘™) and the eigenvector matrix. Run the algorithm on a random symmetric matrix of size 10 ร— 10 for 100 iterations. How close to upper-triangular is the resulting matrix?
9. Run the QR algorithm on the matrix ๐ท10 of size (9 ร— 9) for a suitable number of iterations to get convergence. Compare the results to the theoretical one. Can you find all the eigenvalues and eigenvectors?
10. Using the fact that the eigendecomposition of a symmetric matrix gives orthogonal matrices (which are easy to invert) propose a method to solve the linear system
๐ทx = b
11. Solve the system for b = 0.01^2*sin.(2ฯ€*(0.01:0.01:0.99)) and ๐ท as a 99 ร— 99 matrix. Plot the resulting vector x. Plot on the same axis sin.(2ฯ€*(0.01:0.01:0.99))/4ฯ€^2. Is it similar to x? We have just solved the boundary value problem ๐‘“โ€ณ = sin(2๐œ‹๐‘ฅ) with ๐‘“(0) = ๐‘“(2๐œ‹) = 0. In the next problem set we will see how to do this even quicker using Fourier analysis.
Exercise 2: Low-rank approximation
In this problem we will use the SVD to produce low-rank approximations for matrices. We will then use this to compress images and write fast multiplication algorithms.
In the last problem set we saw that we could exploit structural zeros to speed up algorithms. A matrix is of rank ๐‘Ÿ if we can write it in the form
๐‘Ÿ
๐ด = โˆ‘ ๐œŽ๐‘–u๐‘–vโŠค๐‘–
๐‘–=1
where the u๐‘– and v๐‘– are vectors of length ๐‘, So u๐‘–vโŠค๐‘– is a matrix of size (๐‘ ร— ๐‘), of rank 1.
The SVD of a square ๐‘ ร— ๐‘ matrix ๐ด exactly writes ๐ด in this form:
|
๐ด = ๐‘ˆฮฃ๐‘‰ โŠค = [u1 |
u2 โ‹ฏ | ๐œŽ1
โŽก u๐‘] โŽข ๐œŽ2 โ‹ฑ โˆ’โˆ’
โŽค โŽกโˆ’โˆ’
โŽฅ โŽขโŽข vโŠค1
โŠค โˆ’โˆ’
โˆ’โˆ’โŽคโŽฅโŽฅ = โˆ‘๐‘ ๐œŽ๐‘–u๐‘–vโŠค๐‘–
| | | โŽฃ ๐œŽ๐‘โŽฆ โŽฃ โŽฆ ๐‘–=1
Truncating the summation after the largest ๐‘Ÿ singular values results in a rank๐‘Ÿ approximation of the matrix ๐ด. In fact the Eckhartโ€“Mirskyโ€“Young theorem shows this is the best rank ๐‘Ÿ approximation to ๐ด.
Define the rank ๐‘Ÿ approximation of ๐ด
๐‘Ÿ
๐ด๐‘Ÿ โˆถ= โˆ‘ ๐œŽ๐‘–u๐‘–vโŠค๐‘–
๐‘–=1
where u๐‘Ÿ and v๐‘Ÿ are the singular vectors of ๐ด and ๐œŽ๐‘Ÿ are the singular values of the matrix.
2. Make a square matrix M of size (20 ร— 20) that is all zero, except for an axis-aligned cross centred in the centre. Each arm should be of total width 2 and extend a distance 5 out from the centre in each direction parallel to the axes. The cross should consist of 1s.
3. Remembering that the rank is the column rank, i.e. the number of linearlyindependent columns, how much should the rank be? Use the SVD of M to confirm this. What does the rank-1 approximation of M look like?
4. Now add small random gaussian noise using the randn function, of intensity 0.1. Plot the new matrix (using heatmap). How should this affect the (column) rank of the matrix?
5. Plot the singular values ๐œŽ๐‘› as a function of ๐‘›. What do they tell us? What is a suitable rank-๐‘› approximation to take? What does it look like?
img = testimage(“mandrill”)
imgmat = Array(channelview(Float64.(Gray.(img))))
heatmap(imgmat, c=ColorGradient(:grays), showaxis=false, clims=(0,1), yflip=true)
Here imgmat is a standard Julia matrix that contains greyscale pixel values.
7. Plot the rank-50 approximation to imgmat (using heatmap). How does it compare to the original?
8. Plot the singular values as a function of ๐‘›. You should see an โ€œelbowโ€ shape. Approximately where does it occur?
9. Create an interactive visualization that shows the low-rank approximation for different values of ๐‘Ÿ. What do you observe? After which ๐‘Ÿ are you happy with the quality of the image. Is it related to where the elbow is?
Exercise 3: Dynamic mode decomposition
In this problem we will use the SVD to predict the future! Suppose that we have some data that satisfies an ODE,
xฬ‡ = f(x)
We can solve the ODE and take time snapshots of the result which we can stack into a matrix as follows:
| | |
๐‘‹๐‘›,๐‘š โˆถ= [x(๐‘ก๐‘›) x(๐‘ก๐‘›+1) โ‹ฏ x(๐‘ก๐‘š)]
| | |
We will assume that the dynamics is linear. This means that we expect that two consecutive snapshots should satisfy
x๐‘›+1 = ๐ดx๐‘›
for some matrix ๐ด.
If we have ๐‘ snapshots then we can write this as a matrix equation,
๐‘‹2,๐‘ = ๐ด๐‘‹1,(๐‘โˆ’1)
1. Suppose that we have an eigen-decomposition for ๐ด, i.e. ๐ด = ๐‘Šฮ›๐‘Šโˆ’1, and that we can write the initial time snapshot as a sum over the eigenbasis of ๐ด,
x0 = โˆ‘ ๐‘๐‘—w๐‘—
๐‘—
where w๐‘› is the ๐‘›th eigenvector of ๐ด. Show that future time snapshots can be written as
x๐‘› = โˆ‘ ๐‘๐‘—๐œ†๐‘›๐‘— w๐‘—
๐‘—
where ๐œ†๐‘› is the ๐‘›th eigenvalue of ๐ด.
2. We are now going to try and find the eigendecomposition of ๐ด without ever constructing it! Start by calculating the SVD of ๐‘‹1,(๐‘โˆ’1) = ๐‘ˆฮฃ๐‘‰ โŠค. Find an expression for ๐‘ˆโŠค๐ด๐‘ˆ in terms of ๐‘‹2,๐‘, ฮฃ, ๐‘‰ , ๐‘ˆ. We can then calculate the eigenspectrum of A by taking the eigenvalue decomposition of ๐‘ˆโŠค๐ด๐‘ˆ since they are similar and using use the result in [1.5].
3. Write a function that calculates the eigenspectrum of ๐ด given ๐‘‹1,๐‘. Instead of using the full SVD, use a truncated SVD keeping only the first ๐‘Ÿ singular values and singular vectors; i.e. use the matrices Vr = V[:,
1:r], Ur = U[:, 1:r] and ฮฃr = ฮฃ[1:r, 1:r] in the expression above for ๐‘‰ , ๐‘ˆ, ฮฃ. Your function should be of the form aspectrum(X, r). The reason for truncating is in case A is not full rank, in which case some terms in ฮฃ might be nearly 0 and dividing by them would be a bad idea. 4. Test your function on a random 10 ร— 10 matrix ๐ด generating some data for ๐‘‹ of size (10ร—11) from it starting from a random ๐‘ฅ0. Compare the results of eigen(A) with aspectrum(X, 10). Remember that eigenvectors are the same upto a multiplicative constant.
5. We are now going to apply this to some dynamical data. Use the ODE integrator code below to generate some data for ๐‘ coupled oscillators with ๐‘ฅ1(0) = 0.5 and all the others ๐‘ฅ๐‘–(0) = 0 for N = 10 from ๐‘ก = 0 to ๐‘ก = 10. The coupled system can be written as
๐‘ฅฬˆ1 โˆ’2 1 ๐‘ฅ1
โŽก๐‘ฅฬˆ2 โŽค โŽก 1 โˆ’2 1 โŽค โŽก๐‘ฅ2 โŽค
โŽข๐‘ฅฬˆ3 โŽฅ = โŽข 1 โˆ’2 1 โŽฅ โŽข๐‘ฅ3 โŽฅ
โŽข โ‹ฎ โŽฅ โŽข โ‹ฑ โ‹ฑ โ‹ฑ โŽฅ โŽข โ‹ฎ โŽฅ
โŽฃ๐‘ฅฬˆ๐‘โŽฆ โŽฃ 1 โˆ’2โŽฆ โŽฃ๐‘ฅ๐‘โŽฆ
or as a system of first order equations,
๐‘ฅฬ‡1 0 1 ๐‘ฅ1
โŽก ๐‘ฆฬ‡1 โŽค โŽกโˆ’2 0 1 โŽค โŽก ๐‘ฆ1 โŽค
โŽข๐‘ฅฬ‡2 โŽฅ โŽข 0 0 0 1 โŽฅ โŽข๐‘ฅ2 โŽฅ โŽข ๐‘ฆฬ‡2 โŽฅ โŽข 1 0 โˆ’2 0 1 โŽฅ โŽข ๐‘ฆ2 โŽฅ
โŽข๐‘ฅฬ‡3 โŽฅ = โŽข 0 0 0 0 1 โŽฅ โŽข๐‘ฅ3 โŽฅ
โŽข ๐‘ฆฬ‡3 โŽฅ โŽข 1 0 โˆ’2 0 1 โŽฅ โŽข ๐‘ฆ3 โŽฅ
โŽข โ‹ฎ โŽฅ โŽข โ‹ฑ โ‹ฑ โ‹ฑ โ‹ฑ โ‹ฑ โŽฅ โŽข โ‹ฎ โŽฅ
โŽข๐‘ฅฬ‡๐‘โŽฅ โŽข 0 0 0 0 1โŽฅ โŽข๐‘ฅ๐‘โŽฅ
โŽฃ๐‘ฆฬ‡๐‘ โŽฆ โŽฃ 1 0 โˆ’2 0โŽฆ โŽฃ๐‘ฆ๐‘ โŽฆ
The output is a data matrix with rows ๐‘ฅ1, ๐‘ฅฬ‡1, ๐‘ฅ2, ๐‘ฅฬ‡2, โ‹ฏ
Generate a plot of ๐‘ฅ1(๐‘ก) to check that everything went according to plan.
6. Split the data into two parts ๐‘‹1 and ๐‘‹2, the first half from ๐‘ก = 0 to ๐‘ก = 5 and the second half ๐‘ก = 5 to ๐‘ก = 10. Calculate the spefctrum of ๐ด with ๐‘Ÿ = 10 using ๐‘‹1.
7. Use the first column of ๐‘‹2 as the initial condition. Use the spectrum you found to predict the future dynamics. [Hint: use the initial condition to find the ๐‘๐‘—s, which is a matrix solve. Then use the equations in part 1.1 to calculate the prediction.]
8. Plot the prediction for the 10 springs on the same axis as the true solution. What happens?
9. Repeat [3.6โ€“3.7] for ๐‘Ÿ = 15 and ๐‘Ÿ = 20. What do you observe?
ODE Code:
struct RKMethod{T} c::Vector{T} b::Vector{T} a::Matrix{T} s::Int
# Make sure that the matrices and vectors have the correct size function RKMethod(c::Vector{T}, b::Vector{T}, a::Matrix{T}, s::Int) where T lc = length(c); lb = length(b); sa1, sa2 = size(a) if lc == sa1 == sa2 == s-1 && lb == s new{T}(c, b, a, s)
else
error(“Sizes should be (s = $s) : length(c) = s-1 ($lc) length(b) = s end
end end function (method::RKMethod)(f, x, t, h) # Extract the parameters
a, b, c, s = method.a, method.b, method.c, method.s
# Vector to hold the k terms k = [f(t, x)]
for i in 1:s-1
tn = t + c[i]*h xn = x + h*sum(a[i, j] * k[j] for j in 1:i)
push!(k, f(tn, xn))
end
return x + h * sum(b.*k)
end
function integrate(method, f, x0, t0, tf, h)
# Calculate the number of time steps N = ceil(Int, (tf – t0) / h) hf = tf – (N – 2)*h
#initiate tracking vectors xs = [copy(x0)] ts = [t0]
#step
for i in 1:N-1
push!(xs, method(f, xs[i], ts[i], h)) push!(ts, ts[i] + h)
end
# Special last step push!(xs, method(f, xs[N-1], ts[N-1], hf)) push!(ts, ts[N-1] + hf)
return ts, xs
end
c = [1/2, 1/2, 1] b = (1/6) .* [1, 2, 2, 1] a = [1/2 0 0; 0 1/2 0; 0 0 1] rk4 = RKMethod(c, b, a, 4)
function build_springmat(N) springmat = zeros(2N,2N) for i = 1:2N
(i < 2N) && (springmat[i, i+1] = 1.0)
if iseven(i) springmat[i, i-1] = -2.0
(i > 3) && (springmat[i, i-3] = 1.0) end end springmat
end
N = 10
const spmat = build_springmat(N) spf(t, x) = spmat*x x0 = zeros(2N); x0[1] = 0.5
ts, xs = integrate(rk4, spf, x0, 0.0, 20.0, 0.005); X = hcat(xs…) plot(ts, X[1:2:2N, :]’, leg=:outertopright, box=:on)
Exercise 4: Fourier integrals
In lectures we saw that we could write periodic functions in the form
๐‘ ๐‘“(๐‘ฅ) = โˆ‘ ๐‘“๐‘›ฬ‚ ๐‘’๐‘–๐‘›๐‘ฅ
๐‘›=โˆ’๐‘
ฬ‚2๐œ‹ ๐‘“(๐‘ฅ) ๐‘’โˆ’๐‘–๐‘›๐‘ฅ. where ๐‘“๐‘›
1. Consider the saw-tooth function,
โŽง{๐‘ฅ 0 โ‰ค ๐‘ฅ < ๐œ‹
๐‘“(๐‘ฅ) = โŽจ{โŽฉ2๐‘“(๐œ‹ โˆ’ ๐‘ฅmod(๐‘ฅ, 2๐œ‹)) ๐œ‹ โ‰ค ๐‘ฅ < 2๐œ‹else
Calculate the Fourier series coefficients analytically.
2. Write a function fourier_coefficients(f::Vector, n::Int) that takes in a vector of samples of ๐‘“ uniformly distributed over [0, 2๐œ‹] and returns an approximation to ๐‘“๐‘›ฬ‚ by calculating the integral using the trapezoidal rule.
3. Now calculate ๐‘“๐‘›ฬ‚ using your trapezoidal code using 100 points and ๐‘› = โˆ’3, โˆ’2, โ€ฆ , 3. How do they compare to the theoretical results?
4. Fix ๐‘› to be 1. Plot the relative error between the theoretical result and the result from using fourier_coefficients for calculating ๐‘“1ฬ‚ using a number of points between 10 and 1000. What does the convergence look like? Does it agree with what we discussed in class?
5. Now consider the smooth periodic function exp(cos(๐‘ฅ)). Repeat [4.3โ€“
4.4] for this function. The analytical result is given by ๐‘“๐‘›ฬ‚ = ๐ผ|๐‘›|(1).
You can calculate this using the besseli(abs(n), 1) in SpecialFunctions.jl.
6. Plot the magnitude of ๐‘“๐‘›ฬ‚ as a function of ๐‘› for the two functions. How do the coefficients decay? Is this what you expected?

Reviews

There are no reviews yet.

Be the first to review “18-330 – Exercise 1 : Eigenvalue solvers for a special matrix Solved”

Your email address will not be published. Required fields are marked *

Related products