[Math] Given four points on a cubic function curve, how can I find the curve’s function

interpolationpolynomials

Say I have a curve
$$y = ax^3 + bx^2 + cx + d.$$

I don't know $a$, $b$, $c$ or $d$, but I do know the $(x,y)$ values of four points on this curve.

How can the values of $a$, $b$, $c$ and $d$ be calculated? (Preferably in terms understandable by a novice.)


Follow-up to MJD's answer.

Hello. Many thanks for the informative answer but I'm stuck following it.

(A brief digression about where I'm coming from in asking this question.)

I'm an amateur video-maker and I want to show a oscilloscope-esque monitor with a changing cubic curve. To do this, I'd write a computer program that randomly picks four points and calculates a cubic curve which goes through those points. The scope quickly changes, with each frame having moved each of the four points randomly a little bit and the curve redrawn.

During the play, the hero discovers where one of the points on the curve should be and fixes that point. The other three points continue to move but the curve always remains going through that fixed point.

Later on, the hero discovers another point and fixes it. Now, the curve containues to wobble about but remains fixed at those two points.

The hero continues to discover all four points and finally the curve is locked and saves the day.

(End of digression)

For my program to work, I need to write a function that takes four x/y points (fixed or floating, doesn't matter) and returns the a,b,c and d values that describe that cubic curve.

Going back to MJD's answer, I followed the steps as outlined below. For the x/y inputs, I'm using eight letters rather than using MJD's sub-scripts as I can type these.

Here's the four starting equations. a,b,c,d are unknown. (k,f),(m,g),(n,h),(p,j) are known.

f = ak³ + bk² + ck + d 
g = am³ + bm² + cm + d  
h = an³ + bn² + cn + d  
j = ap³ + bp² + cp + d  

For each equation (except the first), subtract the one above.

(g-f) = am³ - ak³ + bm² - bk² + cm - ck + d - d  
(h-g) = an³ - am³ + bn² - bm² + cn - cm + d - d  
(j-h) = ap³ - an³ + bp² - bn² + cp - cn + d - d  

Simplify.

(g-f) = a(m³-k³) + b(m²-k²) + c(m-k) + (d-d)  
(h-g) = a(n³-m³) + b(n²-m²) + c(n-m) + (d-d)  
(j-h) = a(p³-n³) + b(p²-n²) + c(p-n) + (d-d) 

Remove +(d-d) as this must be zero.

(g-f) = a(m³-k³) + b(m²-k²) + c(m-k)  
(h-g) = a(n³-m³) + b(n²-m²) + c(n-m)  
(j-h) = a(p³-n³) + b(p²-n²) + c(p-n)  

Doing great so far! Now do the same with the three remaining equations to leave two.

(h-g)-(g-f) = a(n³-m³) - a(m³-k³) + b(n²-m²) - b(m²-k²) + c(n-m) - c(m-k)  
(j-h)-(h-g) = a(p³-n³) - a(n³-m³) + b(p²-n²) - b(h²-m²) + c(p-n) - c(n-m)  

Simplify.

(f-2g+h) = a(k³-2m³+n³) + b(k²-2m²+n²) + c(k-2m+n)  
(g-2h+j) = a(m³-2n³+p³) + b(m²-2n²+p²) + c(m-2n+p)  

Here, I'm meant to eliminate the appearances of the unknown 'c' from these equations because in each case, c is being multiplied by zero. However, this step only works because of the x values chosen in the original answer; 1,2,3,4. Both (3-2*2+1) and (4-2*3+2) happen to equal zero.

How can I proceed from this point if my x values are not uniformly spaced?

Best Answer

Say the four points are $(x_1, y_1)\ldots (x_4, y_4)$.

You want $a,b,c,d$ to satisfy the four equations: $$y_1 = ax_1^3 + bx_1^2 + cx_1 + d\\ y_2 = ax_2^3 + bx_2^2 + cx_2 + d\\ y_3 = ax_3^3 + bx_3^2 + cx_3 + d\\ y_4 = ax_4^3 + bx_4^2 + cx_4 + d$$

Do you know how to solve four linear equations in four unknowns?


I will work an example, since it is not hard to pick up. Let's say we have points $(1,3), (2,9), (3,27), (4,63)$. Then we get the four equations:

$$\begin{eqnarray} 3 &=& a + b + c + d\\ 9 &=& 8a + 4b + 2c + d\\ 27 &=& 27a+ 9b + 3c + d\\ 63 &=& 64a + 16b + 4c + d \end{eqnarray}$$

We start by subtracting each equation from the following equation, which eliminates all the $d$'s:

$$\begin{eqnarray} 6 &=& 7a + 3b + c \\ 18 &=& 19a + 5b + c \\ 36 &=& 37a+ 7b + c \end{eqnarray}$$

Again we subtract each equation from the following one, which eliminates the $c$'s:

$$\begin{eqnarray} 12 &=& 12a + 2b \\ 18 &=& 18a + 2b \end{eqnarray}$$

We could subtract again, giving $6a=6$, so $a=1$, or we can solve this pair of equations by inspection: evidently $a=1$ and $b=0$.

Then we substitute $a=1$ and $b=0$ back into one of the previous batch of equations, say $6 = 7a + 3b + c$, giving $6 = 7 + c$ and then $c=-1$.

Then we substitute $a=1, b=0, c=-1$ into one of the original equations, say $3 = a + b + c + d$, giving $3 = 1-1+d$ and so $d=3$.

Now we have $a=1, b=0, c=-1, d=3$, so the cubic polynomial we found is $y= x^3 -x + 3$, which is correct.