I am trying to solve simple first order differential equation using Sympy. But it does not give a closed form solution i.e., still have integral inside the solution. Same happens even after applying initial condition i.e., Q(0) = 5. Although Wolfram alpha solves it without throwing back integral into the solution. The equation is:
\frac{d}{d t} Q{\left(t \right)} = 1.8 \cos{\left(t \right)} + 1.8 - \frac{6 Q{\left(t \right)}}{3 t + 600}
I tried to take help from ChatGPT but no success. My code is below:
t = sm.symbols('t', real = True, positive = True)
Q = sm.symbols('Q', cls = sm.Function)
Q = Q(t)
eq = sm.Eq(Q.diff(t), 9/5*(1+ sm.cos(t)) - (6*Q)/(600+3*t))
sol_g = sm.dsolve(eq, Q)
sol_p = sm.dsolve(eq, Q, ics = {Q.subs(t, 0): 5})
The dsolve function can has a hint parameter that chooses which method to use to generate a solution. You can see the available possibilities for a given ODE by calling classify_ode
:
In [15]: classify_ode(eq)
Out[15]:
('factorable',
'1st_exact',
'1st_linear',
'Bernoulli',
'almost_linear',
'1st_power_series',
'lie_group',
'1st_exact_Integral',
'1st_linear_Integral',
'Bernoulli_Integral',
'almost_linear_Integral')
The solution that you see comes from 1st_exact
but the solution from 1st_linear
is better in this case:
In [18]: dsolve(eq, hint='1st_linear')
Out[18]:
3 2 2
C₁ + 0.6⋅t + 1.8⋅t ⋅sin(t) + 360.0⋅t + 720.0⋅t⋅sin(t) + 3.6⋅t⋅cos(t) + 72000.0⋅t + 71996.4⋅sin(t) + 720.0⋅cos(t)
Q(t) = ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
2
(t + 200)