0% found this document useful (0 votes)
68 views5 pages

Math5335 2018

This document provides instructions for a Matlab computing test consisting of 5 questions. Students are asked to provide solutions to each question in separate M-files (q1.m, q2.m, etc.) and include name and student number. Questions involve matrix computations, solving differential equations numerically, option pricing, and integration. Strict formatting requirements are given around file organization and output. Students are instructed to start Matlab and ensure required files are present upon completion.

Uploaded by

Peper12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views5 pages

Math5335 2018

This document provides instructions for a Matlab computing test consisting of 5 questions. Students are asked to provide solutions to each question in separate M-files (q1.m, q2.m, etc.) and include name and student number. Questions involve matrix computations, solving differential equations numerically, option pricing, and integration. Strict formatting requirements are given around file organization and output. Students are instructed to start Matlab and ensure required files are present upon completion.

Uploaded by

Peper12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

UNIVERSITY OF NEW SOUTH WALES

SCHOOL OF MATHEMATICS AND STATISTICS


MATH5335 Computational Methods for Finance
Session 2, 2018
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.

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

• 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 file blackscholes.m. 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
Consider upper triangular n × n matrix
 
1 1 ... ... 1
0 1 ... ... 1
.
.. ..

A =  .. . . 1
 
. ... ... .. 
 .. .
0 ... ... 0 1

(a) Define the variable n = 103 .

(b) Construct the n by n matrix A efficiently.

(c) Find the SVD factorization of A and store it in the matrices S, D, V such that A =
SDV > . (Hint: help svd)

(d) Check that S is an orthogonal matrix by computing kS T S − Ik2 , where I is the n by


n identity matrix. Store the value of the norm in the variable Scheck.

(e) Define the column vector b = (n, n − 1, n − 2, . . . , 2, 1)T .

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

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


For a function f , Newton’s method can be used to find a value x∗ such that f (x∗ ) = 0. It
is defined by the iteration

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:

S = 100; % underlying asset price


K = 90; % strike price
r = 0.05; % risk-free interest rate
T = 1; % time to expiry
c = 30; % market price of option

(b) Define the anonymous function

g(x) = blackscholes(S, K, r, x, T) − c,

where S, K, r, T, c are given above. Hint: Use


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.

(d) Define the column vector yd to be g(xd).

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

Store the coefficients a1 , a2 , a3 , a4 , a5 , a6 in the column vector als as


als = (a1 , a2 , a3 , a4 , a5 , a6 )> .

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

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


Consider the function
F (x) = x2 exp(−x2 ), x ≥ 0.
R∞
The aim is to approximate the integral I = 0 F (x) dx.
(a) Define the anonymous function F (x) = x2 exp(−x2 ). (Hint: F = @(...).)

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

(d) Use fprintf to display the value of IQ-IT.

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


Let
G(z) = 2z + πz 2 tan(πz).
We consider the initial value problem

u0 (z) + π tan(πz)u(z) =G(z), 0≤z≤1 (2)


u(0) =1.

The goal is to approximate the solution u. The exact solution is known to be

u(z) = z 2 + cos(πz).

You do NOT have to verify this.

(a) (5 marks): Answer in the file q5G18.m

i) Write a function M-file q5G18.m that defines the functions


G = q5G18(z)
to calculate G(z).
ii) If the input argument z is a vector of length n, then the function should produce
a column vector of function values of length n. (Notice that z can either be a
column or a row vector.)
iii) Include comments at the beginning of your function so that
help q5G18
explains input and output arguments and the purpose of the function.

(b) (10 marks): Answer in the file q5.m

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

Un+1 = Un (1 − hπ tan(πzn )) + hG(zn ).

(You do Not have to verify this.)


Use this equation to compute U1 , U2 , . . . , UN .
Store the result in the row vector U, so that U = (U0 , U1 , . . . , UN ).
v) Compute the infinity norm between the row vector U and the vector (u(z0 ), u(z1 ), . . . , u(zN )),
where u is the exact solution. Store the difference as Uerr.

You might also like