
Tyler M. answered 10/28/22
Java and Math Tutor
Let me give you an example so you can figure it out easily
In [2]: from sympy import *
Example: Newton's Law of Cooling (160-degree turkey removed from the oven in a 70-degree room, the temperature is 155 degrees after 5 minutes. When will the temperature be 140 degrees?)
In [3]: # FIRST STEP: Solve the IVP from Newton's Law of Cooling: dT/dt = k*(T-70),
T(0)=160
k,t=symbols('k t')
T=Function('T')
deq=diff(T(t),t)-k*(T(t)-70)
Tsoln=dsolve(deq,T(t),ics={T(0):160})
print('Solution of the ODE is',Tsoln)
In [10]: # SECOND STEP: Use the 5 minute temperature to find k
keqn=Tsoln.rhs.subs(t,5)-155 # NOTE: 'rhs' refers to the right hand side o
f the equation
ksoln=solve(keqn,k)
print(ksoln) # last (5th) solution is real: remember Python starts counting
at ZERO!
print('Using the 5 minute temperature reading,',Tsoln.subs(k,ksoln[4]))
In [12]: # Finally, use the new equation to solve for t when T = 140
Teqn=Tsoln.rhs.subs(k,ksoln[4])
tcooled=solve(Teqn-140,t)
print(tcooled) #only one solution this time...the 0th one
print('The turkey is cool enough to eat after',tcooled[0],'or',tcooled[0].e
valf(),'minutes.')
Example 2: IVP (based on rate in - rate out) is
y' = 2a - 2y/100, y(0)=0
In [13]: matplotlib notebook
Solution of the ODE is Eq(T(t), 90*exp(k*t) + 70)
[log(17**(1/5)*2**(4/5)*3**(3/5)/6) - 4*I*pi/5, log(17**(1/5)*2**(4/5)*3**
(3/5)/6) - 2*I*pi/5, log(17**(1/5)*2**(4/5)*3**(3/5)/6) + 2*I*pi/5, log(17*
*(1/5)*2**(4/5)*3**(3/5)/6) + 4*I*pi/5, log(7344**(1/5)/6)]
Using the 5 minute temperature reading, Eq(T(t), 90*exp(t*log(7344**(1/5)/
6)) + 70)
[log((7/9)**(1/log(7344**(1/5)/6)))]
The turkey is cool enough to eat after log((7/9)**(1/log(7344**(1/5)/6))) o
r 21.9840274945891 minutes.