简体   繁体   English

生成 f 的导数的函数

[英]A function generating the derivative of f

I´m trying to construct a function that return the derivate of f , a function of one variable.我正在尝试构造一个函数,该函数返回f的导数,一个变量的函数。

The return value should be a function approximating the derivative of f' using the symmetric difference quotient, so that the returned function will compute (f(x+h) -f(xh))/2h.返回值应该是一个使用对称差商逼近f'导数的f' ,这样返回的函数将计算(f(x+h) -f(xh))/2h.

The function should start like this:该函数应该像这样开始:

def derivative(f, x):

which should approximate the derivative of function f around the point x.它应该近似函数 f 围绕点 x 的导数。 Does anyone have a clue what type of code I can use to construct this type of function?有没有人知道我可以使用什么类型的代码来构造这种类型的函数?

/Alex /亚历克斯

For a general function f(x), you can straightforwardly obtain a numerical approximation to its first derivative by the standard (second-order) approximation (f(x+h) - f(xh)) /2h.对于一般函数 f(x),您可以通过标准(二阶)近似 (f(x+h) - f(xh)) /2h 直接获得其一阶导数的数值近似。 The main challenge is to choose h to be small compared to the lenghscale over which f(x) shows non-quadratic variation, but sufficiently large to avoid round-off errors when subtracting nearby values of f(x).主要挑战是选择 h 与 f(x) 显示非二次变化的 lenghscale 相比较小,但足够大以避免在减去 f(x) 的附近值时出现舍入误差。

However, if you want an algebraic method of differentiating your function, then things are more challenging.但是,如果您想要一种对函数进行微分的代数方法,那么事情就更具挑战性。 The easy cases are where f(x) is known to be a polynomial, so can be represented by a vector of coefficients of powers of x.最简单的情况是 f(x) 已知为多项式,因此可以用 x 的幂系数向量表示。 In that case, numpy.polyder() can be used to compute the coefficients of the n'th derivative.在这种情况下, numpy.polyder()可用于计算 n 次导数的系数。

For more complicated functions, you may want to look at SymPy .对于更复杂的功能,您可能需要查看SymPy

Both the numpy.polyder() and SymPy options require you to represent your function in a way that is specialized to these particular tools. numpy.polyder() 和 SymPy 选项都要求您以专用于这些特定工​​具的方式表示您的函数。 I'm not aware of any method that can take an ordinary Python function and construct another function that implements the exact derivative.我不知道有任何方法可以采用普通的 Python 函数并构造另一个实现精确导数的函数。

what do you want the function to return?你希望函数返回什么? If you want the value of the derivative in a certain x, you probably need three arguments:如果您想要某个 x 中的导数值,您可能需要三个参数:

def derivative(f, h, x):
    return (f(x+h) - f(x-h))/2h

if you want to get a function which calculates the above for any x you can use:如果你想得到一个函数来计算上面的任何 x 你可以使用:

def derivative(f, h):
    return lambda x: (f(x+h) - f(x-h))/2h

Your best best bet would probably be to use SymPy which can do symbolic integration and differentation among other things:您最好的选择可能是使用 SymPy,它可以进行符号集成和区分等:

>>> from sympy import *
>>> x, y, z = symbols('x y z')
>>> diff(x**2, x)
2*x

First you can define a function f (Example: f(x) = x ^ 2):首先你可以定义一个函数 f(例如:f(x) = x ^ 2):

def f(x): return x ** 2

Next using the definition of derivative:接下来使用导数的定义:

def derivative(function, x, accuracy = 20): # The 'Default' of accuracy is 20 and is an optional argument.
    step = 1 / accuracy
    return (function(x + step) - function(x - step)) / (step * 2)

~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~

By the way, I believe this is a typo:顺便说一句,我认为这是一个错字:

def derivative(f, h):

Since you are approximating the derivative of function f around the point x , it should be:由于您正在逼近函数 f围绕点 x的导数,它应该是:

def derivative(f, x):

As shown in my code如我的代码所示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM