Generalised formula for fitting a cubic between two points with specified slopes at each point As the question says, I'd like to fit a cubic between two points, $(x_1,y_1)$ and $(x_2,y_2)$.  The reason I need a cubic is that I want to specify the first differential at each point.  I can see instances when there would be an infinite number of solutions - for example, if $y_1 = y_2$, $x_2 > x_1$, and the slope at $(x_1,y_1)$ has the opposite sign to that at $(x_2,y_2)$, then a quadratic could do the job; however, if - for example - $x_2 > x_1$, $y_2 > y_1$, the slope at $(x_1,y_1)$ is positive and the slope at $(x_2,y_2)$ is negative, then I think only a cubic can do the job. More generally, I think that if a straight line drawn through $(x_1,y_1)$ with the specified slope would go through $(x_2,y_2)$ but the corresponding line drawn through $(x_2,y_2)$ would not go through $(x_1,y_1)$, then it would need to be a cubic.
It is possible (I believe) to take some simultaneous equations and solve your way though them to find the parameters for the cubic...but I wanted to see if anyone else has already done this and can share the answer! Thoughts?
 A: Look at interpolating with cubic splines
A: Consider a spline segment from $x=0 \ldots h$. If the end points are $Y_1$ and $Y_2$ and the end slopes $\dot{Y}_1$ and $\dot{Y}_2$ then the curve can be defined as:
$$ \begin{align} y(x) & = \left( 2 \left(\tfrac{x}{h}\right)^3-3 \left(\tfrac{x}{h}\right)^2+1 \right) Y_1 \\
& + \left( \left(\tfrac{x}{h}\right)^2 \left(3 - 2 \tfrac{x}{h} \right) \right) Y_2 \\ & +  x \left( \left(\tfrac{x}{h}\right)^2-2 \left(\tfrac{x}{h}\right)+1 \right) \dot{Y}_1 \\ &+  \tfrac{x^2}{h} \left(\left(\tfrac{x}{h}\right)-1\right) \dot{Y}_2
\end{align} $$
To go between slopes and curvatures $\ddot{Y}_1$ and $\ddot{Y}_2$ solve this system of equations:
$$\begin{pmatrix} \dot{Y}_1 \\ \dot{Y}_2 \end{pmatrix} = \begin{vmatrix} -\tfrac{1}{h} & \tfrac{1}{h} \\ -\tfrac{1}{h} & \tfrac{1}{h} \end{vmatrix} \begin{pmatrix} Y_1 \\ Y_2 \end{pmatrix} + \begin{vmatrix} -\tfrac{h}{3} & -\frac{h}{6} \\ \tfrac{h}{6} & \tfrac{h}{3} \end{vmatrix} \begin{pmatrix} \ddot{Y}_1 \\ \ddot{Y}_2 \end{pmatrix} $$
