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)