9951 explained code solutions for 126 technologies


python-scipyHow do I calculate the derivative of a function using Python and SciPy?


In order to calculate the derivative of a function using Python and SciPy, one can use the scipy.misc.derivative function. This function takes three arguments: a function, a point, and a delta. The function argument is the function for which the derivative is to be calculated, the point is the point at which the derivative is to be calculated, and the delta is the step size used in the numerical approximation.

For example, to calculate the derivative of the function f(x) = x^2 at the point x=3, one can use the following code:

from scipy.misc import derivative

def f(x):
    return x**2

derivative(f, 3, dx=1e-6)

The output of this code is 6.000000000012662.

The code can be broken down as follows:

  1. The from scipy.misc import derivative line imports the derivative function from the scipy.misc module.
  2. The def f(x): line defines the function f for which the derivative is to be calculated.
  3. The derivative(f, 3, dx=1e-6) line calls the derivative function, passing in the function f, the point 3, and the step size 1e-6.

Helpful links

Edit this code on GitHub