# Given four points on a cubic function curve, how can I find the curve's function?

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.)

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?

-
If the points are not evenly spaced, you need to scale the equations. After removing +(d-d) divide the first equation by (m-k), the second by (n-m), and the third by (p-n), and the subtraction will make the c's cancel. This is en.wikipedia.org/wiki/Gaussian_elimination –  Ross Millikan Sep 6 '12 at 18:40

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.

-
The identity $(r^n - s^n) = (r-s)\sum_{k=0}^{n-1} r^k s^{n-1-k}$ used repeatedly will be more or less all you need as you proceed this way. –  Eugene Shvarts Jul 10 '12 at 2:10
As an additional note: the matrix underlying the system of equations that Mark solved here is called a Vandermonde matrix. One is implicitly solving a Vandemonde system when one does polynomial interpolation. –  Guess who it is. Jul 10 '12 at 14:04
Thanks for the great answerr, but I got stuck following it. I've edited with the question with where I got stuck and a brief digression about why I'm asking this. –  billpg Sep 6 '12 at 18:29
I also got stuck, it's trickier to eliminate the cs when the x inputs are not equidistant. –  Mooing Duck Dec 10 '14 at 0:42

You can use Lagrange interpolation and expand it out.

-
...or the Newton form, or any number of equivalent forms... –  Guess who it is. Jul 10 '12 at 0:08

pick four (x,y)coordinates (imaginary or useful) that you want it all to get to.Enter the matrix into your calculator. ie: for (2,4)(3,6)(5,12)(6,17) you would enter 8,4,2,1,4 for your first y equation. then the second row (3,6)of the matrix will follow the pattern of the first point (2,4).The the rest of your matrix will follow the same pattern.Then rref it and you will get the values of a, b, c, d.

-