Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sympy ODE solver initial conditions have no effect

Tags:

python

sympy

ode

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.

like image 995
Svaberg Avatar asked Nov 29 '25 06:11

Svaberg


1 Answers

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]))