0% found this document useful (0 votes)
49 views

Math5335 2019

This document provides instructions for a mathematical computing test in Matlab. Students are asked to write script M-files to answer 5 questions. Questions 1-4 require writing code in separate M-files (q1.m, q2.m, etc.) to perform calculations and analysis on matrices and functions. Question 5 requires defining an additional function M-file (F519.m) and writing code in q5.m to numerically solve a differential equation using finite differences. Students are provided data and functions to use in their code and are instructed to save files, use specific formatting, and not use interactive input or variables outside their scripts.

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)
49 views

Math5335 2019

This document provides instructions for a mathematical computing test in Matlab. Students are asked to write script M-files to answer 5 questions. Questions 1-4 require writing code in separate M-files (q1.m, q2.m, etc.) to perform calculations and analysis on matrices and functions. Question 5 requires defining an additional function M-file (F519.m) and writing code in q5.m to numerically solve a differential equation using finite differences. Students are provided data and functions to use in their code and are instructed to save files, use specific formatting, and not use interactive input or variables outside their scripts.

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 Mathematical Computing for Finance
Term 2, 2019
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 F519.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 blank paper and a pen.

• When you first start Matlab, typing

>> what

should list the MAT file 5data19.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 n1 = 11. Consider the n1 × n1 matrix
 
5 0 −3 0 0 −1 0 0 0 0 0
 0
 5 0 −3 0 0 −1 0 0 0 0 

 −2 0 5 0 −3 0 0 −1 0 0 0 
 
 0 −2 0 5 0 −3 0 0 −1 0 0 
 
 0
 0 −2 0 5 0 −3 0 0 −1 0 

A=  0 0 0 −2 0 5 0 −3 0 0 −1 .

 0
 0 0 0 −2 0 5 0 −3 0 0 

 0
 0 0 0 0 −2 0 5 0 −3 0 

 0
 0 0 0 0 0 −2 0 5 0 −3 

 0 0 0 0 0 0 0 −2 0 5 0 
0 0 0 0 0 0 0 0 −2 0 5
(a) Define the variable n1 = 11 and store it in n1.
(b) Construct the n1 by n1 matrix A1 efficiently and store it in A1.
(c) Find the QR factorization of A1 and store the orthogonal matrix in Q1 and the upper
triangular matrix in R1. (Hint: help qr)
(d) Check that Q1 is an orthogonal matrix by computing kQT1 Q1 − Ik2 , where I is the
n1 by n1 identity matrix. Store the value of the norm in the variable Q1check.
(e) Define the column vector b = (n1 , n1 − 1, n1 − 2, . . . , 2, 1)T .
(f) Use the matrices Q1 and R1 to solve the linear system A1 x = b, storing your answer
in the variable xsol. (You may use the backslash \ operator, but only with R1 .)
(g) Calculate the residual vector r = A1 x − b, storing your answer in the variable rsol.
(h) Calculate the 1-norm krk1 , storing your answer in the variable rnorm.

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


You are to find the largest value of x∗ which gives a point of intersection of the functions
x2
f1 (x) = x2 and f2 (x) = e− 4 ,
i.e. f1 (x∗ ) = f2 (x∗ ).
(a) Use a plot of the two functions to determine by hand an element x0 which is close
to the intersection you are seeking.
(b) Define an autonomous function f so that the point of intersection is a zero of f.
(Hint: f = @(...).)
(c) The Newton iteration calculates successive approximations of the root of a function
using the formula
f (xn )
xn+1 = xn − 0 .
f (xn )
Use one step of Newton’s method to compute x1 , using the value x0 as starting point.
Store the answer in x1.

2
(d) Print the calculated value of x1 with 3 significant figures.

(e) Print the value of |f1 (x1 ) − f2 (x1 )| with an appropriate number of significant figures.

(f) Use Newton’s method to compute successive approximations x1 , x2 , x3 , . . . , of the


root x∗ . Set xapp to xn , where xn satisfies |xn − xn−1 | ≤ 2−52 .

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


The file 5data19.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

y = x1 cos(2πt) + x2 cos(4πt) + x3 t2 .

Store the vector (x1 , x2 , x3 )> as a column vector in xls.

(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 ∈ [0, 1]. Make sure you include a legend. Save the Matlab figure as q319.pdf.

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


Consider the function

f (x) = sin(10πx) exp(−x), 0 ≤ x ≤ 10.


R 10
The aim is to approximate the integral I = 0 f (x) dx.

(a) Define the anonymous function f (x) = sin(10πx) exp(−x). (Hint: f = @(...).)

(b) Use the Matlab build-in function quad to approximate the integral. Store the result
in IQ.

(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 any Matlab build in function (like trapz).

i) Set NI = 11.
ii) Define a vector xI of NI equally spaced points such that the first point is 0 and
the last point is 10.

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

(e) The correct value of the integral is 0.0319... and so the error using the composite
trapezoidal rule with 11 nodes is rather large. Define a new value N2 and compute
an approximation IT2 of the integral using the composite trapezoidal rule with N2
nodes such that |IQ-IT2| is smaller than 0.001.

(f) Write a comment in the Matlab file which explains the reason why a large number
of nodes is needed to reduce the error.

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


Let
f (x) = 2 + x + x2 .
We consider the initial value problem

u00 (x) + u(x) =f (x), 0≤x≤1 (1)


u(0) =0
u0 (0) =2.

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

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


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

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

i) Set N to 102 .
ii) Let Un ≈ u(xn ), where xn = n/N for n = 0, 1, . . . , N .
We use the forward difference approximation
U1 − U0
u0 (x0 ) ≈
1/N

and the central difference approximation to approximate


Un+1 − 2Un + Un−1
u00 (xn ) ≈ .
1/N 2

4
Substituting this approximation into (1) we obtain the linear system

U0 =0
−U0 + U1 =2/N
Un−1 + (−2 + N )Un + Un+1 =N −2 f (n/N ),
−2
1 ≤ n ≤ N − 1.

(You do Not have to verify that the linear system is correct.)


Solve the linear system to find the column vector U given by (U0 , U1 , U2 , . . . , UN )> .
Store the result in U.
iii) The explicit solution is given by u(x) = sin(x) + x + x2 . Compute the 2-norm
of the error, i.e. the 2-norm of the vector (Un − u(n/N ))N
n=0 and store the result
in Uerr. (This error should be smaller than 0.024.)
iv) Set now Nlarge = 1e6.
v) Compute the solution vector Ularge given by (U0 , U1 , . . . , U106 )> , where Ularge
is a column vector of length 106 + 1.

You might also like