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

UNIVERSITY OF NEW SOUTH WALES

SCHOOL OF MATHEMATICS AND STATISTICS


MATH5335 Mathematical Computing for Finance
Session 2, 2017
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 q5g.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
Consider upper triangular n × n matrix
 
1 0 ... ... 0
 2 1 0 . . . 0
. .. 
 . .
A =  .. . . . . .

. ... ... 
 .. 0
n ... ... 2 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 the singular value decomposition is correct by computing kA − SDV > k2 .
Store the result in Acheck.

(e) Define a rank 100 approximation of the matrix A (i.e., use only the singular values
σ1 , σ2 , . . . , σ100 ). Store it in the matrix Aapp.

(f) Compute the 1-norm of A-Aapp and store it in Eapp.

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


For a function g, Newton’s method can be used to find a value x∗ such that g(x∗ ) = 0. It
is defined by the iteration
g(xn )
xn+1 = xn − 0 , n ≥ 0. (1)
g (xn )
Let now
f (x) = (x − 1)3 + exp(x), x ∈ R.
The goal is to find the inverse of this function using Newton’s method at the point 2, i.e.
f −1 (2).

(a) Set x0 to 3 and use it as starting value for the following Newton iteration.

(b) Modify Newton’s iteration (1) so that it can be used to approximate the inverse of
a function. Write a Matlab program which uses this modified Newton iteration (do
Not use build-in functions 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
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) Find the linear least-squares fit to the data (tj , yj ), j = 1, . . . , n of the form
2
y = x1 ∗ e−x2 ∗t ∗ tx3 .

(Hint: Take the logarithm of the equation first, to make it linear in log(x1 ), x2 , x3 .)
Store the vector (x1 , x2 , x3 )> in the variable xls as a column vector.

(d) Calculate the cubic spline that interpolates the data and uses the (default) not-a-knot
end condition.

(e) On the same axes, plot the data, the least-squares fit and the cubic spline interpolant
for t ∈ [1, 2]. Make sure you include a legend.

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


Consider the function
f (x) = x3 + exp(sin(x)), x ∈ R.
R1
The aim is to approximate the integral I = 0 f (x) dx.

(a) Define the anonymous function f (x) = x3 + exp(sin(x)). (Hint: f = @(...).)

(b) Use the Matlab build-in function integral to approximate the integral. Store the
result in II. (Hint: Use help integral to find out how this Matlab function works.)

(c) In the following we use the composite Simpson 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

h
QSimp
N (f ) = f (x0 ) + 4f (x1 ) + 2f (x2 ) + 4f (x3 ) + 2f (x4 ) + · · ·
3 
+ 2f (xN −2 ) + 4f (xN −1 ) + f (xN )

to approximate the integral I. (Here N is even.)

i) Set NI = 1000.
ii) Define a vector xI of NI+1 equally spaced points such that the first point is 0
and the last point is 1.
iii) Use the composite Simpson rule to approximate the integral I. Store the value
in IS.

(d) Use fprintf to display the value of II-IS.

3
Question 5: Answer in the files q5g.m and q5.m
Let
g(x) = x2 + exp(x).
We consider the initial value problem
u00 (z) + exp(−z)u(z) =g(z), 0≤z≤1 (2)
u(0) =1
u0 (0) =1.

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

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


g = q5g(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 q5g
explains input and output arguments and the purpose of the function.

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

i) Set N to 103 .
ii) Let Un ≈ u(zn ), where zn = n/N for n = 0, 1, . . . , N .
We use the forward difference approximation
U1 − U0
u0 (z0 ) ≈
1/N
and the central difference approximation to approximate
Un+1 − 2Un + Un−1
u00 (zn ) ≈ .
1/N 2

Substituting this approximation into (2) we obtain the linear system


U0 =1
−U0 + U1 =1/N
U0 + (−2 + N −2 e−1/N )U1 + U2 =N −2 g(1/N )
U1 + (−2 + N −2 e−2/N )U2 + U3 =N −2 g(2/N )
.. ..
. .
UN −2 + (−2 + N −2 e−(N −1)/N )UN −1 + UN =N −2 g((N − 1)/N ).
(You do Not have to verify that the linear system is correct.)
Define an (N + 1) × (N + 1) matrix B and a vector b of length N + 1 in Matlab
such that the above linear system can be written as B*U = b.

4
iii) Solve the linear system to find the vector U.
iv) Set now Nlarge = 1e6.
v) Define an (Nlarge + 1) x (Nlarge + 1) matrix Blarge and a vector blarge
of length Nlarge+1 in Matlab such that the above linear system can be written
as Blarge * Ularge = blarge. (Hint: spdiags)
vi) Solve the linear system Blarge * Ularge = blarge.

You might also like