ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 19 Nov 2019 20:53:29 +0100obtaining all numerical roots of a function in an intervalhttps://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/Hello, thanks for reading.
I'm working on single variable calculus here:
Basically what I need is what "find_root" does, but I need a list of ALL roots in a given interval, not just one.
So I've been playing with "solve". I found this piece of code which works in most cases:
sage: roots = solve(f(x),x,solution_dict=True)
sage: roots = [s[x] for s in roots]
sage: num_roots = map(n, roots)
but it gives an error if the function is periodic and has inifinite roots, becuase the symbolic expression that "solve" gets has infinite solutions too.
Defining a desired interval should solve this issue, but I have no idea how to implement such thing!
Thanks you and have a good day.Sun, 15 Apr 2012 01:17:01 +0200https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/Answer by Emmanuel Charpentier for <p>Hello, thanks for reading.</p>
<p>I'm working on single variable calculus here:
Basically what I need is what "find_root" does, but I need a list of ALL roots in a given interval, not just one.</p>
<p>So I've been playing with "solve". I found this piece of code which works in most cases:</p>
<p>sage: roots = solve(f(x),x,solution_dict=True)</p>
<p>sage: roots = [s[x] for s in roots]</p>
<p>sage: num_roots = map(n, roots)</p>
<p>but it gives an error if the function is periodic and has inifinite roots, becuase the symbolic expression that "solve" gets has infinite solutions too.</p>
<p>Defining a desired interval should solve this issue, but I have no idea how to implement such thing!</p>
<p>Thanks you and have a good day.</p>
https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?answer=48814#post-id-48814Formally, it can't be done. For example, $\sin\displaystyle\frac{1}{x}$ has an infinity of roots between -1 and 1...Tue, 19 Nov 2019 20:53:29 +0100https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?answer=48814#post-id-48814Answer by ndomes for <p>Hello, thanks for reading.</p>
<p>I'm working on single variable calculus here:
Basically what I need is what "find_root" does, but I need a list of ALL roots in a given interval, not just one.</p>
<p>So I've been playing with "solve". I found this piece of code which works in most cases:</p>
<p>sage: roots = solve(f(x),x,solution_dict=True)</p>
<p>sage: roots = [s[x] for s in roots]</p>
<p>sage: num_roots = map(n, roots)</p>
<p>but it gives an error if the function is periodic and has inifinite roots, becuase the symbolic expression that "solve" gets has infinite solutions too.</p>
<p>Defining a desired interval should solve this issue, but I have no idea how to implement such thing!</p>
<p>Thanks you and have a good day.</p>
https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?answer=13210#post-id-13210 def find_root_recursive(func,a,b,tol=0.000000000001):
L = []
try:
x0 = find_root(func,a,b)
L.append(x0)
L += find_root_recursive(func,a,x0-tol,tol)
L += find_root_recursive(func,x0+tol,b,tol)
except:
pass
return L
L = find_root_recursive(sin,-50,50)
L.sort()
LWed, 18 Apr 2012 05:41:35 +0200https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?answer=13210#post-id-13210Answer by Shashank for <p>Hello, thanks for reading.</p>
<p>I'm working on single variable calculus here:
Basically what I need is what "find_root" does, but I need a list of ALL roots in a given interval, not just one.</p>
<p>So I've been playing with "solve". I found this piece of code which works in most cases:</p>
<p>sage: roots = solve(f(x),x,solution_dict=True)</p>
<p>sage: roots = [s[x] for s in roots]</p>
<p>sage: num_roots = map(n, roots)</p>
<p>but it gives an error if the function is periodic and has inifinite roots, becuase the symbolic expression that "solve" gets has infinite solutions too.</p>
<p>Defining a desired interval should solve this issue, but I have no idea how to implement such thing!</p>
<p>Thanks you and have a good day.</p>
https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?answer=13459#post-id-13459If you run the following code you get [-0.973248989467730, 0.973248989467730, -0.229752920547361, 0.229752920547361], but if you uncomment assume(x>0) you get [0.973248989467730, 0.229752920547361].
x=var('x')
#assume(x>0)
f(x) = -x*x+x**4+0.05
roots = solve(f(x),x,solution_dict=True)
roots = [s[x] for s in roots]
num_roots = map(n, roots)
print num_rootsSun, 15 Apr 2012 01:58:13 +0200https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?answer=13459#post-id-13459Comment by Nil for <p>If you run the following code you get [-0.973248989467730, 0.973248989467730, -0.229752920547361, 0.229752920547361], but if you uncomment assume(x>0) you get [0.973248989467730, 0.229752920547361]. </p>
<pre><code>x=var('x')
#assume(x>0)
f(x) = -x*x+x**4+0.05
roots = solve(f(x),x,solution_dict=True)
roots = [s[x] for s in roots]
num_roots = map(n, roots)
print num_roots
</code></pre>
https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?comment=19913#post-id-19913yes. it is: f(x) = 8 x^3 sin(x^2)-12 x cos(x^2) , or simply (cos(x^2))'''. But I believe the problem happens with any function with inifinite roots.Mon, 16 Apr 2012 16:21:31 +0200https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?comment=19913#post-id-19913Comment by Shashank for <p>If you run the following code you get [-0.973248989467730, 0.973248989467730, -0.229752920547361, 0.229752920547361], but if you uncomment assume(x>0) you get [0.973248989467730, 0.229752920547361]. </p>
<pre><code>x=var('x')
#assume(x>0)
f(x) = -x*x+x**4+0.05
roots = solve(f(x),x,solution_dict=True)
roots = [s[x] for s in roots]
num_roots = map(n, roots)
print num_roots
</code></pre>
https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?comment=19925#post-id-19925Can you tell me what is the f you are using? Sun, 15 Apr 2012 19:58:41 +0200https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?comment=19925#post-id-19925Comment by Shashank for <p>If you run the following code you get [-0.973248989467730, 0.973248989467730, -0.229752920547361, 0.229752920547361], but if you uncomment assume(x>0) you get [0.973248989467730, 0.229752920547361]. </p>
<pre><code>x=var('x')
#assume(x>0)
f(x) = -x*x+x**4+0.05
roots = solve(f(x),x,solution_dict=True)
roots = [s[x] for s in roots]
num_roots = map(n, roots)
print num_roots
</code></pre>
https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?comment=19912#post-id-19912For cos(x*x) I get two roots [-1/2*sqrt(pi)*sqrt(2), 1/2*sqrt(pi)*sqrt(2)], but I don't get any error. Mon, 16 Apr 2012 17:16:14 +0200https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?comment=19912#post-id-19912Comment by Nil for <p>If you run the following code you get [-0.973248989467730, 0.973248989467730, -0.229752920547361, 0.229752920547361], but if you uncomment assume(x>0) you get [0.973248989467730, 0.229752920547361]. </p>
<pre><code>x=var('x')
#assume(x>0)
f(x) = -x*x+x**4+0.05
roots = solve(f(x),x,solution_dict=True)
roots = [s[x] for s in roots]
num_roots = map(n, roots)
print num_roots
</code></pre>
https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?comment=19941#post-id-19941Thanks for the reply, but this does not fix the problem with periodic infinte roots. I run into the same error:
Traceback (most recent call last): MT = maxim(f, a, b)
File "", line 1, in <module>
File "/tmp/tmpGBdQkB/___code___.py", line 8, in <module>
MT = maxim(f, a, b)
File "/tmp/tmpbtKw0f/___code___.py", line 20, in maxim
roots = [s[x] for s in roots];
KeyError: xSun, 15 Apr 2012 02:33:40 +0200https://ask.sagemath.org/question/8886/obtaining-all-numerical-roots-of-a-function-in-an-interval/?comment=19941#post-id-19941