Home > OS >  NotImplementedError: Initial conditions produced too many solutions for constants [SymPy]
NotImplementedError: Initial conditions produced too many solutions for constants [SymPy]

Time:10-23

I attempted to solve this differential equation by adapting the code found here.

import sympy as sp

t = sp.symbols('t', real=True)
x = sp.Function('x', real=True)

diffeq = sp.Eq(x(t).diff(t), sp.sqrt(1 - x(t)**2)) 
res = sp.dsolve(diffeq, ics={x(0): 0})

The math.se post suggests that a piecewise function satisfies the equation, but perhaps my code does not add all the necessary assumptions. I get the following traceback:

Traceback (most recent call last):
  File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
    exec(code, self.locals)
  File "/home/galen/testing.py", line 7, in <module>
    res = sp.dsolve(diffeq, ics={x(0): 0})
  File "/usr/lib/python3/dist-packages/sympy/solvers/ode/ode.py", line 639, in dsolve
    return _helper_simplify(eq, hint, hints, simplify, ics=ics)
  File "/usr/lib/python3/dist-packages/sympy/solvers/ode/ode.py", line 694, in _helper_simplify
    solved_constants = solve_ics([rv], [r['func']], cons(rv), ics)
  File "/usr/lib/python3/dist-packages/sympy/solvers/ode/ode.py", line 808, in solve_ics
    raise NotImplementedError("Initial conditions produced too many solutions for constants")
NotImplementedError: Initial conditions produced too many solutions for constants

How do I change my code to correctly solve the ODE?

CodePudding user response:

First, the solver treats the equation as algebraic, that is, it solves x'²=1-x², and it does not get the piecewise solution that are possible, only the fully analytical.

For some reason returning a tuple of possible solutions is not implemented, you can do that manually, see https://stackoverflow.com/a/46979535/3088138

res = sp.dsolve(diffeq)
sol = res.rhs
print([ sol.subs(coeffs) for coeffs in sp.solve(sol.subs(t,0), dict=True) ])

which returns [sin(t), -sin(t)]. The second is not compatible with the ODE in its given form.

  • Related