# I Need help solving for X in third order polynomial

Tags:
1. Jul 31, 2017

### Dubs Mozz

Hello

I have a third order polynomial, for example y(x) = -60000x^3 - 260x^2 + 780x + 0.6

I need to know what is x at y = 28 and/or y= 32.

I can goto matlab and find the roots ( x = - .1158, -.0007, and .1122 )

or I can go to

http://www.wolframalpha.com

and it also finds the roots and even plots it for me.

I know I will have three possible x values for any y value, and given the xrange (which I will know) that might be ok, I can select which one to use, but how do I go from the roots of that poly to knowing how to solve for x when Y is a real number ?

any help, suggestions, anything ...is very much appreciated.

Thanks
Dubs

( btw - I do realize there is a thread on here that addresses this subject from 2013 but I do not understand it so linking me to it doesn't help me at this time )

2. Jul 31, 2017

### Dubs Mozz

Hello

I apologize if how I have worded my challenge is not posed properly.

I suppose I don't know how else to say this but exactly what I need to do - solve for X when Y is a real number in third order polynomial

I gave an example of the type of third order polynomial I will encounter :

y = -60000x^3 - 260x^2 + 780x + 0.6

When we calibrate in one direction, it's easy. For any given value of x, plug in the x value and calculate y.

But sometimes, we need to go in reverse - Sometimes I will be able to measure the Y value and I need to know the x values for that known y

I understand this is a cube root so for some values of Y, there may be multiple values of X.

To be exact, how would I go about solving for x when y = 30 for the equation above?

This is not homework. I've never been posed such a problem in school, this is a work problem. In school the most we ever did was "solve' the polynomial (meaning find the roots when y = 0) and plot it. That's nice but it doesn't help here. A software program must be written to do two things, find y given an x, and find x given a y.

Any help toward that end would be much appreciated.

3. Jul 31, 2017

### StoneTemplePython

I had a posting in here earlier but deleted it prior to your response as I changed my thinking a bit.

Here's a simple, naive approach. Do you know what a companion matrix is? Basically you take your polynomial $y = p(x) = -60000x^3 - 260x^2 + 780x + 0.6$ and encode it in there.

Now if you are interested in the case where y = 30, subtract 30 from your polynomial, getting:

$y = p(x) = -60000x^3 - 260x^2 + 780x + 0.6 - 30$

Now find roots of this -- i.e. eigenvalues of the associated companion matrix. This should do it, though with a bit more thought I should be able to come up with something more efficient.

4. Jul 31, 2017

### Dubs Mozz

Thank you for your suggestion of using Vandermonde matrices, but if I am mistaken, that is used when there would be multiple y values?

I wont have multiple y values, I will have one.

I will have the original calibration equation (the third order polynomial given, or one very similar) and I will have a reading off of a sensor that will be a y value, and I will need to calculate the x value.

Anyone have any ideas?

5. Jul 31, 2017

### StoneTemplePython

note: it is preferable to have your polynomial in monic form, i.e. a coefficient of one with the largest term, so write it out as
$p(x) = x^3 +\frac{260}{60000}x^2 + \frac{-780}{60000}x + \frac{-(0.6 - 30)}{60000}$

6. Jul 31, 2017

### Dubs Mozz

I have minimal experience with matrices.

I have taken data and and done second order curve fits with 3x3 matrices from scratch.

So not sure if I'm thinking of this correctly, a companion matrix for a third order poly with the y value in there would be a 5x5 matrix?

And when you say find the eigenvalues, hmmm.. not sure what that means. I'm sorry, I'm looking it up ....

7. Jul 31, 2017

### Dubs Mozz

--- I'm assuming this is when I make the companion matrix?

8. Jul 31, 2017

### Dubs Mozz

I really appreciate your taking the time to offer help.

I'm trying to find a link to describe how to solve the eigenvalues of a 5x5 matrix (the long hand way, I have to write it out step by step for code, matlab can't be used except to check that the code works) , so just want to be clear, this would be a 5x5 matrix and I solve for it's roots. Correct?

again, thank you for your input. Definitely would not have come up with this approach on my own.

9. Jul 31, 2017

### StoneTemplePython

So in general, a Companion matrix will look like this

$\mathbf C = \begin{bmatrix} 0 & 0& 0& \cdots& 0& -c_o\\ 1 & 0& 0& \cdots& 0& -c_1\\ 0 & 1& 0& \cdots& 0& -c_2\\ 0 & 0& 1& \cdots& 0& -c_3\\ \vdots & \vdots& \vdots& \ddots& \vdots& \vdots\\ 0 & 0& 0& \cdots& 1 & -c_{n-1} \end{bmatrix}$

and you have a monic polynomial in the form of $p(x) = c_0 + c_1 x + c_2 x^2 + x^3$

Notice that i.e. $c_{n-1} = c_2$ so this technically would be a 3 x 3 matrix. But if you're only interested in the question with respect to cubic polynomials, you can skip all this matrix business and look up / apply Cardando's formula.

I'm leery of you heading down the more general matrix route since you are not familiar with the term eigenvalues... note that root finding for 4x4 matrices is a bit tough and for n x n matrices where $n \geq 5$ it is quite a challenging task and not something I'd suggest doing by a long-hand approach (perhaps with the special case of doing power iteration to find a largest eigenvalue but you really should know your way around Linear Algebra quite well before contemplating such a thing) .

10. Jul 31, 2017

### Dubs Mozz

I did look up Carando's forumla, but it was my understanding that it was used to find roots, when y = 0.

I do not see how to apply Carando's formula for the problem at hand.

I'm not a total novice. I am fine solving matrix math, though higher order I'll have to look up the procedure for inverting a matrix or finding it's determinant etc

I have experience solving a Ax = B for a 3x3 matrix to find a second order polynomial from an array of x and y data. I solved for the inverse A matrix for
A' B = x to find my coefficients ( the eigenvalues? ) a, b and c to create the curve y = ax^2 + bx + c

You wrote:
" Now if you are interested in the case where y = 30, subtract 30 from your polynomial
y = p(x) = -60000x^3 - 260x^2 - 780x + 0.6 - 30
now find the roots of this ..."

When I created the 3x3 matrix from the two arrays of data, I knew how to fill it. I used sums of squares of x, and xy ,etc.

You're solution is for me to create an identity matrix from p(x) above. I see four terms. That wouldn't be a 4x4 matrix?

My familiarity with a 4x4 identity matrix has been [ 1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1 ]

As you have written it, an identity matrix multiplied by C matrix with the coefficients in it, I'm seeing that it would just multiply out to be the third order polynomial equation which I already know. Am I missing something?

The 3x3 matrix I was solving what is X, given A = XB - solving gave the coefficients of the second order polynomial.
When I solved the 3x3 matrix I made the inverse A matrix to multiply with the B matrix which I then solved for the coefficients ( eigenvalues I suppose)

What do I do with a simple identity matrix? How does this get 'solved' for eigenvalues when I have nothing to multiply it by?

Please, if I can ask a bit more, what am I solving in an identity matrix? I have no other points, just one y point.
I only have the coefficients of the third order polynomial and a Y value, I will need to find X.

Thanks.

11. Jul 31, 2017

### StoneTemplePython

So let's take a step back:

$y = p(x) = -60000x^3 - 260x^2 + 780x + 0.6$

and you were interested in the case where $y = 30$.

I suggested subtracting 30 from each side of the equation, to get

$= -60000x^3 - 260x^2 + 780x + 0.6 - 30$

now what happens when you input the following number for $x$ in each formula?

$p(-0.13151331)= ?$

setting aside numeric / rounding nits, you'll see you get 30 for the first formula and zero, i.e. a root, for the modified, second, equation. That is by subtracting 30 from each side, I transformed your problem into a root finding exercise, hence Cardano's formula can be used on the second formula. (Though, again, in general its better to have things in monic form.)

If you're only dealing with cubic equations, use a closed form like Cardano. The matrix approach involves eigenvalues and seems too far outside the scope.

12. Aug 1, 2017

### willem2

If you need to solve x^3 + ax^2 + bx = c many times for different values of c:

find all the stationary points of x^3 + ax^2 + bx, so you know where to look for the roots.
At most you have one minimum and one maximum. If c > maximum or c< minimum there will be only one root, if minimum < c < maximum, 3 roots.
Find the roots with newtons method, This will be more efficient than using Cardano's formula, wich will use newtons method for the cube root anyway.

13. Aug 1, 2017

### Dubs Mozz

Alrighty. Thank you for such a patient answer to truly bonehead question. I admire your restraint. I guess nerves of not being able to solve this makes me overlook the Captain obvious.

Much much much thanks.

14. Aug 1, 2017

### Dubs Mozz

Very good points.

Thank you very much for taking your time to offer help.

Much appreciated. What a nice community on here. It takes a village to code a reverse calibration.

15. Aug 1, 2017

### Dubs Mozz

Making progress, but not quite there--

I'm doing great...finding one root ( I need all three).

I put Cardano in Matlab and solve for x.

p = -b / ( 3* a);
q = (p*p*p) + (b*c - 3*a*d)/(6*a*a);
r = c/(3*a);

qsqr = q*q;
psqr = p*p;
rpsqrcube = (r - psqr)*(r - psqr)*(r - psqr);

insideSqrtTerm = qsqr + rpsqrcube;
% sqrtTerm = sqrt(insideSqrtTerm);
sqrtTerm = power(insideSqrtTerm, (1/2)); % result is complex number

insideCubeRoot1 = q + sqrtTerm;
insideCubeRoot2 = q - sqrtTerm;

cubeRootTerm1 = power(insideCubeRoot1, (1/3));
cubeRootTerm2 = power(insideCubeRoot2, (1/3));

x = cubeRootTerm1 + cubeRootTerm2 + p;

Works great... for one root.

Doing a + - for the results of the square root term as we do in a quadratic equation to find the roots of a second order will make no difference, so how do I get the other two roots from this formula? of course the region of the cal curve I'm interested in is in one of the other roots.

( btw - As a check I am finding the roots with Matlab )

a = -60000;
b = -260;
c = 780;
d = -30;
coeffs = [a b c d]
r = roots(coeffs)

Thanks again.
Dubs

16. Aug 1, 2017

### StoneTemplePython

I don't spend much time on cubic roots but there is a simple point here:

if you have one root, then factor it out, and now you have a quadratic you need roots for -- apply quadratic formula to it. As for how to do the actual factoring, that's up to you -- if you don't have any ideas you may want to look up synthetic division.