Math5335 2016

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

UNIVERSITY OF NEW SOUTH WALES

SCHOOL OF MATHEMATICS AND STATISTICS


MATH5335 Computational Methods for Finance
Session 2, 2016
Matlab Computing Test
Time allowed: 90 minutes
Name Student number Signature

Instructions — Read Carefully


• Create script M-files q1.m, q2.m, q3.m, q4.m and q5.m containing your answers to
Questions 1, 2, . . . , 5, respectively.

• 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.

• Use semicolons to avoid producing unnecessary output.

• Save all files in your home folder. Do NOT create any sub-folders.

• No materials are allowed, except a pen.

• When you first start Matlab, typing

>> what

should list the MAT file q3val.mat. At the end you should also see your M-files
q1.m, q2.m, ...

Login and start Matlab


Do not turn over the page until instructed to do so

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

(a) Define the variable n = 10.

(b) Construct A efficiently.

(c) Find the SV D factorization of A. (Hint: help svd)

(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.

(g) Define b = (1, 2, 3, . . . , n − 1, n)T .

(h) Solve Ax = b using the singular value decomposition of A storing the answer in the
variable xsvd.

Question 2 (10 marks): Answer in the file q2.m


Consider the n × n covariance matrix
 
1 0 0 −φ 0 0 0 0 ... 0
 0 1 + φ2 0 0 −φ 0 0 0 ... 0 
 
2
 0
 0 1 + φ 0 0 −φ 0 0 . . . 0 

−φ 2
 0 0 1+φ 0 0 −φ 0 ... 0 
2
 0 −φ 0 0 1 + φ 0 0 −φ . . . 0 
C =  .. ..  .
 
.. .. .. .. .. .. ..
 . . . . . . . . . 
2
 
 0 . . . 0 −φ 0 0 1 + φ 0 0 −φ 
2
 
 0 ... 0 0 −φ 0 0 1+φ 0 0 
1 + φ2 0 
 
 0 ... 0 0 0 −φ 0 0
0 ... 0 0 0 0 −φ 0 0 1

(a) Define n = 100 and φ = 0.99.

2
(b) Construct the n by n matrix C storing only the non-zero elements (Hint: spdiags).

(c) Compute the eigenvalues of C storing them in the vector ev.

(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.

(f) Compute the inverse B −1 of B and in Figure 2 create a spy plot of B −1 .

Question 3 (10 marks): Answer in the file q3.m


The file q3val.mat holds two arrays

tdata = [t1 , t2 , . . . , tn ] and ydata = [y1 , y2 , . . . , yn ].

(a) Use load to create tdata and ydata.

(b) Find the value of n and store it in ndata.

(c) Use a function of the form

y = x1 + x2 t + x3 exp(t)

to approximate the data.

(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.

Question 4 (10 marks): Answer in the file q4.m


Consider the function
f (x) = tan(x) − log x, x > 0.
The aim is to approximate the second derivative f 00 of f at the point x = 4.

(a) Set x = 4 and h = 10−1 .

(b) Define the anonymous function f (x) = tan(x) − log x. (Hint: f = @(...) ... or
use the Matlab command inline.)

(c) Compute the central difference approximation

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.

Question 5: Answer in the files q5f.m and q5.m


Consider the function
f (x) = x2 − cos x.
The derivative of f is given by
f 0 (x) = 2x + sin x.
The aim is to approximate the largest root of this function using Newton’s method.
(a) (5 marks): Answer in the file q5f.m
i) Write a function M-file q5f.m
[f, df] = q5f(x).
to calculate f, the function value f (x), and the derivative df of the function
f 0 (x).
ii) If the input argument x is a vector then the function should produce a column
vector of function values and the corresponding values of the derivative.
iii) Include comments at the beginning of your function so that
help q5f
explains input and output arguments and the purpose of the function.
(b) (10 marks): Answer in the file q5.m
i) Use a plot of the function to determine by hand an element x0 which is close to
the root of the function.
f (x0 )
ii) Compute the next iteration x1 by using the formula x1 = x0 − f 0 (x0 )
. Store the
value in x1.
iii) Compute the iterates xk = xk−1 − f (xk−1 )/f 0 (xk−1 ) for k = 2, . . . , 100 and store
them in the vector x.
iv) Estimate the root of the function using the Matlab command fzero. Store the
value as xzero and compute the difference between xzero and x(100).
v) Display the function value f (x100 ).

You might also like