Help with dsolve

65 views
Skip to first unread message

Filipe Pires Alvarenga Fernandes

unread,
Mar 8, 2014, 3:24:39 PM3/8/14
to sy...@googlegroups.com
Hi,
I'm new to sympy and I'm trying to understand how to use dsolve.  (I'm creating an ipython notebook for a class.)

I'm creating my DE like this:
de = Eq(u(t).diff(t, t) + 4*u(t), 0)
print(de)
4*u(t) + Derivative(u(t), t, t) == 0

soln = dsolve(de, u(t))
print(soln)
u(t) == C1*sin(2*t) + C2*cos(2*t)

So far now everything is perfect.  But if I try to change the number 4 for a "generic" symbol (f**2) I do not get
u(t) == C1*sin(f*t) + C2*cos(f*t) as I expected, insted I get a more "comprehensive" solution below.

What am I doing wrong?

>>> d2udt2 = Eq(u(t).diff(t, t) - f*(-f*u(t)), 0)
>>> print(d2udt2)
f**2*u(t) + Derivative(u(t), t, t) == 0
u(t) == (C1*sin(t*((-re(f)**2 + im(f)**2)**2 + 4*re(f)**2*im(f)**2)**(1/4)*Abs(sin(atan2(-2*re(f)*im(f), -re(f)**2 + im(f)**2)/2))) + C2*cos(t*((-re(f)**2 + im(f)**2)**2 + 4*re(f)**2*im(f)**2)**(1/4)*sin(atan2(-2*re(f)*im(f), -re(f)**2 + im(f)**2)/2)))*exp(-t*((-re(f)**2 + im(f)**2)**2 + 4*re(f)**2*im(f)**2)**(1/4)*cos(atan2(-2*re(f)*im(f), -re(f)**2 + im(f)**2)/2)) + (C3*sin(t*((-re(f)**2 + im(f)**2)**2 + 4*re(f)**2*im(f)**2)**(1/4)*Abs(sin(atan2(-2*re(f)*im(f), -re(f)**2 + im(f)**2)/2))) + C4*cos(t*((-re(f)**2 + im(f)**2)**2 + 4*re(f)**2*im(f)**2)**(1/4)*sin(atan2(-2*re(f)*im(f), -re(f)**2 + im(f)**2)/2)))*exp(t*((-re(f)**2 + im(f)**2)**2 + 4*re(f)**2*im(f)**2)**(1/4)*cos(atan2(-2*re(f)*im(f), -re(f)**2 + im(f)**2)/2))

Thanks,
-Filipe

Aaron Meurer

unread,
Mar 8, 2014, 7:52:29 PM3/8/14
to sy...@googlegroups.com
This is in some sense a bug. The solution is correct mathematically.
The biggest issue with it is actually that there are four constants,
not two. This is because the solver currently outputs four terms and
relies on the constant simplification to reduce them to two. But the
terms don't simplify in this case because they are so complicated.

If you want something simpler, you should assume that f is real, like

f = symbols('f', real=True)

If you do that, you will get

In [8]: print(dsolve(d2udt2, u(t)))
u(t) == C1*sin(t*Abs(f)) + C2*cos(t*Abs(f))

if you don't like the Abs you can assume f is positive instead.

Ideally, dsolve would return a solution in terms of complex
exponentials in these sorts of cases, which would be a lot less
complicated. Any potential GSoC students out there, especially those
interested in the ODE module, this is a nice little project for your
patch requirement.

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 http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/1395f3d9-f6ea-4167-bc0c-3a9b7e1e1e5d%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Harsh Gupta

unread,
Mar 9, 2014, 1:26:35 AM3/9/14
to sy...@googlegroups.com
Our constant simplifier work for only one variable. Someone might go
try to implementing it for multiple variables. Our PDE module needs
that.

There a paper on this by Neil Soiffer called "Collapsing Constants"
that deals with such stuff.
http://www.cs.berkeley.edu/~fateman/papers/CollapsingConstants-Soiffer.pdf
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BKeqD5E8oghS0fw%2BxSzpG%3D94PSOv0LN%2BAfNYcSqZR9Xg%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Harsh

Filipe Pires Alvarenga Fernandes

unread,
Mar 9, 2014, 9:01:55 AM3/9/14
to sy...@googlegroups.com
Thanks!  It worked!  Now I'm trying to get used to the 'ics' conditions syntax I guess it makes sense but it is kind of hard to write.

Thanks a lot.

-Filipe


Aaron Meurer

unread,
Mar 9, 2014, 6:18:29 PM3/9/14
to sy...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages