You are on page 1of 71

Curve Fitting: Interpolation

CE 007 | Numerical Solutions on CE Problems


INTENDED LEARNING OUTCOMES

By the end of this lecture, students should be able to:

• Demonstrate a comprehensive understanding of the


fundamental principles of curve fitting, interpolation, and
collocation methods in numerical analysis.

• Apply various interpolation techniques, such as Lagrange


interpolation and spline interpolation, to accurately
approximate data points and solve engineering problems.
OUTLINE
➢ Collocation
➢ Polynomial Interpolation
-- Lagrange Interpolating Polynomial
-- Newton’s Divided-Difference Interpolating Polynomial
➢ Collocation Issues
-- Extrapolation and Oscillations
➢ Spline Interpolation
-- Linear Spline
-- Cubic Spline
COLLOCATION
➢ Method in which a curve or a series of curves
is made to pass directly through each of the
given points

Commonly Used Approximation Functions:


-- nth degree polynomial functions
-- Transcendental functions
-- Combination of the above functions
COLLOCATION
nth degree polynomial functions

ES 204 – Numerical
1st Semester AY 2022-2023
Methods in Engineering
COLLOCATION
Consider a polynomial of degree n, and make
it pass through (n+1) data points with no
restriction on the spacing of the data:

f (x )= y = a0 + a1 x + a2 x +…+ a n–1
2
x n–1
+ an
x n

where x = Independent variable


y = Dependent variable
ai = Unknown coefficients
(i = 0, 1, 2, …, n)
COLLOCATION
To solve for the unknown coefficients, the given
data points (xi, yi), where i = 0, 1, 2, …, n, shall be
substituted to the given function:

(x 0 , y 0 ): y 0 = a0 + a1 x 0 +…+ an–1 x 0 n–1


+ an x0 n
(x 1 , y 1 ): y 1 = a0 + a1 x1 +…+ an– 1 1
x n–1
+ a x
n 1
n

… …
.
(xn , yn ): y n = a0 + a1 xn +…+ an–1 x n n–1
+ an x n n
COLLOCATION
The previous equations can be rewritten
in matrix notation:

where the coefficient matrix is known as the


Vandermonde matrix.
COLLOCATION

The above equations can be solved using


methods for systems of linear equations,
either direct or iterative approach.
COLLOCATION
SAMPLE PROBLEM
Determine the coefficients of the parabola
ax2 + bx + c, that passes through the
following density values as a function of
temperature.

i 0 1 2
Ti 300 400 500
ρi 0.616 0.525 0.457
COLLOCATION
SAMPLE PROBLEM
0.616 = a(300) + b(300) + c
2

0.525 = a(400) + b(400) + c


2

0.457 = a(500) + b(500) + c


2

In matrix notation:
COLLOCATION
SAMPLE PROBLEM
Using any method to solve the given system:

p = 0.00000115T 2 – 0.001715T + 1.027

FOLLOW-UP QUESTION:
What is the value of the density at T = 350? p = 0.567625
COLLOCATION
NOTES:
➢ A unique solution exists to the formulated
system of linear equations upon solving the
unknown coefficients.
➢ The coefficient matrix (Vandermonde matrix)
is very ill-conditioned.
-- The determination of f(x) through the
solution of the systems of equations
may not be an efficient procedure.
INTERPOLATION
➢ Process of estimating an intermediate value
from a set of discrete (or tabulated) values
➢ A collocation function can be used for
interpolation
➢ Interpolation can be done using polynomials,
spline functions, or Fourier series
-- Polynomial functions are commonly
used for interpolation
INTERPOLATION
SPLINE FUNCTIONS

ES 204 – Numerical
1st Semester AY 2022-2023
Methods in Engineering
INTERPOLATION
FOURIER SERIES

ES 204 – Numerical
1st Semester AY 2022-2023
Methods in Engineering
POLYNOMIAL INTERPOLATION
There are several alternative forms in which the
polynomial can be expressed
-- To avoid the ill-conditioning problem
in the coefficient matrix
-- To ease its computer implementation

ALTERNATIVES:
➢ Lagrange Interpolating Polynomials
➢ Newton’s Divided-Difference
Interpolating Polynomials
LAGRANGE POLYNOMIALS
➢ Consider fitting a quadratic function through
three data points (xi, yi), i = 0, 1, 2.
➢ Instead of expressing the quadratic function in
the usual manner, it will be expressed in terms
of the Lagrange polynomial of degree two
f (x )= y = a0 (x – x1 )(x – x2 )+ a1 (x – x0 )(x – x2 )
+ a (x – x )(x – x )
2 0 1

where ai = unknown coefficients


LAGRANGE POLYNOMIALS
➢ Solving for the unknown coefficients:
LAGRANGE POLYNOMIALS
NOTES:
➢ The points xi need not be uniformly spaced.
➢ The determination of the polynomial
does not involve the solution of a system
of equations.
➢ It can be implemented into a computer
program in a simple manner.
LAGRANGE POLYNOMIALS
LAGRANGE POLYNOMIALS
SAMPLE PROBLEM
Determine the coefficients of the parabola
ax2 + bx + c, that passes through the
following data using Lagrange interpolation
polynomial.

i 0 1 2
Ti 300 400 500
ρi 0.616 0.525 0.457
LAGRANGE POLYNOMIALS
ADDITIONAL NOTES:
➢ The error in Lagrange interpolation depends
on the following factors:
-- Range of the interpolation domain
-- Spacing of the values of xi
-- Degree of the interpolating polynomial
(number of data points, n +1)
LAGRANGE POLYNOMIALS
ADDITIONAL NOTES:
➢ It has the following limitations:
-- If the number of data points is to be
increased or decreased, the results
of the previous calculations cannot be used.

-- The computational effort required is more


for a single interpolation.
LAGRANGE POLYNOMIALS
ADDITIONAL NOTES:
➢ It has the following limitations:
-- If interpolation is required at another value
of x, there will not be any savings in the
computational effort since the results of
the previous computations cannot be used.

-- The estimation of error in interpolation


is not easy.
NEWTON’S POLYNOMIALS
➢ Overcomes all the limitations of the
Lagrange Interpolating Polynomials
➢ The procedure used for a general nth degree
interpolation formula can be seen by
considering the derivation of the lowest
degree interpolation formulas.
-- Linear Interpolation
-- Quadratic Interpolation
-- nth-degree Polynomial Interpolation
LINEAR INTERPOLATION
➢ If two data points are
available as [x0, f(x0)]
and [x1, f(x1)], the points
can be connected by a
straight line to obtain
the simplest form of
interpolation.
➢ The formula can be
derived by considering
the similar triangles
ADE and ABC.
LINEAR INTERPOLATION

DE BC
=
AE AC

where f1(x) denotes the


interpolating polynomial,
with subscript 1 indicating
the degree of polynomial
LINEAR INTERPOLATION
Solving for f1(x):

where a0 = f (x 0 )
f (x1 )– f (x0 ) Approximation of
a1 =
x1 – x0 first derivative
LINEAR INTERPOLATION
SAMPLE PROBLEM
Estimate the natural logarithm of 2 using
linear interpolation. Note that the true
value of ln 2 is 0.6931472.

➢ Perform the computation by interpolating


between ln 1 = 0 and ln 6 = 1.791759.
➢ Repeat the procedure, but use a smaller
interval from ln 1 to ln 4 = 1.386294.
LINEAR INTERPOLATION
SAMPLE PROBLEM

ES 204 – Numerical
1st Semester AY 2022-2023
Methods in Engineering
QUADRATIC INTERPOLATION
➢ If three data points are available as [x0, f(x0)],
[x1, f(x1)], and [x2, f(x2)], we can use a 2nd-degree
or quadratic polynomial to fit the data.

f (x )= a + a (x – x )+ a (x – x )(x – x )
2 0 1 0 2 0 1

where ai = unknown coefficients


(i = 0, 1, 2)
QUADRATIC INTERPOLATION
Solving for the unknown coefficients:
a0 = f (x0 )
f (x1 )– f (x0 ) Approximation of
a1 = first derivative
x1 – x0

Approximation of
second derivative
QUADRATIC INTERPOLATION
NOTES:
➢ All previous computations can still be used
when new data points are considered to
increase the degree of the interpolating
polynomial.
➢ It is expected that the estimate will be
improved due to the introduction of a
curvature term, as manifested from the
expression for a2.
QUADRATIC INTERPOLATION
SAMPLE PROBLEM
Employ a second degree Newton polynomial
to estimate the value of ln 2 with the same
three points from the previous example.

i 0 1 2
xi 1 4 6
f(xi) 0 1.386294 1.791759
QUADRATIC INTERPOLATION
SAMPLE PROBLEM

ES 204 – Numerical
1st Semester AY 2022-2023
Methods in Engineering
nth DEGREE POLYNOMIAL INTERPOLATION
➢ Also known as nth degree Newton’s divided-
difference interpolating polynomial

fn (x )= a0 + a1 (x – x 0 )+ a2 (x – x 0 )(x – x 1 )+…
+ an (x – x 0 )(x – x 1 ). . . (x –x n–1 )

➢ By using the data point [x0, f(x0)], we obtain:


a0 = f (x0 )
nth DEGREE POLYNOMIAL INTERPOLATION
➢ When the next data point [x1, f(x1)] is used:

➢ When the data point [x2, f(x2)] is used:


nth DEGREE POLYNOMIAL INTERPOLATION
➢ Similarly, when other data points are used,
it yields the other constants:

A general recursive formula can be


observed that enables us to compute the
higher degree forward differences from the
lower degree forward differences.
nth DEGREE POLYNOMIAL INTERPOLATION
Forward divided difference table:

where the coefficients can be calculated as:


an = f [xn ,x n–1 ,x n–2 ,…,x1 ,x 0 ]
nth DEGREE POLYNOMIAL INTERPOLATION
SAMPLE PROBLEM
By adding a 4th point [x3 = 5; f(x3) = 1.609438]
from the previous example, estimate ln 2
with a 3rd degree Newton’s interpolating
polynomial.
i 0 1 2
xi 1 4 6
f(xi) 0 1.386294 1.791759
nth DEGREE POLYNOMIAL
INTERPOLATION
SAMPLE PROBLEM

ES 204 – Numerical
1st Semester AY 2022-2023
Methods in Engineering
COLLOCATION ISSUES
EXTRAPOLATION
➢ Process of estimating
a value of f(x) that lies
outside the range of
the known base
points, x1, x2, …, xn.
➢ Extreme care should
be exercised
whenever a case
arises where one
must extrapolate.
COLLOCATION ISSUES
OSCILLATIONS
➢ Higher-degree
polynomials tend
to be very ill-
conditioned.
➢ To illustrate, consider
Runge’s function
1
f (x ) =
1 + 25x2
in the interval [-1,1]
SPLINE INTERPOLATION
➢ Done to address the errors of high-order
polynomial interpolation (round-off errors
and oscillations)
➢ Provides a superior approximation of the
behavior of functions that have local,
abrupt changes

SPLINE FUNCTIONS
➢ Lower-order, connecting polynomials in a
piecewise fashion to subsets of data points
SPLINE FUNCTIONS
LINEAR SPLINES
➢ For n data points (i = 1, 2,…, n), there are
(n-1) intervals
➢ Each interval i has its own spline function, si(x)
LINEAR SPLINES
➢ Each function is merely the straight line
connecting the two points at each end of
the interval
s (x )= a + b (x – x )
i i i i

fi +1 – fi
si (x )= fi + (x – x i )
x i +1 – x i
LINEAR SPLINES

• SAMPLE PROBLEM
• Use linear splines to fit the data below,
• then evaluate the function at x = 5.

i xi fi
1 3.0 2.5
2 4.5 1.0
3 7.0 2.5
4 9.0 0.5
LINEAR SPLINES
NOTES:
➢ Linear splines are not smooth
➢ Slope changes abruptly at knots (data points
where two splines meet)
➢ Use of higher-order splines [at least (n+1) order]
is recommended to ensure smoothness
Cubic Splines

Given 𝑛 data points 𝑥𝑖 , 𝑦𝑖 for 𝑖 = 1, 2, … , 𝑛 find


a piecewise cubic polynomial, 𝑺(𝑥):

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3
if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = 𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3
if 𝑥2 ≤ 𝑥 ≤ 𝑥3
⋮ ⋮
2 3
𝑆𝑛−1 𝑥 = 𝑎𝑛−1 + 𝑏𝑛−1 𝑥 − 𝑥𝑛−1 + 𝑐𝑛−1 𝑥 − 𝑥𝑛−1 + 𝑑𝑛−1 𝑥 − 𝑥𝑛−1 if 𝑥𝑛−1 ≤ 𝑥 ≤ 𝑥𝑛
which satisfies a number of conditions.
The problem becomes:
Solve all 𝑎𝑖 , 𝑏𝑖 , 𝑐𝑖 , and 𝑑𝑖 as a system of 4 𝑛 − 1
equations in 4 𝑛 − 1 unknowns.

ERV
Cubic Splines
Example
𝑛 = 4 data points

𝑆1 𝑥 𝑆3 𝑥
𝑆2 𝑥

ℎ1 ℎ2 ℎ3
𝑥1 𝑥2 𝑥3 𝑥4
𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4
ERV
Cubic Splines
First condition:
(Interpolating property)
For 𝑖 = 1, 2, … , 𝑛 − 1 , 𝑆𝑖 𝑥𝑖 = 𝑦𝑖
“Each spline must pass through the data points
𝑥𝑖 , 𝑦𝑖 .”

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

𝑆1 𝑥1 = 𝑎1 + 𝑏1 𝑥1 − 𝑥1 + 𝑐1 𝑥1 − 𝑥1 2+ 𝑑1 𝑥1 − 𝑥1 3 = 𝑦1
𝑆2 𝑥2 = 𝑎2 + 𝑏2 𝑥2 − 𝑥2 + 𝑐2 𝑥2 − 𝑥2 2+𝑑 𝑥 −𝑥 3 =𝑦
2 2 2 2
2+𝑑 𝑥 −𝑥 3 =𝑦
𝑆3 𝑥3 = 𝑎3 + 𝑏3 𝑥3 − 𝑥3 + 𝑐3 𝑥3 − 𝑥3 3 3 3 3

ERV
Cubic Splines
First condition:
(Interpolating property)
For 𝑖 = 1, 2, … , 𝑛 − 1 , 𝑆𝑖 𝑥𝑖 = 𝑦𝑖
“Each spline must pass through the data points
𝑥𝑖 , 𝑦𝑖 .”
𝑆1 𝑥1 = 𝑎1 + 𝑏1 𝑥1 − 𝑥1 + 𝑐1 𝑥1 − 𝑥1 2+ 𝑑1 𝑥1 − 𝑥1 3 = 𝑦1
𝑆2 𝑥2 = 𝑎2 + 𝑏2 𝑥2 − 𝑥2 + 𝑐2 𝑥2 − 𝑥2 2+𝑑 𝑥 −𝑥 3 =𝑦
2 2 2 2
𝑆3 𝑥3 = 𝑎3 + 𝑏3 𝑥3 − 𝑥3 + 𝑐3 𝑥3 − 𝑥3 2+𝑑 𝑥 −𝑥 3 =𝑦
3 3 3 3
𝑎1 = 𝑦1
𝑎2 = 𝑦2
𝑎3 = 𝑦3
𝑎𝑖 = 𝑦𝑖 for 𝑖 = 1, 2, … , 𝑛 − 1

ERV
Cubic Splines
Second condition:
(Continuity at interior points & rightmost end point)
For 𝑖 = 1, 2, … , 𝑛 − 2 , 𝑆𝑖 𝑥𝑖+1 = 𝑆𝑖+1 𝑥𝑖+1
For the last spline, 𝑆𝑛−1 𝑥𝑛 = 𝑦𝑛
“The right end of a spline must be equal to the
left end of the next spline.”

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

ERV
Cubic Splines
Second condition:
(Continuity at interior points & rightmost end point)

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

𝑆1 𝑥2 = 𝑦1 + 𝑏1 𝑥2 − 𝑥1 + 𝑐1 𝑥2 − 𝑥1 2 + 𝑑1 𝑥2 − 𝑥1 3 = 𝑆2 𝑥2
= 𝑦2 + 𝑏2 𝑥2 − 𝑥2 + 𝑐2 𝑥2 − 𝑥2 2 + 𝑑2 𝑥2 − 𝑥2 3
𝑆2 𝑥3 = 𝑦2 + 𝑏2 𝑥3 − 𝑥2 + 𝑐2 𝑥3 − 𝑥2 2 + 𝑑2 𝑥3 − 𝑥2 3
= 𝑆3 𝑥3 = 𝑦3 + 𝑏3 𝑥3 − 𝑥3 + 𝑐3 𝑥3 − 𝑥3 2 + 𝑑3 𝑥3 − 𝑥3 3

2 3
𝑆3 𝑥4 = 𝑦3 + 𝑏3 𝑥4 − 𝑥3 + 𝑐3 𝑥4 − 𝑥3 + 𝑑3 𝑥4 − 𝑥3 = 𝑦4

𝑦𝑖 + 𝑏𝑖 ℎ𝑖 + 𝑐𝑖 ℎ𝑖2 + 𝑑𝑖 ℎ𝑖3 = 𝑦𝑖+1


for 𝑖 = 1, 2, … , 𝑛 − 1
ERV
Cubic Splines
Third condition:
(Continuity of slopes at interior points)
For 𝑖 = 1, 2, … , 𝑛 − 2 , 𝑆𝑖′ 𝑥𝑖+1 = 𝑆𝑖+1′
𝑥𝑖+1
“The slope at the right end of a spline must be
equal to the slope at the left end of the next
spline.”

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

ERV
Cubic Splines
Third condition:
(Continuity of slopes at interior points)

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

𝑆1′ 𝑥2 = 𝑏1 + 2𝑐1 𝑥2 − 𝑥1 + 3𝑑1 𝑥2 − 𝑥1 2


= 𝑆2′ 𝑥2 = 𝑏2 + 2𝑐2 𝑥2 − 𝑥2 + 3𝑑2 𝑥2 − 𝑥2 2

𝑆2′ 𝑥3 = 𝑏2 + 2𝑐2 𝑥3 − 𝑥2 + 3𝑑2 𝑥3 − 𝑥2 2


= 𝑆3′ 𝑥3 = 𝑏3 + 2𝑐3 𝑥3 − 𝑥3 + 3𝑑3 𝑥3 − 𝑥3 2

𝑏𝑖 + 2𝑐𝑖 ℎ𝑖 + 3𝑑𝑖 ℎ𝑖2 = 𝑏𝑖+1


for 𝑖 = 1, 2, … , 𝑛 − 2
ERV
Cubic Splines
Fourth condition:
(Continuity of curvatures at interior points)
For 𝑖 = 1, 2, … , 𝑛 − 2 , 𝑆𝑖′′ 𝑥𝑖+1 = 𝑆𝑖+1
′′
𝑥𝑖+1
“The curvature at the right end of a spline must be
equal to the curvature at the left end of the next
spline.”

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

ERV
Cubic Splines
Fourth condition:
(Continuity of curvatures at interior points)

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

𝑆1′′ 𝑥2 = 2𝑐1 + 6𝑑1 𝑥2 − 𝑥1 = 𝑆2′′ 𝑥2 = 2𝑐2 + 6𝑑2 𝑥2 − 𝑥2

𝑆2′′ 𝑥3 = 2𝑐2 + 6𝑑2 𝑥3 − 𝑥2 = 𝑆3′′ 𝑥3 = 2𝑐3 + 6𝑑3 𝑥3 − 𝑥3

𝑐𝑖 + 3𝑑𝑖 ℎ𝑖 = 𝑐𝑖+1
for 𝑖 = 1, 2, … , 𝑛 − 2

ERV
Cubic Splines
Fifth condition:
(Zero curvature at end points)
𝑆1′′ 𝑥1 = 0 and 𝑆𝑛−1
′′
𝑥𝑛 = 0
“The curvature at the leftmost and rightmost
endpoints are zero.”

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

Note: Other 5th conditions exist. Shown here is the “natural


spline” condition. Others include: “clamped spline”, “not-a-knot
spline”, and “periodic spline”.
ERV
Cubic Splines
Fifth condition:
(Zero curvature at end points)

𝑆1 𝑥 = 𝑎1 + 𝑏1 𝑥 − 𝑥1 + 𝑐1 𝑥 − 𝑥1 2 + 𝑑1 𝑥 − 𝑥1 3 if 𝑥1 ≤ 𝑥 ≤ 𝑥2
𝑺 𝑥 = ൞𝑆2 𝑥 = 𝑎2 + 𝑏2 𝑥 − 𝑥2 + 𝑐2 𝑥 − 𝑥2 2 + 𝑑2 𝑥 − 𝑥2 3 if 𝑥2 ≤ 𝑥 ≤ 𝑥3
𝑆3 𝑥 = 𝑎3 + 𝑏3 𝑥 − 𝑥3 + 𝑐3 𝑥 − 𝑥3 2 + 𝑑3 𝑥 − 𝑥3 3 if 𝑥3 ≤ 𝑥 ≤ 𝑥4

𝑆1′′ 𝑥1 = 2𝑐1 + 6𝑑1 𝑥1 − 𝑥1 = 0

𝑆3′′ 𝑥4 = 2𝑐3 + 6𝑑3 𝑥4 − 𝑥3 = 0


𝑐3 + 3𝑑3 ℎ3 = 0
Recall the result from 𝑐𝑖 + 3𝑑𝑖 ℎ𝑖 = 𝑐𝑖+1
the 4th condition:
𝑐3 + 3𝑑3 ℎ3 = 𝑐4 Hence, if we pretend, we
have a 𝑐4, then it’s zero.
𝑐1 = 0
𝑐𝑛 = 0 ERV
Gathering all results: 4 𝑛 − 1 equations, 4 𝑛 − 1 unknowns
Condition 1: Eq. (1) 𝑎𝑖 = 𝑦𝑖
for 𝑖 = 1, 2, … , 𝑛−1
Condition 2: Eq. (2) 𝑦𝑖 + 𝑏𝑖 ℎ𝑖 + 𝑐𝑖 ℎ𝑖2 + 𝑑𝑖 ℎ𝑖3 = 𝑦𝑖+1 for 𝑖 = 1, 2, … , 𝑛−1
Condition 3: Eq. (3) 𝑏𝑖 + 2𝑐𝑖 ℎ𝑖 + 3𝑑𝑖 ℎ𝑖2 = 𝑏𝑖+1 for 𝑖 = 1, 2, … , 𝑛−2
Condition 4: Eq. (4) 𝑐𝑖 + 3𝑑𝑖 ℎ𝑖 = 𝑐𝑖+1 for 𝑖 = 1, 2, … , 𝑛−2
Condition 5: Eq. (5) 𝑐1 = 0
Eq. (6) 𝑐𝑛 = 0

Re-arrange Eq. (4):


𝑐𝑖+1 − 𝑐𝑖
Eq. (7) 𝑑𝑖 =
3ℎ𝑖
Substitute Eq. (7) to Eq.(3):
Eq. (8) 𝑏𝑖 + ℎ𝑖 𝑐𝑖 + 𝑐𝑖+1 = 𝑏𝑖+1

Eq. (8) can also be written as:


Eq. (9) 𝑏𝑖−1 + ℎ𝑖−1 𝑐𝑖−1 + 𝑐𝑖 = 𝑏𝑖

ERV
Gathering all results: 4 𝑛 − 1 equations, 4 𝑛 − 1 unknowns
Condition 1: Eq. (1) 𝑎𝑖 = 𝑦𝑖 for 𝑖 = 1, 2, … , 𝑛−1
Condition 2: Eq. (2) 𝑦𝑖 + 𝑏𝑖 ℎ𝑖 + 𝑐𝑖 ℎ𝑖2 + 𝑑𝑖 ℎ𝑖3 = 𝑦𝑖+1 for 𝑖 = 1, 2, … , 𝑛−1
Condition 3: Eq. (3) 𝑏𝑖 + 2𝑐𝑖 ℎ𝑖 + 3𝑑𝑖 ℎ𝑖2 = 𝑏𝑖+1 for 𝑖 = 1, 2, … , 𝑛−2
Condition 4: Eq. (4) 𝑐𝑖 + 3𝑑𝑖 ℎ𝑖 = 𝑐𝑖+1 for 𝑖 = 1, 2, … , 𝑛−2
Condition 5: Eq. (5) 𝑐1 = 0
Eq. (6) 𝑐𝑛 = 0
𝑐𝑖+1 − 𝑐𝑖
Eq. (7) 𝑑𝑖 =
3ℎ𝑖
Eq. (8) 𝑏𝑖 + ℎ𝑖 𝑐𝑖 + 𝑐𝑖+1 = 𝑏𝑖+1
Eq. (9) 𝑏𝑖−1 + ℎ𝑖−1 𝑐𝑖−1 + 𝑐𝑖 = 𝑏𝑖

Substitute Eq. (7) to Eq. (2):


2
Eq. (10) ℎ 𝑖
𝑦𝑖 + 𝑏𝑖 ℎ𝑖 + 2𝑐𝑖 + 𝑐𝑖+1 = 𝑦𝑖+1
3
𝑦𝑖+1 − 𝑦𝑖 ℎ𝑖
Eq. (11) 𝑏𝑖 = − 2𝑐𝑖 + 𝑐𝑖+1
ℎ𝑖 3

ERV
Gathering all results: 4 𝑛 − 1 equations, 4 𝑛 − 1 unknowns
Condition 1: Eq. (1) 𝑎𝑖 = 𝑦𝑖 for 𝑖 = 1, 2, … , 𝑛−1
Condition 2: Eq. (2) 𝑦𝑖 + 𝑏𝑖 ℎ𝑖 + 𝑐𝑖 ℎ𝑖2 + 𝑑𝑖 ℎ𝑖3 = 𝑦𝑖+1 for 𝑖 = 1, 2, … , 𝑛−1
Condition 3: Eq. (3) 𝑏𝑖 + 2𝑐𝑖 ℎ𝑖 + 3𝑑𝑖 ℎ𝑖2 = 𝑏𝑖+1 for 𝑖 = 1, 2, … , 𝑛−2
Condition 4: Eq. (4) 𝑐𝑖 + 3𝑑𝑖 ℎ𝑖 = 𝑐𝑖+1 for 𝑖 = 1, 2, … , 𝑛−2
Condition 5: Eq. (5) 𝑐1 = 0
Eq. (6) 𝑐𝑛 = 0
𝑐𝑖+1 − 𝑐𝑖
Eq. (7) 𝑑𝑖 =
3ℎ𝑖
Eq. (8) 𝑏𝑖 + ℎ𝑖 𝑐𝑖 + 𝑐𝑖+1 = 𝑏𝑖+1
Eq. (9) 𝑏𝑖−1 + ℎ𝑖−1 𝑐𝑖−1 + 𝑐𝑖 = 𝑏𝑖
ℎ𝑖2
Eq. (10) 𝑦𝑖 + 𝑏𝑖 ℎ𝑖 + 2𝑐𝑖 + 𝑐𝑖+1 = 𝑦𝑖+1
3 For ease, let us define:
𝑦 − 𝑦𝑖 ℎ𝑖 𝑦𝑖+1 − 𝑦𝑖
Eq. (11) 𝑏𝑖 = 𝑖+1 − 2𝑐𝑖 + 𝑐𝑖+1 𝑀𝑖 =
ℎ𝑖 3 ℎ𝑖
Substitute Eq. (11) to Eq. (9):
𝑦𝑖 − 𝑦𝑖−1 ℎ𝑖−1 𝑦𝑖+1 − 𝑦𝑖 ℎ𝑖
Eq. (12) ℎ𝑖−1

3
2𝑐𝑖−1 + 𝑐𝑖 + ℎ𝑖−1 𝑐𝑖−1 + 𝑐𝑖 =
ℎ𝑖

3
2𝑐𝑖 + 𝑐𝑖+1

Re-arrange Eq. (12):


Eq. (13) ℎ𝑖−1 𝑐𝑖−1 + 2 ℎ𝑖−1 + ℎ𝑖 𝑐𝑖 + ℎ𝑖 𝑐𝑖+1 = 3 𝑀𝑖 − 𝑀𝑖−1
ERV
Cubic Splines
Important derived equations:
Eq. (1) 𝑎𝑖 = 𝑦𝑖
Eq. (5) 𝑐1 = 0
Eq. (6) 𝑐𝑛 = 0
Eq. (13)
ℎ𝑖−1 𝑐𝑖−1 + 2 ℎ𝑖−1 + ℎ𝑖 𝑐𝑖 + ℎ𝑖 𝑐𝑖+1 = 3 𝑀𝑖 − 𝑀𝑖−1
𝑦𝑖+1 −𝑦𝑖
where 𝑀𝑖 =
ℎ𝑖
Eq. (11) 𝑦𝑖+1 − 𝑦𝑖 ℎ𝑖
𝑏𝑖 = − 2𝑐𝑖 + 𝑐𝑖+1
ℎ𝑖 3
Eq. (7)
𝑐𝑖+1 − 𝑐𝑖
𝑑𝑖 =
3ℎ𝑖

ERV
Cubic Splines
Algorithm:
Given: 𝑛 data points, 𝑥𝑖 , 𝑦𝑖 , for 𝑖 = 1, 2, … , 𝑛
1. Compute for 𝑎𝑖 using Eq. (1).
2. Based on Eqs. (5, 6, 13), compute 𝑐𝑖 by solving the
following system of 𝑛 − 2 equations:
𝑨𝒙 = 𝑩
2 ℎ1 + ℎ2 ℎ2 0 0 ⋯ 0 0
ℎ2 2 ℎ2 + ℎ3 ℎ3 0 ⋯ 0 0
0 ℎ3 2 ℎ3 + ℎ4 ℎ4 ⋯ 0 0
𝑨=
0 0 ⋱ ⋱ ⋱ 0 0
⋮ ⋮ ⋮ ⋱ ⋱ ⋱ ⋮
0 0 0 0 0 ℎ𝑛−2 2 ℎ𝑛−2 + ℎ𝑛−1
𝑐2 3 𝑀2 − 𝑀1
𝑐3 3 𝑀3 − 𝑀2
𝒙= ⋮ 𝑩=

𝑐𝑛−1 3 𝑀𝑛−1 − 𝑀𝑛−2
3. Compute for 𝑏𝑖 using Eq. (11).
4. Compute for 𝑑𝑖 using Eq. (7).
ERV
Example 5.7
Use natural cubic spline to determine 𝑦 at 𝑥 = 4.

𝑥 3 4.5 7 9
𝑦 2.5 1 2.5 0.5

ERV
Example 5.7
Script:
% Data set
x = [3 4.5 7 9]; N = length(x);
y = [2.5 1 2.5 0.5]; close all; clc;

% Perform cubic spline interpolation


a = y(1:end-1); h = diff(x); M = diff(y)./h; % Compute ai, h, M
B = 3*(M(2:end) - M(1:end-1)); % Compute B
A = diag(2*(h(1:end-1)+h(2:end))) + ...
diag(h(2:end-1),-1) + diag(h(2:end-1),1); % Declare matrix A
c = [0 (A\B')']; % Compute ci via Ax=B
b = M - h.*(2*c + [c(2:end) 0])/3; % Compute bi
d = diff([c 0])./h/3; % Compute di

% Report the computed piecewise cubic polynomials:


fprintf(['S%d(x) = %.3f + %.3f(x - %.1f) + '...
'%.3f(x - %.1f)^2 + %.3f(x - %.1f)^3\n'],...
[(1:N-1); a; b; x(1:N-1); c; x(1:N-1); d; x(1:N-1)]);

% Use the result to interpolate yq along xq


xq = linspace(x(1),x(end),100); % Query points in x xq
pos = sum([-Inf x Inf] <= xq',2)-1; % Find which S(x) is required
pos = max(min(pos,N-1),1); % Ensure to pick S_1 to S_(N 1) only
S = [a' b' c' d']; % Gather all polynomial coefficients
yq = arrayfun(@(j) polyval(flip(S(pos(j),:)),...
xq(j)-x(pos(j))),1:length(xq)); % Compute the interpolated yq at xq

% Plot the interpolated values


scatter(x,y,'b','filled','MarkerEdgeColor','k'); % Plot data points
grid on ; box on ; hold on ;
plot(xq,yq,'k','LineWidth',1.2); hold off;ERV
Example 5.7
Result:
S1(x) = 2.500 + -1.420(x - 3.0) + 0.000(x - 3.0)^2 + 0.187(x - 3.0)^3
S2(x) = 1.000 + -0.160(x - 4.5) + 0.840(x - 4.5)^2 + -0.214(x - 4.5)^3
S3(x) = 2.500 + 0.022(x - 7.0) + -0.767(x - 7.0)^2 + 0.128(x - 7.0)^3

S(4) = 1.2668

ERV
References

• Pilario, K. E. (2020). ES 204 Lecture Materials.


University of the Philippines Diliman.

• YAP K. J. (2022). ES 204 Lecture Materials.


University of the Philippines Diliman.

You might also like