Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SymPy gives a wrong solution for an ODE of first order #26593

Closed
terben opened this issue May 12, 2024 · 5 comments
Closed

SymPy gives a wrong solution for an ODE of first order #26593

terben opened this issue May 12, 2024 · 5 comments

Comments

@terben
Copy link

terben commented May 12, 2024

I am trying to solve with SymPy the ODEs:

$$- \sqrt{y_1^{2}{\left(x \right)} - 1} + \frac{d}{d x} y_1{\left(x \right)} = 0$$

and

$$- \sqrt{\left(y_2{\left(x \right)} + 1\right)^{2} - 1} + \frac{d}{d x} y_2{\left(x \right)}=0$$

The solution for the first ODE is $y_1(x)=\cosh(C+x)$ which I can reproduce with SymPy. The solution for the second ODE is $y_2(x)=\cosh(C+x)-1$. This can be seen with the substitution $y_3(x)=y_2(x)+1$. It produces for $y_3(x)$ the same ODE as for $y_1(x)$.

However, SymPy yields the following solution for $y_2(x)$:

$$y_2{\left(x \right)} = e^{- C_{1} - x} + \frac{e^{C_{1} + x}}{4} - 1$$

which cannot be transformed into $y_2(x)=\cosh(C_1+x)-1$.

Here is my interaction with SymPy:

In [5]: x = sp.symbols('x', real=True)

In [6]: y = sp.symbols('y', cls=sp.Function)

In [7]: y = y(x)

In [8]: dgl1 = sp.diff(y, x) - sp.sqrt((y)**2 - 1)

In [9]: sp.dsolve(dgl1)
Out[9]: Eq(y(x), cosh(C1 + x))

In [10]: dgl2 = sp.diff(y, x) - sp.sqrt((y + 1)**2 - 1)

In [11]: sp.dsolve(dgl2)
Out[11]: Eq(y(x), exp(-C1 - x) + exp(C1 + x)/4 - 1)

In [14]: sp.__version__
Out[14]: '1.12'

@oscarbenjamin
Copy link
Contributor

The solution is equivalent:

In [438]: sol = dsolve(dgl2)

In [439]: sol
Out[439]:
                   C+ x
        -C- x   
y(x) =         + ─────── - 1
                     4

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

In [441]: sol.subs(C1, C2+log(2))
Out[441]:
        -C- x    C+ x
                 
y(x) = ──────── + ─────── - 1
          2          2

In [442]: sol.subs(C1, C2+log(2)).rewrite(cos).simplify()
Out[442]: y(x) = cosh(C+ x) - 1

@terben
Copy link
Author

terben commented May 13, 2024

Oh yes. Thanks a lot! I did not see this despite trying for quite some time :-)

Of course it would be nice if SymPy gave immediately the cosh-expression but of course this is only an inconvenience and not an error. From my side we can close this issue.

@asmeurer
Copy link
Member

That's an issue for the integrator, not dsolve. Although even that is not a clear cut thing because some people may prefer for the integrator to return hyperbolic trig functions when possible and some may prefer to always get exponentials.

@oscarbenjamin
Copy link
Contributor

I think that simplify is being used somewhere and the difference is:

In [46]: simplify(exp(x) + exp(-x))
Out[46]: 2cosh(x)

In [47]: simplify(exp(x) + exp(-x)/4)
Out[47]: 
      -x
 x     
  + ───
      4 

The integrator gives an answer with log:

In [53]: eq = diff(f(x), x) - sqrt((f(x))**2 - 1)

In [54]: eq2 = dsolve(eq, hint='separable_Integral').doit()

In [55]: eq2
Out[55]: 
   ⎛   ___________       ⎞         
   ⎜  ╱  2log⎝╲╱  f (x) - 1  + f(x)⎠ = C+ x

In [56]: solve(eq2, f(x))
Out[56]: [cosh(C+ x)]

In [57]: solve(eq2, f(x), simplify=False)
Out[57]: 
⎡ -C- x    C+ x⎤
⎢                ⎥
⎢──────── + ───────⎥
⎣   2          2

@oscarbenjamin
Copy link
Contributor

I think we can close this.

I've opened gh-26595 as a followup for a different problem with this ODE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants