hermite.m
(Hermite polynomial interpolation)

Suppose we would like to fit a polynomial through six points on a plane. We can show using linear algebra (or Lagrangian interpolation) that this can be done exactly with a polynomial with six coefficients (i.e. 5th order). Now suppose we also want the polynomial to satisfy prescribed derivatives (slopes) at these six points. It turns out that this can be done with a polynomial with 12 coefficients. And if we also want the polynomial to satisfy prescribed second derivatives at these six points, then this will require a polynomial with 18 coefficients (and so on for more derivatives). When one or more derivatives are prescribed, this type of polynomial interpolation is often called Hermite interpolation.

Consider the slightly simpler problem of fitting a polynomial through only three points (x1,y1) (x2,y2) (x3,y3) while also having derivatives y1' y2' y3' at these same points. According to what I have said above, this should require a polynomial with six coefficients, which I will represent as:

      y = a + b x + c x2 + d x3 + e x4 + f x5

Note that the derivative of this polynomial is:

      y' = b + 2c x + 3d x2 + 4e x3 + 5f x4

Now the requirement that the point (x1,y1) satisfies the first equation above can be written:

      y1 = a + b x1 + c x12 + d x13 + e x14 + f x15

And of course, we have two more similar equations for the second and third points.

The requirement that the first point has a derivative of y1' satisfying the second equation above can be written:

      y1' = b + 2c x1 + 3d x12 + 4e x13 + 5f x14

And like before the second and third points give us two more similar equations.
So we have six equations in all which can be written in matrix form as follows:

1 x1 x12 x13 x14 x15
1 x2 x22 x23 x24 x25
1 x3 x32 x33 x34 x35
0 1 2 x1 3 x12 4 x13 5 x14
0 1 2 x2 3 x22 4 x23 5 x24
0 1 2 x3 3 x32 4 x33 5 x34
  *  
a
b
c
d
e
f
  =  
y1
y2
y3
y1'
y2'
y3'

Using the notation used in the herm( ) function at the end of hermite.m the blue matrix is called [A; B] where A and B are the top and bottom halves of the matrix. The green matrix is called p and the red matrix is called [y; y'] (again separating the top and bottom halves).

So the matrix equation can be written:

      [A; B] * p = [y; y']

and using Matlab's backslash notation, the solution to this equation is:

      p = [A; B] \ [y; y']


The hermite application fits a polynomial through six points on the plane, also satisfying six derivatives at those same points. The points we want the polynomial to go through are represented by the tails of the white arrows and the direction of the arrows represent the derivatives (i.e. slopes) at each point. (The length of the arrow has no significance, so if you grab the head of an arrow and make it longer, the interpolated polynomial will not change.)

This is what the application looks like when it is first started. Some on-screen text is included to give you basic instructions on how to use the application. These instructions will disappear when you start to move either end of one of the arrows.


Here I have clicked on the head of the last arrow. At first, the usual plus-sign shaped cursor will appear at the end of the arrow. But then after clicking on the "Modify" button, the cursor will change to the diamond shape shown here, which indicates you can drag that point to a new location. (If the persistent edit mode checkbox below the plot had been checked, then the diamond shape cursor would appear as soon as you clicked on the arrowhead... so no need for the Modify button.)


Now I have moved the arrowhead downward (i.e. a more negative slope). I also made the arrow longer, but the length of the arrow is not significant. Notice how the shape near the end of the curve has changed to ensure that the curve is still tangent to the arrow. In this case, the rest of the curve did not change noticeably, although often a small change to an arrowhead may drastically change the shape of the whole curve.


Next, I clicked on the tail of that same vector. After clicking "Modify" the diamond cursor appears. Then I dragged the diamond cursor slightly upwards. Again the shape of the curve changes so that it now passes through the new arrow tail location.

This application demonstrates several programming techniques, including the use of the Pquiv auxiliary function to draw arrows on a plot as well as the 'MotionEdit' parameter to allow the display to update as the arrow is being moved around. The whole application consists of just 28 lines of code (not counting comment lines or the on-screen comments). The fact that a sophisticated application can be created so concisely is a testament to both the expressive power of Matlab as well as the advanced features built into the plt plotting routine.

Copyright © 2024
Paul Mennen