为什么在使用sympy.dsolve时会得到“'list'对象没有属性'func'”?

2024-09-27 14:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解这个微分方程组,但它不起作用。我做错了什么

from sympy import Function, dsolve, Eq, Derivative, sin, cos, symbols
from sympy.abc import x

t = symbols('t')
x, y = symbols('x, y', cls=Function)
eq = (Eq(Derivative(x(t), t), -2 * Derivative(y(t), t) - 2 * x(t) - 5 * y(t) + 77),
      Eq(Derivative(y(t), t), -x(t) - 4 * y(t) + 61))
dsolve(eq)

以下是我得到的错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-32-76dbeaa32a73> in <module>
----> 1 soln = dsolve((eq1, eq2))
      2 soln

~\anaconda3\lib\site-packages\sympy\solvers\ode\ode.py in dsolve(eq, func, hint, simplify, ics, xi, eta, x0, n, **kwargs)
    573     """
    574     if iterable(eq):
--> 575         match = classify_sysode(eq, func)
    576         eq = match['eq']
    577         order = match['order']

~\anaconda3\lib\site-packages\sympy\solvers\ode\ode.py in classify_sysode(eq, funcs, **kwargs)
   1954             if matching_hints['no_of_equation'] == 2:
   1955                 if order_eq == 1:
-> 1956                     type_of_equation = check_linear_2eq_order1(eq, funcs, func_coef)
   1957                 elif order_eq == 2:
   1958                     type_of_equation = check_linear_2eq_order2(eq, funcs, func_coef)

~\anaconda3\lib\site-packages\sympy\solvers\ode\ode.py in check_linear_2eq_order1(eq, func, func_coef)
   1994 
   1995 def check_linear_2eq_order1(eq, func, func_coef):
-> 1996     x = func[0].func
   1997     y = func[1].func
   1998     fc = func_coef

AttributeError: 'list' object has no attribute 'func'

2条回答

这是Symphy中的一个错误,已经修复。安装SymPy 1.8(最新版本):

In [1]: from sympy import Function, dsolve, Eq, Derivative, sin, cos, symbols
   ...: from sympy.abc import x
   ...: 
   ...: t = symbols('t')
   ...: x, y = symbols('x, y', cls=Function)
   ...: eq = (Eq(Derivative(x(t), t), -2 * Derivative(y(t), t) - 2 * x(t) - 5 * y(t) + 77),
   ...:       Eq(Derivative(y(t), t), -x(t) - 4 * y(t) + 61))
   ...: dsolve(eq)
Out[1]: 
⎡               -t       -3⋅t                 -t       -3⋅t     ⎤
⎣x(t) = - 3⋅C₁⋅ℯ   - C₂⋅ℯ     + 1, y(t) = C₁⋅ℯ   + C₂⋅ℯ     + 15⎦

我不知道问题出在哪里,可能是因为第一个方程中y(t)的导数,所以我做了一个替换,得到了我想要的结果,但如果有人能解释我为什么会出现这个错误:“AttributeError:‘list’object没有属性‘func’”,这会很有用

from sympy import *

t = symbols('t')
x = Function('x')
y = Function('y')
dydt =  61 - x(t) - 4*y(t) 
eqs = [
        Eq(x(t).diff(t) + 2*dydt + 2*x(t)+ 5*y(t) -77,0),
        Eq(y(t).diff(t) +x(t) + 4*y(t) -61,0)
    ]

pprint(eqs[0])
pprint(eqs[1])

ics = {x(0): 6, y(0): 12}
a = dsolve(eqs, [x(t), y(t)],ics = ics)
print(a)

相关问题 更多 >

    热门问题