# A simple quadratic equation cannot be solved?

**URL:** <https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263>\
**Category:** Questions\
**Tags:** algebra, equation-solving, tuning-and-debugging\
**Created:** [July 11, 2022, 11:23am UTC](https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263 "2022-07-11T11:23:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [July 11, 2022, 11:23am UTC](https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263/1 "2022-07-11T11:23:19Z")

</div>

Of course a trivial quadratic equation can be solved by Solve:

```
eqn = a1 + b1*x + c1*x^2;
cl = CoefficientList[eqn, x];
{time, sol} = AbsoluteTiming[Solve[eqn == 0, x, Reals]];
Echo[Column[{eqn, Column[cl, Frame -> Thin], sol, time}, Frame -> True]];

```

 ![4843b1.jpg](https://global.discourse-cdn.com/wolfram/original/3X/6/8/68e3e9eca53551fb2804e9fc5dc38ef83f0fa7ca.jpeg)

If you use some more complicated coefficients, the equation is still solved but takes pretty long (about 13 seconds on my machine). For me it’s not understandable why it takes significantly longer. It’s still a trivial quadratic equation. I assume the time is spent during simplification of the conditions.

```
eqn = (-4 + c1^2 + 2 c1*c2 + c2^2 + (s1 + s2)^2)/4 - 
   3/8*(a2*r1 + a1*r2)*(c2*s1 - c1*s2)*x + 
   9/64*(a1^2*r2^2*(c1^2 + s1^2) - 2*a1*a2*r1*r2 )*x^2;
cl = CoefficientList[eqn, x];
{time, sol} = AbsoluteTiming[Solve[eqn == 0, x, Reals]];
Echo[Column[{eqn, Column[cl, Frame -> Thin], sol, time}, Frame -> True]];

```

 ![3938b2.jpg](https://global.discourse-cdn.com/wolfram/original/3X/c/a/caf7777d0ef245556e198c3c56a1eabd63a0b98b.jpeg)

When you make a coefficient even more complicated (here an additional term (c1_c2+s1_s2) within the x^2-coefficient), Solve does not come to an end. At least not after two minutes on my machine. You have to abort the evaluation by Alt+“.” or surround it by TimeConstrained.

```
eqn = (-4 + c1^2 + 2 c1*c2 + c2^2 + (s1 + s2)^2)/4 - 
   3/8*(a2*r1 + a1*r2)*(c2*s1 - c1*s2)*x + 
   9/64*(a1^2*r2^2*(c1^2 + s1^2) - 2*a1*a2*r1*r2 (c1*c2 + s1*s2))*x^2;
cl = CoefficientList[eqn, x];
{time, sol} = AbsoluteTiming[Solve[eqn == 0, x, Reals]];
Echo[Column[{eqn, Column[cl, Frame -> Thin], sol, time}, Frame -> True]];
(* .... $Aborted ...*)

```

Does anyone have an explanation for this strange behavior? And how to overcome it?

I’ve managed it by partially replacing the coefficients with new variables and then examining and interpreting the conditions myself. But this is quite involved and of course very specific to the concrete equation.

---

<div class="post-metadata">

**Author:** ![mariuszi28cg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mariuszi28cg/32/308_2.png) [@mariuszi28cg](https://community.wolfram.com/u/mariuszi28cg)\
**Post date:** [July 11, 2022, 2:30pm UTC](https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263/2 "2022-07-11T14:30:07Z")

</div>

A dirty workaround is:

```
  Solve[eqn == 0, x]

```

without **Reals** in Solve.

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [July 11, 2022, 3:01pm UTC](https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263/3 "2022-07-11T15:01:24Z")

</div>

This works indeed, Mariusz (within 0,06 secs). **But why?**  
I had never thought that solving in Complex would be easier than within Reals.  
But anyway this is a valid solution. I can apply the Reals-condition later to the result.

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [July 11, 2022, 5:55pm UTC](https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263/4 "2022-07-11T17:55:49Z")

</div>

I noticed that you use the construct

```
Echo[something];

```

I think you can save some typing with a simple

```
something

```

without the semicolon.

The semicolon suppresses the output, and `Echo` undoes the suppression. My suggestion is to suppress both semicolon and `Echo`.

---

<div class="post-metadata">

**Author:** ![mroge02](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/mroge02/32/28775_2.png) [@mroge02](https://community.wolfram.com/u/mroge02)\
**Post date:** [July 12, 2022, 2:45am UTC](https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263/5 "2022-07-12T02:45:04Z")

</div>

Solving over the complexes is easier because algebra and analysis are in some sense complete. In your case, an algebraic equation is to be solved, and it helps that the complexes is an [algebraically closed field](https://en.wikipedia.org/wiki/Algebraically_closed_field). To solve over the `Reals`, Mathematica solves for the conditions on the other variables/parameters that specify exactly when the quadratic formula gives real solutions. There’s a fast way to do that, but Mathematica probably tries to reduce the condition (perhaps via `Reduce` or one of its subfunctions). That’s probably what takes so long.

---

<div class="post-metadata">

**Author:** ![wernergg](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wernergg/32/6481_2.png) [@wernergg](https://community.wolfram.com/u/wernergg)\
**Post date:** [July 12, 2022, 8:25am UTC](https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263/6 "2022-07-12T08:25:20Z")

</div>

I know, Gianluca.  
I prefer Echo since it makes the code and the output more readable.  
And since I can easily disable it globally by:

```
$ContextPath = Prepend[$ContextPath, "myContext`"];
Echo[any___] := Null;

```

Actually I use my own echo-function with takes a global variable testLvl that controls which Echo-output shall be produced.

---

<div class="post-metadata">

**Author:** ![gianlucagorni](https://avatars.discourse-cdn.com/v4/letter/g/ec9cab/32.png) [@gianlucagorni](https://community.wolfram.com/u/gianlucagorni)\
**Post date:** [July 12, 2022, 10:12am UTC](https://community.wolfram.com/t/a-simple-quadratic-equation-cannot-be-solved/20263/7 "2022-07-12T10:12:28Z")

</div>

Even in the complex case the full solution can be pretty complicated, if you ask for the full solvability conditions;

```
eqn = (-4 + c1^2 + 2 c1*c2 + c2^2 + (s1 + s2)^2)/4 - 
   3/8*(a2*r1 + a1*r2)*(c2*s1 - c1*s2)*x + 
   9/64*(a1^2*r2^2*(c1^2 + s1^2) - 2*a1*a2*r1*r2)*x^2;
Echo[Reduce[eqn == 0, x]];

```

In the real case, the solvability conditions involve analyzing a semialgebraic variety of high degree in several variables.

It is true that when the generated conditions are too complicated they are hard to use in practice. Would you like Mathematica to return simply

```
Echo[Discriminant[eqn, x] >= 0];

```

as the condition for real solutions?
