In this Python tutorial, we will learn about the “** Python Scipy Derivative of Array**” to differentiate a given function or functions within the array to find the derivative of these functions. Additionally, cover the following topics.

- Python Scipy Derivative of Function
- Python SciPy Second Derivative of Function
- Python Scipy Derivative of Array
- Python SciPy Second Derivative of Array
- Python SciPy Plot Derivative of Array
- Python Scipy Derivative of Array Example

## Python Scipy Derivative of Function

A function’s rate of change concerning an independent variable can vary, and this is what derivatives are. When a variable quantity and a variable rate of change exist, the derivative is most frequently utilized.

The Python Scipy has a method** ** in a module

`derivative()`

*scipy.misc*

that finds a point’s value for a function’s nth derivative.The syntax is given below.

`scipy.misc.derivative(func, x0, dx=1.0, n=1, args=(), order=3)`

Where parameters are:

**func:**It is the input function.**x0(float):**A point where the nth derivative can be discovered.**dx(float):**It is a spacing.**n(int):**The derivative’s order. The default value is 1.**args(tuple):**It is used to provide the arguments.**order(int):**Use just an odd number of points.

Let’s find the derivative of any function by following the below steps:

Import the necessary libraries using the below python code.

`from scipy import misc`

Define the function

whose derivative we need to find using the below code.*x ^{2}+x^{3}*

```
def fun(x):
return x**2 + x**3
```

Now find the derivative of the above function using the below code.

`misc.derivative(fun, 1.5, dx=1e-2)`

This is how to find the derivative of a function using the method

of Python Scipy.*derivative()*

Read: Python Scipy Gamma

## Python SciPy Second Derivative of function

The second derivative, roughly speaking, measures how a quantity’s rate of change is itself changing. For example, the second derivative of an object’s position concerning time is the object’s instantaneous acceleration or the rate at which the object’s velocity is changing concerning time.

Let’s find the 2nd derivative of any function by following the below steps:

Import the necessary libraries using the below python code.

`from scipy import misc`

Define the function

whose derivative we need to find using the below code.*x ^{3}+x^{2}*

```
def fun(x):
return x**3 + x**2
```

Now find the derivative of the above function using the below code.

`misc.derivative(fun,1, n=2, dx=1e-4)`

This is how to find the 2nd derivative of the function using the method `misc.derivative`

of Python Scipy.

Read: Python Scipy Stats Poisson

## Python Scipy Derivative of Array

To compute the derivative of the array, we will use the python loop with the derivative function in this section.

Let’s see with an example by following the below steps:

Import the necessary libraries using the below python code.

`from scipy import misc`

Define the functions using

the function whose derivative we need to find using the below code.*lambda*

```
fun1 = lambda x: x**2+3*x+2
fun2 = lambda x: x**3+3*x+2
```

Create an array of the above functions using the below code.

`fun=[fun1,fun2]`

Compute the derivative of an array containing functions using the below code.

```
for i in range(len(fun)):
ans = derivative(fun[i],1)
print('Derivative of {} function is'.format(i+1),ans)
```

The above output contains the derivative of the above functions that are 5.0 and 7.0.

Read: Python Scipy Kdtree

## Python SciPy Second Derivative of Array

In this section, we will utilize the Python loop with the derivative function to calculate the array’s second derivative.

Let’s see with an example by following the below steps:

Import the necessary libraries using the below python code.

`from scipy import misc`

Define the functions using

the function whose derivative we need to find using the below code.*lambda*

```
func1 = lambda x: x**4+2*x**2+2
func2 = lambda x: x**3+2*x**2+2
```

Create an array of the above functions by using the parameter of the method

using the below code.*derivative()*

`func=[func1,func2]`

Compute the second derivative of an array containing functions using the below code.

```
for i in range(len(fun)):
ans = derivative(fun[i],1,n=2)
print('Derivative of {} function is'.format(i+1),ans)
```

In the above code, we are passing the parameter n=2 to a method derivative() for computing the second-order derivative of the functions within the array.

This is how to calculate the second order of the derivative of the array using the method `derivative()`

with parameter `n`

of Python Scipy.

Read: Python Scipy Eigenvalues

## Python SciPy Plot Derivative of Array

We have already learned about how to compute the derivative from the above subsection, now we will compute the derivative and plot that derivative to see how it looks.

Let’s compute and plot the derivative by following the below steps:

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

```
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import derivative
```

Now define the function using the below code.

```
def fun(x):
return 3*x*x*x+x+2
```

Compute the derivative of the above function using the below code.

```
def derivate(x):
return derivative(fun, x)
```

Define the x-axis intervals using the below code.

`y_val = np.linspace(-10, 10)`

Plot the above function and derivative using the below code.

```
plt.plot(y_val, fun(y_val), color='blue', label='Function')
plt.plot(y_val, derivate(y_val), color='red', label='Derivative')
plt.legend(loc='upper left')
plt.grid(True)
```

This is how to plot the derivative of the array using the matplotlib library methods.

Read: Python Scipy Stats Mode

## Python Scipy Derivative Example

We have already learned about the

method of Python Scipy and how to calculate the derivative of any function. Here in this section, we will take the function and find its derivative in a detailed manner along with mathematical details.*derivative()*

Suppose we have a function f(x)=** 6x^{2}-9x** and we want to find the derivative of this function at x=1, then first we will differentiate it as shown below:

After differentiating, it becomes f(x)=`12x-9`

, now put the value of x in the differentiated expression as f(1) = `12*1-9`

, and it becomes 3. So the answer or derivative of the above function is 3.

Let’s see the same example with the method** ** of module

`derivative()`

*scipy.misc*

of Python Scipy by following the below steps:Import the required libraries or methods using the below python code.

`from scipy.misc import derivative`

Define the function using the below code.

```
def fun(x):
return 6*x**2 -9*x
```

Now find the derivative at x=1 using the below code.

`derivative(fun,1)`

Look at the above code, we have computed the derivative of function ** 6x^{2}-9x **which is 3.0.

You may also like to read the following Python SciPy tutorials.

- Python Scipy Load Mat File
- Python Scipy Butterworth Filter
- Python Scipy Stats Multivariate_Normal
- Python Scipy Minimize [With 8 Examples]
- Python Scipy Distance Matrix
- Python Scipy Confidence Interval

So, in this tutorial, we have covered the “** Python Scipy Derivative of Array**” and covered the following topics.

- Python SciPy Second Derivative of Function
- Python Scipy Derivative of Array
- Python SciPy Second Derivative of Array
- Python SciPy Plot Derivative of Array
- Python Scipy Derivative of Array Example

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etcâ€¦ for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.