Finding the equation of a quartic given its turning points

I'm trying to produce some generalised code to find the equation of a polynomial, given its turning points (and order). I know that for a cubic this can be done using Gaussian Elimination to solve a system of simultaneous equations. For example:

Given the turning points $$[-1, 2]$$ and $$[3, -5]$$, find the function of the form $$f(x) = ax^3 + bx^2 + cx + d.$$



First, we can quite simply get the equations $$2 = -a + b -c+ d\\ -5 = 27a + 9b +3c + d$$

Combining these we get $$2-(-5) = (-1-27)a + (1-9)b + (-1-3)c\\ 7=-28a-8b-4c$$

We also know that $$f'(x) = 3ax^2 + 2bx + c$$

Giving us $$0=3a-2b+c\\ 0=27a+6b+c$$

From the equations $$7=-28a-8b-4c\\ 0=3a-2b+c\\ 0=27a+6b+c$$

We can get the matrix

$$\begin{bmatrix} \left.\begin{matrix} -28 & -8 & -4 \\ 3 & -2 & 1\\ 27 & 6 & 1 \end{matrix}\right|\begin{matrix} 7\\0\\0 \end{matrix} \end{bmatrix}$$

Using Gaussian Elimination (http://onlinemschool.com/math/assistance/equation/gaus/) we can reduce this to

$$\begin{bmatrix} \left.\begin{matrix} 1 & 0 & 0 \\ 0 & 1 & 0\\ 0 & 0 & 1 \end{matrix}\right|\begin{matrix} -3.0625\\2.625\\14.4375 \end{matrix} \end{bmatrix}$$

Which tells us that $$a=-3.0625, b=2.625$$ and $$c=14.4375$$. We can then plug these into $$f(x)$$ using the point $$[-1,2]$$ to find that $$d=10.75$$. We can verify this is correct by testing $$f(3)$$, which then comes out with a value of $$-5$$ as expected.

As I said at the top, I want to generalise this method to work for any polynomial of order n. I know my code for the Gaussian Elimination works correctly, since given the same inputs, it produces the same outputs as the calculator I linked. My issue seems to be in setting up the initial matrix. Take the following example where I try to apply the same methods to a quartic equation.

Given the turning points $$[1,2], [2,-5]$$ and $$[3,-3]$$, find the function of the form $$f(x) = ax^4 + bx^3 + cx^2 + dx + e$$

Following a similar approach as before, we can find $$2 = a + b + c + d + e \ \ \ (A)\\ -5 = 16a + 8b + 4c + 2d + e\ \ \ (B)\\ -3 = 81a + 27b + 9c + 3d + e\ \ \ (C)$$

My thought to combine these into a single equation would be to do $$2(A) - (B) - (C)$$, thus eliminating $$e$$ and giving us $$12 = -95a - 33b - 11c - 3d$$

From $$f'(x)$$, we also know $$0 = 4a + 3b + 2c + d\\ 0 = 32a + 12b + 4c + d\\ 0 = 108a + 27b + 6c + d\\$$

Putting these 4 equations into matrix form, we get

$$\begin{bmatrix} \left.\begin{matrix} -95 & -33 & -11 & -3 \\ 4 & 3 & 2 & 1\\ 32 & 12 & 14 & 1\\ 108 & 27 & 6 & 1 \end{matrix}\right|\begin{matrix} 12 \\ 0 \\ 0 \\ 0 \end{matrix} \end{bmatrix}$$

Again, performing Gaussian Elimination, either using the calculator I linked above, or my own code, this comes out as

$$\begin{bmatrix} \left.\begin{matrix} 1 & -0 & 0 & 0 \\ 0 & 1 & 0 & 0\\ 0 & 0 & 1 & 0\\ 0 & 0 & 0 & 1 \end{matrix}\right|\begin{matrix} -12 \\ 96 \\ -264 \\ 288 \end{matrix} \end{bmatrix}$$

So we have $$a=-12, b=96, c=-264$$ and $$d=288$$. If we plug the point $$[1,2]$$ into $$f(x)$$, we can find $$d=-106$$. This is where the problem arises. If I then try to find $$f(2)$$, I would expect to find a value of $$-5$$, but instead, I get a value of $$-10$$. Similarly, I would expect to find $$f(3)=-3$$, but instead I get $$f(3)=2$$.

I am certain the Gaussian Elimination is working, so I know the problem must lie with the equations I'm using to produce the initial matrix. Can somebody see what I'm doing wrong?

• At the begining you have 6(!) equations vor 5 coefficients. You took 5 equations to calculate all the coefficients. The last equation may be satisfied only by chance. – Jens Schwaiger Oct 19 '18 at 3:29