用Sympy求解耦合微分方程组

时间:2020-05-28 13:10:53

标签: python sympy differential-equations

我有以下一组耦合的微分方程。我想用sympy获得分析解决方案。

from sympy import *
import numpy as np

init_printing(use_unicode=True)
x, y, z, t, w,  V=symbols('x y z t omega V')
c1=Function('c1')
c2=Function('c2')
hq=symbols('hbar',positive=True)

g1=Eq(c2(t)*hq*V*exp(-I*w*t),I*hq*Derivative(c1(t),t))
g2=Eq(c1(t)*hq*V*exp(+I*w*t),I*hq*Derivative(c2(t),t))

eq=(g1,g2)

dsolve(eq,hint='all',ics={c1(0):1,c2(0):0})

当我尝试求解方程组时,出现错误:

ValueError:无法自动为nan检测该函数。

很遗憾,我看不到自己的错误。

编辑:

classify_ode(g1)返回以下提示:

('factorable', 'nth_algebraic', 'separable', '1st_exact', '1st_linear', 'Bernoulli', '1st_power_series', 'lie_group', 'nth_linear_constant_coeff_variation_of_parameters', 'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters', 'nth_algebraic_Integral', 'separable_Integral', '1st_exact_Integral', '1st_linear_Integral', 'Bernoulli_Integral', 'nth_linear_constant_coeff_variation_of_parameters_Integral', 'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters_Integral')

其中每个都会产生上述错误。

手动地,可以通过将Laplace变换应用于c1和c2来解决该系统。这使ODE的系统变成了纯代数方程组,可以通过重新排列和消除方程之间的耦合来求解。结果就是解的变换,因此必须使用拉普拉斯逆变换进行逆变换。

编辑2:

classify_sysode(eq)返回以下内容:

{'no_of_equation': 2,
 'eq': [-c1(t)*exp(-I*omega*t) + I*Derivative(c2(t), t),
  -c2(t)*exp(I*omega*t) + I*Derivative(c1(t), t)],
 'func': [c2(t), c1(t)],
 'order': {c1(t): 1, c2(t): 1},
 'func_coeff': {(0, c2(t), 0): 0,
  (0, c2(t), 1): I,
  (0, c1(t), 0): -exp(-I*omega*t),
  (0, c1(t), 1): 0,
  (1, c2(t), 0): -exp(I*omega*t),
  (1, c2(t), 1): 0,
  (1, c1(t), 0): 0,
  (1, c1(t), 1): I},
 'is_linear': True,
 'type_of_equation': 'type6'}

这意味着对于以下形式的系统,将使用的求解器为Linear, 2 equations, Order 1, Type 6

x'= f(t)x + g(t)y
y'= a [f(t)+ a h(t)] x + a [g(t)-h(t)] y

但是我们的系统看起来更像是Type 7,形式是:

x'= f(t)x + g(t)y
y'= h(t)x + p(t)y

f(t)和p(t)为零的

。文档中提到的7型建议的解决方法也与 Lutz Lehmann 在其评论中提到的类似。

出于完整性考虑,这是错误,它似乎是由_preprocess引发的异常引起的:

Traceback (most recent call last):
  File "problem_ode.py", line 21, in <module>
    dsolve(eq,hint='all',ics={c1(0):1,c2(0):0})
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/ode.py", line 634, in dsolve
    sols = solvefunc(match)
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/ode.py", line 7405, in sysode_linear_2eq_order1
    sol = _linear_2eq_order1_type6(x, y, t, r, eq)
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/ode.py", line 7731, in _linear_2eq_order1_type6
    hint1 = classify_ode(equ)[1]
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/ode.py", line 976, in classify_ode
    eq, func_ = _preprocess(eq, func)
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/deutils.py", line 84, in _preprocess
    raise ValueError('The function cannot be '
ValueError: The function cannot be automatically detected for nan.

0 个答案:

没有答案