Python中使用sympy模块的dsolve函数求解微分方程,具体方法如下:
import sympy as sy #导入sympy模块def differential_equation(x,f):
return sy.diff(f(x),x,2)+f(x)#f(x)''+f(x)=0 #二阶常系数齐次微分方程
x=sy.symbols('x') #约定变量
f=sy.Function('f') #约定函数
print(sy.dsolve(differential_equation(x,f),f(x))) #打印
sy.pprint(sy.dsolve(differential_equation(x,f),f(x)))
输出结果为:
Eq(f(x), C1*sin(x) + C2*cos(x))f(x) = C₁⋅sin(x) + C₂⋅cos(x)