Sie sind auf Seite 1von 17

Splines and Piecewise

ES 204
Numerical Methods in Engineering
What are linear splines?
spline functions - application of lower-order, connecting polynomials in a piecewise fashion
to subsets of data points

For linear splines:

Example: Fit the data with first-order splines;
evaluate f(5)

First-order splines are not smooth; slope changes abruptly at knots; discontinuous first derivative
Use higher order splines to ensure smoothness
What are quadratic splines?
Derive a second-order polynomial for each interval between data points
n data points, n 1 intervals, 3(n 1) unknown constants

3(n 1) equations/conditions

1. Function must pass through all the points (continuity condition)

fi = ai + bi (xi xi ) + ci (xi xi )2 >>> ai = fi >>> >>> 2(n-1) eqtns

2. Function values of adjacent polynomials must be equal at the knots

Can write i = 1, 2, (n 1); n 1 eqtns more

3. First derivatives at the interior nodes must be equal
Amounts to n 2 eqtns; 1 remaining condition

4. Assume that the second derivative is zero at the first point

Example Quadratic Splines
Fit quadratic splines to the same data employed in previous example
Use the results to estimate the value at x = 5

Four data points and 3 intervals; 3(4 1) 1 = 9 conditions are required

Applying the continuity condition (ai = fi ) and zero second derivative (c1 = 0), number of
required conditions is 9 3 1 = 5

Applying equal function values at knots: >>> for i = 1, 2, and 3:

Applying equal derivatives at knots: >>> for n 2 = 2 conditions

Example Quadratic Splines

ai = fi

Because x = 5 lies in the second interval, we use s2 to make the prediction

Problems w/ quadratic splines:

1. straight line connecting the first two points
2. spline for the last interval seems to swing too high
What are cubic splines?
Disadvantages of linear splines
Disadvantages of quadratic splines as discussed previously
Quartic or higher-order splines are not used because they tend to exhibit the instabilities
inherent in higher-order polynomials
Cubic splines are most frequently used in practice
Derive a third-order polynomial for each interval between knots

n data points, n 1 intervals, 4(n 1) unknown coefficients to evaluate

4(n 1) conditions/eqtns

1. Functions pass through the points 2. first derivatives at the knots are equal
3. second derivatives at the knots are also equal

Natural spline - second derivatives at the first and last knots are equal to zero; straight line at
the end nodes
Other conditions: clamped condition and the not-a-knot conditions
How to derive cubic splines?
1. Spline must pass through all the data points
constant in each cubic must be equal to the value of the dependent variable at
the beginning of the interval

2. Each of the cubics must join at the knots; for knot i + 1:

3. First derivatives at the interior nodes must be equal

At an interior node i + 1:

4. Second derivatives at the interior nodes must be equal

At an interior node i + 1:

Index reduced by 1 Index reduced by 1

How to derive cubic splines?

Index reduced by 1 Index reduced by 1

finite differences
Can be written for the interior knots, i = 2, 3, . . . , n 2; gives n 3 simultaneous tridiagonal
equations with n 1 unknown coefficients, c1, c2, . . . , cn-1
We need two more conditions to solve the cs, then determine b and d coefficients
Natural spline - assumes second derivatives at the end knots are equal to zero

first node last node

Equal 2nd derivatives at

interior nodes
How to derive cubic splines?
(end nodes only)

Example: Fit natural cubic splines; estimate the value at x = 5

Function and interval width values:

Example Natural Cubic Splines

We can now compute b and d coefficients

(By continuity condition)

Cubic spline different with

third order regression polynomial
What are other end conditions for
cubic splines?
Clamped End Condition - specify first derivatives at the first and last nodes
E.g. If zero first derivatives, spline levels off or become horizontal at the ends

Not-a-Knot End Condition - force continuity of the third derivative at the second and the
next-to-last knots; first internal knots no longer represent the junction of two different cubic
functions (no longer true knots)

Clamped case - derivatives at the ends are

equal to zero; levels off at the ends
Natural spline - straighten out due to zero
2nd derivative at the ends
Not-a-knot more curvature
Example: Splines in GNU Octave
Use GNU Octave to fit nine equally spaced data points sampled from Runges function in the
interval [1, 1]. Employ (a) a not-a-knot spline and (b) a clamped spline with end slopes of f 1 =
1 and f n1 = 4.

not-a-knot spline clamped spline

True first derivatives will improve the fit

Example: Piecewise Interpolation
You perform a test drive on an automobile where you alternately accelerate the automobile and
then hold it at a steady velocity. Note that you never decelerate during the experiment. The
time series of spot measurements of velocity can be tabulated as

Use Octaves interp1 function to fit these data with (a) linear interpolation, (b) nearest neighbor,
(c) cubic spline with not-a-knot end conditions, and (d) piecewise cubic Hermite interpolation.

default is linear interpolation

Linear interpolation

Not smooth, but do not exhibit any overshoot

Example: Piecewise Interpolation
Nearest neighbor - sets the value of an interpolated point to the value of the nearest existing
data point; zero order polynomials

a series of plateaus; neither a smooth nor an accurate

depiction of the underlying process

Cubic spline

Quite smooth; severe overshoot occurs at

several locations
It appear that the automobile decelerated
several times during the experiment
Example: Piecewise Interpolation
Piecewise cubic Hermite interpolation - uses cubic polynomials to connect data points
with continuous first derivatives
Second derivatives are not necessarily continuous
First derivatives at the knots are chosen so that the interpolation is shape preserving;
interpolated values do not tend to overshoot the data points

Physically realistic

Velocities increase monotonically and

never exhibit deceleration

Not as smooth as for the cubic

splines, but transitions between points
are more gradual
What is bilinear interpolation?
Determining intermediate values for functions
of two variables z = f (xi , yi ) using a linear function

Hold the y value (y1 and y2) fixed and apply one
dimensional linear interpolation in the x direction

Use the point to linearly interpolate along the y dimension

Example: Multidimensional
Interpolation (Bilinear)
Suppose you have measured temperatures at a number of coordinates on the surface of a
rectangular heated plate:

Use bilinear interpolation to estimate the temperature at xi = 5.25 and yi = 4.8.

Octaves two- and three-dimensional piecewise

interpolation: interp2 and interp3