Calculate Derivative in Python

Calculate Derivative in Python

This tutorial will introduce the methods to calculate the derivate of a function in Python.

Derivative With the SymPy Library in Python

The SymPy library is known as the Python Symbolic library. It can be used to perform complex mathematical operations like derivatives on functions in Python. The diff() function inside the SymPy library can be used to calculate the derivative of a function. We can specify the variable with which we want to calculate the derivative with the Symbol() function in Python. See the following code example.

from sympy import *
import numpy as np

x = Symbol('x')
y = x**2 + 1

yprime = y.diff(x)
print(yprime)

Output:

2*x

In the above code, we calculated the derivative of the function x^2 + 1 with the diff() function of the SymPy library in Python. We specified the symbol to be x with the Symbol() function and calculated the derivative with respect to the symbol x.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python Math

  • Calculate Factorial in Python
  • Calculate Inverse of Cosine in Python
  • Calculate Modular Multiplicative Inverse in Python
  • Fit Poisson Distribution to Different Datasets in Python
  • Reduce Fractions in Python
  • Define an Infinite Value in Python