When presenting your solutions to the assignment, please include some explanation in words
to accompany your calculations. It is not necessary to write a lengthy description, just a
few sentences to link the steps in your calculation. Messy, illegible or inadequately explained
solutions may be penalised. The marks awarded for each part are indicated in boxes.
This assignment has 1 question, for a total of 24 marks.

1. Let fj = f(xj ), where xj are nonuniformly spaced grid points x1 < x2 < · · · < xn. Let the
spacing between these points be hj = xj+1 − xj for j = 1, 2, . . . , n − 1.
1 (a) Write down the quadratic Lagrange polynomial that interpolates the data (xj−1, fj−1),
(xj , fj ) and (xj+1, fj+1).
3 (b) By differentiating the polynomial found in (a), derive an approximation f
0
j
for the
derivative f
0
(xj ).
You must write your final answer in terms of fj−1, fj , fj+1, hj−1 and hj .
4 (c) Use the polynomial interpolation error theorem to show that the absolute value of
the error in the derivative approximation from part (b) is bounded by CMh2 where
|f
000| ≤ M for some positive constant M, h = max hj and C is a constant that you
must determine.
2 (d) Write a Matlab function called diff1 that estimates the derivative of nonuniformly
sampled data. Your function must use and document the interface
function dfdx = diff1(f, x)
where f is a vector containing the data values (f1, f2, . . . , fn), x is a vector of the same
size containing the corresponding sample points in ascending order (x1, x2, . . . , xn) and
dfdx is a vector containing estimates of the derivative at each point in x.
• You must use your answer from part (b) to estimate the derivatives for the internal
points j = 2, 3, . . . , n − 1. For the boundary points you must use the following
formulae, which are also derived by differentiating a quadratic interpolating polynomial:
f
0
j−1 = −fj−1
2hj−1 + hj
hj−1(hj + hj−1)
• fj
hj + hj−1
hj−1hj
− fj+1
hj−1
(hj + hj−1)hj
f
0
j+1 = fj−1
hj
hj−1(hj + hj−1)
− fj
hj + hj−1
hj−1hj
• fj+1
2hj + hj−1
(hj + hj−1)hj
• The output vector must be of the same size as the input vectors f and x. You
may assume that the input vectors have the same size.
• Try to vectorise your function by avoiding for and while loops.
• Document your function with comments. The minimum documentation for a
function is a description of what the function does, descriptions of the inputs and
outputs, your name, and the modification date.
• Include a copy of the function in your report.
• Submit your function to Matlab Grader on MyUni for marking.
1 (e) One of the Matlab Grader tests samples the function f(x) = a0 + a1x + a2x
2 at
n randomly spaced grid points on the interval 0 ≤ x ≤ 1, where {a0, a1, a2} are all
randomly generated coefficients, then calls diff1 to estimate the derivative, and finally
computes the maximum absolute error
max
j
|f
0
(xj ) − f
0
j
|.
If your function diff1 is correct, what should the maximum absolute error be for this
test? Does the number of grid points n matter? Explain your answer.
4 (f) Write a script that uses diff1 to estimate the derivative of sin 2x using n = 1 + 2k
,
k = 1, 2, . . . , 8 equispaced points in the interval 0 ≤ x ≤ π, then creates a log-log plot
of the maximum absolute error over the interior points,
max = max
2≤j≤n−1
|f
0
j − f
0
(xj )|,
as a function of the grid spacing h = xj+1 − xj . Make sure that the resulting plot is
properly labelled.
• You do not need to vectorise the script.
• Include a copy of your script in this report.
• Document your script. The minimum documentation for a script is a description
of what the script does, your name and the modification date.
• You do not need to submit your script to Matlab Grader.
Run your script and include the resulting plot in your report. Make sure you include
a title or caption for your figure.
1 (g) Is the dependence of the error on h in your plot consistent with the error bound found
6 (h) Nonuniform grids are handy in applications where a finely-spaced grid is needed to
resolve some local detail, but is not required (and therefore computationally wasteful)
elsewhere. Here is a simple example.
Write a script that:
• Samples and plots the function tanh(5x) using 40 equispaced grid points in the
interval −2 ≤ x ≤ 2. Use symbols for the plot, so that you can see the sample
points.
• Samples and plots the same function tanh(5x) using 40 nonequispaced grid points
determined by first creating a vector of 40 equispaced points ξ in the interval
−1 ≤ ξ ≤ 1, then transforming them according to
x = 2
ξ + αξ(ξ
2 − 1)
(1)
for α = 0.7. Use symbols for the plot, so that you can see the sample points.
• Uses diff1 to estimate the derivative of the function tanh(5x) for the equispaced
and nonequispaced cases above, then plots the absolute error against x for each
case on a single plot.
Page 2
You are given that
d
dx
tanh(5x) = 5 − 5 tanh2
(5x).
• As always, make sure the plots are properly labelled. Include a legend where
necessary.
• Include a copy of your script in your report.
• You do not need to submit your script to Matlab Grader.
2 (i) What is the effect of the transformation (1) on the spacing between the grid points
and the magnitude of the error? Why does the error behave in this way?
Page 3

Sample Solution