Python| sympy.diff() 方法
借助sympy.diff()
方法,我们可以通过sympy.diff()
方法以变量的形式找到数学表达式的微分。
Syntax : sympy.diff(expression, reference variable)
Return : Return differentiation of mathematical expression.
示例 #1:
在这个例子中我们可以看到,通过使用sympy.diff()
方法,我们可以找到数学表达式与变量的微分。这里我们也使用symbols()
方法将变量声明为符号。
# import sympy
from sympy import * x, y = symbols('x y')
gfg_exp = x + y
exp = sympy.expand(gfg_exp**2)
print("Before Differentiation : {}".format(exp))
# Use sympy.diff() method
dif = diff(exp, x)
print("After Differentiation : {}".format(dif))
输出 :
Before Differentiation : x**2 + 2*x*y + y**2
After Differentiation : 2*x + 2*y
示例 #2:
# import sympy
from sympy import * x, y = symbols('x y')
gfg_exp = sin(x)*cos(x)
print("Before Differentiation : {}".format(gfg_exp))
# Use sympy.diff() method
dif = diff(gfg_exp, x)
print("After Differentiation : {}".format(dif))
输出 :
Before Differentiation : sin(x)*cos(x)
After Differentiation : -sin(x)**2 + cos(x)**2