Python Scipy Derivative of Array

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 derivative() in a module 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 x2+x3 whose derivative we need to find using the below code.

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)
Python Scipy Derivative of Function
Python Scipy Derivative of Function

This is how to find the derivative of a function using the method derivative() of Python Scipy.

READ:  NumPy array to a string in Python [6 Methods]

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 x3+x2 whose derivative we need to find using the below code.

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)
Python Second Derivative of function
Python Second Derivative of function

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 lambda the function whose derivative we need to find using the below code.

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)
Python Scipy Derivative of Array
Python Scipy Derivative of Array

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:

READ:  Tensorflow convert string to int

Import the necessary libraries using the below python code.

from scipy import misc

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

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 derivative() using the below code.

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.

Python Second Derivative of Array
Python Second Derivative of 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)
Python Plot Derivative of Array
Python Plot Derivative of Array

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 derivative() 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.

READ:  How to Use Django Built-In Login System

Suppose we have a function f(x)=6x2-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 derivative() of module 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)
Python Scipy Derivative Example
Python Scipy Derivative Example

Look at the above code, we have computed the derivative of function 6x2-9x which is 3.0.

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

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