Loading [MathJax]/jax/output/HTML-CSS/jax.js

First time here? Check out the FAQ!

Ask Your Question
3

problem with solve function

asked 1 year ago

vyt gravatar image

updated 1 year ago

Emmanuel Charpentier gravatar image

Solve function in SageMath produces some strange output while finding roots of a derivative of function (exp(x)*(1-x))^n/x^m. Entering the code below

var('x, m, n')
hh=(exp(x)*(1-x))^n/x^m
solve(hh.diff(x)==0,x)

produces output

(x, m, n)
[x^2 == (m*x^2 - m)/n]

What does `[x^2 == (m*x^2 - m)/n] is supposed to mean? By contrast Sympy quickly produces a correct easy to understand answer :

[mm(m4n)2n,m+m(m4n)2n]

Preview: (hide)

Comments

Edited for readability.

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 1 year ago )

2 Answers

Sort by » oldest newest most voted
1

answered 1 year ago

Emmanuel Charpentier gravatar image

updated 1 year ago

Step by step :

sage: var('x, m, n')
(x, m, n)
sage: hh=(exp(x)*(1-x))^n/x^m
sage: solve(hh.diff(x)==0,x)
[x^2 == (m*x^2 - m)/n]

That's an implicit solution. Sage's default solver (Maxima's) can be coaxed to find for a more explocit answer :

sage: solve(hh.diff(x)==0,x, to_poly_solve=True)
[x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))]

Or you may use solve recursively) :

sage: solve(solve(hh.diff(x)==0,x),x)
[x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))]

or

sage: solve(hh.diff(x)==0,x)[0].solve(x)
[x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))]

But beware : you should check these vatious solutions.And be wary of simplify_full which may use simplifications validonly certain assumptions...

Left as an exercise for the reader...

HTH,

EDIT :

the final solution [x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))] that you obtained by repeated application of solve procedure is wrong, so this is the bug of "Maxima" after all.

Indeed :

sage: [Eq.subs(t).simplify_full().is_zero() for t in flatten([s.solve(x) for s in S1])]
[False, False]

This is now #35541

Workarounds :

sage: Ex.factor().solve(x)
[x == 1, x == 1/2*(m - sqrt(m^2 - 4*m*n))/n, x == 1/2*(m + sqrt(m^2 - 4*m*n))/n, x == 0]
sage: [Ex.subs(s).is_zero() for s in Ex.factor().solve(x)]
[True, True, True, True]
sage: Ex.solve(x, algorithm="giac")
[1/2*(m - sqrt(m^2 - 4*m*n))/n, 1/2*(m + sqrt(m^2 - 4*m*n))/n]
sage: [Ex.subs(x==s).is_zero() for s in Ex.solve(x, algorithm="giac")]
[True, True]
sage: [{v[1].sage():v[2].sage() for v in s} for s in mathematica.Solve(Ex==0, x)]
[{x: -1/2*(sqrt(m - 4*n)*sqrt(m) - m)/n},
 {x: 1/2*(sqrt(m - 4*n)*sqrt(m) + m)/n}]
sage: [Ex.subs(s).is_zero() for s in [{v[1].sage():v[2].sage() for v in s} for s in mathematica.Solve(Ex==0, x)]]
[True, True]

HTH,

Preview: (hide)
link

Comments

Thank you for the clarification that the solution provided by "solve()" function uses "Maxima" and is supposed to be understood as an implicit solution! However, if this is the case then the final solution [x == -sqrt(m/(m - n)), x == sqrt(m/(m - n))] that you obtained by repeated application of solve procedure is wrong, so this is the bug of "Maxima" after all.

vyt gravatar imagevyt ( 1 year ago )
1

answered 1 year ago

tolga gravatar image

Simplifying the function may help:

var('x, m, n');
hh=(exp(x)*(1-x))^n/x^m
solve((hh.diff(x)).simplify_full()==0,x)
Preview: (hide)
link

Comments

Thank you very much! Applying method "simplify_full()" produces expression that "solve" function can correctly solve. However, as a new user of SageMath I would like to ask a follow up question, as to what was the problem with the initial solution of the equation? Was the original answer [x^2 == (m*x^2 - m)/n] provided by solve() function incorrect and a result of a bug, or could it be interpreted to provide a correct answer?

vyt gravatar imagevyt ( 1 year ago )

Both in fact :

  • This has the form of an implicit answer, which I (incorrectly) tried to explicit.

  • It turns out that this implicit answer is wrong.

See the Github issue I cite in my answer's EDIT.

HTH,

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 1 year ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 1 year ago

Seen: 254 times

Last updated: Apr 19 '23