diff(func,x,n)
其中,func是要求导的函数,x是要对其求导的变量,n是可选的,表示求n阶导数,默认为1阶导数。
注意,在用diff进行求导之前,需要用symbols函数定义变量
from sympy import diff
from sympy import symbols
def func(x):
return x**4
x = symbols("x")
print(diff(func(x),x))
输出结果为:4*x**3
from sympy import diff
from sympy import symbols
def func(x):
return x**4
x = symbols("x")
print(diff(func(x),x,2))
输出结果为:12*x**2
from sympy import diff
from sympy import symbols
def func(x,y):
return x**4+y**3
x,y = symbols("x,y")
print(diff(func(x,y),x))
输出结果为:4*x**3
from sympy import diff
from sympy import symbols
def func(x,y):
return x**4
x = symbols("x")
print(diff(func(x),x).subs(x,2)) # 表示将x = 2,带入导函数4*x**3中
输出结果为32