• Aucun résultat trouvé

Derivatives for the Spline Function

Dans le document Scientifi c Computing (Page 166-174)

Tips and Tricks

Chapter 3. Linear Systems of Equations

4.3 Piecewise Interpolation with Polynomials

4.3.2 Derivatives for the Spline Function

As we saw in the previous section, we need derivatives at the nodes in order to construct a spline function. In principle, we could prescribe any value for them; however, it makes sense to estimate the derivatives from the given

function values. A simple estimate for the derivative at point (xi, yi) is given by the slope of the straight line through the neighboring points (see Figure 4.5),

yi= yi+1−yi−1 xi+1−xi1

= yi+1−yi−1 hi+hi1

i= 2,3, . . . , n1. (4.52) Using (4.52), we can compute derivatives for all inner nodes. For the two

xi−1 xi xi+1

yi−1

yi

yi+1

yi

Figure 4.5.Slope of the line through neighboring points

boundary points, we have several possibilities:

1. Use the slope of the line through the first two (respectively the last two) points

y1= y2−y1

x2−x1

respectively yn= yn−yn−1 xn−xn1

.

WithMatlabthe derivativesysfor this case can be computed by n=length(x);

ys=(y(3:n)-y(1:n-2))./(x(3:n)-x(1:n-2));

ys=[(y(2)-y(1))/(x(2)-x(1)); ys; (y(n)-y(n-1))/(x(n)-x(n-1))];

2. Natural boundary conditions: they are defined such that the second derivative vanishes. From the equations

Q1(0) = 0 and Qn−1(1) = 0 we obtain

y1= 3 2

y2−y1

h1 1

2y2 respectively yn=3 2

yn−yn−1 hn−1 1

2yn1.

Piecewise Interpolation with Polynomials 149

3. An important special case are periodic boundary conditions. If the function values belong to a periodic function theny1=ynand we can choose

y1=yn = y2−yn1

h1+hn−1.

This corresponds again to the slope of the line through neighboring points.

Spline functions which are computed with these constructed derivatives are calleddefective spline functions.

Let us now interpolate again the points given in Table 4.43. Using the slopes of the neighboring points and the natural boundary conditions we obtain the defective spline of Figure4.6. We have also plotted the derivatives g andg. We see thatg (dotted line) is continuous whileg(dashed line) is discontinuous.

0 2 4 6 8 10 12 14 16 18 20

−10

−5 0 5 10 15

g(x)

g’(x) ... continuous g’’(x) −−− not continuous

Figure 4.6.Defective Spline

The question is whether it is possible to choose the derivativesyi in such a way that the second derivative g will also be continuous. We would like to have

Pi(xi+1) =Pi+1 (xi+1) for i= 1,2, . . . , n2. (4.53) Written in the local variabletwithQi(t) =Pi(xi+hit), the conditions (4.53) become

Qi(1)

h2i = Qi+1(0)

h2i+1 for i= 1,2, . . . , n2. (4.54)

If we differentiate Qiin (4.49) we get

Qi(t) =yi(6+12t)+yi+1(612t)+hiyi(4+6t)+hiyi+1 (2+6t), (4.55) and if we insert this into (4.54), we obtain

6 Equation (4.56) is a linear equation for the unknown derivatives yi, yi+1 and yi+2. We can write such an equation fori= 1,2, . . . , n2. In matrix notation, we get a linear system ofn−2 equations withnunknowns,

Ay=c, (4.57)

atridiagonal matrix with the elements bi= 1

hi and ai= 2 hi + 2

hi+1 i= 1,2, . . . , n2.

The right hand side of Equation (4.57) is

c=

where we have used the abbreviation di= yi+1−yi

h2i i= 1,2, . . . , n1. (4.58) Thus we have obtained n−2 linear equations for the unknown derivatives.

We need two more equations to determine them uniquely. Just as for the defective splines, we can ask for further boundary conditions. We consider three possibilities:

1. Natural boundary conditions: P1(x1) = Pn1(xn) = 0. These condi-tions give two more equacondi-tions:

2

Piecewise Interpolation with Polynomials 151

which are obtained from Q1(0) = Qn−1(1) = 0 using (4.55). If we add them to the system of equations (4.57), then we can compute the derivativesyiby solving a linear system of equations with atridiagonal matrix. Using the tridiagonal solver Thomas (see Algorithm 3.10 in Chapter3), we obtain the derivatives in this case with the statements h=x(2:n)-x(1:n-1); of a thin wooden spline that passes through the given points, as the following theorem shows.

Theorem 4.9. For a given set of points(xi, yi), i= 1,2, . . . , n, lets: [x1, xn]Rbe the classical cubic spline function satisfyings(xi) =yi, i= 1,2, . . . , n. Then for any twice continuously differentiable function f : [x1, xn]R satisfyingf(xi) =yi,i= 1,2, . . . , n and i.e. the spline functions(x)minimizes the energy defined by the integral in (4.61)1.

Proof. Let s(x) be the minimizer of 4xn

x1 (s(x))2dx. Every twice continuously differentiable function passing through the points (xi, yi), i= 1,2, . . . , ncan be written in the form

f(x) :=s(x) +h(x),

where R and h(x) is a twice continuously differentiable function with zeros atxi, h(xi) = 0,i= 1,2, . . . , n. The minimality condition

1The correct energy integral of a thin wooden spline would actually be4xn

x1 s(x)2/(1+

s(x)2)5/2dx, but is too difficult to treat mathematically, see [25]: “Die Extremaleigen-schaft des interpolierenden Splines wird h¨aufig f¨ur die grosse praktische N¨utzlichkeit der Splines verantwortlich gemacht. Dies ist jedoch glatter ’Volksbetrug’.”

For a fixed functionh(x), this condition is satisfied for allif and only

if xn

x1

s(x)h(x)dx= 0, (4.62) as one can see by differentiation. Integration by parts leads to

s(x)h(x)|xxn0 xn

x1

s(x)h(x)dx= 0. (4.63) Now condition (4.60) implies that the boundary term in (4.63) vanishes.

Since s(x) is constant in each interval (xi1, xi),i= 2,3, . . . , n, say equal to the constantCi, the second term in (4.63) becomes

xn sincehvanishes at the nodes, and therefore (4.62) holds, which implies

(4.61) and concludes the proof.

We see that in addition to the natural boundary conditions s(x1) = s(xn) = 0 from (4.60), the so called clamped boundary conditions s(x1) =f(x1) ands(xn) =f(xn) also lead to an energy minimizing spline among all interpolating functionsf with these slopes at the end-points. When interpolating a functionf with a spline, it is better to use clamped boundary conditions, since with free boundary conditions, the approximation order is polluted from the boundary and drops from O(h4) toO(h2). As an alternative, one can use the approach described next, which does not require the knowledge of derivatives off, but then looses the energy minimizing property toward the boundaries.

2. Not-a-knot conditionof de Boor: Here we want the two polynomials in the first two and in the last two intervals to be the same:

P1(x)≡P2(x) and Pn−2(x)≡Pn−1(x). (4.64) We get here the two equations:

1 At first sight condition (4.64) might look strange. The motivation is as follows. If the function values yi are equidistant with step-size h and belong to a smooth differentiable functionf, then according to the

Piecewise Interpolation with Polynomials 153

error estimate (4.8) we would expect an interpolation error h4. It can be shown in this case for a spline function with natural boundary conditions that the interpolation error is∼h2. The natural boundary condition is not at all natural from the viewpoint of approximating a function. In fact it is not clear why the function f that we wish to approximate should always have a vanishing second derivative at the endpoints. De Boor’s Condition (4.64) yields an approximation ∼h4 [24].

To obtain (4.65), it is sufficient to demand that the first two (respec-tively the last two) polynomials have the same third derivative. Since the third derivative of a cubic polynomial is constant, the two polyno-mials must be the same. For the first two polynopolyno-mials, the equation

P1(x2) =P2(x2)

Equation (4.66) can be added to the system (4.57) as its first equation.

Similarly, the second equation of (4.64) produces an equation that is added as the last equation to the system (4.57). Thus, we obtain once againn equations withn unknowns. Unfortunately, the matrix is no longer tridiagonal. If we wish to solve the system with a tridiagonal solver, we have to replace (4.66) by an equivalent one which contains only the unknownsy1 and y2. Denote by (I) Equation (4.66) and by (II) the first equation of system (4.57). Both equations contain the unknownsy1, y2 andy3. With the linear combination

(I) + 1 h2(II)

we eliminate the unknowny3 and obtain after the division with (h1

1+h1 which we use now instead of (4.66) to preserve the tridiagonal structure.

Similarly we obtain the second equation of (4.65).

To solve a linear system with a tridiagonal matrix we make again use of the functionThomas(see Algorithm3.10).

Note that the Matlab-function YY=spline(X,Y,XX) provides in YY, the values of the interpolating function at XX. The spline is ordinarily constructed using the not-a-knot end conditions.

3. Periodic boundary conditions: if yi are function values of a periodic function, then y1 = yn. In addition, we require that the first and second derivatives be the same:

P1(x1) = Pn−1 (xn),

P1(x1) = Pn−1 (xn). (4.68) The first condition in (4.68) is equivalent to

y1 =yn, (4.69)

and the second, when expressed inQi, becomes Q1(0)

The matrix has the form

B=

where we have again used the abbreviations bi= 1

Piecewise Interpolation with Polynomials 155

The right hand side cof the system is computed by the coefficients di defined in Equation (4.58),

c=

⎜⎜

⎜⎝

3(d1+dn−1) 3(d2+d1)

... 3(dn−1+dn−2)

⎟⎟

⎟⎠. (4.72)

Except for the two matrix elements at the bottom left and top right the matrix would be tridiagonal. In the next section we will see how to solve such a system efficiently.

Dans le document Scientifi c Computing (Page 166-174)