In this section we describe a simple way of fitting a curve over a number of points, we show why it should be preferred to regular polynomial interpola- tion, and we explain how it functions. To give an interesting example, we show in Figure 4.5 a series of points situated along a transversal section, near the stern of a passenger ship. As this section is symmetric, it is sufficient to show only half of it. For those familiar with the terminology of naval architecture, we may say that the points belong to a ship station. We are looking for a way of fitting a fair curve over the given points. The naval architectural term fair curve says much more than ‘smooth curve’; it covers not only continuity, but also continuities of the first and the second derivatives and some aesthetical features that are difficult to define. To compare different possibilities without having to input several times the same point coordinates, let us write a script file that we call SplineTrial. We first try a polynomial of the highest degree
FIGURE 4.4: Curve fitted over fresh-water, kinematic-viscosity points
compatible with the given number of points. The first part of the script file is
%SPLINETRIAL Experiments with the MATLAB spline % enter station points
x = [ -9.1361 -7.7103 -6.1394 -3.9366 -2.5037 -1.5628 -0.9030 ... -0.5933 -0.4555 ];
y = [ 11.0276 8.6891 7.0619 5.3533 4.1221 2.8927 1.6643 ...
0.6349 0.1994 ];
% plot given points plot(x, y, ’ro’), grid axis equal xlabel(’Half-breadth’) ylabel(’Depth’) pause %%%%%%%%%%%%%%% polynomial interpolation %%%%%%%%%%%%%%%% n = length(x); m = n - 1;
% coefficients of polynomial interpolation, degree m
c = polyfit(x, y, m)
−10 −8 −6 −4 −2 0 2 1 2 3 4 5 6 7 8 9 10 11 Half−breadth Depth
FIGURE 4.5: Points on a ship station
yi = polyval(c, xi); % polynomial interpolation
plot(x, y, ’ro’, xi, yi, ’k-’), grid axis equal
title([ ’Polynomial fit, degree ’ num2str(m) ]) xlabel(’Half-breadth’)
ylabel(’Depth’)
The statements before the pause command plot the given points as small circles. The statements that follow
• use the function polyfit to calculate the array, c, of the coefficients of the polynomial that passes through all given points;
• use the function polyval to interpolate points between the given ones; • over the given points plot the curve defined by the interpolated points. The result of the polynomial interpolation is shown in Figure 4.6; it is far from being satisfactory. Experience shows that the higher the degree of the interpolating polynomial, the larger are its oscillations. The phenomenon is known as polynomial inflexibility. The quest for an acceptable solution leads to the idea of piecewise interpolation, in which the interval is divided into sev- eral arcs. Lower order polynomials are fitted for each arc, while conditions of continuity are imposed at the points where two arcs meet. The degree of the polynomial is usually chosen as three, the lowest degree for which the second derivative exists. This allows the representation of points of inflexion. The basic MATLAB package includes a function called spline that implements
−15 −10 −5 0 5 −8 −6 −4 −2 0 2 4 6 8 10 12
Polynomial fit, degree 8
Half−breadth
Depth
FIGURE 4.6: Ship station - Polynomial interpolation
this idea. The term spline was taken from naval architecture where it refers to a flexible, metallic or wooden stripe used to draw curves. The first mathe- matician to use the term for new mathematical objects was Isaac Schoenberg (Galatz-Romania 1903, 1990). Let us first try the MATLAB spline function and explain its functioning afterwards. To continue our script file we first add the command pause and after it the lines that call the MATLAB function spline:
pause
%%%%%%%%%%%%%%%% spline interpolation % %%%%%%%%%%%%%%%%%% yi = spline(x, y, xi);
% plot MATLAB spline
plot(x, y, ’ro’, xi, yi, ’k-’) axis equal
title(’Cubic spline interpolation’) xlabel(’Half-breadth’)
ylabel(’Depth’)
The function spline is called with three arguments: • the array of x− coordinates of the given points; • the array of y−coordinates of the given points;
The output is the array of y−coordinates of the interpolated points. In Figure 4.7 we plot the given points as circles, and the interpolated curve as a solid line. In this case the result is satisfactory. The MATLAB spline function has its limitations and, in some cases, it may yield unsatisfactory results. Then, other splines may be used. Being satisfied, for the moment, by the spline function, let us explain how it is calculated. Given n points with the coordinates xi, yi, i = 1 . . . n, we want to fit in each interval [xi, xi+1], a cubic polynomial of the form
Si= ci1(x− xi)3+ ci2(x− xi)2+ ci3(x− xi) + ci4 (4.1) subject to the conditions of continuity to be described immediately. We need n− 1 splines, each one having four coefficients, in total 4(n − 1) coefficients. Each polynomial must pass through the two points that define the interval of the spline. As we have n− 1 intervals, we obtain 2(n − 1) conditions of the form Si(xi) = yi, Si(xi+1) = yi+1. We ask also for the continuity of tangents at the common point of two intervals. This yields n− 2 conditions of the form ˙Si(xi+1) = ˙Si+1(xi+1). Finally, we ask for the continuity of curvature at the common point of two intervals. This yields n− 2 conditions of the form
¨
Si(xi+1) = ¨Si+1(xi+1). Up to now we have defined 4n− 6 conditions; two more are needed. The usual way to complete the set of conditions is to impose zero curvature at the first and last points, that is ¨S1(x1) = 0, ¨Sn−1(xn) = 0. This condition is called natural, and the resulting curve, natural spline. It is possible to find out the details of the spline fitted by MATLAB. To exemplify this let us first verify that we still have in our workspace the arrays of coordinates, x and y used in Figure 4.5. Next we call the spline function with two arguments only, and the output argument pp, an acronym for piecewise polynomial. The resulting display shows that pp is a structure with five fields:
breaks a 1-by-9 array of double-precision numbers;
coefs an 8-by-4 array of double-precision numbers;
pieces containing the value 8;
order containing the value 4;
x x = Columns 1 through 6 -9.1361 -7.7103 -6.1394 -3.9366 -2.5037 -1.5628 Columns 7 through 9 -0.9030 -0.5933 -0.4555 y y = Columns 1 through 6 11.0276 8.6891 7.0619 5.3533 4.1221 2.8927 Columns 7 through 9 1.6643 0.6349 0.1994 pp = spline(x, y) pp = form: ’pp’ breaks: [1x9 double] coefs: [8x4 double] pieces: 8 order: 4 dim: 1
We defer the discussion of the term double-precision until Chapter 5, and that of the term structure until Chapter 6. For the moment we are interested only in the meaning of the information contained in pp. To access this infor- mation we use the function unmkpp called with pp as input argument, and the five fields of pp as output arguments:
[ breaks, coefs, l, k, d ] = unmkpp(pp)
From the results shown below it appears that the array breaks is identical to the array x. As to the array coefs, it contains the coefficients cij of the splines Si. There are eight rows, the i−th row corresponding to the spline Si fitted between the breaks i and i + 1. In each row the coefficients appear in the descending order of the powers of (x− xi). The variable l contains the number of splines; as expected, it is equal to the number of breaks minus one. The variable order is the order of the spline, in spline terminology the highest power of the piecewise polynomials plus one. The variable dim shows that the spline is one-dimensional.
breaks = Columns 1 through 6 -9.1361 -7.7103 -6.1394 -3.9366 -2.5037 -1.5628 Columns 7 through 9 -0.9030 -0.5933 -0.4555 coefs = -0.0313 0.3402 -2.0615 11.0276 -0.0313 0.2062 -1.2824 8.6891 -0.0078 0.0586 -0.8665 7.0619 -0.0711 0.0067 -0.7228 5.3533 0.1314 -0.2990 -1.1416 4.1221 -1.2725 0.0719 -1.3553 2.8927 3.7140 -2.4469 -2.9223 1.6643 3.7140 1.0038 -3.3692 0.6349 l = 8 k = 4 d = 1
To show that the MATLAB spline is that calculated by Equation 4.1, we continue our file SplineTrial by adding the lines listed below. After calling the function unmkpp, we use the recovered information to implement Equa- tion 4.1 in a loop that is repeated l times. The output yielded by unmkpp allows us to do even more. In some applications we may be interested in the area under the fitted curve. For our spline, and with the notation used above, this area is given by
A = l i=1 xi+1 xi Sidx = l i=1 c i1 4 (x− xi) 4+ci2 3 (x− xi) 3+ci3 2 (x− xi) 2+ c i4(x− xi) xi+1 xi = l i=1 ci1 4 (xi+1− xi) 4+ci2 3 (xi+1− xi) 3+ ci3 2 (xi+1− xi) 2+ c i4(xi+1− xi) (4.2) We include the corresponding MATLAB statements in the lines to be added to our file. The lines for integration are called within the loop over the number of splines.
pause
%%%%%%%%%%%%% Write own spline and calculate area under it %%%%%%%%%%% hold on
% find details of MATLAB spline pp = spline(x, y)
[breaks,coefs,l,k,d] = unmkpp(pp)
I = 0 % initialize integral
% loop over spline segments for n = 1:l
% create interpolating scale xi = breaks(n): 0.1: breaks(n+1); % calculate interpolated points
yi = coefs(n, 1)*(xi - breaks(n)).^3 + ... coefs(n, 2)*(xi - breaks(n)).^2 + ... coefs(n, 3)*(xi- breaks(n)) + coefs(n, 4); % calculate area under the spline curve
dx = breaks(n+1) - breaks(n)
I0 = coefs(n, 1)*dx^4/4 + coefs(n, 2)*dx^3/3 + ... coefs(n, 3)*dx^2/2 + coefs(n, 4)*dx
% plot interpolated spline in red, over the MATLAB spline in black plot(xi, yi, ’r-’)
I = I + I0
end
% end loop over spline segments
t = [ ’Cubic spline and its integral, \int ydx = ’ num2str(I, 2) ] title(t)
legend(’Given points’, ’MATLAB spline’, ’My spline’) hold off
Run the completed file. In the last figure the spline drawn in red by the last program lines cannot be distinguished from the curve plotted by the MATLAB built-in function spline; the two curves are identical. The title of the figure contains the value of the area under the curve. Note that in the string printed as title we used the TEX command ‘\int’ to show the symbol of integration.
Exercise 4.1 Checking the spline on the cosine function
Return to the file SplineTrial developed in Section 4.5 and modify it to in- terpolate the function cos α and calculate its integral in the interval 0 . . . π/2. In continuation, calculate analytically the area under the curve, in the given interval, and compare with the results yielded by integrating the spline func- tion.
−10 −8 −6 −4 −2 0 2 1 2 3 4 5 6 7 8 9 10 11
Cubic spline interpolation
Half−breadth
Depth
FIGURE 4.7: Ship station – Interpolation by the MATLAB spline function