Ask Your Question

How to find multiple roots?

asked 2017-04-23 10:59:42 -0500

screened00 gravatar image

find_root is finding only "one" root, when there are many in a given range? Very simple question. For ex: sin(x)=cos(pi/2+x) may have multiple solutions in the selected range.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2017-04-24 03:37:42 -0500

ndomes gravatar image
edit flag offensive delete link more

answered 2017-04-23 12:59:50 -0500

mforets gravatar image

updated 2017-04-23 15:42:58 -0500


find_root gives only one root in the given interval because it implements a secant-based method that is called Brent's algorithm. It is provided by SciPy's scipy.optimize.brentq.

If you want to find the zeros numerically, my first attempt would be to partition the given interval into smaller subintervals; the precondition for several of these root-finding algorithms (as explained in the docs) is that the function changes sign in the given interval.

If you want to find the roots symbolically, Sage provides the solve method, you can access the documentation by typing solve? and the ENTER key. In your example,

sage: solve(sin(x) == cos(pi/2 + x), x)
[x == 0]

In this case solve, which refers to Maxima's solve algorithm, gives only the root at x=0, although other cases (eg. try with polynomials), it can give all roots. But Sage allows to interface many different mathematical software. We can try SymPy's solveset:

sage: from sympy import solveset, symbols, sin, cos, pi
sage: x = symbols('x')
sage: solveset(sin(x) - cos(pi/2 + x), x)
ImageSet(Lambda(_n, 2*_n*pi), Integers()) U ImageSet(Lambda(_n, 2*_n*pi + pi), Integers())

One interprets this answer as:

$$ S = \{ 2 n \pi : n \in \mathbb{Z}\} \cup \{ 2 n \pi + \pi : n \in \mathbb{Z}\}. $$

The nuisance with this approach is that you have to make many imports by hand.. it is sometimes helpful to use the function sympy.sympify, as in sympify(sin(x) - cos(pi/2 + x)), which does the imports automatically.

edit flag offensive delete link more


@mforets It's interesting. But how do I find all the roots of a say 10nth degree equation, as i can't use symbolically, but would have to go for numerically.

screened00 gravatar imagescreened00 ( 2017-04-24 09:53:54 -0500 )edit

the degree by itself does not imply that the program cannot give the precise roots, for instance if f = x^10 - 55*x^9 + 1320*x^8 - 18150*x^7 + 157773*x^6 - 902055*x^5 + 3416930*x^4 - 8409500*x^3 + 12753576*x^2 - 10628640*x + 3628800, then f.roots() gives the integers $1,\ldots,10$. but (f+1).roots() will tell you that no explicit root is found. but numerically you can get them, try ndomes' find_root_recursive !

mforets gravatar imagemforets ( 2017-04-24 10:17:50 -0500 )edit

@mforets find_root_recursive runs fine, but how come ? Because the default depth is only 1000 for recursive functions, won't it put a load on memory ? Is there any other way without recursion?

screened00 gravatar imagescreened00 ( 2017-04-25 06:40:14 -0500 )edit

@screened00 : you are encouraged to post more specific (but sage-related) questions in this site. for alternative ways without recursion i would check for existent algorithms in other software / relevant scientific literature (eg. numerical analysis).

mforets gravatar imagemforets ( 2017-04-26 14:46:37 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2017-04-23 10:59:42 -0500

Seen: 75 times

Last updated: Apr 24