sympy: dsolve for a system of differential equations

78 views
Skip to first unread message

frank zack

unread,
Jan 13, 2017, 12:53:33 PM1/13/17
to sympy
How can I solve a system of ode's?

That's what I've tried:

    from sympy import *;
    init_printing()
   
    Ex,Ey,Ez,Bx,By,Bz,c,m0,t,u,we,wb,q=symbols('E_x E_y E_z B_x B_y B_z c m_0 t u omega_E omega_B q')
    tau=dynamicsymbols('tau')
   
    u0=Function('u^0')(tau)
    u1=Function('u^1')(tau)
    u2=Function('u^2')(tau)
    u3=Function('u^0')(tau)
   
    u=Matrix([u0,u1,u2,u3])
    Emat=Matrix([[0,Ex/c,Ey/c,Ez/c],[Ex/c,0,Bz,-By],[Ey/c,-Bz,0,Bx],[Ez/c,By,-Bx,0]])
    RHS=q/m0*Emat*u
   
    odesys=[Eq(u0.diff(),RHS[0]),Eq(u1.diff(),RHS[1]),Eq(u2.diff(),RHS[2]),Eq(u3.diff(),RHS[3])]
    odesys
    dsolve(odesys,[u0(tau),u1(tau),u2(tau),u3(tau)])

Unfortunately it gives me a TypeError:

    TypeError                                 Traceback (most recent call last)
    <ipython-input-94-f4af1f7e485c> in <module>()
         16 odesys=[Eq(u0.diff(),RHS[0]),Eq(u1.diff(),RHS[1]),Eq(u2.diff(),RHS[2]),Eq(u3.diff(),RHS[3])]
         17 odesys
    ---> 18 dsolve(odesys,[u0(t),u1(t),u2(t),u3(t)])
   
    TypeError: 'u^0' object is not callable

I've tried so many things, but I just can't figure out the proper syntax for this to work.
Any ideas?


BTW: It doesn't help if I use dynamicsymbols instead of functions:

    ...
    dsolve(odesys,[u0(tau),u1(tau),u2(tau),u3(tau)])
    ...
    dsolve(odesys,[u0(t),u1(t),u2(t),u3(t)])

-> Same TypeError

Aaron Meurer

unread,
Jan 13, 2017, 2:59:18 PM1/13/17
to sy...@googlegroups.com
You have a couple of issues. Unfortunately, even after fixing them I
was unable to get a solution from SymPy.

First, u0 is already u^0(tau). You do not need to call u0(tau) again.

Second, you have a typo. You wrote

u3=Function('u^0')(tau)

instead of

u3=Function('u^3')(tau)

I fixed these but I hit another issue that looks like a bug in SymPy

UnboundLocalError: local variable 'eq_no' referenced before assignment

It seems dsolve() doesn't like the use of dynamicssymbols. You should
replace dynamicssymbols('tau') with a regular symbols() object for the
call to dsolve as a workaround.

I did this but then dsolve just hung and eventually returned None. So
this is a bug in SymPy.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/c944eae7-b863-4cd7-977b-67b5cb734d2f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages