calculate cubic equation from two points and two slopes, variably

621 Views Asked by At

I'm trying to variably calculate the cubic equation between two points and the slopes at said points. (Ultimately to get an approximation of a bezier spline where i can calculate the y value from a given x value)

I have two 2-dimensional points, $(x0,y0)$ and $(x1,y1)$. They both have a slope value, lets call them $m0$ and $m1$.

From this i tried to derivate 4 linear simultaneous equations, using the basic cubic equation as well as the first derivation like this:

$y0=a*x0^3+b*x0^2+c*x0+d$
$y1=a*x1^3+b*x1^2+c*x1+d$
$m0=3*a*x0^2+2*b*x0+c$
$m1=3*a*x1^2+2*b*x1+c$

Since i have control over the points and can move $x0$ to $0$, the equations are relatively easy to solve (or so I thought):

$y0=d$ and $m0=c$. If I use these to solve for $a$ and $b$ i get

$a=(2/x1^2)*( (m0-m1)/2)+ (y0-y1)/x2)$
$b=(m0-m1)/(-2*x1) - (3*a*x1)/2$

using those to individually calculate $a$, $b$, $c$ and $d$ from any given pair of points (including slopes) should give me the optimal curve, but it only works for $m1=0$. All the other values I can change and get the expected results (except for $x0$ which was established as the fix $0$).

If it helps, I will gladly provide my step-by-step way how i got to those numbers, but for now I'm kinda stomped at where i went wrong.

Link to Desmos Graphing Calculator for Visualisation

Any help is appreciated.

1

There are 1 best solutions below

1
On BEST ANSWER

The conditions are

$$ \left\{ \begin{array}{c} a x_0^3+b x_0^2+c x_0+d=y_0 \\ a x_1^3+b x_1^2+c x_1+d=y_1 \\ 3 a x_0^2+2 b x_0+c=m_0 \\ 3 a x_1^2+2 b x_1+c=m_1 \\ \end{array} \right. $$

and solving this linear system we obtain the values $a,b,c,d$

$$ \left\{ \begin{array}{rcl} a&=& \frac{\left(m_0+m_1\right) \left(x_0-x_1\right)-2 y_0+2 y_1}{\left(x_0-x_1\right){}^3} \\ b&=& \frac{-m_0 \left(x_0-x_1\right) \left(x_0+2 x_1\right)+m_1 \left(-2 x_0^2+x_1 x_0+x_1^2\right)+3 \left(x_0+x_1\right) \left(y_0-y_1\right)}{\left(x_0-x_1\right){}^3} \\ c&=& \frac{m_1 x_0 \left(x_0-x_1\right) \left(x_0+2 x_1\right)-x_1 \left(m_0 \left(-2 x_0^2+x_1 x_0+x_1^2\right)+6 x_0 \left(y_0-y_1\right)\right)}{\left(x_0-x_1\right){}^3} \\ d&=& \frac{\left(x_0-3 x_1\right) y_1 x_0^2+x_1 \left(x_0 \left(x_1-x_0\right) \left(m_1 x_0+m_0 x_1\right)-x_1 \left(x_1-3 x_0\right) y_0\right)}{\left(x_0-x_1\right){}^3} \\ \end{array} \right. $$