# Revision history [back]

This depends very much on the type of equation you are considering. For polynomial equations

sage: x = polygen(ZZ)
sage: (x**5 - x -1 1).roots(RealField(1024))
[1.1673039782614186842560458998548421807]


If you want a more specific answer, ask a more precise question.

This depends very much on the type of equation you are considering. For polynomial equations

sage: x = polygen(ZZ)
sage: (x**5 - x -1 1).roots(RealField(1024))
1).roots(RealField(128))
[1.1673039782614186842560458998548421807]


If you want a more specific answer, ask a more precise question.

This depends very much on the type of equation you are considering. For polynomial equations

sage: x = polygen(ZZ)
sage: (x**5 - x -1 - 1).roots(RealField(128))
[1.1673039782614186842560458998548421807]


If you want a more specific answer, ask a more precise question.

This depends very much on the type of equation you are considering. For polynomial equations

sage: x = polygen(ZZ)
sage: (x**5 - x - 1).roots(RealField(128))
[1.1673039782614186842560458998548421807]


If you want a more specific answer, ask a more precise question.

EDITED:

Sadly, Sage has no rountines for more general arbitrary general root finding. Here is a straightforward implementation of the dichotomy

def dichotomy(f, left, right, n):
sl = f(left).sign()
sr = f(right).sign()
if sl == sr:
raise ValueError
for _ in range(n):
middle = (left + right) / 2
sm = f(middle).sign()
if sm == 0:
return sm
elif sm == sl:
left = middle
else:
right = middle
return (left,right)


which you can use as

sage: f = lambda x: x.sin()
sage: R = RealField(256)
sage: dichotomy(f, R(3), R(3.5), 10)
(3.141113281250000000000000000000000000000000000000000000000000000000000000000,
3.141601562500000000000000000000000000000000000000000000000000000000000000000)
sage: dichotomy(f, R(3), R(3.5), 256)
(3.141592653589793238462643383279502884197169399375105820974944592307816406286,
3.141592653589793238462643383279502884197169399375105820974944592307816406286)


This depends very much on the type of equation you are considering. For polynomial equations

sage: x = polygen(ZZ)
sage: (x**5 - x - 1).roots(RealField(128))
[1.1673039782614186842560458998548421807]


If you want a more specific answer, ask a more precise question.

EDITED:

Sadly, Sage has no rountines for more general arbitrary general root finding. Here is a straightforward implementation of the dichotomy

def dichotomy(f, left, right, n):
sl = f(left).sign()
sr = f(right).sign()
if sl == sr:
raise ValueError
for _ in range(n):
middle = (left + right) / 2
sm = f(middle).sign()
if sm == 0:
return sm
elif sm == sl:
left = middle
else:
right = middle
return (left,right)


which you can use as

sage: f = lambda x: x.sin()
sage: R = RealField(256)
sage: dichotomy(f, R(3), R(3.5), 10)
(3.141113281250000000000000000000000000000000000000000000000000000000000000000,
3.141601562500000000000000000000000000000000000000000000000000000000000000000)
sage: dichotomy(f, R(3), R(3.5), 256)
(3.141592653589793238462643383279502884197169399375105820974944592307816406286,
3.141592653589793238462643383279502884197169399375105820974944592307816406286)


EDITED BIS: For a non-trivial example involving functions with cos/sin/cosh/sinh

sage: f(x) = -2*cos(x)**2 + 5 * sin(x) + cos(3*x) * cosh(x) - sinh(2*x - 1)
sage: g = fast_callable(f, vars=[x], domain=R)
sage: dichotomy(g, R(-0.3), R(0), 100)
(-0.09451007784336178406072003155719521150577190161782642363759963932690055798957,
-0.09451007784336178406072003155695855323420559807620290506801480345799859605904)


To find the appropriate initial conditions for f you need to either carry on some analysis on paper or look at its graph via

sage: plot(f)