[我正在尝试使用SymPy绘制ODE解决方案的序列形式,并且在绘制之前需要删除'O'项。
from IPython.display import display
from sympy import *
from sympy.plotting import plot
x = Function('x')
t = Symbol('t')
ode = Derivative(x(t),t) + t * x(t) - t**3
sol_series = dsolve(ode, hint='1st_power_series', n=8, ics={x(0): 1})
res = sol_series.removeO()
但是出现错误
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-66-2775b6fd4150> in <module>
----> 1 res = sol_series.removeO()
2 display(res)
AttributeError: 'Equality' object has no attribute 'removeO'
如何解决这个问题?
这是因为dsolve
的返回值是Eq
(等式对象),而removeO
方法适用于Expr
,因此您应在等式的rhs中调用removeO
:
In [5]: res = Eq(sol_series.lhs, sol_series.rhs.removeO())
In [6]: res
Out[6]:
6 4 2
t 3⋅t t
x(t) = - ── + ──── - ── + 1
16 8 2