[Math] Finding a third degree equation that fits two points with given slopes

polynomials

I'm trying to find an easy way of getting coefficients of a third degree polynomial $y = ax^3 + bx^2 + cx + d$ with given points $(x_1,y_1)$ and $(x_2,y_2)$ the slopes are also given $k_1$, $k_2$. I have asked the question before in Stackoverflow but couldn't understand it: https://stackoverflow.com/questions/5577527/finding-a-third-degree-equation-that-fits-two-points-with-given-slopes-in-javascr.

Best Answer

Assuming $k_1$ is the slope in $x_1$ and $k_2$ is the slope in $x_2$.

Okay, say your polynomial is $y = a_3 x^3 + a_2 x^2 + a_1 x + a_0$. As shamovich already have stated you get a system of equations $Xa = y$ where $X$ is a matrix and $y$ and $a$ are vectors:

$$ \underbrace{ \begin{pmatrix} x_1^3 & x_1^2 & x_1 & 1 \\ x_2^3 & x_2^2 & x_2 & 1 \\ 3x_1^2 & 2x_1 & 1 & 0 \\ 3x_2^2 & 2x_2 & 1 & 0 \end{pmatrix} }_{=X} \underbrace{ \begin{pmatrix} a_3 \\ a_2 \\ a_1 \\ a_0 \end{pmatrix} }_{=a} = \underbrace{ \begin{pmatrix} y_1 \\ y_2 \\ k_1 \\ k_2 \end{pmatrix} }_{=y} $$

The first two rows are simply the equations $y_i = a_3 x_i^3 + a_2x_i^2 + a_1x_i + a_0$ and the last two rows are the equations $k_i = 3a_3 x_i^2 + 2a_2 x_i + a_1$.

I assume you know your points $(x_i, y_i)$ for $i = 1,2$ and $k_1, k_2$, so $X$ and $y$ are known. Solve for $x$ (you can use e.g. Matlab or Mathematica for this). As long as $x_1 \neq x_2$ there is a solution, since the determinant of the matrix is $-(x_1 - x_2)^4$.

You can derive the inverse $X^{-1}$. It has the form:

$$X^{-1} = \begin{pmatrix} \frac{2}{\left(-x_1+x_2\right){}^3} & \frac{2}{\left(x_1-x_2\right){}^3} & \frac{1}{\left(x_1-x_2\right){}^2} & \frac{1}{\left(x_1-x_2\right){}^2} \\ \frac{3 \left(x_1+x_2\right)}{\left(x_1-x_2\right){}^3} & -\frac{3 \left(x_1+x_2\right)}{\left(x_1-x_2\right){}^3} & -\frac{x_1+2 x_2}{\left(x_1-x_2\right){}^2} & -\frac{2 x_1+x_2}{\left(x_1-x_2\right){}^2} \\ -\frac{6 x_1 x_2}{\left(x_1-x_2\right){}^3} & \frac{6 x_1 x_2}{\left(x_1-x_2\right){}^3} & \frac{x_2 \left(2 x_1+x_2\right)}{\left(x_1-x_2\right){}^2} & \frac{x_1 \left(x_1+2 x_2\right)}{\left(x_1-x_2\right){}^2} \\ \frac{\left(3 x_1-x_2\right) x_2^2}{\left(x_1-x_2\right){}^3} & \frac{x_1^2 \left(x_1-3 x_2\right)}{\left(x_1-x_2\right){}^3} & -\frac{x_1 x_2^2}{\left(x_1-x_2\right){}^2} & -\frac{x_1^2 x_2}{\left(x_1-x_2\right){}^2} \end{pmatrix}$$

So $a = X^{-1} y$ (but, as always, if you are using software, it is better to use built-in solvers than using the inverse explicitly).

Explicit formulas for the coefficients one by one is given by:

$$\begin{align} a_3 &= \frac{\left(k_1+k_2\right) \left(x_1-x_2\right)-2 y_1+2 y_2}{\left(x_1-x_2\right){}^3} \\ a_2 &= \frac{-k_1 \left(x_1-x_2\right) \left(x_1+2 x_2\right)+k_2 \left(-2 x_1^2+x_1 x_2+x_2^2\right)+3 \left(x_1+x_2\right) \left(y_1-y_2\right)}{\left(x_1-x_2\right){}^3} \\ a_1 &= \frac{k_2 x_1 \left(x_1-x_2\right) \left(x_1+2 x_2\right)-x_2 \left(k_1 \left(-2 x_1^2+x_1 x_2+x_2^2\right)+6 x_1 \left(y_1-y_2\right)\right)}{\left(x_1-x_2\right){}^3} \\ a_0 &= \frac{x_2 \left(x_1 \left(-x_1+x_2\right) \left(k_2 x_1+k_1 x_2\right)-x_2 \left(-3 x_1+x_2\right) y_1\right)+x_1^2 \left(x_1-3 x_2\right) y_2}{\left(x_1-x_2\right){}^3} \end{align}$$

Related Question