[Math] equation of a curve given 3 points and additional (constant) requirements

algebraic-curvesfunctions

Given 3 pairs of coordinates, $x_1, y_1, x_2, y_2, x_3, y_3$, I need a function $y(x)$ that will return the $y$ coordinate of any $x$ coordinate between $x_1$ and $x_3$ (it can be assumed that $x_1 < x_2 < x_3$).
$y(x_1)$ must equal $y_1$, $y(x_2)$ must equal $y_2$, and $y(x_3)$ must equal $y_3$.

However, it must never return a $y$ coordinate that is larger than the largest of $y_1$, $y_2$, and $y_3$, or smaller than the smallest of $y_1$, $y_2$, and $y_3$. This is the part which I have not yet figured out how to do.

(This is not schoolwork. I'm writing an audio creation library, and these curves will be used to define volume over time.)

Best Answer

What you apparently want is a monotonic interpolation scheme. As mentioned already, fitting a single polynomial to your data will result in an interpolant that does not necessarily satisfy your constraints. Using a piecewise polynomial function (a function that has a different polynomial representation at each interval) should do a better job.

Classically, the most-used class of piecewise polynomial functions is the piecewise cubic Hermite interpolant, whose representation in an interval $[x_i,x_{i+1}]$ looks like

$y_i+y_i^{\prime}\left(x-x_i\right)+c_i\left(x-x_i\right)^2+d_i\left(x-x_i\right)^3$

where

$c_i=\frac{3s_i-2y_i^\prime-y_{i+1}^\prime}{x_{i+1}-x_i}$

$d_i=\frac{y_i^\prime+y_{i+1}^\prime-2s_i}{\left(x_{i+1}-x_i\right)^2}$

$s_i=\frac{y_{i+1}-y_i}{x_{i+1}-x_i}$

and ${y_i^\prime,y_{i+1}^\prime}$ are the slopes (derivative values) of your interpolant at the corresponding points $(x_i,y_i)$, $(x_{i+1},y_{i+1})$.

Now, you will rightly ask, "how do I determine the $y_i^\prime$?" There are a number of schemes to do this: one of the most popular is the cubic spline, which imposes the additional constraint that the interpolant have two continuous derivatives. The problem with this approach is that there can still be extraneous wiggles in your interpolant.

One approach to ensure the monotonicity (i.e. the curve representing your data to fit against has no spurious wiggles) of your piecewise cubic Hermite interpolant is to use the Stineman scheme. The formula for computing slopes is a bit more elaborate, so I'll just have to refer you to the paper. As an aside, Stineman also proposes a rational interpolant you can use with the slope determination scheme in the linked paper; you can try that one instead if for some reason using a piecewise Hermite cubic is unsatisfactory.

Related Question