THE UNIVERSITY OF WESTERN ONTARIO LONDON ONTARIO

Paul Klein Office: SSC 4028 Phone: 661-2111 ext. 85227 Email: [email protected] URL: www.ssc.uwo.ca/economics/faculty/klein/

Finite element approximation

In these notes we consider function approximation where the basis functions do not have full support (are zero on most of the domain).

1 Piecewise linear

Let the grid be given by {x1, x2, . . . , xn}. Then we define the following ”tent” functions.  x − xi−1  if x ∈ [xi−1, xi]  xi − xi−1  xi+1 − x ψi(x) = if x ∈ [xi, xi+1] (1)  xi+1 − xi   0 elsewhere. These functions are illustrated in Figure1 with a grid given by {0, 1/4, 2/3, 1}. Notice that the points needn’t be evenly spaced.

One may verify that if n ˆ X f(x) = f(xi)ψi(x) i=1

1 1

φ φ φ φ 0.9 1 2 3 4

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

Figure 1: Basis functions for piecewise linear interpolation.

2 ˆ then f(x) = f(x) for each x ∈ {x1, x2, . . . , xn}. In the notation of the handout on multi-dimensional interpolation, Ψ = I.

In several dimensions, life is a little bit more complicated. Then the usual approach is to let fˆ be piecewise multilinear, i.e. on each element (rectangle), fˆ is linear in each of the dimensions separately, keeping the others constant.1

We will define a piecewise bilinear approximation for the two-dimensional case. For arbitrarily many dimensions, see the handout on multi-dimensional interpolation.

Let f be a function of two variables x1 and x2, and let the grids be given by

 1 1 1  2 2 2 j x1, x2, . . . , xn1 and x1, x2, . . . , xn2 . Write ψi for the ith tent function associ- ated with the jth grid. Then

n1 n2 ˆ 1 2 X X 1 2 1 1 2 2 f(x , x ) = f(xi , xj ) ψi (x ) ψj (x ). i=1 j=1

1 1 2 2 Notice that this is not a linear function on a rectangle [xi , xi+1] × [xj , xj+1] but that it is bilinear on such a rectangle.

A smart way of calculating the relevant sum is based on the fact that, in any given dimension, only two of the basis functions are non-zero, and these are consecutive; call them ψk and ψk+1. To find k given x you use a locating algo- rithm that essentially proceeds by bisection.

1It is possible, and for some purposes preferable, to define elementwise linear functions on multi-dimensional domains. But we won’t do that here.

3 2 Cubic splines

Piecewise linear interpolation is nice, but it yields an interpolating function that is not differentiable at the grid points. An interpolation technique that does yield differentiable approximating functions, and has other nice proper- ties as well, is the cubic spline.

Like linear interpolation, cubic spline interpolation begins with a grid of x- values {x1, x2, . . . , xn} and the value of the approximating function fb at those points: {y1, y2, . . . , yn}.

A cubic spline specifies fb to be a separate cubic function on each segment

[xi, xi+1). Each cubic function has four parameters. This means that there are 4(n − 1) parameters to determine. To do this, we impose the following conditions:

1. fb(xi) = yi; i = 1, 2, . . . , n (n conditions)

2. Continuity of fb at x2, x3, . . . , xn−1 (n − 2 conditions)

0 3. Continuity of fb at x2, x3, . . . , xn−1 (n − 2 conditions)

00 4. Continuity of fb at x2, x3, . . . , xn−1 (n − 2 conditions)

00 00 5. fb (x1) = fb (xn−1) = 0 (2 conditions).

The final two conditions can of course easily be modified to something else depending on what is suitable in the particular case at hand.

In principle, one could impose all of these conditions at once, and the result

4 would be a large linear system. However, this problem is best approached in

00 two steps. First, we impose fb(xi) = yi, continuity of fb and continuity of fb at the gridpoints. It turns out that these requirements force us to write the cubic on each interval [xi, xi+1) as

xi+1 − x x − xi fb(x) = · yi + · yi+1+ xi+1 − xi xi+1 − xi

( " 3 # " 3 #) 1 2 xi+1 − x xi+1 − x x − xi x − xi + (xi+1−xi) ai − + ai+1 − 6 xi+1 − xi xi+1 − xi xi+1 − xi xi+1 − xi

where ai; i = 1, 2, . . . , n remain to be determined. With this specification, it 00 turns out that fb (xi) = ai. We now proceed to determine these second deriva- 0 00 00 tives. We do it by imposing continuity of fb and fb (x1) = fb (xn−1) = 0. Conti- nuity of fb0 at the gridpoints boils down to

yi+1 − yi 1 1 + (xi+1 − xi)ai + (xi+1 − xi)ai+1 = xi+1 − xi 6 3

yi+2 − yi+1 1 1 = − (xi+2 − xi+1)ai+1 − (xi+2 − xi+1)ai+2 xi+2 − xi+1 3 6 for i = 1, 2, . . . , n − 2. These equations, together with a1 = an = 0 (this choice for the endpoints is called a “natural” spline), together constitute a tridiagonal linear system of equations. Specifically, we have Ma = b where   0    q2 q1   −   h2 h1   q q   3 − 2    b =  h3 h2   .   .     qn−1 qn−2   −   hn−1 hn−2    0

5 and   1 0 0 0 0 ··· 0      h1 2(h1 + h2) h2 0 0 ··· 0      1  0 h2 2(h2 + h3) h3 0 ··· 0  M =   6  ......   ......       0 0 0 0 hn−2 2(hn−2 + hn−1) hn−1    0 0 0 0 0 0 1 where qi = yi+1 − yi and hi = xi+1 − xi.

Setting a1 = an = 0 is of course a bit arbitrary. Another, equally arbitrary choice would be a1 = a2 and that an = an−1.

2.1 Not-a-knot cubic splines

n So far we have worked with nodes (or gridpoints) {xi}i=1 and until now that concept has had two distinct connotations. One is that we have data (function values) for these points, and the other is that the interpolating function fbis a on each subinterval [xi, xi+1]. Now we will change the terminology somewhat and distinguish between a node, for which we have a given function value, and a knot, where we switch from using one polynomial to another.

Notice that a knot may or may not be a node and a node may or may not be a knot.

The not-a-knot approach to cubic splines is an alternative to setting a1 = an = 0 or a1 = a2 and an = an−1. It imposes continuity of the third derivative at the nodes x2 and xn−1. This of course amounts to insisting that these nodes are

6 not really knots in the sense that the first cubic polynomial, i.e. the one valid for the interval [x1, x2), is in fact identical to the second cubic polynomial, i.e. the one valid for the interval [x2, x3), and similarly for the (n − 1)th and nth cubic . Equating the relevant third derivatives, we have −a + a −a + a 1 2 = 2 3 x2 − x1 x3 − x2 and −a + a −a + a n−2 n−1 = n−1 n xn−1 − xn−2 xn − xn−1

This modification leads to the following modified coefficient matrix of our lin- ear system Ma = b. The modified b is left as an exercise for the reader.

  −h2 h1 + h2 −h1 0 0 ··· 0      h1 2(h1 + h2) h2 0 0 ··· 0      1  0 h2 2(h2 + h3) h3 0 ··· 0  M =   6  ......   ......       0 0 0 0 hn−2 2(hn−2 + hn−1) hn−1    0 0 0 0 −hn−1 hn−2 + hn−1 −hn−2

3 Shape-preserving splines

Sometimes it is important that the interpolating function preserve the “shape” of the true function. More precisely, it should be increasing (decreasing) wher- ever the true function is increasing (decreasing) and convex (concave) wherever the true function is convex (concave). Use Schumaker(1983) with the slope ap- proximation of Butland(1980), a combination suggested by Iqbal(1992), who

7 shows that Butland’s choice of slope approximations implies that Schumaker’s conditions 3.3 for co-monotonicity are satisfied. Here is a cook-book description of Iqbal’s Butland-Schumaker algorithm.

What is given is the following: Nodes xi; i = 1, 2,...,N and function values at these nodes yi; i = 1, 2,...,N.

What we want to compute are knots zj; j = 1, 2,...,K and coefficients Aj, Bj and Cj; j = 1, 2,...,K so that the following function defines a shape-preserving quadratic interpolating spline, defined on the interval [x1, xN ]:

2 fb(x) = Aj + Bj(x − xj) + Cj(x − xj) if x ∈ [zj, zj+1)

Except by fluke, we have that K = 2N − 2 because exactly one extra knot is inserted inside each interval between adjacent nodes. But the algorithm allows for fluke and so K is determined endogenously.

1. Define “raw” slopes yi+1 − yi δi = xi+1 − xi 2. Compute slope approximations  2δi−1δi  if δi−1δi > 0 di = δi−1 + δi  0 otherwise

for i = 2, 3, . . . , n − 1, and, for the endpoints,

2x1 − x2 − x3 x1 − x3 d1 = y1 · + y2 · + (x1 − x2)(x1 − x3) (x2 − x1)(x2 − x3)

x1 − x2 +y3 · (x3 − x1)(x3 − x2)

8 and

xN − xN−1 xN − xN−2 dN = yN−2 · + yN−1 · + (xN−2 − xN−1)(xN−2 − xN ) (xN−1 − xN−2)(xN−1 − xN )

2xN − xN−1 − xN−2 +yN · (xN − xN−2)(xN − xN−1) where the latter are three-point difference approximations taken from the more general formula

0 2x − xi − xi+1 2x − xi−1 − xi+1 f (x) ≈ yi−1 + yi + (xi−1 − xi)(xi−1 − xi+1) (xi − xi−1)(xi − xi+1)

2x − xi−1 − xi +yi+1 (xi+1 − xi−1)(xi+1 − xi)

9 3. Finding the knots and coefficients

j = 0 for i = 1 : n − 1

if di + di+1 = 2δi

j = j + 1, zj = xi, Aj = yi, Bj = di, Cj = (di+1 − di)/[2(xi+1 − xi)] else

a = (di − δi)

b = (di+1 − δi) if ab ≥ 0

ξi = (xi+1 + xi)/2 else if |a| > |b|

ξi = xi+1 + a(xi+1 − xi)/(di+1 − di) else

ξi = xi + b(xi+1 − xi)/(di+1 − di) end if

di = (2δi − di+1) + (di+1 − di)(ξi − xi)/(xi+1 − xi)

ηi = (di − di)/(ξi − xi)

j = j + 1, zj = xi, Aj = yi, Bj = di, Cj = ηi/2 2 j = j + 1, zj = ξi, Aj = yi + di(ξi − xi) + ηi(ξi − xi) /2, Bj = di,

Cj = (di+1 − di)/[2(xi+1 − ξi)] end if end if end for

10 References

Butland, J. (1980). A method of interpolating reasonably-shaped curves through any data. Proc. Computer Graphics 80, 409–422.

Iqbal, R. (1992). A one-pass algorithm for shape-preserving quadratic spline interpolation. Journal of Scientific Computing 7(4), 359–376.

Schumaker, L. L. (1983, August). On shape-preserving quadratic spline inter- polation. SIAM Journal on 20(4), 854–864.

11