solve() determines incorrect number of roots

21 views
Skip to first unread message

Long Nguyen

unread,
Nov 29, 2016, 6:32:27 PM11/29/16
to sage-support
Hello,

I'm encountering an issue in using solve:

assume(x,"real")
assume(x>=0)
f(x)=(0.01+x^2)/(1+x^2)- 0.4*x
roots = solve(f(x) == 0,x)
num_roots = len(roots)
print "roots ", num_roots
roots
plot(f(x),(x,0,3))

I'm expecting 3 roots, but this only finds one.  Is this intended? Can some provide insight on why this occuring?

Would the work-around be to do this numerically using find_root instead?

Thank you.

Cheers, 
Long

'SageMath version 7.3, Release Date: 2016-08-04'

Dima Pasechnik

unread,
Nov 29, 2016, 7:11:51 PM11/29/16
to sage-support
You are doing inexact computations, and imaginary errors creep in. Indeed, let's first run without assume()'s:

sage: s=solve(f(x) == 0,x)
sage: [t.rhs().n() for t in s]

[0.464973569257452 - 1.11022302462516e-16*I,
 0.0267727617252126 + 1.11022302462516e-16*I,
 2.00825366901734]

so you see a tiny imaginary part in two of the three solutions.
And they get dropped:

sage: assume(x,"real")
....: assume(x>=0)
....: 
sage: s=solve(f(x) == 0,x)
sage: [t.rhs().n() for t in s]
[2.00825366901734]

HTH,
Dima
Reply all
Reply to author
Forward
0 new messages