1

SinceSympy version 1.2, python Sympy has implemented the ability to solve for the constants in a simple differential equation, given some initial conditions. I was trying to test out this feature, but keep getting an error that I don't know how to solve.

The documentation indicates the following format for initial conditions, and I tried to follow what was specified in the actual pull request that implemented the feature. Here is the code and the error.

import sympy as sp

t = sp.symbols('t')
x = sp.Function('x')(t)

diffeq = sp.Eq(x.diff(t,t) - x, sp.cos(t))
res = sp.dsolve(diffeq, t, ics={x(0): 0, 
                            x.diff(t).subs(t, 0): 0})

The error is:

Traceback (most recent call last):

  File "<ipython-input-20-75c3e1d53138>", line 1, in <module>
    res = sp.dsolve(diffeq, t, ics={x(0): 0, sp.diff(x(t), t).subs(t,0): 0})

TypeError: 'x' object is not callable
krishnab
  • 9,270
  • 12
  • 66
  • 123
  • Related and not a duplicate: https://stackoverflow.com/questions/74165228/notimplementederror-initial-conditions-produced-too-many-solutions-for-constant – Galen Oct 22 '22 at 16:27

1 Answers1

1

I am not a heavy user of sympy, but I got that to work - the problem is that when you define x = sp.Function('x')(t) you already got the parameter t to it, and can no longer pass 0 for it at the line res = sp.dsolve(diffeq, t, ics={x(0): 0, sp.diff(x(t), t).subs(t,0): 0}) - The call of x with (t) makes it a "defined function".

So, leaving x as an undefined function, and just passing t for it in the points it is needed when creating the differential equation is the way to go:


import sympy as sp

t = sp.symbols('t')
x = sp.Function('x')

diffeq = sp.Eq(x(t).diff(t, t) - x(t), sp.cos(t)) 
res = sp.dsolve(diffeq, ics={x(0): 0, sp.diff(x(t), t).subs(t,0): 0})

(Also, trying to pass t in the second parameter do dsolve gives another error. The docs tell that sympy should be able to correctly guess it, so I left it out - only to find the correct argument there would be x(t) later)

This gives me res =

Eq(x(t), exp(t)/4 - cos(t)/2 + exp(-t)/4)
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • Oh thanks for finding this. That is not an easy error to find, since it is in the definition of the variable. Thanks for your help on this. What CAS do you normally use? I am trying to stick with free stuff. I like using the Julia Sympy version, but it is missing some simple things like Laplace Transforms. Python Sympy has it, but then there are these small coding issues. – krishnab Sep 20 '19 at 21:06
  • Actually, I'd settle for Python's with no second though. Fact is I am more a code person than a math person. Python is cool due to its consistency, and being able to work on several domains and still remaining a general purpose language. For one understanding Python, given the error message, it was actually easy to spot the problem here. – jsbueno Sep 20 '19 at 22:24
  • Do not rely too much on sympy, even in version 1.4 the system `diffeq = [ sp.Eq(x(t).diff(t), z(t)),sp.Eq(y(t).diff(t), y(t)),sp.Eq(z(t).diff(t), x(t))];` gives a wrong second component, does not treat `y` as decoupled from `x,z`. – Lutz Lehmann Sep 21 '19 at 05:53
  • @LutzL Oh thanks for the warning. Which CAS is a better choice then, for symbolic solutions to differential equations? I was hoping to find something free and open source? Is Sage a better choice? – krishnab Sep 21 '19 at 15:15
  • The problem is being worked on, but it seems slow to migrate to the official distributions: https://github.com/sympy/sympy/issues/14312, https://github.com/sympy/sympy/pull/15449, https://github.com/sympy/sympy/pull/14445 – Lutz Lehmann Sep 21 '19 at 16:28