# How can I find out 2 unknowns in a cubic equation?

I need to give a bit of a background first, so please bare with me. I have a set of values that represent servo motor position values. By default I end up with a large set of values and I'd like to simply it.

Here's a test plot of the positions and the moving average "local extrema" points marked with white boxes:

The two graphs are for the two servos used in this particular test.

I'm currently happy with the points extracted and would like to interpolate between these. I can use linear interpolation, but the motion looks robotic and I'd like to preserve the nice easing that was recorded. I would like to use some sort of bezier interpolation to get smoother/more organic results regarding motion, but here's where I'm stuck.

I don't a strong mathematical background, so this is difficult for me. I've looked on Wikipedia at the Bezier curve equation and Hermite curve which look like what I might need.

For any 2 'keyframes'/key servo positions (marked with white) I can use the start and end points for a cubic equation, but I need to find out the 'anchor' points/other two members of the equation so I can compute a smooth interpolated value.

Can someone please explain ('dumbed down' for me if possible) how I can find 2 members of a bezier equation knowing the other two ?

Any other ideas/suggestions are welcome and I can provide more explanations and (Processing)code if needed.

-
The two middle points are called control points. You need to know something about your function inbetween the two endpoints to work these out. –  Daryl Aug 28 '12 at 22:05
Oh, I think I'm bit lost in terminology. So the the anchor points are the two points I do know (start point and end point of the curve) and the two I need to find out in between are the control points, right ? Since I've got all the positions I compute velocities (by subtracting the previous from the current position for the anchor points/positions) but I'm not how I can use that. Is that something that can help ? –  George Profenza Aug 28 '12 at 22:11
I would probably suggest, like @MUD, that cubic splines will be easier for you to approximate your functions. –  Daryl Aug 29 '12 at 1:29

You don't really need Bezier curves because you are graphing a function, not drawing a curve. You want $y = f(x)$ (which is the graph of a function) rather than $P(t) = (x(t),y(t))$ (which is a 2D curve).

But you can use what mathematicians call "real-valued" Bezier curves.

Suppose you have a data point at $(x_1, y_1)$ and the next one is at $(x_2, y_2)$. We want a smooth curve between these two points. And you say that each data point is a "turning point" (either a maximum or a minimum), so we know we want the curve to have horizontal tangents at the two points. The magic formula for the "ramp" function is:

$$y(x) = y_1 + \frac{(x - x_1)^2 (3 x_2 - x_1 - 2x) (y_2 - y_1)}{(x_2 - x_1)^3}$$

The idea here is much the same as in Ross Millikan's answer, but I'm using a diffent ramp function that's more closely related to Bezier curves.

You can easily confirm that $y(x_1) = y_1$, $y(x_2) = y_2$, $y'(x_1) = 0$, and $y'(x_2) = 0$.

In a sense, this is a Bezier curve. If we let $h = (x_2 - x_1)/3$, then the four control points of the curve are $(x_1,y_1)$, $(x_1 + h,y_1)$, $(x_2 - h,y_2)$, $(x_2,y_2)$. The middle two of these are the ones you were missing.

-

If you white dots are local extrema, the tangent is horizontal there. You might want to just put a half a sine wave between them, as it comes with the zero slope. If $(x_1,y_1)$ and $(x_2,y_2)$ are successive dots, so one is a minimum and one is a maximum, the half wavelength is $x_2-x_1$ and the full amplitude is $y_2-y_1$. Your interpolating function is then $$y=y_1+\frac 12(y_2-y_1)\left(1-\cos (\frac {x-x_1}{x_2-x_1}\pi)\right)$$

-

I suggest that instead of Bézier splines, you use interpolating cubic splines.

For cubic splines, you give the points that you want the curve to pass through, and the method finds a nice smooth piecewise-cubic curve that passes through those points.

-
That sounds exactly like what I need. I had a quick look at Hermite interpolation and I know the values for P0 and P1, but I'm not sure how I can find m0 and m1. Am I looking in the right place ? –  George Profenza Aug 28 '12 at 22:18
For Hermite interpolation, $m_0$ and $m_1$ are the slopes you want the curve to have at $p_0$ and $p_1$. I think in this case you might prefer to use the interpolating splines. Or you can choose the slopes using one of several methods, such as this one. –  MJD Aug 28 '12 at 22:24
Ok, I think I'm starting to understand: mk is the current 'knot'/control point and I know pk and pk+1. Once I've computed mk, in which equation do I use/substitute it ? I see this one but I imagine m0 and m1 are equal in this case and also see this but I'm not sure I understand the (3 k) part. Is it a vector with 2 components ? Sorry, I'm attempting to do math 'by ear' and am not familiar with many concepts. –  George Profenza Aug 28 '12 at 22:40
No, the $p_i$ are control points that the curve will pass through, and $m_i$ is the slope that the curve will have as it passes through $p_i$—that is, the direction is going on the way through. Maybe this is the point at which you should get a real book, with diagrams and careful discussion, instead of Wikipedia, which is of variable quality. –  MJD Aug 28 '12 at 23:00
Ok. So once I have pi and mi which formula do I use to get p(t) ? –  George Profenza Aug 28 '12 at 23:24