Math5335 2018
Math5335 2018
• At the beginning of each file write your name and student number.
• Question 5 also requires you to define a function M-file q5G18.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 file blackscholes.m. 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
Consider upper triangular n × n matrix
1 1 ... ... 1
0 1 ... ... 1
.
.. ..
A = .. . . 1
. ... ... ..
.. .
0 ... ... 0 1
(c) Find the SVD factorization of A and store it in the matrices S, D, V such that A =
SDV > . (Hint: help svd)
(f) Use the matrices S, V, D to solve the linear system Ax = b, storing your answer in
the variable x. (You may use the backslash \ operator, but only with D.)
(g) Calculate the residual vector r = Ax − b, storing your answer in the variable r.
f (xn )
xn+1 = xn − , n ≥ 0. (1)
f 0 (xn )
Let now
f (x) = x3 + log(x), x ∈ [1/2, 1].
The goal is to find the root of this function using Newton’s method.
(a) Set x0 to 1 and use it as starting value for the following Newton iteration.
(b) Write a Matlab program which uses the Newton iteration (do Not use build-in func-
tions like fzero in Matlab to do that). Use 3 iterations and store the result in
x3.
(c) Run the Newton iteration till the difference between successive approximations is
smaller than the machine epsilon eps, i.e. |xn+1 − xn | < eps = 2−52 . Store the result
in xe.
(d) Use fprintf to print the values x3 and xe. Do not type in the value directly, but
use the print function to do that.
2
Question 3 (10 marks): Answer in the file q3.m
(a) Define the following variables:
g(x) = blackscholes(S, K, r, x, T) − c,
(c) Define the column vector xd to be (x1 , x2 , . . . , x21 ) where xj = (j − 1)/20 for j =
1, 2, . . . , 21.
(e) Find the linear least-squares fit to the data (xd, yd) of the form
y = a1 + a2 x + a3 x 2 + a4 x 3 + a5 x 4 + a6 x 5 .
(f) Calculate the cubic spline that interpolates the data and uses the (default) not-a-knot
end condition.
(g) Define the row vector xpl as 101 linearly spaced points from 0 to 1 (the 101 points
include the point 0 and the point 1).
(h) Define the row vector yls as the least squares approximations using the values in
xpl and define the row vector ysp as the spline approximations using the values in
xpl.
(i) On the same axes, plot the data, the least-squares fit and the cubic spline interpolant
(using xpl, yls and ysp for the least-squares and spline). Make sure you include a
legend.
(b) Use the Matlab build-in function integral to approximate the integral. Store the
result in IQ. (Hint: Use Inf to define ∞ in Matlab.)
3
(c) In the following we use the composite trapezoidal rule, which for a grid a = x0 <
x1 < x2 < . . . < xN = b with h = xi − xi−1 = 1/N for i = 1, 2, . . . , N , is defined by
trap F (x0 ) F (xN )
QN (F ) = h + F (x1 ) + F (x2 ) + · · · + F (xN −1 ) +
2 2
to approximate the integral I. Do Not use the Matlab build in function trapz.
i) Set NI = (1e6) + 1.
ii) Define a vector xI of NI equally spaced points such that the first point is a = 0
and the last point is b = 100.
iii) Use the composite trapezoidal rule to approximate the integral I. Store the
value in IT.
u(z) = z 2 + cos(πz).
i) Set N to 101.
ii) Let zn = n/N for n = 0, 1, . . . , N and set h to zn − zn−1 .
4
iii) In the following let Un ≈ u(zn ). Set U0 = 1.
iv) We use the forward difference approximation to approximate
Un+1 − Un
u0 (zn ) ≈ .
h
Substituting this approximation into (2) and rearranging the equation we obtain
that