Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Assignment 2: Root-finding, linear systems
and least squares fitting.
Note: This assignment is worth 20% of the total assessment in MAST30028. When you submit the assignment,
you should submit two files: one pdf file and one zip file. The pdf file contains the answer you write, the
numerical results you generated including figures or tables(0 mark if numerical results are not included in the
pdf file), the comment or the explanation of the results, and the printout of your code (you may use Matlab
publish or Matlab live scripts). The zip file should only contains all the m-files.
1 Root-finding [10 marks]
a. Use Newton’s method to find roots of the following functions with the given initial guesses x0 and the
absolute and relative tolerance being 10?12. In each case report and explain the results.
(i)
In the case of (iii), investigate the order of convergence.
b. MATLAB has only one built-in function for finding roots : fzero. (The Optimization Toolbox has more).
To find out about it, start up Matlab and type
help fzero
Try out fzero on the examples above. Interrogate the structure output (given by fzero) to find out what
happened. Use the same tolerance as in the Newton’s method.
2 Operation counts [7 marks]
Derive the operation count (multiplications and divisions only) for
a. Choleski factorization (without pivoting)
The detailed algorithm for Choleski factorization is that implemented in CholScalar.
b. Gauss elimination (equivalent to LU factorization) without pivoting of an Upper Hessenberg matrix.
An Upper Hessenberg matrix is one with zeroes below the subdiagonal. Just modify the derivation for
Gauss elimination in lectures.
1
c. solution of a tridiagonal system by LU factorization without pivoting (also called the Thomas algorithm)
This is the algorithm implemented in the code tridisolve in the Asst2 folder.
2
3 Sparse matrices. [7 marks]
MATLAB has support for sparse matrices. Banded matrices are an important class of sparse matrices. Here
you explore the sparse matrix facilities by concentrating on the tridiagonal matrix from Moler Exercise 2.19 but
with n arbitrary instead of 100. Let’s call it A. It has a diagonal full of 2s and a sub- and superdiagonal full of
-1s.
Form the matrix A in 2 ways:
a. using 3 calls to diag to form A1
b. using spdiags instead, to form A2
For each of these forms, use tic,toc to benchmark, for n = 200 : 200 : 2000 the time to solve Ax = b for
some b using \. I suggest you time a loop of, say, 100 solves in order to get sensible results. If you have a fast
machine, you may need to increase n.
Also try the tridiagonal solver tridisolve from the Asst2 folder to solve the same problem. It will be
slower because it’s an M-file, not a built-in function.
How does the time taken by each of these 3 solvers scale with n? On the basis of Q2, what do you think
Matlab’s \ is doing? By using whos, describe another benefit of using sparse.
4 Norm equivalence [6 marks]
Prove the following inequalities, for arbitrary m,n. Here x is an m-vector and A is an m× n matrix.
||x||2 ≤ ||x||1
||x||1 ≤
√
m||x||2
||A||2 ≤
√
n||A||1
||A||1 ≤
√
m||A||2
In the first two cases, give an example of a vector x where equality holds.
5 Conditioning [4 marks]
Let X be the n× n matrix defined by
[I,J] = ndgrid(1:n);
X = min(I,J) + 2*eye(n,n) - 2;
Which, if any, of the triangular factorizations chol(X), lu(X), and qr(X) reveal the poor conditioning of X
when n = 20? Explain, using results from the lectures.
6 Are all fits equally good? [9 marks]
Examine the M-file polyfit.m (edit polyfit) and understand what lines 59–67 do.
Do Moler Exercise 5.7 but use the the values tk = (k ? 1)/5, yk = erf(tk), k = 1 : 11.
For part c, plot the errors of the three fitted functions with 5 parameters (i.e. a degree 4 polynomial, a degree
9 odd polynomial and the function in part c) on a loglinear scale. Which form fits the data best?
Hint: You may want to write your own M-file to help generate the least squares fits for parts b and c using
polyfit/polyval as a template (although it’s not necessary to do so).