from sympy import *
if __name__ == '__main__':
# 函数表达式变量
x, y = symbols('x y')
# 二元函数表达式
f = x ** 2 + 3 * x * y + y - 1
# f关于x的偏导数,y是常量
fx = diff(f, x)
print(fx)
# f关于y的偏导数,x是常量
fy = diff(f, y)
print(fy)
result1 = fx.subs({x: 4, y: -5})
print(result1)
result2 = fy.subs([(x, 4), (y, -5)])
print(result2)
# 再来一例二元三角函数的偏导数
# 求 f = y*sin(x*y)关于y的偏导数,x是常量
print(diff(y * sin(x * y), y))
输出:
2*x + 3*y
3*x + 1
-7
13
x*y*cos(x*y) + sin(x*y)