You have four options

  1. Finite Differences
  2. Automatic Derivatives
  3. Symbolic Differentiation
  4. Compute derivatives by hand.

Finite differences require no external tools but are prone to numerical error and, if you're in a multivariate situation, can take a while.

Symbolic differentiation is ideal if your problem is simple enough. Symbolic methods are getting quite robust these days. SymPy is an excellent project for this that integrates well with NumPy. Look at the autowrap or lambdify functions or check out Jensen's blogpost about a similar question.

Automatic derivatives are very cool, aren't prone to numeric errors, but do require some additional libraries (google for this, there are a few good options). This is the most robust but also the most sophisticated/difficult to set up choice. If you're fine restricting yourself to numpy syntax then Theano might be a good choice.

Here is an example using SymPy

In [1]: from sympy import *
In [2]: import numpy as np
In [3]: x = Symbol('x')
In [4]: y = x**2 + 1
In [5]: yprime = y.diff(x)
In [6]: yprime
Out[6]: 2⋅x

In [7]: f = lambdify(x, yprime, 'numpy')
In [8]: f(np.ones(5))
Out[8]: [ 2.  2.  2.  2.  2.]
Answer from MRocklin on Stack Overflow
Top answer
1 of 9
208

You have four options

  1. Finite Differences
  2. Automatic Derivatives
  3. Symbolic Differentiation
  4. Compute derivatives by hand.

Finite differences require no external tools but are prone to numerical error and, if you're in a multivariate situation, can take a while.

Symbolic differentiation is ideal if your problem is simple enough. Symbolic methods are getting quite robust these days. SymPy is an excellent project for this that integrates well with NumPy. Look at the autowrap or lambdify functions or check out Jensen's blogpost about a similar question.

Automatic derivatives are very cool, aren't prone to numeric errors, but do require some additional libraries (google for this, there are a few good options). This is the most robust but also the most sophisticated/difficult to set up choice. If you're fine restricting yourself to numpy syntax then Theano might be a good choice.

Here is an example using SymPy

In [1]: from sympy import *
In [2]: import numpy as np
In [3]: x = Symbol('x')
In [4]: y = x**2 + 1
In [5]: yprime = y.diff(x)
In [6]: yprime
Out[6]: 2⋅x

In [7]: f = lambdify(x, yprime, 'numpy')
In [8]: f(np.ones(5))
Out[8]: [ 2.  2.  2.  2.  2.]
2 of 9
82

The most straight-forward way I can think of is using numpy's gradient function:

x = numpy.linspace(0,10,1000)
dx = x[1]-x[0]
y = x**2 + 1
dydx = numpy.gradient(y, dx)

This way, dydx will be computed using central differences and will have the same length as y, unlike numpy.diff, which uses forward differences and will return (n-1) size vector.

🌐
Turing
turing.com › kb › derivative-functions-in-python
A Quick Guide to Calculating Derivatives in Python
With autograd, we can easily compute derivatives of univariate and multivariate functions. It automatically handles complex computations and provides accurate gradients without the need for explicit differentiation. Incorporating autograd into our Python code lets us streamline the process of derivative calculations and efficiently optimize functions in various domains.
Discussions

Derivatives in python
As some people have said, doing approximate derivatives numerically is pretty simple. To expand, you can make a higher order function that takes in a function and returns the function's approximate derivative as follows: def make_derivative(func,h): def derivative(x): return (func(x+h) - func(x))/h return derivative This returns a function that behaves like a derivative, and of course the smaller your h, the better your approximation. This is nice because you don't have to enter the function you are differentiating every time you compute a value of a given derivative. For example: def square(x): return x*x d_square = make_derivative(square,.0000001) >>>d_square(1) 2.0000001010878066 >>>d_square(2) 4.000000091153311 More on reddit.com
🌐 r/Python
22
19
December 8, 2011
I made a derivative calculator using Python!
I see SICP, I upvote. Cheers! More on reddit.com
🌐 r/Python
8
64
February 21, 2023
🌐
DEV Community
dev.to › erikwhiting88 › calculate-derivative-functions-in-python-h58
Derivative Python: Calculate Derivative Functions in Python - DEV Community
September 3, 2019 - Derivatives have a lot of use in tons of fields, but if you're trying to figure out how to calculate one with Python, you probably don't need much more explanation from me, so lets just dive in. If you don't already have the SymPy library, go ahead and run pip install sympy.
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.differentiate.derivative.html
derivative — SciPy v1.17.0 Manual
An optional user-supplied function to be called before the first iteration and after each iteration. Called as callback(res), where res is a _RichResult similar to that returned by derivative (but containing the current iterate’s values of all variables). If callback raises a StopIteration, the algorithm will terminate immediately and derivative will return a result.
🌐
Towards Data Science
towardsdatascience.com › home › latest › taking derivatives in python
Taking Derivatives in Python | Towards Data Science
January 28, 2025 - Your function f(x) is equal to x to the fifth. Now use the power rule to calculate the derivative. It’s pretty straightforward: Now let’s take a look at how to calculate it in Python.
🌐
Medium
medium.com › @jamesetaylor › create-a-derivative-calculator-in-python-72ee7bc734a4
Create A Derivative Calculator in Python | by James Taylor | Medium
February 13, 2018 - It would take a value and then return the output of the function. ... Simple enough. Now we have to derive it. I created a new python function that would take two paraments. The first parameter was a function — like f — and the value at which to derive and find the slope.
🌐
Learning About Electronics
learningaboutelectronics.com › Articles › How-to-find-the-derivative-of-a-function-in-Python.php
How to Find the Derivative of a Function in Python
If we want to calculate the value of the derivative at a particular value of x- for example, when x=4, we use the subs() method. This is shown below. Running this code gives the following output shown below. And this is all that is required to find the derivative of a function in Python.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sympy-derivative-method
Python | sympy.Derivative() method - GeeksforGeeks
July 12, 2025 - Syntax: Derivative(expression, reference variable) Parameters: expression - A SymPy expression whose unevaluated derivative is found. reference variable - Variable with respect to which derivative is found.
Find elsewhere
🌐
Medium
medium.com › @whyamit404 › understanding-derivatives-with-numpy-e54d65fcbc52
Understanding Derivatives with NumPy | by whyamit404 | Medium
February 8, 2025 - This might surprise you: Even though np.gradient() is designed for numerical data, it can give you the derivative of any function, provided you have the right data points. ... Let’s get into some code to make this clearer.
🌐
TutorialsPoint
tutorialspoint.com › how-to-calculate-and-plot-the-derivative-of-a-function-using-python-ndash-matplotlib
How to Calculate and Plot the Derivative of a Function Using Python – Matplotlib?
October 11, 2023 - We have created x-axis values using the linspace() function from numpy, and used them to calculate the derivative values. Finally, we plot the function and its derivative using the plot function and add a legend to the graph using the legend function. In this article, we have calculated the ...
🌐
AskPython
askpython.com › home › derivatives in python using sympy
Derivatives in Python using SymPy - AskPython
August 6, 2022 - ... Let’s see how can we achieve this using SymPy diff() function. #Importing sympy from sympy import * # create a "symbol" called x x = Symbol('x') #Define function f = x**2 #Calculating Derivative derivative_f = f.diff(x) derivative_f
🌐
Andrea Minini
andreaminini.net › computer-science › python › calculating-derivatives-of-a-function-in-python
Calculating Derivatives of a Function in Python - Andrea Minini
To compute the first, second, and third derivatives of a function in Python, you can use the diff() function from the SymPy library, which is designed for symbolic mathematics.
🌐
Joshua Bowen's Notes
softwarenotebook.com › 2022 › 01 › 01 › calculate-derivative-functions-in-python
Calculate Derivative Functions in Python – Joshua Bowen's Notes
January 2, 2022 - Symbol is basically just the SymPy term for a variable. Our example function f(x) = 2x2+5 has one variable x, so let’s create a variable for it: ... Symbols can be used to create symbolic expressions in Python code.
🌐
YouTube
youtube.com › shorts › MPcHmfDgAVs
TWO Ways To Take A Derivative In PYTHON #python #coding #shorts - YouTube
Check out my course on UDEMY: learn the skills you need for coding in STEM:https://www.udemy.com/course/python-stem-essentials/A quick tutorial for numerical...
Published   November 15, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-calculate-and-plot-the-derivative-of-a-function-using-matplotlib-and-python
How to calculate and plot the derivative of a function using matplotlib and python ? - GeeksforGeeks
March 28, 2023 - SciPy: Python has a library named as SciPy that is used for mathematical, scientific and engineering calculations. This library depends on NumPy, and provides various numerical operations. ... Use numpy linspace function to make x-axis spacing. ... Explanation: The scipy.misc library has a derivative() function which accepts one argument as a function and other is the variable w.r.t which we will differentiate the function.
🌐
Svitla Systems
svitla.com › home › articles › numerical differentiation methods in python
Python for Numerical Differentiation: Methods & Tools
January 14, 2021 - We’re going to use the scipy derivative to calculate the first derivative of the function. Please don’t write your own code to calculate the derivative of a function until you know why you need it.
Price   $$$
Address   100 Meadowcreek Drive, Suite 102, 94925, Corte Madera
🌐
Delft Stack
delftstack.com › home › howto › python › python derivative
How to Calculate Derivative in Python | Delft Stack
February 26, 2025 - Learn how to calculate derivatives in Python using the SymPy library. This article provides step-by-step instructions and code examples for differentiating simple and complex functions, including polynomials and trigonometric functions. Discover how to use SymPy's lambdify function for numerical ...
🌐
Reddit
reddit.com › r/physics › how to take derivatives in python: 3 different types of scenarios
r/Physics on Reddit: How To Take Derivatives In Python: 3 Different Types of Scenarios
August 9, 2021 - The first scenario is when you have an explicit form for your function, such as f(x)=x2 or f(x)=ex sin(x). In such a scenario, the sympy library can be used to take first, second, up to nth derivatives of a function.