Math5335 2016
Math5335 2016
Math5335 2016
• Question 5 also requires you to define a function M-file q5f.m, in addition to q5.m.
• Questions 1, 2, 3, and 4 are each worth 10 marks, while Question 5 is worth 15 marks.
• Each script must require no interactive input, or depend on any variables defined
outside the script. To obtain the output for Question 1, you must be able to type
>> clear
>> q1
Do not include the clear statement in your scripts q1.m, q2.m etc.
• Save all files in your home folder. Do NOT create any sub-folders.
>> what
should list the MAT file q3val.mat. At the end you should also see your M-files
q1.m, q2.m, ...
1
Question 1 (10 marks): Answer in the file q1.m
Let n = 10. Consider the n × n matrix
2 0 1 0 3 0 0 0 0 0
0 2 0 1 0 3 0 0 0 0
0 0 2 0 1 0 3 0 0 0
0 0 0 2 0 1 0 3 0 0
3 0 0 0 2 0 1 0 3 0
A= −2
.
3 0 0 0 2 0 1 0 3
0 −2 3 0 0 0 2 0 1 0
0
0 −2 3 0 0 0 2 0 1
0 0 0 −2 3 0 0 0 2 0
0 0 0 0 −2 3 0 0 0 2
(d) Verify your result by computing, and then printing out, the value of the matrix
norm kA − U SV T k∞ , where U, S, V are the matrices obtained from the SV D factor-
ization.
(e) Compute the largest and smallest singular value of the singular value decomposition
storing it in the variables lsv and ssv.
(f) Compute the condition number in 2-norm of the matrix A using the singular values
of A.
(h) Solve Ax = b using the singular value decomposition of A storing the answer in the
variable xsvd.
2
(b) Construct the n by n matrix C storing only the non-zero elements (Hint: spdiags).
(d) Compute the inverse matrix C −1 using inv. In Figure 1 create a spy plot of C −1
(Hint: spy).
(e) Use the Matlab command symamd to re-order the rows and columns of C. Store the
re-ordered matrix in the variable B.
y = x1 + x2 t + x3 exp(t)
(d) Print out the values of the coefficients x1 , x2 and x3 , with at least 4 significant figures.
(e) Calculate the cubic spline that interpolates the data which has slope 0 at the first
and last data points.
(f) On the same axes, plot the data, the least-squares fit and the cubic spline interpolant
for t ∈ [0, 1]. Make sure you include a legend.
(b) Define the anonymous function f (x) = tan(x) − log x. (Hint: f = @(...) ... or
use the Matlab command inline.)
f (x + h) − 2f (x) + f (x − h)
h2
to the second derivative of f and store it as df.
3
(d) Compute the exact value of the derivative given by
2 sin(x) 1
3
+ 2
cos (x) x
and compute the error in the approximation. Store the error in the variable dferror.
(e) Define a vector hvec as (10−1 , 10−2 , 10−3 , . . . , 10−10 ) (i.e., the jth component has
value 10−j ).
(f) Compute the absolute errors of the central difference approximation to the derivative
for the values in the vector hvec. Store these errors as dferrorvec.
(g) Plot the error versus hvec in loglog scale. (Hint: Use loglog.)
(h) Determine for which value of h you get the smallest value and use fprintf to display
this value.