Finding multiple roots of a nonlinear equation in Python
Regarding the title, there are several approaches to finding multiple roots of a nonlinear equation in Python using numerical methods. Multiple roots of a nonlinear equation in Python can be solved in a number of different ways. I will share code snippets of three major techniques: the Newton-Raphson method, the Bisection method and the Secant Method. Every method will return all the roots it has found for the given equation.
In each method:
Where f(x) is the nonlinear equation you want to solve.
I need to tune initial_guesses, a, and b to match the expected range of roots, so the root finder only looks into where I have roots.
This mean the function will give you all the roots which found within this tolerance and max iterations.
1. Newton-Raphson Method
Here’s an example to find multiple roots of a nonlinear equation in Python,This iterative method starts with an initial guess and then refines it using the tangent line to the curve. It usually converges fast but may not converge or converge to the wrong root if the initial guess is far from the actual root.
import numpy as np def f(x): return x**3 - 6*x**2 + 11*x - 6 def f_prime(x): return 3*x**2 - 12*x + 11 def newton_raphson(f, f_prime, x0, tol=1e-6, max_iter=100): roots = [] for guess in x0: x = guess for _ in range(max_iter): x_new = x - f(x) / f_prime(x) if np.abs(x_new - x) < tol: roots.append(x_new) break x = x_new return roots # Example usage initial_guesses = [0, 1, 2] # Initial guesses for roots roots = newton_raphson(f, f_prime, initial_guesses) print("Roots found with Newton-Raphson method:", roots)
OUTPUT: Roots found with Newton-Raphson method: [1.0, 2.0, 3.0]
Find the root of an equation using Newton Raphson method in C++
2. Bisection Method
This is bracketing method which needs 2 guesses for sure enclosing root Which is halves the range that the root lies in until it converges to the required tolerance. It is stable and the solns will converge provided that an initial range is given which brackets a root. To get this we can write code as follows.
def bisection(f, a, b, tol=1e-6, max_iter=100): roots = [] for i in range(max_iter): c = (a + b) / 2 if f(a) * f(c) < 0: b = c else: a = c if np.abs(b - a) < tol: roots.append(c) a = b return roots # Example usage roots = bisection(f, 0, 2) print("Roots found with Bisection method:", roots)
OUTPUT:
Roots found with Bisection method: [0.9999990463256836, 2.9999990463256836]
3. Secant Method
A method that is quite similar to Newton-Raphson method with finding the roots of a function but instead of using the derivative of the function, it uses the derivative approximate of the function by using a finite differences between the two points. Unlike Newton-Raphson it does not need to know the form of the derivative, but it often converges slower. //newton raphson method Write to this code a new file
def secant(f, x0, x1, tol=1e-6, max_iter=100): roots = [] for i in range(max_iter): x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0)) if np.abs(x2 - x1) < tol: roots.append(x2) x0 = x1 x0, x1 = x1, x2 return roots # Example usage roots = secant(f, 0, 2) print("Roots found with Secant method:", roots)
OUTPUT:
Roots found with Secant method: [1.0000000912322435, 2.9999998044420553]
Leave a Reply