ODE give wrong result for integral operation

32 Views Asked by At

I'm trying to code an algorithm that uses the ODE function, but I'm getting the wrong result for some input. for text the correct answer should be: text

like it can be seen here: https://www.wolframalpha.com/input?i=dsolve%28x%27%28t%29%3D+1%2F%28C+-+t%29%29

but what I get is:

>>> from sympy import symbols, Eq, Function
>>> from sympy import pprint
>>> from sympy import dsolve
>>> x = Function("x")
>>> t = symbols("t")
>>> C1 = symbols("C1")
>>> eq = Eq(x(t).diff(t) - 1/(C1-t))
>>> pprint(eq)
d            1       
──(x(t)) - ────── = 0
dt         C₁ - t    
>>> pprint(dsolve(eq))
x(t) = C₂ - log(-C₁ + t)

which isn't the correct answer: https://www.wolframalpha.com/input?i=is+-log%28c-x%29+%3D+-log%28x-c%29+%3F

How can I fix the code?

1

There are 1 best solutions below

2
Oscar Benjamin On

The answer returned here by dsolve is correct and so is the one given by Wolfram Alpha as well. There is not a unique form for the general solution of an ODE because there are different ways to define the arbitrary constants. Here the difference between log(t - C1) and log(C1 - t) is just I*pi which can be absorbed into the arbitrary constant C2. The two different forms for the general solution are both correct but would have different values for C2 in order to satisfy the same initial conditions.

If you expected everything to be real (rather than complex) then log(C1 - t) will be defined only for t < C1 and log(t - C1) will be defined only for t > C1. It isn't necessary to return different forms for t < C1 and t > C1 though because both general solutions given hold for all (complex) t != C1 provided you understand that the constant C2 is not constrained to be real.