# Solving a polynomial equation with a condition of equality on roots

Let the following equation have two equal roots:

f[x_] := x^3 - p x^2 + q x - r


And I want to find out what the three roots are. Not knowing how to put this condition within a Solve or Reduce, I have to rely on transcribing some algebra into MMA. So if the above equation has two equal roots, then

fr[x_] := 3 x^2 - 2 p x + q


has one of them, out of a theorem on the depression of equations. If I put this into Solve this leads to:

Solve[x^3 - p x^2 + q  x - r == 0   && 3 x^2 - 2 p  x + q == 0, x]


No error message but MMA is unable to give here one the the duplicate solution. So I put more algebra into the code:

PolynomialRemainder[f[x], fr[x], x]    (* the remainder must he equal to zero *)
rootduplicate = x /. Solve[% == 0, x]  (* gives the value of the two equal roots *)


The third root is within the third factor of f[x]:

PolynomialQuotient[f[x], (x - rootduplicate[[1]])^2, x] // FullSimplify
Solve[% == 0, x]
root3 = x /. {%}


Let's check it up with an example:

Solve[(x^3 - p x^2 + q  x - r /. {p -> 4, q -> 5, r -> 2}) ==  0   && (3 x^2 - 2 p  x + q /. {p -> 4, q -> 5}) == 0, x]    (* now MMA finds a double root *)
Solve[(x^3 - p x^2 + q  x - r /. {p -> 4, q -> 5, r -> 2}) ==  0   , x]
rootduplicate /. {p -> 4, q -> 5, r -> 2}
root3 /. {p -> 4, q -> 5, r -> 2}
root3 == {(p - 2 rootduplicate)} // FullSimplify (* another verification out of algebra   - is there a way with MMA to find out this relation without hardcoding it? *)


My questions:

• Why MMA (V8) is unable to find a solution in my first Solve ?
• Is there a better way relying less on algebra to get these solutions?
• What specifically are you looking to obtain for a solution? Possibly SolveAlways[x^3 - p x^2 + q x - r == (x - r1)^2*(x - r2), x] is along the lines of what you want? – Daniel Lichtblau Jan 17 '14 at 20:52
• I want an expression for r1 and r2 which involves only p, q, r. You suggest an expression for p, q, r which involves r1 and r2. That's different. – Sigis K Jan 18 '14 at 8:19

I would say that if your polynomial has two identical roots then : $$x^3 - p x^2 + q x - r == (x - xs)^2 (x - xd)$$

and you can solve for the unknowns xs, xs in terms of the other parameters.

sol = Reduce[
CoefficientList[(x - xs)^2 (x - xd) - (x^3 - p x^2 + q x - r), x] == 0]
, {xs, xd}] ;


Check :

example = {p -> 4, q -> 5, r -> 2} ;

sol //. example
(* xs == 1 && xd == 4 - 2 xs *)

Solve[(x^3 - p x^2 + q x - r /. example) == 0, x]
(* {{x -> 1}, {x -> 1}, {x -> 2}} *)

• Ingenious! I wonder why MMA in the ConditionalExpression list xd is always expressed as xd == p - 2 xs (which is correct) and not an expression involving only p,q,r (root3 in my question) – Sigis K Jan 18 '14 at 16:16
• xd in your solution is the simple root, xs the double root – Sigis K Jan 18 '14 at 16:43
• You can use Solve[sol /. example] to get an explicit solution for xs, xd. – b.gates.you.know.what Jan 18 '14 at 16:49
• @SigismondKmiecik Yes, "d" for different and "s" for same. – b.gates.you.know.what Jan 18 '14 at 17:49

I would approach the problem like this:

f[x_] := x^3 - p x^2 + q x - r


Now try

Solve[{f[x]==0, f'[x]==0}, x]


which returns {} meaning that there are no solutions. More specifically it means that there are no solutions for arbitrary p, q and r, but there might be solutions if these parameters satisfy certain conditions. We can ask Mathematica to generate those conditions:

Solve[{f[x] == 0, f'[x] == 0}, x, MaxExtraConditions -> Infinity]

{{x -> ConditionalExpression[p/3, p^2 - 3 q == 0 && p^3 - 27 r == 0]},
{x -> ConditionalExpression[p/3, p^2 - 3 q == 0 && p^3 - 27 r == 0]},
{x -> ConditionalExpression[(p q - 9 r)/(2 (p^2 - 3 q)), -p^2 q^2 + 4 q^3 + 4 p^3 r - 18 p q r + 27 r^2 == 0]}}


Could do

Solve[{x^3 - p x^2 + q x - r == 0, r1 + r2 + r3 == p, r1 == r2,
r1*r2 + r1*r3 + r2*r3 == q, r1*r2*r3 == r}, {r1, r2, r3}, {x, p}]


The requirement of a double root places a relation on {p,q,r}, so I chose to eliminate p. An alternative is to solve for one of them e.g. p, or to do

Solve[{x^3 - p x^2 + q x - r == 0, r1 + r2 + r3 == p, r1 == r2,
r1*r2 + r1*r3 + r2*r3 == q, r1*r2*r3 == r}, {r1, r2, r3}, {x},
MaxExtraConditions -> 1]

(* {{r1 -> ConditionalExpression[(p q - 9 r)/(
2 (p^2 - 3 q)), -p^2 q^2 + 4 q^3 + 4 p^3 r - 18 p q r + 27 r^2 ==
0], r2 ->
ConditionalExpression[(p q - 9 r)/(
2 (p^2 - 3 q)), -p^2 q^2 + 4 q^3 + 4 p^3 r - 18 p q r + 27 r^2 ==
0], r3 ->
ConditionalExpression[(p^3 - 4 p q + 9 r)/(
p^2 - 3 q), -p^2 q^2 + 4 q^3 + 4 p^3 r - 18 p q r + 27 r^2 == 0]}} *)

• I'm impressed. How did you find out the relations r1*r2 + r1*r3 + r2*r3 == q, r1*r2*r3 == r - It's not a MMA question but an algebra question. – Sigis K Jan 18 '14 at 23:25
• @Sigismond Kmiecik Just expand (x-r1)*(x-r2)*(x-r3), then equate coefficients in powers of x to those of x^3-p*x^2+q*x-r. – Daniel Lichtblau Jan 19 '14 at 20:31
• Indeed! Easier with MMA than with algebra demonstrations! Thanks – Sigis K Jan 19 '14 at 21:14

Equating coefficients makes sense: the polynomial x^3 - p x^2 + q x - r must be equal to

Expand[(x - a)^2 (x - b)]
-a^2 b + a^2 x + 2 a b x - 2 a x^2 - b x^2 + x^3


Hence

p == b + 2 a
q == a^2 + 2 a b
r == a^2 b


Applying Solve gives several possible answers in the form of ConditionalExpressions.

Solve[p == b + 2 a && q == a^2 + 2 a b && r == a^2 b, {a, b},
MaxExtraConditions -> Infinity]


If all you need is a single numerical example:

  FindInstance[p == b + 2 a && q == a^2 + 2 a b && r == a^2 b,
{a, b, p, q, r}, Reals]

{{a -> 33/10, b -> 8/5, p -> 41/5, q -> 429/20, r -> 2178/125}}


If you want more answers, you can ask FindInstance for more, as noted by Sigismond. If you want answers with specific values, you can do it manually. For instance, to find three answer sets where p=5

FindInstance[5 == b + 2 a && q == a^2 + 2 a b && r == a^2 b,
{a, b, q, r}, Reals, 3]

• I like your answer as it is easy with it to find n different instances just by adding ,n at the end of your FindInstance. Is it possible to get an instance by replacing any of the p,q,r parametres by a numerical value? I tried FindInstance[ p == b + 2 a && q == a^2 + 2 a b && r == -a^2 b, {a, b, p, q, r}, Reals] /. {p -> 5}  but this does not work – Sigis K Jan 18 '14 at 20:13
• The update shows one way of getting specific answers. – bill s Jan 18 '14 at 20:23
• Try Solve[(x^3 - p x^2 + q x - r /. {p -> 41/5, q -> 429/20, r -> -2178/125}) == 0, x] (* your solution *) Solve[(x^3 - p x^2 + q x - r /. {p -> 41/5, q -> 429/20, r -> 2178/125}) == 0, x] (* r sign changed*)  Updated prompted by @DanielLichtBlau answer. – Sigis K Jan 18 '14 at 23:16
• I had switched the sign on r, it's now switched back. – bill s Jan 18 '14 at 23:27