Name: INSERT YOUR NAME HERE. Due to dropbox by 6pm PDT, Wednesday, December 14, 2011

Similar documents
Scientific Computing

CS227-Scientific Computing. Lecture 4: A Crash Course in Linear Algebra

Numerical Methods I Solving Square Linear Systems: GEM and LU factorization

Gaussian Elimination without/with Pivoting and Cholesky Decomposition

LU Factorization. LU factorization is the most common way of solving linear systems! Ax = b LUx = b

Lecture 9. Errors in solving Linear Systems. J. Chaudhry (Zeb) Department of Mathematics and Statistics University of New Mexico

Lecture 7. Gaussian Elimination with Pivoting. David Semeraro. University of Illinois at Urbana-Champaign. February 11, 2014

Numerical Linear Algebra

Direct Methods for Solving Linear Systems. Matrix Factorization

AMS 147 Computational Methods and Applications Lecture 17 Copyright by Hongyun Wang, UCSC

Linear Systems of n equations for n unknowns

Bindel, Fall 2011 Intro to Scientific Computing (CS 3220) Week 3: Wednesday, Jan 9

AMS526: Numerical Analysis I (Numerical Linear Algebra)

Scientific Computing: Dense Linear Systems

Linear Algebra Section 2.6 : LU Decomposition Section 2.7 : Permutations and transposes Wednesday, February 13th Math 301 Week #4

Getting Started with Communications Engineering

8 The SVD Applied to Signal and Image Deblurring

The Solution of Linear Systems AX = B

Numerical Linear Algebra

Program Lecture 2. Numerical Linear Algebra. Gaussian elimination (2) Gaussian elimination. Decompositions, numerical aspects

6 The SVD Applied to Signal and Image Deblurring

Linear Least-Squares Data Fitting

Numerical Linear Algebra

8 The SVD Applied to Signal and Image Deblurring

Outline. Math Numerical Analysis. Errors. Lecture Notes Linear Algebra: Part B. Joseph M. Mahaffy,

Cheat Sheet for MATH461

Solving linear systems (6 lectures)

Bindel, Fall 2016 Matrix Computations (CS 6210) Notes for

Chapter 2 - Linear Equations

MATH2071: LAB #5: Norms, Errors and Condition Numbers

LU Factorization. Marco Chiarandini. DM559 Linear and Integer Programming. Department of Mathematics & Computer Science University of Southern Denmark

Solving linear equations with Gaussian Elimination (I)

5 Solving Systems of Linear Equations

Homework 2 Foundations of Computational Math 2 Spring 2019

Chapter 2. Solving Systems of Equations. 2.1 Gaussian elimination

Iterative solvers for linear equations

Review Questions REVIEW QUESTIONS 71

1 GSW Sets of Systems

7.2 Steepest Descent and Preconditioning

AM 205: lecture 8. Last time: Cholesky factorization, QR factorization Today: how to compute the QR factorization, the Singular Value Decomposition

CS168: The Modern Algorithmic Toolbox Lecture #8: How PCA Works

Computational Methods. Systems of Linear Equations

Math 471 (Numerical methods) Chapter 3 (second half). System of equations

7.6 The Inverse of a Square Matrix

Numerical Methods - Numerical Linear Algebra

Lab 1: Iterative Methods for Solving Linear Systems

lecture 2 and 3: algorithms for linear algebra

MAT 343 Laboratory 3 The LU factorization

Applied Mathematics 205. Unit II: Numerical Linear Algebra. Lecturer: Dr. David Knezevic

AM 205: lecture 6. Last time: finished the data fitting topic Today s lecture: numerical linear algebra, LU factorization

Least squares: the big idea

Applied Linear Algebra in Geoscience Using MATLAB

Vector and Matrix Norms. Vector and Matrix Norms

Laboratory #3: Linear Algebra. Contents. Grace Yung

GAUSSIAN ELIMINATION AND LU DECOMPOSITION (SUPPLEMENT FOR MA511)

MODULE 7. where A is an m n real (or complex) matrix. 2) Let K(t, s) be a function of two variables which is continuous on the square [0, 1] [0, 1].

Background Mathematics (2/2) 1. David Barber

5.6. PSEUDOINVERSES 101. A H w.

CS 323: Numerical Analysis and Computing

Statistical Geometry Processing Winter Semester 2011/2012

6 Linear Systems of Equations

AM 205: lecture 6. Last time: finished the data fitting topic Today s lecture: numerical linear algebra, LU factorization

Since the determinant of a diagonal matrix is the product of its diagonal elements it is trivial to see that det(a) = α 2. = max. A 1 x.

Preliminary/Qualifying Exam in Numerical Analysis (Math 502a) Spring 2012

1.Chapter Objectives

Notes for CS542G (Iterative Solvers for Linear Systems)

Linear Algebraic Equations

Introduction to Mathematical Programming

1 Multiply Eq. E i by λ 0: (λe i ) (E i ) 2 Multiply Eq. E j by λ and add to Eq. E i : (E i + λe j ) (E i )

Lab 2 Worksheet. Problems. Problem 1: Geometry and Linear Equations

Section 3.5 LU Decomposition (Factorization) Key terms. Matrix factorization Forward and back substitution LU-decomposition Storage economization

Math/Phys/Engr 428, Math 529/Phys 528 Numerical Methods - Summer Homework 3 Due: Tuesday, July 3, 2018

AMSC/CMSC 466 Problem set 3

Lecture 10: Powers of Matrices, Difference Equations

CS513, Spring 2007 Prof. Amos Ron Assignment #5 Solutions Prepared by Houssain Kettani. a mj i,j [2,n] a 11

Bindel, Fall 2016 Matrix Computations (CS 6210) Notes for

Linear Algebra, part 3 QR and SVD

Review problems for MA 54, Fall 2004.

7. LU factorization. factor-solve method. LU factorization. solving Ax = b with A nonsingular. the inverse of a nonsingular matrix

Bindel, Fall 2016 Matrix Computations (CS 6210) Notes for

n 1 f n 1 c 1 n+1 = c 1 n $ c 1 n 1. After taking logs, this becomes

lecture 3 and 4: algorithms for linear algebra

Gaussian Elimination and Back Substitution

Math 552 Scientific Computing II Spring SOLUTIONS: Homework Set 1

B553 Lecture 5: Matrix Algebra Review

Singular Value Decomposition

MTH 464: Computational Linear Algebra

Notes on Eigenvalues, Singular Values and QR

AMS 209, Fall 2015 Final Project Type A Numerical Linear Algebra: Gaussian Elimination with Pivoting for Solving Linear Systems

Solving Linear Systems of Equations

2.1 Gaussian Elimination

AM205: Assignment 2. i=1

Review. Example 1. Elementary matrices in action: (a) a b c. d e f = g h i. d e f = a b c. a b c. (b) d e f. d e f.

CS264: Beyond Worst-Case Analysis Lecture #15: Topic Modeling and Nonnegative Matrix Factorization

PHYS 7411 Spring 2015 Computational Physics Homework 3

Linear Algebra and Eigenproblems

CS412: Lecture #17. Mridul Aanjaneya. March 19, 2015

Linear Algebra and Matrices

Singular Value Decomposition

Roundoff Analysis of Gaussian Elimination

Transcription:

AMath 584 Name: INSERT YOUR NAME HERE Take-home Final UWNetID: INSERT YOUR NETID Due to dropbox by 6pm PDT, Wednesday, December 14, 2011 The main part of the assignment (Problems 1 3) is worth 80 points. The extra credit problem is worth 40 points. You will not be penalized if you don t do it. Please use latex for this assignment! Submit a pdf file to the dropbox. You can also submit Matlab or Python codes directly rather than including in verbatim environments. Please make it clear what code goes with which problem and write them nicely. Comments in the code are a good idea! Problem #1. (a) Using Matlab or Python (one of these!), write the following two functions and test them on a small example to make sure they work: lu nopivot: Takes a square matrix A as input and returns matrices L and U corresponding to the LU factorization found by Gaussian elimination without pivoting. If the function encounters a divide-by-zero in the course of trying to do this (which might happen even if A is nonsingular), give an error message and abort. solve nopivot: Takes as input L, U, b, the results of the LU factorization and a right hand side b of the appropriate length, and returns the solution x to the linear system, computed by first solving Ly = b by forward substitution and then Ux = y by back substitution. Matlab hints: You can put each function in a separate m-file, e.g. lu nopivot.m, starting with the line function [L,U] = lu_nopivot(a) You will need to determine the size of A by doing: m = size(a,1) This is the number of rows in A. Check that the number of columns is the same. If not, return an error, e.g. if size(a,2) ~= m disp( ** Error: A is not square ) return end You can use algorithm 20.1 on page 151 fairly directly. Note that the m m identity matrix is obtained with eye(m) and the vector operation in the last line of the algorithm can be written as 1

U(j,k:m) = u(j,k:m) - L(j,k)*U(k,k:m) Python hints: The Python form of the function would be something like: def lu_nopivot(a): m = A.shape[0] if A.shape[1]!= m: raise Exception( ** Error: A is not square ) #etc... return L,U You can use algorithm 20.1 on page 151 fairly directly, but you ll need to correct for the fact that Python indexing starts at 0. Note that the m m identity matrix is obtained with eye(m) and the vector operation in the last line of the algorithm can be written as U[j,k:] = u[j,k:] - L[j,k]*U[k,k:] (b) Write a function testge that takes a matrix A (of arbitrary size) and a vector x (of the correct dimension) as input and does the following: Compute b = Ax to use as a right hand side in tests below. Print the 2-norm condition number κ(a) and also ɛ m κ(a), where ɛ m = 1.1 10 15 is machine epsilon. This is the size of relative error you might hope for in solving the system Ax = b. Use your functions to factor A and solve Ax = b without pivoting to obtain a result x. Compute and print the 2-norm of the error x x and the residual A x b. Solve the system Ax = b using backslash in Matlab or the NumPy routine solve to obtain a solution x. Compute and print the 2-norm of x x and the residual Ax b. These functions will be used in Problems 2 and 3 to see how well Gaussian elimination without pivoting works on some sample problems. Solution: 2

Problem #2. (a) A Toeplitz matrix has constant values along each diagonal, e.g. c 0 r 1 r 2 r 3 A = c 1 c 0 r 1 r 2 c 2 c 1 c 0 r 1. c 3 c 2 c 1 c 0 A Toeplitz matrix can be specified in terms of its first column and first row in either Matlab or Python (NumPy) via A = toeplitz(c,r) (or simply toeplitz(c) if r = c, in which case the matrix is symmetric.) Consider the Toeplitz matrix in which c j = r j = α j for some value α. If α < 1 then the values along each diagonal are decaying exponentially away from the main diagonal. (a) Show that if α < 0.5 then this matrix is strictly column diagonally dominant, meaning it satisfies m a kk > a jk. j=1 In other words, the diagonal term in each row is larger than the sum of all the off-diagonals. In this case it can be shown that no row interchanges will take place if Gaussian elimination with partial pivoting is applied. Hence factoring without pivoting should work fine in this case. (b) Test out a Toeplitz matrix of this form for α = 0.45 and m = 200 with your routines and see how the resulting solution compares to what the standard solver (backslash or solve) gives. (c) Now try α = 0.95 and m = 200. The matrix is not diagonally dominant but the off diagonal at least decay and you should get reasonable results and similar to what seen with the standard solver. (d) Now try α = 1.1 and m = 200. In this case the matrix elements grow exponentially away from the diagonal and pivoting would definitely interchange rows. Compare the accuracy of the solution obtained with your solver and with the standard solver and comment on whether each algorithm appears to be backward stable. (e) Write a program that tests 50 random matrices of size 100 100 and produces a plot similar to the one below. You can generate a matrix whose entries are each normally distributed with mean 0 and variance 1 using the function randn(100,100). Generate a vector x with random 3

components using randn(100,1). Use this to generate b = Ax and then solve the system both with and without pivoting as in Problem 1. Each matrix has a different condition number κ(a) and the plot shows the relative error obtained with and without pivoting plotted against κ(a)ɛ m, on a base 10 logarithmic scale. Solution: 4

Problem #3. A Toeplitz matrix is called circulant if for example r 0 = c 0, r i = c m+1 i for i = 1, 2,..., m 1, C = c 0 c 3 c 2 c 1 c 1 c 0 c 3 c 2 c 2 c 1 c 0 c 3 c 3 c 2 c 1 c 0 S = 0 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0. (1) The specific example S is a shift matrix or cyclic permutation matrix that shifts each element in a vector by 1 position (with periodic boundary conditions so the last element moves to the first location). Circulant matrices naturally arise if a finite difference operator is applied to a vector representing values of a function at m points on a domain with periodic boundary conditions. For example a centered approximation to a first derivative on a grid with spacing h might be approximated by such a matrix with r 1 = 1/(2h) and c 1 = 1/(2h) and all other elements 0. (Such matrices come up in 585 and 586). (a) Show that any circulant matrix has eigenvectors of the form 1 ω ω 2 x = 1 m. ω m 1 (2) for any value of ω C satisfying ω m = 1. (Note: the scalar factor 1/ m is immaterial for this problem, but useful below.) (b) What is the corresponding eigenvalue λ (in terms of ω and c 0,..., c m 1 )? (c) The complex numbers ω satisfying ω m = 1 are the mth roots of unity and are equally spaced around the unit circle in the complex plane. For example, for m = 4 they are at ±1 and ±i. Determine the eigenvalues and eigenvectors of the 4 4 shift matrix S from (1) (d) Let X be the matrix of eigenvectors of the 4 4 shift matrix S, normalized as in (2). Verify (by hand or computer) that X X = I. Hence the eigenvector matrix is unitary. Notes on this problem to put it in context: For general m, we can write the m possible values of ω as where i = 1. ω k = e 2πik/m = cos(2πk/m) + i sin(2πk/m), k = 0, 1,..., m 1, The matrix of eigenvectors X thus has a very special form: X jk = ω j k = e2πijk/m 5

This matrix of eigenvectors of an m m circulant matrix (scaled as in (2)) is always unitary. If we want to apply a circulant matrix C to a vector y we could write the product as where Λ is the matrix of eigenvalues of C. Cy = XΛX y This looks like a slow way to compute a matrix-vector product, replacing it with 3 matrixvector products, first multiplying by X, then by Λ, and then by X. But in fact each of these can be done very quickly. Λ is diagonal so that is quick. The others are quick because multiplying by X and X correspond to doing a Fourier transform and its inverse, due to the special form of the ω k. These products can therefore be computed using the Fast Fourier Transform (FFT) algorithm, which is a clever way of multiplying these matrices by a vector requiring only O(m log m) work instead of O(m 2 ) work. For large values of m this is a huge speedup. (The trick relies on the fact that although X is dense, it can be written as the product of sparse matrices.) Routines for doing the FFT and inverse FFT are available in Matlab and NumPy (fft and ifft). The fact that circulant matrices are related to Fourier transforms is also heavily used in the analysis of finite difference methods. Solution: 6

Extra-Credit Problem. Let C denote the set of continuous functions f defined on the interval 0 s 1. C is a linear space since it is closed under addition of functions and scalar multiplication of a function by a real number. Let K be a function that is continuous in [ 1, 1] and f C. Then the function g defined by g(t) = 1 is also in C and is called the convolution of K and f. 0 K(t s)f(s) ds for any t [0, 1] (3) If we fix the function K and consider the expression (3) it defines a linear operator G : C C by g = G(f). This mapping is linear because if f 1, f 2 C and α 1, α 2 lr, then G(α 1 f 1 + α 2 f 2 ) = α 1 G(f 1 ) + α 2 G(f 2 ). (4) Functions in the infinite-dimensional space C can be approximated by vectors in lr m if we pick m points in the interval, say the equally spaced points s i = ih for i = 1, 2,..., m where h = 1/m, and then evaluate f(s i ) to define a vector of length m. f = f(s 1 ). f(s m ) lr m. The convolution integral of (3) can be replaced by a discrete version that maps a vector f lr m to a new vector ḡ lr m if we replace the integral by a numerical approximation based on the m points, and define for example g(t i ) = 1 0 K(t i s)f(s) ds h m K(t i s j )f(s j ). (5) This is just a matrix-vector product, so ḡ = Ḡ f where the m m matrix has elements j=1 Ḡ ij = hk(t i s j ), i, j = 1, 2,..., m. (6) Note here that t i = ih and s j = jh (the same set of grid points are used for both s and t) and so Ḡ ij = hk((i j)h), i, j = 1, 2,..., m. (7) Note that Ḡ is a Toeplitz matrix. As a specific example, consider the Gaussian function ( K(s) = γ e βs2 + e β(s 1)2 + e β(s+1)2). (8) The three terms make the function periodic with period 1 over the interval [ 1, 1], and hence the matrix Ḡ will be circulant. The constant γ is a scale factor that depends on β, and should be chosen so that 1 0 K(s) ds = 1, but we ll choose it below to make the matrix have a related property. 7

This is a smoothing kernal in the sense that convolving K with a function f produces a smoother version of f. Intuitively, the integral (3) averages the values of f in a neighborhood of t to produce the value g(t). The larger β is, the more this looks like a delta function so that g(t) depends only on f(s) for s very close to t. Smaller β gives more smoothing. The figure below shows K(t s) as a function of s for three different values of t. Note that for t = 0.95 the periodic nature of the kernal is apparent. This means that in computing g(0.95) the values of f(s) for s near 0 also come into the average. Another way to view this is that we simply apply a Gaussian smoothing kernal, but to a periodic extension of the original f(s) so that it is defined for all s and integrate over all s. The next figure shows a function f(s) and the resulting function g that is obtained by convolving with the K shown above. Now consider the inverse problem: Given the smoothed version g, try to reconstruct the function f for which g = G(f). This sort of inverse problem comes up when trying to sharpen an image, for example. The discrete version corresponds being given a vector ḡ lr m and trying to determine f lr m so that Ḡ f = ḡ. It can be shown that under certain conditions the matrix Ḡ should be nonsingular. However, it will often be very ill-conditioned. To see why we expect ill-conditioning, suppose we change the function f used above by adding a perturbation that is highly oscillatory. The figure below shows the modified function and 8

the result obtained by convolving with the same smoothing kernal as before. The smoothed version g looks almost identical to the smoothed version of the original f. In fact they are identical to nearly machine accuracy. This means the inverse problem is very ill-conditioned: a tiny change in ḡ can give a huge change in Ḡ 1 f. So trying to solve the inverse problem by solving the system Ḡ f = ḡ directly can not be expected to give good results. As an example of how bad, the figure below shows what we get if we solve the linear system (with m = 400). (a) Compute the matrix Ḡ for this problem with β = 500 and m = 100. Use the formulas (7) and (8) with γ chosen so that the sum of elements in the first row of Ḡ is 1. (First form the matrix without γ and then compute the row sum in order to figure out how to scale the matrix.) You should find that every row of Ḡ then sums to 1. Explain this fact based on what you learned in Problem 3. (b) Let f lr 100 be the vector obtained by evaluating the function f(s) = cos(2πs) + 0.3 cos(20πs 2 ) at s j = jh with h = 0.01. This is the function that was used in the figures above. 9

Plot f vs. s, along with ḡ = Ḡ f, which should give a figure similar to the one above but on a coarser grid. (c) Try solving the linear system to recover f from ḡ and confirm that you get poor results. (d) Compute the SVD of Ḡ. Plot log 10(σ i ) vs. i to see how small some of the singular values are. (e) You should find that some singular values are at the level of rounding error. These values (and the corresponding vectors) have no accuracy. Solve the linear system using a pseudoinverse of Ḡ obtained by throwing away the singular values and vectors corresponding to any σ i < ɛ. Try various values of the tolerance ɛ to see how this affects the function you recover Ḡ + f = ˆV ˆΣ 1 Û f. (Note: From Problem 3 you might note that this could be done more efficiently using the FFT, but do it using the SVD.) (f) Try the same thing but starting with the function { 2 if x < 0.5 f(s) = 2s if x > 0.5 How well can you recover this discontinuous function from its smoothed version? Experiment with different cutoff levels ɛ. Note that a discontinuous function requires many high frequencies to represent it as a Fourier series and the pseudo-inverse is essentially throwing some of these away. Notes: A similar approach of setting small singular values to zero can be used for other inverse problems. It is also possible to define an approximate inverse by replacing 1/σ j that would appear in the exact inverse (when written in terms of Σ 1 ) by σ j /(σ 2 j + ɛ2 ) for some small value of ɛ > ɛ m. This is related to Tikhonov regularization. For the present problem this technique is also a variant of Fourier or band-pass filtering. The matrix Ḡ is symmetric and so the singular vectors are eigenvectors. Since the matrix is real, they are real vectors in spite of the fact that we know eigenvectors can have the form (2) where ω in general is complex. You might want to think about this and perhaps plot some of the singular vectors (plot a column of U against s). Hint: What is the multiplicity of each eigenvalue? Solution: 10