# Python Scipy Optimize Root

We will learn about the “Python Scipy Optimize Root” to find the root of the given function using different methods such as Brenth, Ridder, and also cover the following topics.

• Python Scipy Optimize Root
• How to find the root of the scalar function
• How to use the method Lm to find the root of the function
• Python Scipy Optimize Root Jacobian
• How to find the root using the method Brentq
• How to find the root using the method Brenth
• Python Scipy Optimize Root Ridder

## Python Scipy Optimize Root

SciPy optimizes package provides routines for minimizing (or maximizing), sometimes subject to limitations, objective functions. Constrained and nonlinear least-squares, linear programming, nonlinear least-squares, curve fitting, and root finding are some of the nonlinear problem solvers that are included.

Here in this section, we will use the method `root()` of Python Scipy that exists in a module `scipy.optimize` that find a vector function’s root.

The syntax is given below.

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

Where parameters are:

• fun: A vector function for root finding.
• x0: (ndarray): Initial guess.
• args(tuple): Passed additional inputs to the objective function’s Jacobian.
• method(str): Solver type, ought to be one of lm, hybr, broyden1, broyden2, etc.
• jac(boolean): Fun is supposed to return the value of the Jacobian along with the objective function if jac is a Boolean and True. The Jacobian will be numerically estimated if False. Jac can also return the Jacobian of fun when called. It must in this instance accept the same justifications as fun.
• tol(folat): Tolerance for stopping. Use solver-specific settings for fine-grained control.
• callback: A callback function that is optional. Callback(x, f), where x is the current solution and f is the associated residual, is made on each iteration. for all techniques besides “hybr” and “lm.”
• options(dictionary): A list of possible solvers. For example, xtol or maxiter.

The `OptimizeResult` object, which is returned by this function, represents the solution. There are three primary components returned as part of the object:

Let’s take an example by following the below steps:

Import the required libraries or methods using the below python code.

``````import numpy as np
from scipy.optimize import root``````

The nonlinear equation system and its Jacobian are defined by the following functions.

``````def func(x):
return [x[0]  + 0.4 * (x[0] - x[1])**2 - 1.0,
0.4 * (x[1] - x[0])**2 + x[1]]

def jacb(x):
return np.array([[1 + 1.4 * (x[0] - x[1])**2,
-1.4 * (x[0] - x[1])**2],
[-1.4 * (x[1] - x[0])**2,
1 + 1.4 * (x[1] - x[0])**2]])``````

Find the solution using the below code.

``````solution = root(func, [0, 0], jac=jacb, method='hybr')
solution.x``````

This is how to find the root of the function using the method `root()` of Python Scipy with the method `hybr`.

## Python Scipy Optimize Root Scalar

The Python Scipy contains a method `root_scalar()` in a module `scipy.optimize` that identify the scalar function’s root.

The syntax is given below.

``scipy.optimize.root_scalar(f, args=(), method=None, bracket=None, fprime=None, fprime2=None, x0=None, x1=None, xtol=None, rtol=None, maxiter=None, options=None)``

Where parameters are:

• fun: A vector function for root finding.
• x0: (ndarray): Initial guess.
• x1(float): The second guess.
• args(tuple): Passed additional inputs to the objective function’s Jacobian.
• method(str): Solver type, ought to be one of bisect, newton, rider, etc.
• backet(A sequence of 2 floats): The signs of f(x, *args) at the two ends must differ in order for the interval to have a root.
• fprime(boolean, callable): The objective function and derivative values are thought to be returned by f if fprime is a boolean and are True. The callable fprime can also return the f derivative. It must in this instance accept the same justifications as f.
• fprime2(boolean, callable): The objective function’s value, as well as the first and second derivatives, are thought to be returned by f if fprime2 is a boolean and is True. The callable fprime2 can also return f’s second derivative. It must in this instance accept the same justifications as f.
• xtol(float): Absolute tolerance for termination.
• rtol(float): Relative tolerance towards termination.
• maxiter: Maximum number of iteration.
• options(dictionary): A list of possible solvers.

Let’s take an example by following the below steps:

Import the required libraries or methods using the below python code.

``from scipy.optimize import root_scalar``

Define cubic function using the below code.

``````def fun(x):
return (x**3 - 1)``````

Find the root of the cubic using the below code.

``````solution = root_scalar(fun, bracket=[0, 3], method='brentq')
solution.root, solution.iterations, solution.function_calls``````

This is how to find the root of the scalar function using the method `root_scalar()` of Python Scipy.

## Python Scipy Optimize Root Lm

The method `scipy.optimize.root()` accepts a parameter `method` that can take different methods for finding the root. There is a value `lm` for this parameter that uses Levenberg-Marquardt to find the least squares solution.

The syntax is given below.

``scipy.optimize.root(fun, tol=None, callback=None, options={'col_deriv': 0,, x0, args=(), method='lm', jac=None, 'xtol': 1.49012e-08, 'ftol': 1.49012e-08, 'gtol': 0.0, 'maxiter': 0, 'eps': 0.0, 'factor': 100, 'diag': None})``

Where parameters are:

col_deriv(boolean): For the Jacobian function to calculate derivatives down the columns, the value must be non-zero.

• ftol(float): Desired relative error in the sum of squares.
• xtol(float): Desired relative error in the approximation solution.
• gtol(floa): Desired orthogonality between the Jacobian’s column columns and the function vector.
• maxiter(int): The most calls that can be made to the function. If the value is zero, the maximum value is 100*(N+1), where N is the number of components in x0.
• epsfcn(float): a good step size for the forward-difference Jacobian approximation (for Dfun=None). It is believed that the relative errors in the functions are on the order of the machine precision if epsfcn is less than the machine precision.
• factor(float): a variable that controls the first step’s bound (factor * || diag * x||). Should be spaced out (0.1, 100).
• diag(sequence): The scale factors for the variables are N positive entries.

For the rest of the parameters, please refer to the first subsection “Python Scipy Optimize Root”.

## Python Scipy Optimize Root Jacobian

We have already learned about how the method `root()` works from the above subsection of this tutorial, now we will create a function to compute the Jacobian.

Let’s take an example by following the below steps:

``````import numpy as np
from scipy.optimize import root``````

Built a vector and Jacobian function using the below code.

``````def basefunc(x):
return [
x[1]  + 1.4 * (x[1] + x[0])**2 - 3.0,
0.4 * (x[0] - x[1])**3 + x[0]]

def jacob_func(a):
return np.array([[1 + 1.4 * (a[0])**2,
-2.4 * (a[0] - a[1])**2],
[-1.4 * (a[1] - a[0])**3,
1 + 1.4 * (a[0] + a[1])**2]]
)``````

To use the Anderson helper technique to determine the vector function’s root, use the `optimize.root` function.

``result = root(basefunc, [0, 0], jac=jacob_func, method='anderson')``

Check the result using the below code.

``print(result.x)``

This is how to find the root of the function with the jacobian function using the method `root()` of Python Scipy.

## Python Scipy Optimize Root Brentq

Python Scipy has a method `brentq()` in a module `scipy.optimize` that uses Brent’s approach to locate a function’s root in a bracketing interval.

The function f’s zero on the sign-changing interval [a, b] is determined using the traditional Brent’s method. The root finding procedure used here is regarded as the best in general. Utilizing inverse quadratic extrapolation is a secure variation of the secant approach.

Inverse quadratic interpolation, interval bisection, and root bracketing are all combined in Brent’s approach. The van Wijngaarden-Dekker-Brent approach is another name for it. According to Brent (1973), convergence is assured for functions that can be calculated within [a,b].

The syntax is given below.

``scipy.optimize.brentq(f, a, b, maxiter=100,  args=(), xtol=2e-12, rtol=8.881784197001252e-16, full_output=False, disp=True)``

Where parameters are:

• f(): Returning a number is the Python function. It is a requirement that f(a) and f(b) have opposite signs and that f is a continuous function.
• a(scalar): One end bracketing range [a,b].
• b(scalar): The other end bracketing range [a,b].
• xtol(number): np.allclose(x, x0, atol=xtol, rtol=rtol), where x represents the precise root, will be satisfied by the computed root, x0. The variable must not be negative. With xtol/2 and rtol/2, Brent’s technique frequently satisfies the aforementioned criterion for pleasant functions.
• rtol(number): np.allclose(x, x0, atol=xtol, rtol=rtol), where x represents the precise root, will be satisfied by the computed root, x0. The parameter’s default value of 4*np.finfo cannot be smaller (float). eps. With xtol/2 and rtol/2, Brent’s technique frequently satisfies the aforementioned criterion for pleasant functions.
• maxiter(int): An error is raised if convergence cannot be reached in the maximum number of repetitions. Must be more than or equal to 0.
• args(tuple): Applying (f, (x)+args) calls the function f with additional arguments.
• full_output(boolean): A return of the root occurs if full_output is False. Full-output returns the value (x, r), where x is the root and r is a RootResults object if full-output is True.
• disp(boolean): If True, if the algorithm didn’t converge, report RuntimeError. Otherwise, any RootResults return object contains a record of the convergence state.

The method `brentq()` returns `x0` (between a and b, f has zero).

Let’s take an example and find the root of the function using the method `brenqt()` by following the below steps:

Import the required libraries or methods using the below python code.

``from scipy.optimize import brentq``

Define the function whose root we want to find using the below code.

``````def fun(x):
return (x**3 - 1)``````

Now find the root of the function using the below code.

``brentq(fun, 1,0)``

This is how to find the root of the function in bracketing interval using the method `brentq()` of Python Scipy.

## Python Scipy Optimize Root Brenth

The method `brenth()` of Python Scipy module `scipy.optimize` uses Brent’s approach and hyperbolic extrapolation, one can find a function’s root in a bracketing interval.

An alternative to the traditional Brent method that employs inverse quadratic extrapolation instead of hyperbolic extrapolation to locate a zero of the function f between the parameters a and b.

The top bound of function evaluations using this method, according to Bus & Dekker (1975), is 4 or 5 times lower than that for bisection, guaranteeing convergence for this method.

The signs of f(a) and f(b) cannot be the same. On par with the brent routine generally, but not as rigorously tested. This hyperbolic extrapolation-based secant approach is a secure variation on the original.

The syntax is given below.

``scipy.optimize.brenth(f, a, b, args=(), xtol=2e-12, rtol=8.881784197001252e-16, maxiter=100, full_output=False, disp=True)``

Where parameters are:

• f(): Returning a number is the Python function. It is a requirement that f(a) and f(b) have opposite signs and that f is a continuous function.
• a(scalar): One end bracketing range [a,b].
• b(scalar): The other end bracketing range [a,b].
• xtol(number): np.allclose(x, x0, atol=xtol, rtol=rtol), where x represents the precise root, will be satisfied by the computed root, x0. The variable must not be negative. With xtol/2 and rtol/2, Brent’s technique frequently satisfies the aforementioned criterion for pleasant functions.
• rtol(number): np.allclose(x, x0, atol=xtol, rtol=rtol), where x represents the precise root, will be satisfied by the computed root, x0. The parameter’s default value of 4*np.finfo cannot be smaller (float). eps. With xtol/2 and rtol/2, Brent’s technique frequently satisfies the aforementioned criterion for pleasant functions.
• maxiter(int): An error is raised if convergence cannot be reached in the maximum number of repetitions. Must be more than or equal to 0.
• args(tuple): Applying (f, (x)+args) calls the function f with additional arguments.
• full_output(boolean): A return of the root occurs if full_output is False. Full-output returns the value (x, r), where x is the root and r is a RootResults object if full-output is True.
• disp(boolean): If True, if the algorithm didn’t converge, report RuntimeError. Otherwise, any RootResults return object contains a record of the convergence state.

The method `brenth()` returns `x0` (between a and b, f has zero).

Let’s take an example and find the root of the function using the method `brenth()` by following the below steps:

Import the required libraries or methods using the below python code.

``from scipy.optimize import brenth``

Define the function whose root we want to find using the below code.

``````def fun(x):
return (x**3 - 1)``````

Now find the root of the function using the below code.

``brentq(fun, 1,0)``

This is how to find the root of the function in bracketing interval with hyperbolic extrapolation, using the method `brentq()` of Python Scipy.

## Python Scipy Optimize Root Ridder

The method `ridder()` of Python Scipy module scipy.optimize uses Ridder’s method and locates a function’s interval root.

The syntax is given below.

``scipy.optimize.ridder(f, a, b, args=(), xtol=2e-12, rtol=8.881784197001252e-16, maxiter=100, full_output=False, disp=True)``
• f(): Returning a number is the Python function. It is a requirement that f(a) and f(b) have opposite signs and that f is a continuous function.
• a(scalar): One end bracketing range [a,b].
• b(scalar): The other end bracketing range [a,b].
• xtol(number): np.allclose(x, x0, atol=xtol, rtol=rtol), where x represents the precise root, will be satisfied by the computed root, x0. The variable must not be negative. With xtol/2 and rtol/2, Brent’s technique frequently satisfies the aforementioned criterion for pleasant functions.
• rtol(number): np.allclose(x, x0, atol=xtol, rtol=rtol), where x represents the precise root, will be satisfied by the computed root, x0. The parameter’s default value of 4*np.finfo cannot be smaller (float). eps. With xtol/2 and rtol/2, Brent’s technique frequently satisfies the aforementioned criterion for pleasant functions.
• maxiter(int): An error is raised if convergence cannot be reached in the maximum number of repetitions. Must be more than or equal to 0.
• args(tuple): Applying (f, (x)+args) calls the function f with additional arguments.
• full_output(boolean): A return of the root occurs if full_output is False. Full-output returns the value (x, r), where x is the root and r is a RootResults object if full-output is True.
• disp(boolean): If True, if the algorithm didn’t converge, report RuntimeError. Otherwise, any RootResults return object contains a record of the convergence state.

The method `ridder()` returns `x0` (between a and b, f has zero).

Let’s take an example and find the root of the function using the method `brenth()` by following the below steps:

Import the required libraries or methods using the below python code.

``from scipy.optimize import ridder``

Define the function whose root we want to find using the below code.

``````def fun(x):
return (x**4 - 2)``````

Now find the root of the function using the below code.

``ridder(fun, 0,3)``

We have learned about how to compute the root of the given function or scalar functions using methods like Ridder, Brentq, Brenth and ect, with the following topics.

• Python Scipy Optimize Root
• How to find the root of the scalar function
• How to use the method Lm to find the root of the function
• Python Scipy Optimize Root Jacobian
• How to find the root using the method Brentq
• How to find the root using the method Brenth
• Python Scipy Optimize Root Ridder

You may like the following Python Scipy tutorials: