3

I want to use dsolve to solve an initial value problem but the initial condition ics appear to have no effect. Both dsolve in the example give the same result.

from sympy import *
x = symbols('x')
f = Function('f')

eq = Eq(Derivative(f(x), x), f(x))

# No initial condition
pprint(dsolve(eq, f(x)))

# With initial condition f(0) = 1
pprint(dsolve(eq, f(x), ics={f(0):1}))

In both cases I get the same solution

           x
f(x) = C1 ℯ

C1 is not replaced by the value 1 even with ics. Sympy second order ode mentions sympy issue 4720 but that issue is now closed. I am using SymPy 1.1.1.

Svaberg
  • 1,501
  • 1
  • 19
  • 40

1 Answers1

3

The PR implementing the initial conditions is not a part of SymPy 1.1.1 but should be in 1.2. You can test whether your version of SymPy has it by executing

from sympy.solvers.ode import solve_ics

(if it throws an error, you don't have ICS support except for power series). In addition to the post you mentioned, my answer has a short discussion of how to use solve (which has a habit of returning different types in different circumstances) to find and substitute the constants.

eq = Eq(Derivative(f(x), x), f(x))
sol = dsolve(eq, f(x)).rhs
constants = solve([sol.subs(x,0)-1], dict=True)
print(sol.subs(constants[0]))
  • 2
    That pull request was merged after the SymPy 1.1 release. The 1.1.1 release was just a small bugfix release which only included critical fixes on top of 1.1. It did not include major changes that were merged after 1.1. The branch should be included in the next major version (1.2). Or you can use the git master version of SymPy to get it now (see http://docs.sympy.org/latest/install.html#git). – asmeurer Oct 28 '17 at 00:32
  • 1
    What was most confusing to me is that [branch 1.1.1 has solve_ics](https://github.com/sympy/sympy/blob/1.1.1/sympy/solvers/ode.py). –  Oct 28 '17 at 00:50
  • 1
    Oh thanks for pointing that out. The release branch should have been deleted a long time ago. It had the function in it because I merged master into it after doing the release (so it could be merged back into master). The correct thing to look at is the [sympy-1.1.1 tag](https://github.com/sympy/sympy/blob/sympy-1.1.1/sympy/solvers/ode.py). – asmeurer Oct 28 '17 at 05:46
  • I'm confused by the mention of version 1.2. As far as I can tell 1.1.1 is the latest version, no? – Addem Nov 13 '17 at 06:17
  • @Addem Yes, 1.2 is not released yet. But it was mentioned by asmeurer above, and he is the lead developer of SymPy, so I take his word for it. –  Nov 13 '17 at 11:41