Python

SciPy Optimize Root

A “Root” of a vector function is a point where the function is equivalent to zero. Finding the roots of a function is useful for many applications, such as finding the equilibrium points of a dynamical system, solving inverse problems, or optimizing nonlinear functions. The “scipy.optimize.root()” function in Python is used to find a root of a vector function using various methods, such as hybrid, Levenberg-Marquardt, etc.

This post provides a complete guide on Python’s “scipy.optimize.root()” function via the below content:

What is the “scipy.optimize.root()” Function in Python?

In Python, the “scipy.optimize.root()” function finds the root of a vector function. Roots are values that give zero results when passed into a function. The “scipy.optimize.root()” function can use various methods/parameters to find the root of a function, including the “Newton-Raphson” method, the bisection method, or the secant method.

Syntax

scipy.optimize.root(fun, x0, args=(), method='hybr', jac=None, tol=None, callback=None, options=None)

 

Parameters

In the above syntax:

  • The “fun” parameter specifies the vector function for which we need to find a root. This function must accept a vector of arguments and retrieve a vector of equal size.
  • The “x0” parameter is the initial/starting root guess.
  • The “args” is an extra parameter added to the objective function and its Jacobian.
  • The “method” is an extra parameter to specify the type of solver method, such as hybr, lm, broyden1, etc.
  • The “jac” parameter is a Boolean or a callable. When “jac” is set to “True”, the function “fun” also returns the Jacobian value besides the objective function value. If “jac” is “False”, the Jacobian will be computed numerically. If “jac” is callable, it must return the Jacobian of “fun”.
  • The “tol”, callback”, and “options” are optional parameters that can also be used to perform specific operations.

Return Value

The return value of the “scipy.optimize.root()” function is an “OptimizeResult” object. This object consists of the below attributes:

  • x”: The solution vector.
  • success”: Indicates whether or not the algorithm has succeeded.
  • message”: A string explaining the reason for the termination.

Example 1: Applying the “scipy.optimize.root()” Function to Find the Root of the Function

The following code uses the “scipy.optimize.root()” function to find the root of the function “f(x) = x**4 – 4”.

import numpy
import scipy
def func(x):
    return x**4 - 4
x0 = 1.0
result = scipy.optimize.root(func, x0)
print('OptimizeResult:\n', result)
print('\nSolution Vector: ', result.x)

 

In the above code:

  • The “numpy” and the “scipy” libraries are imported.
  • The function named “func” is defined to take “x” and return “x” raised to the power of “4” minus “4”.
  • The “0” value is assigned to a variable named “x0”, which will be used as an initial guess for the root of the function.
  • The “scipy.optimize.root()” function finds a zero of the function using “x0” as a starting point. This function returns an “OptimizeResult” object containing information about the optimization process and the solution.
  • The solution vector from the “OptimizeResult” object is obtained using the dot notation. The solution vector is an array that contains the root of the function.

Output

The above snippet shows the optimization status, such as the number/amount of iterations, function calculations, and other details, along with the solution vector.

Example 2: Applying the “scipy.optimize.root()” Function to Find the Root of the Function Using the “hybrid” Method

The “hybrid” method is a combination of the “Newton-Raphson” and the bisection methods. The “scipy.optimize.root()” function uses the hybrid method as it is a good compromise between speed and robustness.

The following code is utilized to find the root of the equation using the “hybrid” method:

import numpy
import scipy
def func(x):
    return numpy.sin(x) - 0.5
x0 = 1.0
result = scipy.optimize.root(func, x0, method='hybr')
print('OptimizeResult:\n', result)
print('\nSolution Vector: ',result.x)

 

In the above code, the “scipy.optimize.root()” function uses the “hybrid” method i.e., “hybr” to find the root of the function “f(x) = np.sin(x) – 0.5”.

Output

The above output shows the optimized result object along with the solution vector.

Conclusion

In Python, the “scipy.optimize.root()” function of the “seaborn” library is utilized to determine the root of a vector function. This function can use various parameters/methods to find the root of a function, such as the “Newton-Raphson”, “bisection”, and “secant” methods. This Python article delivered a complete guide on the “scipy.optimize.root()” function using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.