How can I evaluate the constants C1 and C2 from a solution of a differential equation SymPy gives me? There are the initial condition f(0)=0 and f(pi/2)=3.
>>> from sympy import *
>>> f = Function('f')
>>> x = Symbol('x')
>>> dsolve(f(x).diff(x,2)+f(x),f(x))
f(x) == C1*sin(x) + C2*cos(x)
I tried some ics
stuff but it's not working. Example:
>>> dsolve(f(x).diff(x,2)+f(x),f(x), ics={f(0):0, f(pi/2):3})
f(x) == C1*sin(x) + C2*cos(x)
By the way: C2 = 0 and C1 = 3.
There's a pull request implementing initial/boundary conditions, which was merged and should be released in SymPy 1.2. Meanwhile, one can solve for constants like this:
The code returns
final_answer
as3.0*sin(x)
.Remarks
solve
may return a list of solutions, in which case one would have to substituteconstants[0]
, etc. To force it to return a list in any case (for consistency), usedict=True
:If the equation contains parameters,
solve
may or may not solve for the variables you want (C1 and C2). This can be ensured as follows:where again,
dict=True
would force the list format of the output.