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
Python has excellent mathematical libraries such as NumPy and SciPy, along with packages like SymPy and autograd, making it ideal for calculating derivatives.
Discussions

Calculate derivative of function
Suppose I have a function def f(x): return torch.sin(x) I can find the derivative of the function at a point as folllows: x = torch.tensor([2.], requires_grad=True) y = f(x) y.backward(retain_graph=True) x.grad However, this requires computing the value of f(x) in order to find it’s derivative, ... More on discuss.pytorch.org
🌐 discuss.pytorch.org
8
0
September 30, 2019
How To Take Derivatives In Python: 3 Different Types of Scenarios
How To Take Derivatives In Python: 3 Different Types of Scenarios In this video I show how to properly take derivatives in python in 3 different types of scenarios. 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. This comes in handy for complicated functions, but can later on be EXTREMELY useful for computing Lagrange's equations of motion given strange trajectories. The second scenario is when you collect data and want to compute a derivative. In such a scenario, the data is often noisey, and taking a simple derivative will fail since it will amplify the high-frequency component of the data. In such a case, one needs to smooth data before taking a derivative. The ideal library for managing this is numpy. The third scenario involves functions of an irregular form. By this, I mean that your function can't be written down as simply as "sin(x)" or "ex". For example. f(x) = "solve an ode using some complex odesolver with parameter abserr=x and compute the integral of the answer". In this case, derivatives can't be computed symbolically, but one can use scipy's derivative method to get a good estimate of df/dx at certain values of x. More on reddit.com
🌐 r/Physics
4
408
August 9, 2021
I made a derivative calculator using Python!
I see SICP, I upvote. Cheers! More on reddit.com
🌐 r/Python
8
64
February 21, 2023
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
🌐
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 - ... 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.
🌐
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 - Create A Derivative Calculator in Python I first learned of derivatives in my sophomore year of high school. They astounded me. Its amazing that we can use infinity and limits to find certain slopes …
🌐
DEV Community
dev.to › erikwhiting88 › calculate-derivative-functions-in-python-h58
Derivative Python: Calculate Derivative Functions in Python - DEV Community
September 3, 2019 - If you have a function that can be expressed as f(x) = 2x^2 + 3 then the derivative of that function, or the rate at which that function is changing, can be calculated with f'(x) = 4x.
🌐
Medium
medium.com › @whyamit404 › understanding-derivatives-with-numpy-e54d65fcbc52
Understanding Derivatives with NumPy | by whyamit404 | Medium
February 8, 2025 - Now that you have a basic understanding of derivatives, let’s look at how NumPy can help us calculate them. In NumPy, we don’t have a dedicated function for derivatives. Instead, we use np.gradient(). This function calculates the derivative using numerical differentiation.
Find elsewhere
🌐
Svitla Systems
svitla.com › home › articles › numerical differentiation methods in python
Python for Numerical Differentiation: Methods & Tools
January 14, 2021 - To get more information about scipy.misc.derivative, please refer to this manual. It allows you to calculate the first order derivative, second order derivative, and so on. It accepts functions as input and this function can be represented as a Python function.
Price   $$$
Address   100 Meadowcreek Drive, Suite 102, 94925, Corte Madera
🌐
AskPython
askpython.com › home › derivatives in python using sympy
Derivatives in Python using SymPy - AskPython
August 6, 2022 - We can use SymPy library to calculate derivatives in Python. We can calculate differentiation, derivative, partial derivative using diff(), lambdify().
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.differentiate.derivative.html
derivative — SciPy v1.17.0 Manual
For each element of the output of f, derivative approximates the first derivative of f at the corresponding element of x using finite difference differentiation. This function works elementwise when x, step_direction, and args contain (broadcastable) arrays.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-sympy-derivative-method
Python | sympy.Derivative() method - GeeksforGeeks
July 12, 2025 - With the help of sympy.Derivative() method, we can create an unevaluated derivative of a SymPy expression. It has the same syntax as diff() method.
🌐
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
So, the first thing, we must do is import Symbol and Derivative from the sympy module. As explained above, this module must be installed by you. The next thing we must do is specify the variable (or symbol) that we want to differentiate with respect to. In our function, the independent variable ...
🌐
Medium
medium.com › @rajat01221 › how-to-create-a-python-program-to-solve-differentiation-2a90cc1cef8f
How to Create a Python Program to Solve Differentiation | by Rajat Sharma | Medium
July 25, 2024 - For example, let’s differentiate the function f(x)=x²+3x+5 ... We can use the central difference method to approximate the derivative. The central difference method is given by: ... Now, we can evaluate the numerical derivative at a specific ...
🌐
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 - In this article, we will be calculating the derivative of a function using the NumPy library and visualize it with the help of Matplotlib in Python. A derivative is the rate at which a function changes with respect to its input. It gives the slope of the tangent line of the function at any point, and can also be used to find maxima, minima, critical points, and inflection points.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-compute-derivative-using-numpy
How to compute derivative using Numpy? - GeeksforGeeks
July 23, 2025 - However, NumPy can compute the special cases of one-dimensional polynomials using the functions numpy.poly1d() and deriv().
🌐
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.
🌐
Quora
quora.com › How-do-you-write-a-code-that-calculates-the-derivative-numerically-of-the-function-f-x-2sin-x-10cos-2x-2-in-Python-NumPy-Matplotlib
How to write a code that calculates the derivative numerically of the function f(x) = 2sin(x) +10cos(2x) +2 in Python (NumPy, Matplotlib) - Quora
Answer (1 of 3): I assume you’re aware of the sympy library? That would give you the means to generate a symbolic solution. However you’re asking for a numeric solution. After you define the function f in ordinary Python (see below), compute a large number of (f(x+h) - f(x))/h slope values, call...
🌐
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 - The first parameter of the diff function should be the function you want to take the derivative of. The second parameter should be the variable you are taking the derivative with respect to.