常微分方程的通式:
1. 微分方程的解析解
以求解阻尼谐振子的二阶ODE为例,其表达式为:
前提条件:
import sympy
def apply_ics(sol, ics, x, known_params):
free_params = sol.free_symbols - set(known_params)
eqs = [(sol.lhs.diff(x, n) - sol.rhs.diff(x, n)).subs(x, 0).subs(ics) for n in range(len(ics))]
sol_params = sympy.solve(eqs, free_params)
return sol.subs(sol_params)
# 初始化打印环境
sympy.init_printing()
# 标记参数,且均为正
t, omega0, gamma = sympy.symbols("t, omega_0, gamma", positive=True)
# 标记x是微分函数,非变量