Use bisection_find_roots to automatically search for multiple roots in an interval:
from root_finding.bisection.bisection_find_roots import bisection_find_roots# Find all roots in [-3, 3]roots = bisection_find_roots(f, xmin=-3, xmax=3, tol=1e-6)print(f"Roots found: {roots}")
Roots found: [np.float64(-2.0), np.float64(2.0)]
3. Newton-Raphson Method
For faster convergence, use Newton’s method (requires the derivative):
from root_finding.newton1d import newton1d# Define the derivativedef df(x):return2*x# Find roots starting from different initial guessesroots1 = newton1d(f, df, x0=1.0, tol1=1e-6)roots2 = newton1d(f, df, x0=-1.0, tol1=1e-6)print(f"Root from x0=1.0: {roots1}")print(f"Root from x0=-1.0: {roots2}")
Root from x0=1.0: [2.]
Root from x0=-1.0: [-2.]
4. Hybrid Method
The hybrid method combines bisection and Newton-Raphson for both robustness and speed:
from root_finding.hybrid import hybrid# Find all roots in the intervalroots = hybrid(f, df, xmin=-3, xmax=3, tol1=1e-6, tol2=1e-6)print(f"All roots found: {roots}")
All roots found: [-2. 2.]
Visualizing Results
Use the plotting function to visualize the function and its roots:
from root_finding.plot_root import plot_root# Plot the function and show the rootsplot_root(f, df, xmin=-3, xmax=3, tol1=1e-6, tol2=1e-6)
This will create a plot showing: - The function curve - The roots marked on the x-axis - Grid lines for reference
More Complex Example
Let’s try a more interesting function:
import numpy as np# A more complex function with multiple rootsdef g(x):return np.sin(x) -0.5*xdef dg(x):return np.cos(x) -0.5# Find roots in [0, 10]roots = hybrid(g, dg, xmin=0, xmax=10, tol1=1e-8, tol2=1e-8)print(f"Roots of sin(x) - 0.5x in [0, 10]: {roots}")
Roots of sin(x) - 0.5x in [0, 10]: [0. 1.89549427]
Tips and Best Practices
Choose the right method:
Use bisection when you need guaranteed convergence
Use Newton-Raphson when you have the derivative and want speed
Use hybrid for the best of both worlds
Set appropriate tolerances:
tol or tol1: Controls convergence tolerance (smaller = more accurate)
tol2: For bisection part in hybrid method
Initial guesses matter (for Newton’s method):
Choose initial guesses close to the expected root
Multiple initial guesses can find multiple roots
Check convergence:
Always verify that f(root) is close to zero
If convergence fails, try adjusting tolerances or max iterations
Next Steps
Check out the API Reference for complete documentation