Python - How To Calculate Derivative in Python

ID : 363

viewed : 75

Tags : PythonPython Math

vote vote

98

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

Derivative With the SymPy Library in Python

The 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.

  • Related HOW TO?