How to find multiple roots?
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.
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.
What's about recursion? See my answer to this question:
https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/
Hi,
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.
@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.
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 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 : 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).
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2017-04-23 17:59:42 +0100
Seen: 3,881 times
Last updated: Apr 24 '17