1
$\begingroup$

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.

  • 0
    In English they are called "polynomials" (singular "polynomial"), not "polynoms" (fake singular "polynom").2011-04-07
  • 1
    I wasn't implying anything of the sort; it seems a lot of foreigners translate it as "polynom", and was letting you know. But it seems you rather proclaim to the four winds that the issue is that you are just too lazy to bother typing the word correctly, so thanks for that "clarification."2011-04-07

2 Answers 2

4

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}$$

  • 0
    $a_3, a_2, a_1, a_0$ are what in the question are called $a, b, c, d$ (in that order).2011-04-07
  • 0
    If you have *Mathematica* to begin with, Hermite interpolation (which is what the OP apparently wants) is easy with `InterpolatingPolynomial[]`. Of course, solving the (confluent) Vandermonde system is the most straightforward if you're deriving by hand. On the other hand, the solution looks neater if you express the polynomial as powers of $(x-x_1)$ or $(x-x_2)$, whatever your preference may be.2011-04-10
2

Basically what they've told you at StackOverflow is that to find $a,b,c,d$ you need to solve the system of linear equations:

$a x_1^3 + b x_1^2 + c x_1 +d = y_1$

$a x_2^3 + b x_2^2 + c x_2 +d = y_2$

$3 a x_1^2 + 2 b x_1 + c = k_1$

$3 a x_2^2 + 2 b x_2 + c = k_2$

  • 0
    No I dont want that formula. The first answer is what I need. The other is to cumbersome.2011-04-07
  • 0
    Isn't there any formula for this?2011-04-07
  • 0
    Can you get the coeffecients formulas solving the above equations? I have trouble with it2011-04-07
  • 0
    So do you or don't you want the formula? I don't understand...2011-04-07
  • 0
    Well I want the formula for the coefficients, but also how you derive it2011-04-07
  • 0
    The formula is a bit complex to write down properly here. But actually since you know $(x_1,y_1)$, $(x_2,y_2)$ and $K1,k2$, the system of equations described above is just a linear system. It can be solved using any math software, for example Mathematica. Use the function "LinearSolve"2011-04-07
  • 0
    If you look at the first answer in StackOverflow is it correct? I dont know what he did. Shamovic, I'm creating an application that you might be using in the future, for that I need the formula for the coefficients, I'm a programmer and designer I don't use Mathematica. But could you please post the formula somewhere? I really need it2011-04-07
  • 0
    The formula will be used in javascript code2011-04-07
  • 0
    http://www.js-x.com/page/javascripts__example.html?view=10682011-04-07