Issue in the ODE dsolve module (GSOC)

60 views
Skip to first unread message

kang li

unread,
Apr 3, 2022, 6:39:41 PM4/3/22
to sympy
There is an issue in the sympy.dsolve module. When I was trying to solve the ode: -B(t)^2 - B'(t) +1 =0. The proper result should be tanh(C_1 - t), while the given result is the 1/tanh(C_1 - t).

I can help to solve this issue as a GSOC project if provided the permission from the community. I often use the sympy package, and I love it as it is a competitive alternative to the Wolfram Mathematica.

And I am now a master's student at the Mathematical Institute, Oxford University. Before coming here, I possess a computer science undergraduate degree. Here is my Linkdin.

IMG_7136.jpg

emanuel.c...@gmail.com

unread,
Apr 3, 2022, 7:14:29 PM4/3/22
to sympy
Nope.

Using `sympy` through `sage` :

```
sage: reset()
sage: t=var("t")
sage: B=function("B")
sage: Eq=-B(t)^2-B(t).diff(t)+1==0
sage: import sympy
sage: Sol=sympy.dsolve(*map(sympy.sympify, (Eq, B(t))))._sage_() ; Sol
B(t) == -1/tanh(C1 - t)
sage: C1=var("C1")
sage: foo(t)=Sol.rhs()
sage: bool(Eq.substitute_function(B, foo))
True
sage: bar(t)=tanh(C1-t)
sage: bool(Eq.substitute_function(B, bar))
False
```

HTH,

emanuel.c...@gmail.com

unread,
Apr 3, 2022, 7:25:16 PM4/3/22
to sympy
But note that 

```
sage: bool(Eq.substitute_function(B,tanh(t-C1).function(t)))
True
```

Sign errors happen...

Oscar Benjamin

unread,
Apr 3, 2022, 7:46:04 PM4/3/22
to sympy
You have a sign mismatch in your equation (+-Bt).

--
Oscar

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/sympy/b9c1f6c8-c355-40cb-a049-7c95c8ba5118n%40googlegroups.com.
Message has been deleted

kang li

unread,
Apr 4, 2022, 10:31:52 PM4/4/22
to sympy
Sorry for the typo, the equation is  -B(t)^2 + B'(t) +1 =0. And the picture is right. The problem still remains.

Oscar Benjamin

unread,
Apr 5, 2022, 7:54:17 AM4/5/22
to sympy
On Sun, 3 Apr 2022 at 23:39, kang li <kang.mat...@gmail.com> wrote:
There is an issue in the sympy.dsolve module. When I was trying to solve the ode: -B(t)^2 - B'(t) +1 =0. The proper result should be tanh(C_1 - t), while the given result is the 1/tanh(C_1 - t).

On Tue, 5 Apr 2022 at 03:31, kang li <kang.mat...@gmail.com> wrote:
Sorry for the typo, the equation is  -B(t)^2 + B'(t) +1 =0. And the picture is right. The problem still remains.

Both answers tanh(C1-t) and 1/tanh(C1-t) are equivalent for different values of the constant. It's important to understand that the constants can be non-real:

In [48]: C1, C2 = symbols('C1, C2')

In [49]: sol1 = 1/tanh(C1 - t)

In [50]: sol2 = tanh(C2 - t)

In [51]: sol1.subs(C1, C2 - I*pi/2).rewrite(tanh) == sol2
Out[51]: True

Of course ideally the answer should be given in a form such that for real initial conditions the constants are real-valued. In this particular case it isn't possible to do that for all possible choices of real initial conditions:

If the initial condition is in the range (-1, 1) then the tanh(C - t) form can satisfy them with real C. However if the initial condition is greater than 1 or less than -1 then only the 1/tanh(C-t) form will satisfy the initial condition with real C. Neither form of the solution is preferable for all possible choices of initial conditions.

What is an issue is the fact that if you give initial conditions then dsolve errors out:

In [59]: dsolve(eq, B(t), ics={B(0):0})
...
NotImplementedError: Initial conditions produced too many solutions for constants

This happens because solving for the constants gives:
[{C1: -I*pi/2}, {C1: I*pi/2}]

Either choice of value for the constant gives the correct solution though:

In [11]: sol = dsolve(eq, B(t))
     
In [12]: print(sol)
Eq(B(t), 1/tanh(C1 - t))

In [13]: print(sol.subs(C1, -I*pi/2))
Eq(B(t), -1/coth(t))

In [14]: print(sol.subs(C1, I*pi/2))
Eq(B(t), -1/coth(t))

The initial condition handling code in dsolve should be fixed to handle cases like this.

Oscar 
Reply all
Reply to author
Forward
0 new messages