Lecture 19
Lecture 19
A Chemical Reaction
In a chemical reaction the concentration level y of the product at time t was measured every half hour. The
following results were found:
t 0 .5 1.0 1.5 2.0
y 0 .19 .26 .29 .31
Matlab automatically connects the data with line segments, so the graph has corners. What if we want a
smoother graph? Try
≫ plot ( t1 , y1 , ’* ’)
which will produce just asterisks at the data points. Next click on Tools, then click on the Basic Fitting
option. This should produce a small window with several fitting options. Begin clicking them one at a time,
clicking them off before clicking the next. Which ones produce a good-looking fit? You should note that
the spline, the shape-preserving interpolant, and the 4th degree polynomial produce very good curves. The
others do not.
Polynomial Interpolation
Suppose we have n data points {(xi , yi )}ni=1 . A interpolant is a function f (x) such that yi = f (xi ) for
i = 1, . . . , n. The most general polynomial with degree d is
which has d + 1 coefficients. A polynomial interpolant with degree d thus must satisfy
This system is a linear system in the unknowns a0 , . . . , an and solving linear systems is what computers do
best. If n = d + 1, then the system of equations has n equations and n unknowns, so in general there is a
74
Introduction to Numerical Methods. . . by Young and Mohlenkamp ©2023 75
unique solution. This is the case in the example above: there are 5 data points so there is exactly one 4th
degree polynomial that interpolates the data.
If n < d + 1 then the system is underdetermined and so in general will have an infinite number of solutions.
When we tried to use a 5th or higher degree polynomial above, Matlab returned a warning that the
polynomial is not unique since “degree >= number of data points”.
If the data {(xi , yi )}ni=1 has repeated x-values with distinct y-values, then the system of equations is incon-
sistent and there will be no solution. If n > d + 1 then the system has more equations than unknowns, so
it is overdetermined and in general will have no solution. In these cases Matlab does produce a function,
but it does not satisfy p(xi ) = yi for i = 1, . . . , n and so is not an interpolant. Instead it is a least-squares
fit, which we will study in Lecture 20.
Suppose we want to use the data to extrapolate into the future. Set the plot to the 4th degree polynomial.
Then click the Edit button and select the Axes Properties option. A box should appear that allows you
to adjust the domain of the x axes. Change the upper limit of the x-axis from 2 to 4. Based on the 4th
degree polynomial, what will the chemical reaction do in the future? Is this reasonable?
Next change from 4th degree polynomial to spline interpolant. According to the spline, what will the chemical
reaction do in the future? Try the shape-preserving interpolant also.
From our (limited) knowledge of chemical reactions, what should be the behavior as time goes on? It should
reach a limiting value (chemical equilibrium). Could we use the data to predict this equilibrium value? Yes,
we could and it is done all the time in many different contexts, but to do so we need to know that there is
an equilibrium to predict. This requires that we understand the chemistry of the problem. Thus we have
the following principle: To extrapolate beyond the data, one must have some knowledge of the process.
More data
Generally one would think that more data is better. Input the following data vectors:
≫ t2 = [ 0 .1 .4 .5 .6 1.0 1.4 1.5 1.6 1.9 2.0]
≫ y2 = [ 0 .06 .17 .19 .21 .26 .29 .29 .30 .31 .31]
There are 11 data points, so a 10-th degree polynomial will fit the data. However, this does not give a good
graph. Thus: Polynomial interpolation is better for small data sets.
≫ plot (x ,y , ’* ’)
There are 10 data points, so there is a unique 9 degree polynomial that fits the data. Under Tools and
Basic Fitting select the 9th degree polynomial fit. How does it look? De-select the 9th degree polynomial
and select the spline interpolant. This should produce a much more satisfactory graph and the shape-
preserving spline should be even better.
The general idea of a spline is this: on each interval between data points, represent the graph with a simple
function. The simplest spline is something very familiar to you; it is obtained by connecting the data with
lines. Since linear is the most simple function of all, linear interpolation is the simplest form of spline. The
next simplest function is quadratic. If we put a quadratic function on each interval then we should be able
to make the graph a lot smoother. If we were really careful then we should be able to make the curve smooth
at the data points themselves by matching up the derivatives. This can be done and the result is called a
quadratic spline. Using cubic functions or 4th degree functions should be smoother still. So, where should
we stop? There is an almost universal consensus that cubic is the optimal degree for splines and so we focus
the rest of the lecture on cubic splines.
Cubic spline
Again, the basic idea of the cubic spline is that we represent the function by a different cubic function on
each interval between data points. That is, if there are n data points, then the spline S(x) is the function
C1 (x), x0 ≤ x ≤ x1
S(x) = Ci (x), xi−1 ≤ x ≤ xi
Cn (x), xn−1 ≤ x ≤ xn
where each Ci is a cubic function. The most general cubic function has the form
Ci (x) = ai + bi x + ci x2 + di x3 .
To determine the spline we must determine the coefficients, ai , bi , ci , and di for each i. Since there are n
intervals, there are 4n coefficients to determine. First we require that the spline interpolate by requiring
Notice that there are 2n of these conditions. Then to make S(x) as smooth as possible we require
There are 2(n − 1) of these conditions. Since each Ci is cubic, there are a total of 4n coefficients in the
formula for S(x). So far we have 4n − 2 equations, so we are 2 equations short of being able to determine
all the coefficients. At this point we have to make a choice. The usual choice is to require
These are called natural or simple boundary conditions. The other common option is called clamped boundary
conditions:
C1′ (x0 ) = Cn′ (xn ) = 0.
The terminology used here is obviously parallel to that used for beams. That is not the only parallel between
beams and cubic splines. It is an interesting fact that a cubic spline is exactly the shape of a (linear) beam
restrained to match the data by simple supports.
Note that the equations above are all linear equations with respect to the unknowns (coefficients). This
feature makes splines easy to calculate since solving linear systems is what computers do best.
Exercises
19.1 You are given the following data:
≫ t = [ 0 .1 .499 .5 .6 1.0 1.4 1.5 1.899 1.9 2.0 ]
≫ y = [ 0 .06 .17 .19 .21 .26 .29 .29 .30 .31 .31 ]
(a) Plot the data, using ’*’ at the data points, then try a polynomial fit of the correct degree to
interpolate this number of data points.
• What do you observe? Look closely at the data and identify the points that force the
interpolating polynomial to have large derivative.
• What would happen if the t value of 1.899 was rounded to 1.9? Is the problem of converting
this data to its interpolating polynomial well conditioned or badly conditioned?
(b) Plot the data along with a spline interpolant. How does this compare with the plot above? What
is a way to make the plot better?