Math5335 2019
Math5335 2019
• 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.
• Save all files in your home folder. Do NOT create any sub-folders.
>> what
should list the MAT file 5data19.mat. 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
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.
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.
(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 .
(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.
(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.
(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.
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
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.