# Solving a 4th order polynomial [closed]

I have a fourth order polynomial of the form: $$y = a_{0} + a_1 x + a_{2}x^{2} + a_{3}x^{3} + a_{4}x^{4}$$ Where the coefficients $a_{0}$, $a_{1}$, $a_{2}$, $a_{3}$ and $a_{4}$ are determined from the result of a fit, and are real numbers. How can I solve this equation in terms of x where $y$ is both real and positive? $x$ should be real, but not necessarily positive.

## closed as unclear what you're asking by MarcoB, José Antonio Díaz Navas, Coolwater, m_goldberg, swishMay 1 '18 at 17:34

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Complex solutions come in pairs, so if there is a real root, there must be a second (and possibly a third and fourth). Which one do you want? – John Doty Apr 23 '18 at 23:14
• @JohnDoty Thanks for the reply, I guess all of them and I'd like to use test them so I can make a prediction for $x$ based on what $y$ is, which is known to me. – Q.P. Apr 23 '18 at 23:21
• Do you want to solve for x or "in terms of x"? Isn't y already in terms of x? – Michael E2 Apr 23 '18 at 23:24
• @MichaelE2 I want to solve for x. – Q.P. Apr 23 '18 at 23:30
• Maybe Solve[poly == y && y > 0 && x \[Element] Reals, x]? – Michael E2 Apr 23 '18 at 23:30

There's nothing wrong with the Solve::ratnz message per se. It's just notifying the user of how the solution was computed. Since Solve is purportedly an exact solver, it notifies the user that some rounding may have occurred in computing the solution, in case the user wishes to control the rounding themselves.

Here is a constructed example, which can be analyzed, since it is known what function is being approximated (Exp[x]):

p = InterpolatingPolynomial[{#, Exp@N@#} &[{-2., -1.5, 0., 1.5, 2.}] // Transpose, x];
datarange = {-2., 2.};
sols = x /. Solve[p == y && y > 0 && datarange[[1]] < x < datarange[[2]], x];
Plot[{Log[y], sols} // Flatten // Evaluate, {y, Exp[-2], Exp[2]}]


Solve::ratnz: Solve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.

Now a degree-4 equation $p(x) = y$ is going to have, generically, 0, 2, or 4 real roots, and never just 1 except at one value of y, namely an absolute extremum of $p(x)$. Hence, I used datarange[[1]] < x < datarange[[2]] to exclude a spurious solution. Here datarange is an interval of feasible values for x. Implicitly, I'm assuming that extrapolation is to be avoided, and datarange was chosen to be the range of the x coordinates in the data. If extrapolation is acceptable, then datarange may be set to an appropriate interval.

Depending on the fit of p to the data, it's possible for p not to be monotonic over the interval of interest, which presents problems since there will be more than one prediction for x given y. In such a case, the research may have to examine thoroughly the situation and determine any number of things, including the suitability of the fitted polynomial.

Get some coefficients:

{a0, a1, a2, a3, a4} = RandomReal[1, 5]
(* {0.59997, 0.0773325, 0.466742, 0.106902, 0.227533} *)


Define a function:

x[y_] := x /. NSolve[y == a0 + a1 x + a2 x^2 + a3 x^3 + a4 x^4, Reals]


Try it:

x[5.5]
(* {-2.04995, 1.82863} *)


Two solutions at y==5.5 for these coefficients. Plot the first:

Plot[x[yy][[1]], {yy, 1, 10}]


To plot the second, change [[1]] to [[2]]. If the number of solutions changes, and you try to plot one this doesn't exist, it'll complain. You can get it to ignore these points by wrapping Plot[] in Quiet[].

• Thanks for providing a nice answer! Is there a way I can return this as a function? So $x(y)$? – Q.P. Apr 24 '18 at 0:15
• x[y_] is a function. Please be clear what you want. That's the first rule of succeeding with Mathematica. – John Doty Apr 24 '18 at 0:17
• Sorry for the ambiguity, I require an equation: $x(y) = ...$ – Q.P. Apr 24 '18 at 0:21
• You mean you want a formula? You can do Solve[y == a0 + a1 x + a2 x^2 + a3 x^3 + a4 x^4, x, Quartics -> False] to trivially get all four solutions as Root objects (be sure you have no definitions of the coefficients hanging around). Or, if you want an incredibly messy, and possibly unstable, set of radical expressions, set Quartics->True. You'll have to sort out which are real after defining the coefficients. – John Doty Apr 24 '18 at 0:32
• NSolve[y == a0 + a1 x + a2 x^2 + a3 x^3 + a4 x^4, x] yields formulae that are horribly unstable in an experiment. – John Doty Apr 24 '18 at 0:47