Stef1611
Stef1611

Reputation: 2387

Sympy Differential Equation : How to mowe some lhs terms to rhs?

I have the following differential equation : eq1

t = sp.Symbol("t")
f1=sp.Function("f1")(t)
f2=sp.Function("f2")(t)
eq1=sp.Eq(f1+f1.diff(t,2)-f2+f2.diff(t,1),0)

To move f2 terms from the lhs to the rhs, I used this code :

eq2=sp.Eq(eq1.lhs.subs(f2,0).doit(),eq1.lhs.subs(f1,0).doit()*-1)

Is it the right way to do that or is there a simpler solution ?

Thanks for answer.

Upvotes: 0

Views: 211

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14500

That might fail if e.g. you have a 1/f1 somewhere. I'd do it like this:

In [18]: eq1
Out[18]: 
                  2                       
                 d           d            
f₁(t) - f₂(t) + ───(f₁(t)) + ──(f₂(t)) = 0
                  2          dt           
                dt                        

In [19]: lhs, neg_rhs = (eq1.lhs - eq1.rhs).as_independent(f2, as_Add=True)

In [20]: eq2 = Eq(lhs, -neg_rhs)

In [21]: eq2
Out[21]: 
          2                           
         d                   d        
f₁(t) + ───(f₁(t)) = f₂(t) - ──(f₂(t))
          2                  dt       
        dt 

https://docs.sympy.org/latest/modules/core.html#sympy.core.expr.Expr.as_independent

Upvotes: 2

Related Questions