Understanding Akima

The Akima method has stood the test of time and continues to be one of the most cited curve drawing algorithms.  This post is to remind me how it works and to mention a modification, called makima, available in Matlab.

Akima's method is one of the family of piecewise cubic interpolants which as we have seen, involve an estimate of the slope at each data point.  To explain the method, and to keep things simple, we look at the slope estimate at the third data point (x3, y3), which depends on its 4 neighbours, 2 each side. Let mi be the slope of the chord joining (xi, yi) to (xi+1, yi+1).  Then Akima estimates the slope, s3 as a weighted average of m2 and m3, the two chords meeting at (x3, y3).  The weights w1 and w2 are chosen as follows. Let a1 = abs(m4 - m3) and a2 = abs(m2 - m1), and let b = a1 + a2.  Then we set w1 = a1/b and w2 = a2/b.  This gives the weighted average estimate: 

s3 = w1 * m2 + w2 * m3        (1)

Akima in his 1970 paper provides a geometric derivation.  But intuitively we notice the relative weighting depends on the difference in the two chord slopes on either side.  A big difference in the left pair relative to the difference in the right, gives less weight to the left chord - presumably because then less confidence can be had in the corresponding slope? 

We notice also that if m3 = m4 and m1 = m2, then w1 and w2 are not defined.  In this situation, Akima simply sets the weights equal, i.e. an average of the two slopes is chosen.  Thus in this case: 

s3 = 0.5 * (m2 + m3)        (2)

Recently Matlab has introduced a variation on Akima's method.  This is called Makima (Modified Akima) and was introduced in Matlab 2019b.  It is described in a blogpost by Cosmin Ionita in Cleve's Corner: see https://blogs.mathworks.com/cleve/2019/04/29/makima-piecewise-cubic-interpolation/.

This notes the following unsatisfactory feature of Akima's method.  In the case where the weights are not defined, because the two pairs of slopes either side of the data point are equal, the averaging formula (2) is unstable - small changes to the data to make the slopes no longer equal will cause a switch to formula (1) and a quite different estimate of the slope.  

To see this, assume for simplicity all slopes are positive, and let m4 = m3 = p, and m2 = m1 = q.  The averaging formula gives s3 = 0.5*(p+q).  Now add eps to y4 so that m4 = p - eps and m3 = p + eps.  Then in the above notation, a1 = p - 2*eps and a2 = 0, with b = p - 2*eps.  Then w1 = (p - 2*eps)/(p - 2*eps) = 1 and w2 = 0.  Thus from (1), s3 = q.  So the estimate of slope changes from 0.5*(p+q) to q.

Ionita demonstrates the problem with the following example dataset:

x 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0
y -1.0 -1.0 -1.0 0.0 1.0 1.0 1.0 1.0

Here is the Akima plot.  Notice at x=5, the pairs of slopes either side are equal (slopes = 1 on left, and 0 on right).  Akima then selects the average slope, 0.5, at x=5.  

But then if we make a small perturbation so that the slopes are not equal, Akima's method uses its normal formula rather then the average.  We can see the difference if we perturb the y-value at x=6 by adding eps, the machine precision, so the value of y is 1+eps.  Here is the result:

Notice the steeper slope at x=5 in the perturbed case. Indeed it appears that the slope is equal to 1, rather than 0.5 for the unperturbed case - as predicted in the analysis above, when we saw the estimate was q.  This is exhibited in the form of an exaggerated overshoot at x=5.

Interestingly, if we perturb the value at x=4 by adding eps, we get a quite different curve again, as shown below.  The perturbation this time causes the slope at x=5 to be zero - that is, in the analysis above, the slope is now estimated as p, because the perturbation is to the left of x=5 rather than the right!

An obvious follow-on question is: what happens if we perturb the y-value at both sides, ie at x=4 and x=6.  Following the same analysis as above, we get a1 = 2*eps and a2 = 2*eps, so w1 = 0.5 and w2 = 0.5.  So the Akima formula reduces to the averaging formula!  Here is the plot we get when we add eps to the y-values at x=4 and x=6:

As expected, the slope at x=5 is 0.5, and the plot is identical to that produced by the Akima Method when the pairs of slopes are precisely equal (top figure above)..

In the next post, we'll have a look at the Makima Method and how it overcomes the instability of the original method.

Note that for these plots we save the PDF as 5.35x5.88 portrait style, and edit the width and height of the inserted iframe to be 535x588.


Comments

Popular posts from this blog

Piecewise Linear Interpolation of Cubic Polynomial

Wiggins Methods

Curve Drawing Over the Years