Some small / interesting / etc DE applications

Estimating time of death
From
https : // www . quora . com/What - are - some - things - in - everyday - life - that - use - differential - equations
“””
The doctor took the temperature of a dead body at 11:30 pm which was 94.6 F. He took the temperature of the body again after one hour , which was 93.4 F. If the temperature of the room was 70 F, estimate the time of death. Taking normal temperature of body as 98.6 F.
“””
* Identify k, the cooling constant (assuming constant) by checking the temperature difference after 60 minutes.
* Identify the time when the temperature was 98.6°F: -180.755 (minutes), about 3 hours ago
So, it seems that the person died about  08:30

sol = DSolveValue[{y'[t] == -k(y[t]- 70),y[0]==94.6},y[t],t]
% // Simplify
(* Identify k: after 60 minutes, the temperature was 93.4°F *)
solK = SolveValues[% == 93.4 /. t -> 60,k]
sol /. k -> solK
(* When was the temperature normal (98.6°F?) *)
diffMin = SolveValues[% == 98.6,t]
TimeObject[{11,30}] + Quantity[diffMin, "Minutes"]
diffMin / 60

Compounded interest
From
https : // www . quora . com/What - are - some - things - in - everyday - life - that - use - differential - equations
“””
If the interest js compounded continuously at 6% pa, how much worth $1000 will be after 10 years? How long will it take to double $1000?
“””
1) It’s worth $1822.12
    But it should be 1790.85? Well, maybe that’s the effective interest?
    Note: See Boyce page 44 about this. The proper formula is 
        s0 (1+ r/m)^(m t)
    where s0 is initial value, r is the rate, and m is the compound period (i.e. per year), e.g. 1, 12, etc. The DE equation is the limit when m -> ∞.
2) It will double after 11.5 years.

(* this is the effective interest *)
TimeValue[1000,EffectiveInterest[.06,1],10] 

(* This is the compound interest *)
sol = DSolveValue[{y'[t] == 0.06 y[t],y[0] == 1000},y[t],t]
% /. t->10

Solve[sol == 2000,t]

DSolveValue[y''[x] == -k^2 y[x],y[x],x]
% // TrigToExp

Financial calculations
https://www.quora.com/What-are-the-real-life-applications-of-first-order-differential-equations
“””
Suppose you spend more when you have more money than a certain number k.

Then, - dy/dt= y-k, where y stands for your amount of money.

But when y<k then you stop spending and start earning. This is true with the above equation as dy/dt will be positive for y<k.

So, dy/dx=k-y gives a vague idea about your financial situation.

If you believe in that relationships try solving it to have better control on your finances.

It is not in the equation but it is in your understanding of the system that you are looking at, which you are wrapping up into an equation.
“””

DSolveValue[{y'[t] == k-y[t],y[0]== 1000},y[t],t]
% /. k -> 2000
% /. t -> 10
% // N

From Boyce et al. Chapter 1, Probem 20
A certain drug is being administered intravenously to a hospital
patient. Fluid containing 5 mg/cm^3 of the drug enters the patient’s
bloodstream at a rate of 100 cm^3 /h. The drug is absorbed by body
tissues or otherwise leaves the bloodstream at a rate proportional to
the amount present, with a rate constant of 0.4/h.
a. Assuming that the drug is always uniformly distributed
throughout the bloodstream, write a differential equation for the
amount of the drug that is present in the bloodstream at any time.
b. How much of the drug is present in the bloodstream after a
long time?

Input:  5mg/cm^3 at a rate of 100cm^3/h
Output: 0.4 * drug[t]

drug’[t] = 5 * 100 - 0.4*drug[t]

eq = drug'[t] ==  5*100 - 0.4*drug[t]
D[5*100 - 0.4*d,d] (* stable equilibrium *)
VectorPlot[{1,5*100 - 0.4*d},{t,0,100},{d,0,2000}]
DSolve[eq,drug[t],t]
Print["Equilibrium:"]
Solve[5*100 - 0.4*d == 0,d]
sol=DSolveValue[{eq,drug[0] == 1},drug[t],t]
Plot[sol,{t,0,100},PlotRange->All]
Print["Time to reach near 1249 (near 1250):"]
Solve[sol == 1249,t]

Chemical in a pond
From Boyce et al, Page 9, problem 17
A pond initially contains 1,000,000 gal of water and an unknown
amount of an undesirable chemical. Water containing 0.01 grams of
this chemical per gallon flows into the pond at a rate of 300 gal/h. The
mixture flows out at the same rate, so the amount of water in the pond
remains constant. Assume that the chemical is uniformly distributed
throughout the pond.
a. Write a differential equation for the amount of chemical in
the pond at any time.
b. How much of the chemical will be in the pond after a very
long time? Does this limiting amount depend on the amount that
was present initially?
c. Write a differential equation for the concentration of the
chemical in the pond at time t. Hint: The concentration is
c = a/v = a( t) /10^6 .

The answers should be:
a) da/dt = 3- 3 * 10^-4 a
b) a -> 10^4.  No.
c) dc/dt = 3 * 10^-6 - 3 * 10^-4 c   c(10) = a(0) / 10^6 = a0 / 10^6 ??
   But I don’t understand the c(10) = ... That’s not in the problem statement.

eq = a'[t] == 0.01*300 - 300a[t]/10^6
Solve[0.01*300 - 300a/10^6 == 0,a]
DSolveValue[{c'[t] == 0.01*300/10^6 - 300c[t]/10^6,c[0] == c0},c[t],t]
% // Simplify

Chemical in a pond
From Boyce et al, Chapter 1, Problem 14
A pond containing 1,000,000 gal of water is initially free
of a certain undesirable chemical (see Problem 17 of Section 1.1).
Water containing 0.01 g/gal of the chemical flows into the pond at
a rate of 300 gal/h, and water also flows out of the pond at the same
rate. Assume that the chemical is uniformly distributed throughout the
pond.
a. Let Q( t) be the amount of the chemical in the pond at time
t. Write down an initial value problem for Q( t) .
b. Solve the problem in part a for Q( t) . How much chemical is
in the pond after 1 year?
c. At the end of 1 year the source of the chemical in the pond
is removed; thereafter pure water flows into the pond, and the
mixture flows out at the same rate as before. Write down the
initial value problem that describes this new situation.
d. Solve the initial value problem in part c. How much chemical
remains in the pond after 1 additional year (2 years from the
beginning of the problem)?
e. How long does it take for Q( t) to be reduced to 10 g?
G f. Plot Q( t) versus t for 3 years.

Chemical in: 0.01g / gal
Flow in and out: 300 gallons / hour
Q(0) = 0 since the pond has no chemical from the start.

Ah, the equation should be
   Q’(t) = 0.01 * 300 g/h - Q/10^6 * 300

For question e) it’s a little unclear where the start time is.
Ah, the question f) should be all the times, including the start...

(* a: *) 
Print["a:"]
300/100 - 300 q[t]/10^6
DSolveValue[{q'[t] == 300*0.01 - 300 q[t]/10^6,q[0] == 0},q[t],t]
solA = %// FullSimplify

(* b: how much chemials after one year, in hours *)
Print["b:"]
amountB = solA /. t-> 24*365

(* c *)
Print["c:"]
solC = DSolveValue[{q'[t] == - 300 q[t]/10^6,q[0] == amountB},q[t],t]
amountC = solC /. t-> 24*365

Print["d:"]
solD = DSolveValue[{q'[t] ==- 300 q[t]/10^6,q[0] == amountB},q[t],t]

Print["e:"]
SolveValues[solD == 10,t]
% / (24 * 365)

Print["f:"]
Plot[ solD,{t,0,20000},PlotRange->All]

Population of a village
From  https://www.quora.com/What-are-some-things-in-everyday-life-that-use-differential-equations
“””
The population of a village increases continuously at the rate proportional to the number of its inhabitants present at any time. If the population of the village was 20, 000 in 1999 and 25000 in the year 2004, what will be the population of the village in 2009?
“””
pop 1999: 20 000
pop 2004: 25 000
k = 0.0446287
pop 2009 (t0 + 20): 61 035

sol = DSolveValue[{pop'[t] == k pop[t],pop[0] == 20000},pop[t],t]
kval = Solve[(% /. t-> 4) == 25000,k,Assumptions->k >=0];
kval // N
sol /. kval
% /. t -> 20 // N

Generate random DEs:
From https://mathematica.stackexchange.com/questions/249203/creating-random-differential-equations .
Evaluate “eqn” to get a new equation.

eqn:={y'[t]==RandomChoice[Range[7]] y[t]+RandomChoice[{Sin,Cos,Exp}][t],y[0]==RandomChoice[Range[4]]}

eqn