
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























Up to this point, we have restricted the approximating functions that were used for interpolation to a single polynomial with a unique definition over the interval corresponding to the set of data points. The results can be less than satisfactory for a number of reasons related to either the order of the polynomial or the general profile of the data points. When the order is too low or too high, relative to the variability and number of data points, the interpolating polynomial may exhibit significant errors.
Figure 5.1 shows a set of nine data points from a function f ( x ), a second order polynomial f 2 ( x ) through the middle and two end data points, and finally the eighth order polynomial f 8 ( x ) through all the data points. A visual inspection of the data points suggests f ( x ) approaches a limiting value in an asymptotic fashion. High order polynomials are not well suited when it comes to approximating this type of behavior.
Figure 5.1 Example of a Function f ( x ) Not Well Suited for Polynomial Approximation
Another approach to obtain an approximating function for interpolation is based on the idea of formulating the approximating function in a piecewise fashion. In fact, we have already done this in a somewhat elementary way. Figure 1.1 in Section 1 illustrates the use of piecewise linear segments to define the approximating function. This is
f 2 ( x )
f ( x )
Data Points For Determining f 2 ( x ) (0,1), (4,8.1951), (10,8.9988)
f 8 ( x )
equivalent to using multiple first order interpolating polynomials restricted to intervals between adjacent data points.
Suppose we wish to approximate a function f ( x ), defined in tabular form by a set of data points [ xi , f ( xi )], i = 0,1,2,…, n with lower order polynomials, each valid for a limited portion of the interval x 0 ≤ x ≤ xn. These lower order polynomials are referred to as spline functions. The term originates from drafting where a smooth curve is drawn through a set of control points using a flexible strip called a "spline". The spline is weighted at various points along its length to keep it in contact with the drafting surface as the curve is drawn. The simplest implementation is to use linear splines in the intervals between consecutive data points. Doing this, we denote the approximating function F 1 ( x ) and the " n " linear splines which comprise it as f 1 , i ( x ). This leads to
F x
f x x x x
f x x x x
f x x x x
i i i
n n n
1
1 1 0 1
1 1
1 1
,
,
,
R
S
| | | | | T
| | | | |
−
−
Using Newton first order divided-difference interpolating polynomials for the linear splines,
F x
a b x x x x x
a b x x x x x
a b x x x x x
i i i i i
n n n n n
1
1 1 0 0 1
1 1
1 1
R
S
| | | ||
T
| | | | |
− −
− −
The coefficients a (^) i and bi define the individual splines f (^) 1, i ( ) and are given by x
a (^) i = f ( x (^) i − 1 ), i =1 2 3, , ,..., n (5.3)
b
f x f x x x i i^ i i^ n i i
−
1
1 2 3 (5.3a)
»xint=0.5:1:9. »Fint=interp1(x,F_x,xint)
xint = 0.5000 1.5000 2.5000 3.5000 4.5000 5.5000 6.5000 7.5000 8.
Fint = 1.7285 3.6140 5.8602 7.5723 8.4551 8.8091 8.9353 8.9727 8.9831 8.
Figure 5.2 Approximating Function for Data in Table 5.1 Consisting of Linear Splines
There are situations when the approximating function is required to pass through each data point and appear smooth at the same time. A composite function consisting of linear splines will satisfy the first requirement. However, unless the data points are very tightly spaced (See Figure 1.3), the combined function will have a zig-zag look as opposed to a smooth appearance. Piecewise continuous functions like F 1 ( x ) in Figure 5. are not smooth because of the changes in slope at each of the internal data points, or knots as they are often referred to. Linear spline functions are "tied down" at these knots.
Smoothness refers to the continuity of a function and its derivatives. A function is infinitely smooth at a point if the derivatives of every order are continuous at that point. Clearly, the first derivative of F 1 ( x ) in Figure 5.2 is discontinuous at each knot. Unless there are three adjacent data points which happen to be collinear, piecewise linear functions like F 1 ( x ) will always exhibit a change in slope at each knot.
( x 0 , y 0 )
( x 1 , y 1 )
( x 2 , y 2 )
( x 8 , y 8 )
f x y
y y x x 1 2 1 2 1 x 2 1
L N
M
O Q
P
f x y
y y x x 1 1 0 1 0 x 1 0
L N
M
O Q
P
F 1 ( x ) = f (^) 1 , i ( ), x x (^) i − 1 ≤ x ≤ xi i = 1, 2, …,
The use of piecewise second order polynomials offers a partial solution to the problem if we restrict the quadratic polynomials to the intervals between adjacent data points. That is, with n +1 data points, [ xi , f ( xi )], i = 0,1,2,…, n there will be n quadratic splines, one per interval. If you're wondering why each quadratic spline passes through only two consecutive data points even though we could fit a quadratic function across subintervals comprised of three data points, look at Figure 5.3.
Figure 5.3 Piecewise Quadratic Polynomials Through Subintervals of Three Data Points
The composite function in Figure 5.3 consisting of quadratic polynomials fitted to uosubintervals of three data points each is not a smooth function. There is a discontinuity in slopes at the transition between the first and second quadratic as well as the second and third quadratics. Table 5.2 lists the four quadratic polynomials and their first derivatives. The MATLAB 'polyfit' function was used to obtain the quadratic polynomial coefficients .
Interval Quadratic First Derivative 0 ≤ x ≤ (^215). x^2 + 0 5. x 3 x + 0 5. 2 ≤ x ≤ (^4) − 2 5. x^2 + 235. x − 30 −^5 x^ +^235. 4 ≤ x ≤ (^6) − 3 x^2 + 25 x − 28 −^6 x +^25
1 st^ Quadratic
2 nd^ Quadratic
3 rd^ Quadratic
4 th^ Quadratic
of each interval. Each of the 3 interior points produces only a single smoothness constraint, equality of the first derivatives of adjacent quadratic splines, increasing the total number of constraints to 11. Consequently, the system of linear equations for determining the 12 coefficients is not over constrained. On the contrary, an additional constraint, i.e. equation is needed to produce a consistent system of equations with a unique solution for the 12 coefficients. The missing equation will be revealed in the general discussion of quadratic splines which follows, although you may have figured it out on your own if you looked carefully at the first quadratic spline.
Figure 5.5 A Composite Function F ( x ) of Piecewise Quadratic Splines
Given a set of data points [ xi , f ( xi )], i = 0,1,2,…, n , there are n quadratic splines to be determined, one per interval. The splines may be represented in either the standard form, i.e. a linear combination of the monomials 1, x , and x^2 as in Equation (2.7), or in the Newton divided-difference form of Equation (3.7). Since the smoothness constraint involves the first derivative, it is not advisable to employ the Lagrange form which is the most cumbersome to differentiate. Lagrange polynomials are better suited in applications where the constraints are based solely on the function values, as in Figure 5.3, where each piecewise quadratic was required to fit a subset of three data points.
Choosing the Newton divided-difference form, the function F 2 ( x ) comprised of quadratic splines is expressed as shown in Equation (5.4). Referring to Figure 5.6, there are 3 coefficients to be determined for each of n splines, hence a total of 3 n coefficients.
Coefficients: 3 3 3 3
Equality Constraints
function: 2 2 2 2 derivative: 1 1 1
Equations to determine these coefficients are obtained by imposing a like number of constraints on the splines.
F x
a b x x c x x x x x x x
a b x x c x x x x x x x
a b x x c x x x x x x x
a b x x c x x x x x x x
a b x x c x x
i i i i i i i i
i i i i i i i i
i i i i i
2
1 1 0 1 0 1 0 1
2 2 1 2 1 2 1 2
1 1 2 1 2 1 2 1
1 1 1
1 1 1
− − − − − − − −
− − −
x x x x x
a b x x c x x x x x x x
a b x x c x x x x x x x
i i i
n n n n n n n n
n n n n n n n n
R
S
| | | | | | | | | | T
| | | | | | | | | |
− − − − − − − −
− − −
1 1
1 1 2 1 2 1 2 1
1 1 1
Figure 5.6 Quadratic Splines Represented in Newton Divided-Difference Form
a 1 + b 1( x - x 0)+ c 1 ( x - x 0)( x -
a 2 + b 2( x - x 1 )+ c 2( x - x 1)( x - x 2)
a (^) n + b (^) n ( x - x (^) n- 1 )+ c (^) n ( x - x (^) n- 1 )( x - x (^) n )
ai + b (^) i ( x - x (^) i- 1 )+ c (^) i ( x - x (^) i- 1 )( x - x (^) i )
x 0 x 1 x 2 xi -2 xi -1 xi xi +1 xn -1 xn
f ( x (^) i ) =^ f ( x (^) i − 1 ) +^ bi ( x (^) i − xi − 1 ) (5.19)
b
f x f x x x i i^ i i^ n i i
−
1
Equations (5.17) and (5.20) are solutions for a (^) i and bi of each spline , accounting for 2 n out of the 3 n coefficients. Thus, n equations involving ci , i = 1,2,3,…, n are required before we can uniquely determine the set of quadratic splines.
We now impose the smoothness constraint at the knots (interior data points) which produces n -1 new equations.
II. Continuity of First Derivative at Interior Data Points:
Lim Lim x x (^) i x xi
d dx
F x
d dx
F x i n → −^ →+
At the first interior point,
d dx
a b x x c x x x x
d dx 1 +^1 (^ −^0 )^ +^1 (^ −^0 )(^ −^1 )^ x = x (^) 1 =^ a^ 2 +^ b 2^ (^ x^ −^ x 1^ )^ +^ c^ 2 (^ x^ −^ x 1^ )(^ x^ − x 2 )^ x = x 1 (5.22)
b 1 (^) + c 1 (^) ( 2 x − x (^) 0 − x (^) 1 ) (^) x = x 1 (^) = b 2 (^) + c 2 (^) ( 2 x − x (^) 1 − x 2 ) (^) x = x 1 (5.23)
b 1 (^) + c (^) 1 ( x 1 (^) − x (^) 0 ) = b 2 (^) + c 2 (^) ( x (^) 1 − x 2 ) (5.24)
c x x x x
c b b (^2) x x 1 0 1 2
1 1 2 1 2
F H G
I K J +^
A similar condition applies at each of the n -1 knots. That is,
c
x x x x
c
b b x x i i^ i i^ n i i
i i i i i
−
F H G
I K J +^
1
1 1
Equation (5.26) represents a system of n -1 equations in n unknowns once the coefficients b (^) i are computed. Since there is no other obvious condition involving one or more ci to impose, we are left with a single degree of freedom to choose one of the coefficients ci arbitrarily. It is customary to let either c 1 or cn be zero, meaning either the first or last spline will be a linear rather than quadratic function. If we choose c 1 =0, Equation (5.26) can be solved sequentially for ci , i = 2,3, …, cn. An example is now given to demonstrate the relative ease in finding the quadratic splines corresponding to a set of data points.
Example 5.
A highway engineer must design a new road which will connect two existing roads. In order to maintain safe clearances from a lake and several structures, measurements were taken to determine several points along the centerline of the proposed road. The geographical locations of these points, relative to some point of reference, are given in Table 5.3. Quadratic splines are to be used to specify the equations of the centerlines for each road segment.
i xi (miles) yi (miles)
0 0.0 1. 1 0.2 1. 2 0.6 1. 3 1.0 1. 4 1.4 0. 5 1.6 0. 6 1.8 0. 7 2.0 0.
Table 5.3 End Point Data for Segments of Proposed Roadway
The following MATLAB m-file was written to plot the given centerline points, determine the quadratic splines, and plot the continuous centerline comprised of the those splines. The results are illustrated in Figure 5.7 which shows the smooth centerline trajectory of the proposed connector between the existing roads. (Despite the smooth appearance of the connecting road, the curvature at certain points may not conform to existing civil engineering standards for roadway design.)
% Example 5. x=[0 0.2 0.6 1 1.4 1.6 1.8 2]; % Centerline y=[1 1 1.2 1.1 0.6 0.5 0.5 0.5]; % Data Points v=[0 2 0 1.6]; plot(x,y,'.') % Plot Centerline Data Points xlabel('x (miles)') ylabel('y (miles') axis(v) hold on axis manual n=8; % Find Newton Polynomial Coefficients for i=1:n- a(i)=y(i); b(i)=(y(i+1)-y(i))/(x(i+1)-x(i)); end
which is readily solved sequentially given the starting value for c 1.
A composite function consisting of quadratic splines has a visually smooth appearance since the first derivative is continuous at the knots. The second derivative, however is not continuous at the knots. What are the implications of the second derivative being discontinuous at these points? To answer this question, we focus our discussion on the curvature of a function. In precise mathematical terms, curvature is the instantaneous rate of change of the direction of the tangent vector at a point on the curve with respect to the arc length. Intuitively, it makes sense that the faster the tangent vector is changing direction at a point, the greater will be the curvature of the function at the point.
From Calculus, the curvature κ at a point on the function y = f ( x ) is given by
κ =
f x
f x
/ 1 2
3 2 c h
Consider the simple polynomial function y = f ( x ) = x^2 shown in Figure 5.8. From Equation (5.30) the curvature of f ( x ) at the origin is
κ =
=
2 3 2 0
/ x c h (^) x
The circle of curvature is the circle with center (of curvature) along the normal to the curve and radius (of curvature) ρ equal to 1/κ. The curvature properties of f ( x ) are shown at several points along the function in the quadrants of Figure 5.8. Note the curvature of f ( x ) = x^2 is a maximum at the origin and diminishes as x increases.
Returning to our discussion of splines, it is evident that piecewise functions composed of quadratic splines may be smooth (based on the first derivative being continuous) at the knots but the curvature may change in a stepwise manner at these points. Equation (5.30) implies the change in curvature at a knot is proportional to the discontinuity of the second derivative since the first derivative and hence the denominator is continuous there.
There are indeed applications where sudden changes in the curvature of a smooth looking function are unacceptable. Jarring forces can result on a roller coaster ride when the radius of curvature changes abruptly. Highways consist of sections of road where a smooth transition requires a continuous curvature.
Figure 5.8 Curvature and Radius of Curvature of a Function at Several Points
The MATLAB script file used to determine as well as plot the circle of curvature is listed below.
for i=1: x=linspace(-2.5,2.5,500); y=x.x; v=[-5 5 0 10]; subplot(2,2,i) plot(x,y,'-') xlabel('x'),ylabel('y') axis(v) hold on axis square axis equal x0=(i-1)/4; % x value of f(x)where curvature is determined xc=-4x0^3; % x coord for center of circle of curvature yc=0.5+3x0^2; % y coord for center of circle of curvature r=0.5(1+4*x0^2)^(1.5); % radius of curvature of f(x) at x x=linspace(xc-r,xc+r,200); disc=(r^2-(x-xc).^2).^0.5; ycir_upper=yc+disc; % upper half of circle of curvature ycir_lower=yc-disc; % lower half of circle of curvature plot(x,ycir_upper,'g-') plot(x,ycir_lower,'g-') end
x 0 = κ= ρ=0.
x 0 =0. κ=1. ρ=0.
x 0 =0. κ=0. ρ=1.
x 0 =0. κ=0. ρ=2.
f x A x x
i i i x x
i i i i
− −
1 1
1 1
where Ai -1 and Ai are the numerical values, yet to be determined, of the second derivative at xi -1 and xi respectively, (See Figure 5.9).
Figure 5.9 Cubic Splines and Their First Two Derivatives
Integrating the second derivative in Equation (5.37) twice, yields expressions for f (^) i '( x ) and f (^) i ( x) with constants of integration present.
f x
x x
x x i i^ i A^ x^ x i i
i '( ) = (^) i ( (^) i ) i
F H G
I K J
−
− − −
1 1
1
2 1 1 2
b g α (5.38)
f x
x x
x x A
x x i i^ i x^ x i i
i i
i ( ) = (^) i ( (^) i ) i
F H G
I K J
−
− −
− −
1 1
1
3 1
1
2 1 6 2
b g b g α β (^) (5.39)
The constants of integration are determined from the endpoint constraints of Equation (5.34). The result is
xi -2 xi -1 xi xi +
xi -2 xi -1 xi xi +
xi -2 xi -1 xi xi +
F "( x )
F '( x )
F ( x )
Ai -
Ai -
Ai
yi -
yi -
yi
f (^) i ( x )
f (^) i -1 ( x )
β (^) i = yi (^) − 1 , i =1 2 3, , ,..., n (5.40)
α (^) i i^ i i i
i i i i
y y x x
= x x i n
F + H G
I K
− J − = −
− −
1 1
1 1
b g, 1 2 3, , ,..., (5.41)
Substituting Equations (5.40) and (5.41) into Equation (5.39) yields expressions for the cubic splines which pass through the data points ( xi , yi ), i = 0,1,2,…, n.
f x
x x
x x
i i^ i x^ x i i
i i ( ) = ( ) ( (^) i )
L
N
M
O
Q
− P − + − −
− − −
1 1
1
3 1 1
2 (^6) b g 2
F + HG^
I KJ^
L N
M
O Q
P −^ +^ =
− −
− − − −
y y x x
i i x x x x y i n i i
i i i i i i
1 1
1 1 1 1
b g (^ )^ ,^ 1 2 3,^ , ,...,
At first glance it appears that we may have complicated things unnecessarily due to the presence of the second derivative terms Ai and Ai -1 in Equation (5.42). On the positive side, however, we are not confronted with a system of 4 n simultaneous equations as would be the case if we employed the standard polynomial form to represent the cubic splines. In fact, all that remains is a way of determining the constants Ai , i = 0, 1, 2, …, n.
Equations for these constants are obtained by imposing the continuity requirement of the first derivative at the knots, Equation (5.35). Note, continuity of the second derivative at each knot is assured by virtue of Equation (5.37). The process is straightforward but tedious due to the manipulation of numerous terms appearing in the expressions for the first derivatives f (^) i ′− 1 ( xi − 1 ) and f (^) i ′( xi − 1 ). After much simplification,
the result is
x x A
x x A
x x A
y y x x
y y x x
i i i
i i i
i i i
i i i i
i i i i
F^ − H G
I K J +^
F − H G
I K J +^
F − HG^
I KJ^
− − −
− − −
− −
− − − −
1 2 1
1 2 2
1 1
1 2 (^6 3 6 1 )
Equation (5.43) applies for i = 2,3,…, n which accounts for n -1 equations relating the n +1 constants Ai , i = 0,1,2,…, n. It is customary to select or compute numerical values for A 0 and An based on additional information or requirements imposed on the endmost splines. For example, choosing A 0 = An = 0, produces what is referred to as a "natural" cubic spline. In this case, the spline is relaxed at both ends approaching straight lines at the endpoints. A "clamped" cubic spline results when the endpoint constants A 0 and An are calculated based on known values for the first derivatives at the endpoints, i.e. f (^) 1 ′ ( x 0 ) and f (^) n ′ ( xn ). Alternatively, A 0 and An can be extrapolated values based on A 1 , A 2
and An -2 , An -1 respectively. Finally, selecting A 0 = A 1 and An = An -1 forces the two end splines to reduce to quadratics (see Equation (5.37)).
plot(x,y,'.') axis(v) axis manual hold on n=7; % Number of data points X=zeros(n-1,n-1); % X is (n-1) by (n-1) %Create first row of X matrix and b vector X(1,1)=(x(3)-x(1))/3; X(1,2)=(x(3)-x(2))/6; b(1)=(y(3)-y(2))/(x(3)-x(2)) - (y(2)-y(1))/(x(2)-x(1)); % Create rows 2 thru n-2 of X matrix and b vector for i=2:n- X(i,i-1)=(x(i+1)-x(i))/6; X(i,i)=(x(i+2)-x(i))/3; X(i,i+1)=(x(i+2)-x(i+1))/6; b(i)=(y(i+2)-y(i+1))/(x(i+2)-x(i+1)) - (y(i+1)-y(i))/(x(i+1)-x(i)); end % Create (n-1)st row of X matrix and b vector X(n-1,n-2)=(x(n)-x(n-2))/6; X(n-1,n-1)=(x(n+1)-x(n-1))/3; b(n-1)=(y(n+1)-y(n))/(x(n+1)-x(n)) - (y(n)-y(n-1))/(x(n)-x(n-1)); a=inv(X)b'; % Find a=[A(1)....A(n-1)] for i=1:n dx=x(i+1)-x(i); dy=y(i+1)-y(i); xspace=linspace(x(i),x(i+1),75); xx=xspace-x(i); if (i==1) T1=(1/6)(a(1)/dx)xx.^3; T3=((dy/dx)-(a(1)/6)dx)xx + y(1); f_1=T1+T3; plot(xspace,f_1,'-') % Plot 1st cubic spline elseif(i~=1&i~=n) T1=(1/6)((a(i)-a(i-1))/dx)xx.^3; T2=(a(i-1)/2)xx.^2; T3=((dy/dx)-((a(i)+2a(i-1))/6)dx)xx + y(i); f_i=T1+T2+T3; plot(xspace,f_i,'-') % Plot 2nd thru (n-1)st cubic spline elseif(i==n) T1=(1/6)(-a(n-1)/dx)xx.^3; T2=(a(i-1)/2)xx.^2; T3=((dy/dx)-(a(n-1)/3)dx)xx + y(n); f_n=T1+T2+T3; plot(xspace,f_n,'-') % Plot nth cubic spline end end
As you might expect MATLAB has its own built-in routine for cubic splines. The statement yi = spline(x, y, xi) invokes the cubic spline function where arrays x and y define the data points while (xi, yi) are the smoothed point(s) in between them. Instead of a natural spline, MATLAB constrains the third derivatives of the first two and last two cubic splines to be equal. The constants A 0 and An are not zero; however the vector a = [ A 0 A 1 …. An ] T^ is uniquely determined. The 'spline' function exploits the sparse nature of the coefficient matrix obtained from Equation (5.43) and is therefore more efficient than matrix inversion to solve the equations.
Figure 5.10 Roadway Centerline Determined From Quadratic and Cubic Splines
If you're curious about the coefficients for each cubic spline, you can call 'spline' with arguments x and y only and it returns a 'pp' or piecewise polynomial array form which can be decomposed using 'unmkpp' to reveal the polynomial coefficients and other useful information. To illustrate, suppose we use the data points from Example 5.3.
»x=[0^ 0.2^ 0.6^1 1.4^ 1.6^ 1.8^ 2]; »y=[1 1 1.2 1.1 0.6 0.5 0.5 0.5]; pp=spline(x,y); [x_values,spline_coefs,num_splines,num_coefs]=unmkpp(pp)
The following output results.
x _values = 0 0.2000 0.6000 1.0000 1.4000 1.6000 1.8000 2.
spline_coefs = -2.0032 2.4359 -0.4070 1. -2.0032 1.2340 0.3269 1. -0.8416 -1.1698 0.3526 1. 3.8069 -2.1797 -0.9872 1. -1.8512 2.3886 -0.9037 0. -2.1298 1.2779 -0.1704 0. -2.1298 0.0000 0.0852 0.
num_splines = 7
Quadratic Splines (Example 5.2)
Cubic Splines (Example 5.3)