2

I am trying to solve an equation with Maxima so that I only get real solutions if they exist; if there are no real solutions, or if there are not even complex solutions, I would like Maxima to return an empty list.

For example, I would like that when solving x^2+100-x=0 using solve(x^2+100-x,x), which has only complex solutions, Maxima would return an empty list. Or that when solving log(x)-x=0 using solve(log(x)-x,x), which also has no real solutions, Maxima would return an empty list. In this second example, what I get instead is [x=log(x)].

How could this be achieved with Maxima?

User1234321
  • 321
  • 1
  • 10

2 Answers2

2

Inhibit implicit solutions:

(%i1) solve(log(x)-x);
(%o1)                            [x = log(x)]
(%i2) solve(log(x)-x), solveexplicit: true;
(%o2)                                 []

Keep only solutions without an imaginary part:

(%i1) s: solve(x^2+100-x);
                       sqrt(399) %i - 1      sqrt(399) %i + 1
(%o1)           [x = - ----------------, x = ----------------]
                              2                     2
(%i2) sublist(s, imagpart);
(%o2)                                 []
(%i3) s: solve(x^2+2 * x + 1);
(%o3)                              [x = - 1]
(%i4) sublist(s, imagpart);
(%o4)                              [x = - 1]
slitvinov
  • 5,693
  • 20
  • 31
  • Thanks, it solves the problem! Maybe you could take a look at this related question I posted: https://stackoverflow.com/questions/71615022/how-to-get-only-the-real-solutions-in-maxima. I tried `solve(sin(x)-x), solveexplicit: true` but it still does not work with trigonometric equations... – User1234321 Mar 26 '22 at 13:37
0

Maybe this could also help your answer:

(%i80)  realroots(x^2+100-x=0);
(%o80)  []
Time Step
  • 45
  • 4
  • Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** This is even more important here since there’s already an accepted answer. – Jeremy Caney Jul 23 '23 at 01:18