SciPy - Finding Roots of Scalar Functions



In numerical analysis, finding the roots of scalar functions is a fundamental task. A root of a scalar function f(x) is a value x such that f(x)=0. This is commonly used in solving equations where the goal is to find the value of x that makes the function zero. SciPy a powerful scientific computing library in Python provides several tools to solve for the roots of scalar functions using various numerical methods.

Key features of Finding root

Here are the key features of finding root of scalar functions in SciPy −

  • Multiple Algorithms: SciPy offers a variety of algorithms to solve root-finding problems as mentioned below −
    • Bisection Method: This method is ideal for continuous functions with opposite signs at the interval endpoints.
    • Newton's Method: It is used when the function is differentiable and its derivative is available.
    • Hybrid Methods: These methods which combine several techniques for robust root-finding.
  • Handling Nonlinear Equations: Suitable for finding roots of nonlinear scalar functions whether smooth or with multiple roots.
  • Initial Guess: Suitable for finding roots of nonlinear scalar functions whether smooth or with multiple roots.
  • Initial Guess: Provides flexibility with initial guesses for root location, helping guide the algorithm toward convergence.
  • Customizable Tolerance: Control the accuracy of the solution with customizable tolerance values e.g., tol.
  • Flexible Constraints: Some methods support constraints or boundaries on variables during root search, especially in optimization contexts.
  • Convergence Monitoring: Returns detailed information about the convergence status and messages by allowing users to check if the solution has been found.
  • Support for Derivative-Free Methods: Methods such as Nelder-Mead are useful when the derivative of the function is not available or the function is non-smooth.
  • Multi-Root Capabilities: Some algorithms can be adapted to find multiple roots or solutions for a set of equations.

Functions to find roots of Scalar functions

Here are the key functions which are used to find the roots of Scalar Functions −

S.No. Function & Description
1 scipy.optimize.root()
Finds the roots of a scalar or system of nonlinear equations.
2 scipy.optimize.newton()
Finds the root of a function using the Newton-Raphson method.
3 scipy.optimize.bisect()
Finds a root of a function within a specified interval using the bisection method.
4 scipy.optimize.brentq()
Finds a root of a function within a specified interval using Brent's method.
5 scipy.optimize.fsolve()
Finds the roots of a function using a numerical method for systems of nonlinear equations.
6 scipy.optimize.minimize_scalar()
Minimizes a scalar function using various optimization algorithms.
7 scipy.optimize.ridder()
Finds the root of a function using Ridder's method, a root-finding algorithm based on bracketing.

Using the Bisection Method

The Bisection method is a simple and robust root-finding method that requires an interval in which the function changes sign. It's particularly useful when we have a continuous function and know that the root exists within the given interval. Here is the using the Bisection method with the help of scipy.optimize.bisect() method for finding the roots of Scalar functions −

from scipy.optimize import bisect

# Define the objective function
def objective_function(x):
    return x**2 - 4  # root at x = 2 or x = -2

# Perform the root finding in the interval [1, 3]
root = bisect(objective_function, 1, 3)

# Display the result
print("Root found at:", root)

Following is the output of using the scipy.optimize.bisect() function −

Root found at: 2.0

Using the Newton-Raphson Method

The Newton-Raphson method is an efficient root-finding technique that requires the first derivative of the function. It is often faster than bisection but requires an initial guess near the root. In Scipy we have the function scipy.optimize.newton() to perform the Newton-Raphson method. Following is the example which shows how to use the scipy.optimize.newton() function to find the roots of scalar functions −

from scipy.optimize import newton

# Define the objective function and its derivative
def objective_function(x):
    return x**2 - 4

def derivative_function(x):
    return 2*x

# Perform the root finding with an initial guess of 1.5
root = newton(objective_function, x0=1.5, fprime=derivative_function)

# Display the result
print("Root found at:", root)

Following is the output of using the scipy.optimize.newton() function −

Root found at: 2.0

Using the scipy.optimize.root for general equation

The root function in SciPy can be used for more complex root-finding tasks including systems of equations. In SciPy we have the function scipy.optimize.root() function to find the roots for a general non linear equation. Below is the example which shows how to use the scipy.optimize.root() function −

from scipy.optimize import root

# Define the objective function
def objective_function(x):
    return [x[0]**2 - 4, x[1] - x[0]]

# Initial guess
initial_guess = [1, 1]

# Find the root of the system
solution = root(objective_function, initial_guess)

# Display the result
print("Root found at:", solution.x)

Following is the output of using the scipy.optimize.root() function −

Root found at: [2. 2.]
Advertisements