Search code examples
pythonsympyodedifferential-equations

How can I remove higher order term using SymPy? .removeO() does not work here


I am trying to plot the series form of an ODE's solution using SymPy, and I need to remove the 'O' term before plotting.

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()

But here comes error

---------------------------------------------------------------------------
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'

How to solve this problem?


Solution

  • That's because the return from dsolve is an Eq (equation object) and the removeO method is for Expr so you should call removeO in the rhs of the equation:

    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