[Math] Generalised formula for fitting a cubic between two points with specified slopes at each point

cubicslinear algebrapolynomials

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?

Best Answer

Your polynomial is some $p(x)=ax^3+bx^2+cx+d$, $p'(x)=3ax^2+2bx+c$, with four unknown parameters $a,b,c,d$.

Two points give two equations $p(x_i)=y_i$, two slopes two more: $p'(x_i) = m_i$.

In matrix form: $$ A= \left( \begin{matrix} 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{matrix} \right) \quad x= \left( \begin{matrix} a \\ b \\ c \\ d \end{matrix} \right) \quad b= \left( \begin{matrix} y_1 \\ y_2 \\ m_1 \\ m_2 \end{matrix} \right) \\ Ax=b \\ x = A^{-1} b $$ Then for $x_1 \ne x_2$: $$ \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ x_2^3 & x_2^2 & x_2 & 1 & y_2 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ 3x_2^2 & 2x_2 & 1 & 0 & m_2 \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ 3(x_2^2 - x_1^2) & 2(x_2 - x_1) & 0 & 0 & m_2 - m_1 \\ x_2^3 & x_2^2 & x_2 & 1 & y_2 \\ \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ 3(x_2^2 - x_1^2) & 2(x_2 - x_1) & 0 & 0 & m_2 - m_1 \\ x_2^3 - x_1^3 & x_2^2 - x_1^2 & x_2 - x_1 & 0 & y_2 - y_1 \\ \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ 3(x_2^2 - x_1^2) & 2(x_2 - x_1) & 0 & 0 & m_2 - m_1 \\ x_1^2 + x_1 x_2 + x_2^2 & x_1 + x_2 & 1 & 0 & \frac{y_2 - y_1}{x_2 - x_1} \\ \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ 3(x_2^2 - x_1^2) & 2(x_2 - x_1) & 0 & 0 & m_2 - m_1 \\ -2 x_1^2 + x_1 x_2 + x_2^2 & x_2 - x_1 & 0 & 0 & \frac{y_2 - y_1}{x_2 - x_1} - m_1 \\ \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ 3(x_2^2 - x_1^2) & 2(x_2 - x_1) & 0 & 0 & m_2 - m_1 \\ -4 x_1^2 + 2 x_1 x_2 + 2 x_2^2 & 2(x_2 - x_1) & 0 & 0 & 2\frac{y_2 - y_1}{x_2 - x_1} - 2m_1 \\ \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ x_1^2 - 2 x_1 x_2 + x_2^2 & 0 & 0 & 0 & m_1 + m_2 - 2\frac{y_2 - y_1}{x_2 - x_1}\\ -4 x_1^2 + 2 x_1 x_2 + 2 x_2^2 & 2(x_2 - x_1) & 0 & 0 & 2\frac{y_2 - y_1}{x_2 - x_1} - 2m_1 \\ \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ 3 (x_2^2-x_1^2) & 2(x_2 - x_1) & 0 & 0 & m_2 - m_1 \\ x_1^2 - 2 x_1 x_2 + x_2^2 & 0 & 0 & 0 & m_1 + m_2 - 2\frac{y_2 - y_1}{x_2 - x_1}\\ \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ \frac{3}{2} (x_1+x_2) & 1 & 0 & 0 & \frac{m_2 - m_1}{2(x_2-x_1)} \\ (x_1 - x_2)^2 & 0 & 0 & 0 & m_1 + m_2 - 2\frac{y_2 - y_1}{x_2 - x_1}\\ \end{array} \right] \to \left[ \begin{array}{cccc|c} x_1^3 & x_1^2 & x_1 & 1 & y_1 \\ 3x_1^2 & 2x_1 & 1 & 0 & m_1 \\ \frac{3}{2} (x_1+x_2) & 1 & 0 & 0 & \frac{m_2 - m_1}{2(x_2-x_1)} \\ 1 & 0 & 0 & 0 & \frac{m_1 + m_2 - 2\frac{y_2 - y_1}{x_2 - x_1}}{(x_1 - x_2)^2} \\ \end{array} \right] $$ This gives: \begin{align} a &= \frac{m_1 + m_2 - 2\frac{y_2 - y_1}{x_2 - x_1}}{(x_1 - x_2)^2} \\ b &= \frac{m_2 - m_1}{2(x_2-x_1)} - \frac{3}{2} (x_1+x_2) a \quad (*) \\ c &= m_1 - 3x_1^2 a - 2x_1 b \\ d &= y_1 - x_1^3 a - x_1^2 b - x_1 c \end{align}

The last two equations seem obvious in hind sight.

Test:

octave:1> x1 = 2
x1 =  2
octave:2> x2 = 5
x2 =  5
octave:3> m1 = 3
m1 =  3
octave:4> m2 = -1
m2 = -1
octave:6> A = [x1**3, x1**2, x1, 1; x2**3, x2**2, x2**1, 1;
3*x1**2,2*x1,1,0; 3*x2**2,2*x2,1,0]
A =
     8     4     2     1
   125    25     5     1
    12     4     1     0
    75    10     1     0

octave:17> y1=-2
y1 = -2
octave:18> y2=5
y2 =  5
octave:19> b = [y1;y2;m1;m2]
b =
  -2
   5
   3
  -1

octave:20> x = inv(A)*b
x =
  -0.29630
   2.44444
  -3.22222
  -2.96296

Now we compare this with equation $(*)$

octave:22> a = (m1+m2-2*(y2-y1)/(x2-x1))/(x1-x2)**2
a = -0.29630
octave:23> b = (m2-m1)/(2*(x2-x1))-(3/2)*(x1+x2)*a
b =  2.4444
octave:24> c = m1-3*(x1**2)*a -2*x1*b
c = -3.2222
octave:25> d = y1-(x1**3)*a -(x1**2)*b-x1*c
d = -2.9630
Related Question