Python 无法计算某个点的导数
Python can't evaluate derivatives at a point
我使用 Jupyter Notebook。当我想计算 P 点的偏导数时,为什么这不起作用?
from sympy import *
P = (-3,-2)
def f(x,y):
return x**3+3*x**2-9*x+y**3-12*y
def f_x(x,y):
return diff(f(x,y), x)
当我键入 f(P[0],P[1]) 时,我得到答案 43。
当我键入 f_x(x,y) 时,我得到了 f wrt 的导数。 x
然后,当我输入 f_x(P[0],P[1]) 时,我得到这个错误:
ValueError Traceback (most recent call last) <ipython-input-2-2afed429d2a2> in <module>
7 return diff(f(x,y), x)
8
----> 9 f_x(P[0],P[1])
<ipython-input-2-2afed429d2a2> in f_x(x, y)
5 return x**3+3*x**2-9*x+y**3-12*y
6 def f_x(x,y):
----> 7 return diff(f(x,y), x)
8
9 f_x(P[0],P[1])
~\anaconda3\lib\site-packages\sympy\core\function.py in diff(f,
*symbols, **kwargs) 2503 return f.diff(*symbols, **kwargs) 2504 kwargs.setdefault('evaluate', True)
-> 2505 return _derivative_dispatch(f, *symbols, **kwargs) 2506 2507
~\anaconda3\lib\site-packages\sympy\core\function.py in
_derivative_dispatch(expr, *variables, **kwargs) 1945 from sympy.tensor.array.array_derivatives import ArrayDerivative 1946 return ArrayDerivative(expr, *variables, **kwargs)
-> 1947 return Derivative(expr, *variables, **kwargs) 1948 1949
~\anaconda3\lib\site-packages\sympy\core\function.py in __new__(cls, expr, *variables, **kwargs) 1312 if isinstance(v, Integer): 1313 if i == 0:
-> 1314 raise ValueError("First variable cannot be a number: %i" % v) 1315 count = v 1316 prev, prevcount = variable_count[-1]
ValueError: First variable cannot be a number: -3
你在做什么:
f_x(P[0], P[1])
变为
f_x(-3, -2)
变为
diff(f(-3, -2), -3)
变为
diff(43, -3)
给出错误:您无法计算常数函数 43
对数字 (-3
).
的导数
在 sympy 中,函数通常只是写成包含符号变量的表达式 (symbols
)。直接根据表达式计算导数。请注意,diff(f, x)
也可以写成 f.diff(x)
,这可能更容易阅读。
from sympy import symbols
x, y = symbols('x y')
P = (-3, -2)
# let f be a function of x and y:
f = x**3 + 3*x**2 - 9*x + y**3 - 12*y
# calculate the derivative of f towards x
f_x = f.diff(x) # so, f_x = 3*x**2 + 6*x - 9
# now, fill in the values into the derivative:
f_x.subs({x: P[0], y: P[1]}) # 0
我使用 Jupyter Notebook。当我想计算 P 点的偏导数时,为什么这不起作用?
from sympy import *
P = (-3,-2)
def f(x,y):
return x**3+3*x**2-9*x+y**3-12*y
def f_x(x,y):
return diff(f(x,y), x)
当我键入 f(P[0],P[1]) 时,我得到答案 43。
当我键入 f_x(x,y) 时,我得到了 f wrt 的导数。 x
然后,当我输入 f_x(P[0],P[1]) 时,我得到这个错误:
ValueError Traceback (most recent call last) <ipython-input-2-2afed429d2a2> in <module>
7 return diff(f(x,y), x)
8
----> 9 f_x(P[0],P[1])
<ipython-input-2-2afed429d2a2> in f_x(x, y)
5 return x**3+3*x**2-9*x+y**3-12*y
6 def f_x(x,y):
----> 7 return diff(f(x,y), x)
8
9 f_x(P[0],P[1])
~\anaconda3\lib\site-packages\sympy\core\function.py in diff(f,
*symbols, **kwargs) 2503 return f.diff(*symbols, **kwargs) 2504 kwargs.setdefault('evaluate', True)
-> 2505 return _derivative_dispatch(f, *symbols, **kwargs) 2506 2507
~\anaconda3\lib\site-packages\sympy\core\function.py in
_derivative_dispatch(expr, *variables, **kwargs) 1945 from sympy.tensor.array.array_derivatives import ArrayDerivative 1946 return ArrayDerivative(expr, *variables, **kwargs)
-> 1947 return Derivative(expr, *variables, **kwargs) 1948 1949
~\anaconda3\lib\site-packages\sympy\core\function.py in __new__(cls, expr, *variables, **kwargs) 1312 if isinstance(v, Integer): 1313 if i == 0:
-> 1314 raise ValueError("First variable cannot be a number: %i" % v) 1315 count = v 1316 prev, prevcount = variable_count[-1]
ValueError: First variable cannot be a number: -3
你在做什么:
f_x(P[0], P[1])
变为
f_x(-3, -2)
变为
diff(f(-3, -2), -3)
变为
diff(43, -3)
给出错误:您无法计算常数函数 43
对数字 (-3
).
在 sympy 中,函数通常只是写成包含符号变量的表达式 (symbols
)。直接根据表达式计算导数。请注意,diff(f, x)
也可以写成 f.diff(x)
,这可能更容易阅读。
from sympy import symbols
x, y = symbols('x y')
P = (-3, -2)
# let f be a function of x and y:
f = x**3 + 3*x**2 - 9*x + y**3 - 12*y
# calculate the derivative of f towards x
f_x = f.diff(x) # so, f_x = 3*x**2 + 6*x - 9
# now, fill in the values into the derivative:
f_x.subs({x: P[0], y: P[1]}) # 0