Python

SciPy Optimize Root

Python is an object-oriented, structured, and functional computer language. Its application is not just limited to specific types which makes it a general-purpose language. It is a well-familiar programming language, and it offers a variety of libraries for writing the different software programs in this language. The script of this language is also similar to the English language. By virtue of this fact, Python has the credit of being a beginner-friendly programming language for applications such as scientific and technical computing, integration, signal and image processing, and the interpolation of Python library which is “Scipy”, that deals with all such specific functions.

Scipy has an attribute of the function named “optimize. root ()”. This function contains the optimizers. The optimisers represent the set of those functions or the procedures that reduce the function’s ultimate value/output or the root of that function to the smallest value possible.

Procedure:

The procedure of this article follows some important steps. First of all, we will learn about the “optimize. root ()” function. Then, we will get to know what modules from the SciPy are required to work with this function. We will also learn about the syntax of optimize.root() function in the Python script. Then, we will do some examples to get hands on with this topic.

Syntax:

The following is the syntax and the declaration method for the calling of the optimize.root function in the Python program:

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

The first parameter function in the input argument list of this function is the “fun”. This is the equation or the function on which we want to apply the “optimize. root() “function. Next is “x0” which is an initial guest to compute for the roots. The “args” is the additional parameter that we can add to the function’s objective. And the “method” is an important parameter that defines the type of the solver of the optimizer function. “Jac” means jacobian. If its value is specified to the “True” value, the function returns the jacobian’s value with the objection function. In an alternate case, the jacobian is calculated numerically and it is another optional parameter. Then the “tol” is the tolerance for the stopping of the function and depends on the type of the solver; again, an optional parameter.

Return Value:

The function returns the optimized result type object in the output which has the array that represents the solution. The success message is a Boolean flag which means that the function is exited with success, and the cause of the termination message.

Example 1:

The “optimize.root()” function optimizes the function and reduces or increases the value as per the requirement in the program. It also finds the roots for a function which can be in the form of some non-linear equation.

With the previous syntax that we have to learn, let’s do a very basic example for this optimize.root() function and find the roots for a non-linear equation or function. The platform that we will use to use the Python compiler is “Google Collab”. This is an open-source program and provides all the packages that are already installed and downloaded. To start with the new program, allocate a new notebook in Google Drive in the collab.

After successfully creating a notebook, let’s integrate the required Python packages into the program. For that, we will integrate the SciPy. Since the SciPy’s attribute optimize has the “root” module, we have to import the root from the “scipy.Optimize”. Since we already discussed that the optimize.root() takes the “func” in its input argument list and the func is simply a form of the non-linear equation, we integrate the “math” library to write these non-linear equations with the module that is required in the function’s equation. The equation for the function of this example is as follows:

a + sin(a)

The given equation has the trigonometric function in it which is “sin”. We integrate the math library as the “sin”. With this import of the required Python packages, we now define the equation in the function. For this task, we create a function with the “func” name and the return type “def”. We pass the variable “a” to the argument of the function. Then, we make the function return the equation as “return x + sin(x)”.

Now, we call this function to use this equation in the “root()” method as its input argument. Call the “root ()” function and pass the name of the function for which we define the equation as “func” along with the initial guess value equal to “0” for the roots. Now, display the results with the print() method. The program for this function with its output is shown in the following snippet:

from scipy.optimize import root

from math import sin

def func(a):

 return a + sin(a)

value = root(func, 0)

print(value)

The function returned the solution array as the root of the equation. The solution has converged.

Example 2:

With the same method as we learned in the first example, we can add the “jac” function with the equation and apply the “optimize.root()” function on it to get the minimum value for the function. Import the “scipy.optimize” as “root” and the “NumPy” as “np”. Declare the “func” function and return the equation in it as “ [a[0]  + 0.4 * (a[0] – a[1])**2 – 1.0, 0.4 * (a[1] – a[0])**2 + a[1]]”. Define another function, “jacobian”, which is the additional parameter and return the equation in it as “ [[1 + 1.4 * (a[0] – a[1])**2,  -1.4 * (a[0] – a[1])**2], [-1.4 * (a[1] – a[0])**2, 1 + 1.4 * (a[1] – a[0])**2]]”. Pass the “func”, initial guess “x0”, the “jacobian”, and the “solver /method” as “hybr” to the “root()” function and run the program as follows:

import numpy as np

from scipy.optimize import root

def func(a):

  return [a[0]  + 0.4 * (a[0] - a[1])**2 - 1.0,

       0.4 * (a[1] - a[0])**2 + a[1]]

def jacobian(a):

 return np.array([[1 + 1.4 * (a[0] - a[1])**2, -1.4 * (a[0] - a[1])**2][-1.4 * (a[1] - a[0])**2, 1 + 1.4 * (a[1] - a[0])**2]])

value = root(func, [00]jac=jacobian, method='hybr')

print(value)

The function returned the optimized roots as the solution array with the other two parameters.

Conclusion

The working and implementation of the “SciPy’s optimize.root()” function is shown in this article. We performed the root() function on two separate examples and with different function parameters.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content