Sympy Dsolve Couldn't Solve For Initial Conditions
I am trying to solve the following differential equation with ics using SymPy's dsolve: from sympy import Function, Derivative, dsolve, symbols t, k = symbols('t,k', real=True) M
Solution 1:
As Oscar pointed out, dsolve failed to solve the ODE, because only one initial condition was required. However, both conditions are required in order to 1) solve the ODE and 2) determine the parameter k. Which must be done separately. The following code accomplishes this:
from sympy import Function, Derivative, dsolve, solve, symbols, Eqt, k = symbols('t,k', real=True)
t1 = 0
M1 = 100
t2 = 6
M2 = 97
M = Function('M')(t)
M_ = Derivative(M,t)
Eqn = Eq(M_,k*M)
sol = dsolve(Eqn, ics={M.subs(t,t1): M1})
expr = sol.rhs.subs(t,t2)
eqn2 = Eq(expr,M2)
k_value = float(solve(eqn2,k)[-1])
sol_with_k = sol.subs(k,k_value)
print(sol_with_k)
Which gives the correct output:
Eq(M(t), 100*exp(-0.00507653458078476*t))
Post a Comment for "Sympy Dsolve Couldn't Solve For Initial Conditions"