MATH3511 Assignment 2
MATH3511 Assignment 2
Assignment 2
Phoebe Grosser, u7294693
14th March 2023
xn x0 x1 x2 x3
x 0 2 3 4
f (x) 7 11 28 63
Table 1. Data nodes for Question 1
We will first develop the Newton form of the polynomial, which can be done by successively building up the degrees of
the polynomial. The Newton method requires that successive polynomials pn (x) follow the form:
wherein the constant c is determined at each new polynomial formulation such that pn (xn ) = f (xn ). We can first recognise
that the 0-th degree polynomial, p0 (x), that fits the node pair (x0 , f (x0 )) will be equal to:
The requirement that p1 (x1 ) = f (x1 ) (ie. that p1 (2) = 11) requires that the constant c is equal to 2. Hence,
p1 (x) = 7 + 2x (3)
Further iterations of the Newton polynomial, up until the final form p3 (x) is achieved, can be repeated exactly as above
by following Equation 1.
p2 (x) formulation:
p3 (x) formulation:
This polynomial can equivalently be expressed in expanded form as p3 (x) = x3 − 2x + 7. Hence, the Newton form of the
interpolated polynomial is p(x) = 7 − 2x + x3 .
Page 1
We now want to find the interpolated polynomial via the Lagrange method, which requires the use of cardinal functions.
Cardinal functions are specific to each data node pair, and return a value of 1 at the node xn and a value of 0 at all other
xm̸=n nodes. The cardinal function Ij (x) for the node pair (xj , f (xj )) is equal to:
n
Y (x − xi )
Ij (x) = (4)
(xj − xi )
i=0,i̸=j
where n is the index of the final node. Once all the cardinal functions have been constructed, the resultant interpolated
polynomial can be determined as:
n
X
p(x) = f (xj ) · Ij (x) (5)
j=0
Hence, by noting the node pairs in Table 1 and following the cardinal function forms in Equation 4, the following cardinal
functions can be constructed:
I0 (x) formulation:
(x − x1 )(x − x2 )(x − x3 )
I0 (x) =
(x0 − x1 )(x0 − x2 )(x0 − x3 )
(x − 2)(x − 3)(x − 4)
=
(0 − 2)(0 − 3)(0 − 4)
1
= − ((x2 − 5x + 6)(x − 4))
24
1
= − (x3 − 9x2 + 26x − 24)
24
I1 (x) formulation:
(x − x0 )(x − x2 )(x − x3 )
I1 (x) =
(x1 − x0 )(x1 − x2 )(x1 − x3 )
(x − 0)(x − 3)(x − 4)
=
(2 − 0)(2 − 3)(2 − 4)
1
= (x(x2 − 7x + 12))
4
1
= (x3 − 7x2 + 12x)
4
I2 (x) formulation:
(x − x0 )(x − x1 )(x − x3 )
I2 (x) =
(x2 − x0 )(x2 − x1 )(x2 − x3 )
(x − 0)(x − 2)(x − 4)
=
(3 − 0)(3 − 2)(3 − 4)
−1
= (x(x2 − 6x + 8))
3
−1 3
= (x − 6x2 + 8x)
3
I3 (x) formulation:
(x − x0 )(x − x1 )(x − x2 )
I3 (x) =
(x3 − x0 )(x3 − x1 )(x3 − x2 )
(x − 0)(x − 2)(x − 3)
=
(4 − 0)(4 − 2)(4 − 3)
1
= (x(x2 − 5x + 6))
8
1
= (x3 − 5x2 + 6x)
8
Page 2
Hence, the final interpolated polynomial, via Equation 5, can be determined as:
p(x) = f (x0 )I0 (x) + f (x1 )I1 (x) + f (x2 )I2 (x) + f (x3 )I3 (x)
1 3 2 1 3 2 −1 3 2 1 3 2
= 7 − (x − 9x + 26x − 24) + 11 (x − 7x + 12x) + 28 (x − 6x + 8x) + 63 (x − 5x + 6x)
24 4 3 8
−7 3 63 2 182 11 3 77 2 132 28 3 168 2 224 63 3 315 2 378
= x + x − x+7 + x − x + x + − x + x − + x − x + x
24 24 24 4 4 4 3 3 3 8 8 8
= 7 − 2x + x3
Importantly, the interpolated polynomial derived via Newton’s method is the same as the one derived via the Lagrange
method, which confirms that both methods are equivalent formulations of polynomial interpolation.
Page 3
Question 2: Spline Interpolation
We want to determine the values of a, b and c such that the following is a cubic spline having nodes at 0, 1 and 2:
(
3 + x − 9x2 x ∈ [0, 1]
f (x) = 2 3
a + b(x − 1) + c(x − 1) + d(x − 1) x ∈ [1, 2]
We will assign the first cubic spline as S1 (x) and the second cubic spline as S2 (x). Then, as we require continuity between
the two splines in terms of their value, first derivative and second derivative at the mutual node, we can note that the
conditions on S2 (x) are such that:
S1 (1) = S2 (1) (6)
S1′ (1) = S2′ (1) (7)
S1′′ (1) = S2′′ (1) (8)
To enforce these conditions, we must determine S1 (1), S1′ (1) and S1′′ (1), first by finding S1′ (x) and S1′′ (x):
S1′ (x) = −18x + 1
S1′′ (x) = −18
which gives:
S1 (1) = 3 + (1) − 9(1)2 = −5
S1′ (1) = −18(1) + 1 = −17
S1′′ (1) = −18
Similarly, we can find S2′ (x) and S2′′ (x) as:
S2′ (x) = b + 2c(x − 1) + 3d(x − 1)2
S2′′ (x) = 2c + 6d(x − 1)
Hence, the continuity requirement in Equation 6 at x = 1 enforces that:
S2 (1) = −5 =⇒ a + b(0) + c(0)2 + d(0)3 = −5
=⇒ a = −5
The continuity requirement in Equation 7 at x = 1 then enforces that:
S2′ (1) = −17 =⇒ b + 2c(0) + 3d(0)2 = −17
=⇒ b = −17
Finally, the continuity requirement in Equation 8 at x = 1 enforces that:
S2′′ (1) = −18 =⇒ 2c + 6d(0) = −18
=⇒ c = −9
Hence, the coefficients of the polynomial S2 (x) are a = −5, b = −17 and c = −9. The coefficient d can be found via one
R2
of two boundary conditions - either d can be found such that 0 [f ′′ (x)]2 dx is a minimum, or it can be found such that
f ′′ (2) = 0.
R2 R2
To find d such that 0 [f ′′ (x)]2 dx is a minimum, 0 [f ′′ (x)]2 dx must first be found, as below:
Z 2 Z 1 Z 2
[f ′′ (x)]2 dx = [S1′′ (x)]2 dx + [S2′′ (x)]2 dx
0 0 1
Z 1 Z 2
2
= (−18) dx + (−18 + 6d(x − 1))2 dx
0 1
Z 1 Z 2
= 324 dx + 324 − 216d(x − 1) + 36d2 (x − 1)2 dx
0 1
1 2
= [324x]0 + 12d2 x3 − 36d(d + 3)x2 + 36(d + 3)2 x 1
Page 4
R2
To find d such that 0
[f ′′ (x)]2 dx is a minimum, we require that:
Z 2
d
[f ′′ (x)]2 dx = 0 (9)
dd 0
R2
(Apologies about the poor notation choice here). Nonetheless, by substituting in the result above that 0
[f ′′ (x)]2 dx =
12d2 − 108d + 648, the d that minimises this function can be easily found as:
Z 2
d
[f ′′ (x)]2 dx = 0
dd 0
d
12d2 − 108d + 648 = 0
=⇒
dd
=⇒ 24d − 108 = 0
9
=⇒ d =
2
Alternatively, d can be found such that f ′′ (2) = 0. Because the sole cubic spline that goes through the point x = 2 is
S2 (x), this implies that S2′′ (2) = 0. Using the second derivative of S2 (x) determined above, d for this condition can be
found as:
−18 + 6d(2 − 1) = 0
=⇒ 6d = 18
=⇒ d = 3
The reason that the value of d is different for the two cases is because the first condition minimises the absolute value
of the second derivative of S2 (x) over the entire domain of the second cubic spline, whereas the second condition only
minimises the second derivative of S2 (x) at the endpoint of the domain.
Page 5
Question 3: Estimating Derivatives
We want to derive the following two formulas for approximating the third derivative and find their error terms:
1
f ′′′ (x) ≈ [f (x + 3h) − 3f (x + 2h) + 3f (x + h) − f (x)] (10)
h3
1
f ′′′ (x) ≈ [f (x + 2h) − 2f (x + h) + 2f (x − h) − f (x − 2h)] (11)
2h3
We will first derive Equation 10. It can be seen that this a forward finite difference scheme, so we will construct this
approximation from the following forward Taylor series around the point f (x) (written up to the fifth order):
h2 ′′ h3 h4 h5 ′′′′′
f (x + h) = f (x) + hf ′ (x) + f (x) + f ′′′ (x) + f ′′′′ (x) + f (x) + O(h6 ) (12)
2 6 24 120
(2h)2 ′′ (2h)3 ′′′ (2h)4 ′′′′ (2h)5 ′′′′′
f (x + 2h) = f (x) + (2h)f ′ (x) + f (x) + f (x) + f (x) + f (x) + O(h6 ) (13)
2 6 24 120
(3h)2 ′′ (3h)3 ′′′ (3h)4 ′′′′ (3h)5 ′′′′′
f (x + 3h) = f (x) + (3h)f ′ (x) + f (x) + f (x) + f (x) + f (x) + O(h6 ) (14)
2 6 24 120
If we take Equation 14 −3 × Equation 13 + 3 × Equation 12, then slight rearranging will generate the approximation for
the third derivative in Equation 10:
3 5
f (x + 3h) − 3f (x + 2h) + 3f (x + h) = f (x) + 0f ′ (x) + 0f ′′ (x) + h3 f ′′′ (x) + h4 f ′′′′ (x) + h5 f ′′′′′ (x) + O(h6 )
2 4
3 5
=⇒ f (x + 3h) − 3f (x + 2h) + 3f (x + h) − f (x) = h3 f ′′′ (x) + h4 f ′′′′ (x) + h5 f ′′′′′ (x) + O(h6 )
2 4
(f (x + 3h) − 3f (x + 2h) + 3f (x + h) − f (x)) 3 5
=⇒ = f ′′′ (x) + hf ′′′′ (x) + h2 f ′′′′′ (x) + O(h3 )
h3 2 4
1 3 ′′′′ 5
=⇒ 3 (f (x + 3h) − 3f (x + 2h) + 3f (x + h) − f (x)) = f (x) + hf (x) + h2 f ′′′′′ (x) + O(h3 )
′′′
h 2 4
Thus, we have recovered that f ′′′ (x) ≈ h13 (f (x + 3h) − 3f (x + 2h) + 3f (x + h) − f (x)). The first two error terms are
′′′′
3
2 hf (x) and 54 h2 f ′′′′′ (x). If the Taylor series were expanded to order n, then the successive error terms will follow the
general formula:
(3h)n+3 − 3 · (2h)n+3 + 3 · hn+3
n
ϵ(h ) = f (n+3) (x) (15)
(n + 3)!
where ϵ(hn ) is the error term proportional to hn . In general, this method is first-order accurate as the dominant error
term is proportional to h.
Now we want to derive Equation 11. This is a centered finite difference scheme, so we will construct this approximation
from the following forward and backward Taylor series around the point f (x) (written up to the sixth order):
h2 ′′ h3 h4 h5 ′′′′′ h6 (6)
f (x + h) = f (x) + hf ′ (x) + f (x) + f ′′′ (x) + f ′′′′ (x) + f (x) + f (x) + O(h7 ) (16)
2 6 24 120 720
(2h)2 ′′ (2h)3 ′′′ (2h)4 ′′′′ (2h)5 ′′′′′ (2h)6 (6)
f (x + 2h) = f (x) + (2h)f ′ (x) + f (x) + f (x) + f (x) + f (x) + f (x) + O(h7 ) (17)
2 6 24 120 720
h2 h3 h4 h5 ′′′′′ h6 (6)
f (x − h) = f (x) − hf ′ (x) + f ′′ (x) − f ′′′ (x) + f ′′′′ (x) − f (x) + f (x) − O(h7 ) (18)
2 6 24 120 720
(2h)2 ′′ (2h)3 ′′′ (2h)4 ′′′′ (2h)5 ′′′′′ (2h)6 (6)
f (x − 2h) = f (x) − (2h)f ′ (x) + f (x) − f (x) + f (x) − f (x) + f (x) − O(h7 ) (19)
2 6 24 120 720
If we take Equation 17 − Equation 19 + 2 × Equation 18 − 2 × Equation 16, then rearranging will generate the
approximation for the third derivative in Equation 11:
f (x + 2h) − f (x − 2h) + 2f (x − h) − 2f (x + h) = 0f (x) + 0f ′ (x) + 0f ′′ (x) + 2h3 f ′′′ (x) + 0f ′′′′ (x) + ...
1
... + h5 f ′′′′′ (x) + 0f (6) (x) + O(h7 )
2
1
=⇒ f (x + 2h) − f (x − 2h) + 2f (x − h) − 2f (x + h) = 2h3 f ′′′ (x) + h5 f ′′′′′ (x) + O(h7 )
2
(f (x + 2h) − f (x − 2h) + 2f (x − h) − 2f (x + h)) 1
=⇒ 3
= f ′′′ (x) + h2 f ′′′′′ (x) + O(h4 )
2h 4
Page 6
Thus, we have recovered that f ′′′ (x) ≈ 2h1 3 (f (x + 2h) − 2f (x + h) + 2f (x − h) − f (x − 2h)). The first error term is
1 2 ′′′′′
2h f (x). If the Taylor series were expanded to order n, then the successive error terms will follow the general formula:
2 · (2h)n+3 − 4 · hn+3
ϵ(hn ) = f (n+3) (x) (20)
(n + 3)!
where n only refers to even integers. Via construction of the centered finite difference scheme, all error terms proportional
to hn , where n is an odd integer, will be 0. In general, this method is second-order accurate as the dominant error term
is proportional to h2 , and hence it is more accurate than the approximation to the third derivative given in Equation 10.
Page 7
Question 4: Richardson Extrapolation
If ϕ(h) = L − c1 h1/2 − c2 h2/2 − c3 h3/2 − ..., then we want to determine which combination of ϕ(h) and ϕ( h2 ) will give a
more accurate estimate of L. To find a more accurate estimate, all we need to do is eliminate the lowest-order error term
(in this case, the error term proportional to h1/2 ). First note that, via the definition of ϕ(h) given above, ϕ( h2 ) will be
given by:
1/2 2/2 3/2
h h h h
ϕ = L − c1 − c2 − c3 + ... (21)
2 2 2 2
√
2 × ϕ h2 − ϕ(h), then we will get:
If we take
√ √ √ c1 √ c2 √ c3
h
2×ϕ − ϕ(h) = ( 2L − L) + (− 2 √ + c1 )h1/2 + (− 2 + c2 )h2/2 + (− 2 3/2 + c3 )h3/2 + ...
2 2 2 2
√ √
h c2 c3
=⇒ 2 × ϕ − ϕ(h) = ( 2 − 1)L + (− √ + c2 )h2/2 + (− + c3 )h3/2 + ...
2 2 2
√
Hence, taking 2 × ϕ h2 − ϕ(h) has eliminated the error term proportional to h1/2 . By rearranging the equation directly
Page 8
Question 5: Numerical Differentiation
We want to compare the accuracy of Equation 10 (a forward difference approximation to the third derivative) and Equation
11 (a centered difference approximation to the third derivative) by evaluating ln (x) at x = 1.8 for different values of h.
To do this, two functions in Matlab were created that evaluated the approximations to the third derivative described by
these schemes for different h values, and the error in these approximations was subsequently computed and plotted. The
true third derivative of f (x) = ln (x) is:
1
f ′′′ (x) = − (22)
x2
And thus f ′′′ (1.8) = −0.308641975308641975.... All third derivative approximations were compared to this value to
determine the error. The results are shown below in Figure 1.
Figure 1: Accuracy comparison of the approximations to the third derivative in Equations 10 and 11
It can be seen in Figure 1 that the results behave as expected. Importantly, the error decreases as h decreases for both
approximations, up until a point where the floating point error becomes too dominant. It was determined in Question
3 that Equation 10, the forward finite difference approximation to the third derivative, was first-order accurate, and it
can be seen that the error for this approximation decreases proportionally to the O(h) indication line. At the start,
the gradient of Equation 10 does not precisely follow the O(h) indication line because h here is too large to deem the
truncated terms in the Taylor series (that is, the higher order derivatives) negligible.
Equation 11, the centered finite difference approximation to the third derivative, was found to be second-order accurate,
and the error for this approximation accordingly can be seen to decrease proportionally to the O(h2 ) indication line.
Thus, the plot confirms the accuracy of the approximations derived in Question 3.
Page 9
The code used to generate the previous plot is included below:
1 %% Assignment 2 , Question 5 Code
2 clear all ;
3 format long ; format compact
4 f = @( x ) log ( x ) ;
5 x = 1.8;
6
7 i = 0;
8
9 for n =1:0.2:14
10 i = i + 1;
11 h ( i ) = 2^( - n ) ;
12
13 approx_1 = t h i r d _ d e r i v _ a p p r o x _ 1 (f , x , h ( i ) ) ;
14 approx_2 = t h i r d _ d e r i v _ a p p r o x _ 2 (f , x , h ( i ) ) ;
15
16 err_1 ( i ) = abs (2/(1.8) ^3 - approx_1 ) ;
17 err_2 ( i ) = abs (2/(1.8) ^3 - approx_2 ) ;
18 err_order_1 ( i ) = h ( i ) ;
19 err_order_2 ( i ) = h ( i ) ^2;
20 end
21
22 figure (1)
23 hold on
24 plot (h , err_order_1 , ' -r ')
25 plot (h , err_order_2 , ' -b ')
26 plot (h , err_1 , ' xr ')
27 plot (h , err_2 , ' xb ')
28 hold off
29 ylabel ( ' Error ')
30 xlabel ( 'h ')
31 set ( gca , ' XScale ' , ' Log ' , ' YScale ' , ' Log ' , ' xdir ' , ' reverse ')
32 title ( ' Accuracy comparison of two approximations of the third derivative ')
33 legend ( 'O ( h ) ' , 'O ( h ^2) ' , ' Forward difference scheme ' , ' Centered difference scheme ' , '
Location ' , ' southwest ')
34 % xlim ([ -4.5 -0.2])
35
36 function [ third_derive ] = t h i r d _ d e r i v _ a p p r o x _ 1 (f , x , h )
37 % Find the approximation to f ' ' '( x ) using Equation (1)
38 third_derive = 1/( h ^3) *( f ( x +3* h ) - 3* f ( x +2* h ) + 3* f ( x + h ) - f ( x ) ) ;
39 third_derive ;
40 end
41
42 function [ third_derive ] = t h i r d _ d e r i v _ a p p r o x _ 2 (f , x , h )
43 % Find the approximation to f ' ' '( x ) using Equation (2)
44 third_derive = 1/(2* h ^3) *( f ( x +2* h ) - 2* f ( x + h ) + 2* f (x - h ) - f (x -2* h ) ) ;
45 third_derive ;
46 end
Page 10