Ask Your Question
1

obtaining all numerical roots of a function in an interval

asked 2012-04-15 01:17:01 +0100

Nil gravatar image

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.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
2

answered 2012-04-18 05:41:35 +0100

ndomes gravatar image

updated 2012-05-02 18:48:15 +0100

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()
L
edit flag offensive delete link more
0

answered 2019-11-19 20:53:29 +0100

Emmanuel Charpentier gravatar image

Formally, it can't be done. For example, $\sin\displaystyle\frac{1}{x}$ has an infinity of roots between -1 and 1...

edit flag offensive delete link more
0

answered 2012-04-15 01:58:13 +0100

Shashank gravatar image

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].

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
edit flag offensive delete link more

Comments

Thanks 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: x

Nil gravatar imageNil ( 2012-04-15 02:33:40 +0100 )edit

Can you tell me what is the f you are using?

Shashank gravatar imageShashank ( 2012-04-15 19:58:41 +0100 )edit

yes. 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.

Nil gravatar imageNil ( 2012-04-16 16:21:31 +0100 )edit

For 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.

Shashank gravatar imageShashank ( 2012-04-16 17:16:14 +0100 )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

Stats

Asked: 2012-04-15 01:17:01 +0100

Seen: 4,171 times

Last updated: Nov 19 '19