Python

SciPy Misc Derivative

This Python guide will make you learn about the SciPy misc derivative to compute the derivative of the given data. Python programming language is becoming famous and a favorite language of all kinds of developers due to its amazing libraries and built-in functions. These functions allow us to perform the complex and practical tasks quickly and automatically without making any mistakes. Python made the development a very simple and easy task. We can perform any statistical, scientific, or mathematical operation using the built-in functions of the Python programming language. By the end of this article, you will be able to understand and use the SciPy misc derivative function in your Python programs.

What Is Derivative?

The derivative of a function is concerned with the rate of change of a function according to the independent variable. Derivatives are utilized when the rate of change and the quantity of the variable exists. The Python programming language provides the derivative() function in the misc module of the SciPy library to find the derivative of a function. In this tutorial, we will guide you on how you can use the derivative() function and what elements do you need to use the derivative function in a Python program. We will demonstrate some examples for your guidance. But before that, let us learn and understand the syntax of the derivative function.

Syntax of the Derivative() Function

The derivative() function of the SciPy library performs the same function that we accomplish manually for a math problem. Here, we need to provide the same inputs for the derivative function. See the syntax of the following derivative function:

As discussed previously, the SciPy library and its misc module provide the derivative() function. It takes almost 6 parameters out of them, some are required parameters and some are optional. The “function” parameter represents the function of which the derivatives need to be found. The “x” parameter represents the nth derivative point. The “dx” parameter specifies the spacing. The “n” parameter specifies the order of derivative. The “args” parameter is used to provide the different arguments. Lastly, the “order” parameter specifies the number of points for the derivative function. Now, let us find the derivatives of different functions in the following example section.

Example 1:

In the first example, we will guide you on the step-by-step process and make you learn how to find the first derivative of a function using the derivative() function. Consider the given sample code in the following code snippet:

from scipy import misc
def fun(x):
    return x**5 + x**8
misc.derivative(fun, 1.8, dx=1e-2)

First, we need to import the misc module from the SciPy library so that we can use the derivative() function without facing any errors. After that, a function named “fun” is declared and the function = x**5 + x**8 is used for the derivative. The function and other parameters are passed to the derivative() function to find the derivative. Now, let’s check the output of the following derivative() function:

Example 2:

Let us consider another example where one function depends on the second function. Previously, we provided the equation in the function and simply returned the result. Here, we pass the variable value and call one function but from another function. Consider the given sample code in the following snippet:

from math import *
from scipy import misc
def f(x):
    return exp(x)
def df(x):
    return misc.derivative(f,x)
df(5)

We will use the exp() function so we import the math library into the program. After that, the SciPy library and misc module are imported so that we can use the derivative() function. As you can see, there are two functions – the first is f() and the second is df(). The function named df() receives the value of the variable and calls the f()function for derivative computation. The f()function performs the computation. It is supposed to perform and return the result to the df() function. The output of the derivative() function is given in the following:

Example 3:

Now that we learned to find the derivative of one function, let’s practice on determining the array of functions’ derivatives. You can learn how to obtain the function array’s derivative from this example. See the following sample code:

from scipy import misc
fun1 = lambda x: x**1 + 3*x + 1
fun2 = lambda x: x**2 + 3*x + 2
fun3 = lambda x: x**3 + 3*x + 3
fun4 = lambda x: x**4 + 3*x + 4
fun5 = lambda x: x**5 + 3*x + 5
funs=[fun1 ,fun2, fun3, fun4, fun5]
for i in range(len(funs)):
    dev = derivative(funs[i],1)
    print('Derivative of function {} is = '.format(i+1), dev)

First, the SciPy library and misc module are imported which is essential to import. Then, five functions are declared. The array of five functions is listed in the funs variable. The “for;” loop is used to iterate through each function listed in the array and perform the derivative

Example 4:

The derivative of a function and the derivative of an array of functions are concepts that we learned in the previous sections. Now, we understand how the derivative function is used. Let us display the output of the derivative function in a graph. Consider the following sample code:

import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import derivative
def fun(x):
    return x**5 + 3*x + 5
def derivate(x):
    return derivative(fun, x)
y = np.linspace(-10, 10)
plt.plot(y, fun(y), color='green', label='Function')
plt.plot(y, derivate(y), color='yellow', label='Derivative')
plt.legend(loc='lower right')
plt.grid(True)

We need three libraries in this program: NumPy, matplotlib, and SciPy. The NumPy library allows us to use the linespace() function. The SciPy library allows us to use the derivative function. Lastly, the matplotlib allows us to plot the result onto the graph. Two functions are declared just like in the previous example. One function calls another function for derivative computation. To plot the graph, we use the np.linespace(-10, 10) area. All the cosmetics of the graph are done in the plt.plot() function. Now, let us see the following graph:

Conclusion

This tutorial is a complete guide on the derivative() function of the SciPy library. The SciPy library provides the misc module that offers the derivative() function. The derivative() is a built-in function of the misc module which performs the same function that we manually perform to solve a math problem. It allows us to solve the derivative automatically just by taking the few parameters of the function. The examples of the derivative() function thoroughly explain how to implement the derivative() function in a Python program. Practicing these examples will help you get a command of the derivative() function.

About the author

Kalsoom Bibi

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