The Question Behind the Equation
A 100-litre tank holds salt water. Fresh brine flows in at the top, and a stirrer keeps the contents perfectly mixed. A drain at the bottom releases the mixture at the same rate. How much salt is in the tank at every moment? Trivial-looking question — and yet it sits behind drug infusion, lake pollution, blood-oxygen dynamics, reservoir desalination, and every chemical reactor on the planet.
What makes mixing problems special is that the rate at which something leaves a container depends on how much is currently inside. That single feedback is what turns a one-line bookkeeping problem into a first-order linear differential equation — and what creates the universal curve you see in every textbook plot.
The mixing-tank ODE
Let be the mass of dissolved substance at time . Inflow brings it in at concentration and rate . Outflow takes it out at rate at whatever concentration the tank currently has. Then:
When inflow and outflow rates are equal, stays constant and this becomes a linear ODE with constant coefficients — the kind we solved in .
The Rate-In / Rate-Out Balance
Every conservation problem starts the same way. Pick a quantity. Account for everything that increases it. Subtract everything that decreases it. The rate of change of the quantity is the net of those two.
| Quantity | Why it increases | Why it decreases |
|---|---|---|
| Salt in tank y(t) | Inflow carries brine of concentration c_in at rate r_in | Drain removes mixed solution of concentration y/V at rate r_out |
| Drug mass in blood | IV infusion at fixed mg/min | Kidneys clear at a rate proportional to current concentration |
| Pollutant in lake | Polluted stream feeding the lake | Outflow river carrying the well-mixed lake water |
Notice the pattern: the in column is usually a constant per unit time, while the out column is proportional to the current amount. That asymmetry is the entire reason the solution involves an exponential — it is the same asymmetry that makes Newton's law of cooling, RC discharge, and radioactive decay all look alike.
Why outflow scales with y, not r_out alone
The drain doesn't care about the original salt: it pulls out litres per minute of whatever the tank currently is. Multiply by the current concentration to get the mass leaving per minute. That is the term.
Building the ODE
Set up the conservation statement and translate it line-by-line into symbols. Assume for now that inflow and outflow rates are equal — call that common value — so the volume is constant at .
- Rate in. Brine arrives at L/min carrying kg/L. Mass per minute entering: .
- Concentration in tank. Perfect mixing means the tank is uniform; concentration is at every instant.
- Rate out. Drain removes L/min at concentration . Mass per minute leaving: .
- Net rate. .
Rearranged into standard linear form,
Compare with the canonical linear ODE : here is the constant decay rate and is the constant forcing. Everything we did in applies — and the integrating-factor recipe will give the answer in a few clean lines.
Solving the Linear ODE
The integrating factor is . Multiply both sides:
- Integrate both sides in :
- Divide by :
- Apply :
Substitute the constant back and we have the full closed-form answer:
Sanity-check by differentiating
and . The two match. ✓
Equilibrium and Time Constant
Two quantities deserve names because they describe the entire long-term behaviour of the tank.
Equilibrium
Set in the ODE: , which gives:
At equilibrium, the tank's concentration matches the inflow concentration. That is the only steady state — and the only place the curve can flatten out.
Time Constant
The coefficient on in the exponential is . Its reciprocal is the natural clock of the system:
After one time constant the deviation from equilibrium has shrunk by a factor , i.e. you have closed of the gap. After three time constants you are within . After five you are within — engineers casually call this "steady state".
Reading the solution. Rewrite in words: start at , close the gap to exponentially fast, with characteristic time . Every mixing-tank answer fits this template.
Interactive Tank Explorer
Drag the volume, flow rate, inflow concentration, and starting salt. The tank shades darker as concentration rises; the curve to the right plots against time with the equilibrium line in red. The three coloured bars at the bottom show the rate balance at the current moment — note how the green (in) and red (out) bars start mismatched and gradually equalise as the curve approaches the red equilibrium line.
What to play with first
- Set and watch the tank charge up toward . Now set and watch it discharge down. Both directions, same .
- Triple . The equilibrium height doesn't change but the curve gets dramatically faster — because shrank.
- Triple . Equilibrium triples (more tank = more salt at the same concentration) and triples (bigger tank reacts slowly).
- Set . Pure water in, drain pulls salt out. The tank empties exponentially: the classic decay equation falls out as a special case.
One Equilibrium, Many Starts
Different initial salt amounts all relax to the same long-time value. Below, the dashed red line is , and each coloured curve starts at a different . Watch how the spacing between curves shrinks exponentially — that is the geometric meaning of .
A linear ODE's general solution always splits into steady-state plus transient. Here the steady state is and the transient is . The steady state forgets ; the transient remembers it. As memory of the initial condition vanishes — that is what the exponential is doing.
When the Tank Is Filling or Draining
Drop the assumption and the volume itself becomes a function of time:
The ODE keeps its linear form, but the coefficient on is no longer constant:
The recipe is still integrating factor — just integrate instead of a constant. A clean example: tank starts with L at kg, brine of kg/L flows in at L/min, drain runs at L/min. So .
The integrating factor is . Multiplying through and integrating gives . Apply : . The solution is:
What changes vs constant volume
With growing, the concentration still approaches , but itself can grow without bound because the tank keeps gaining volume. Conversely, if the tank drains (), there is a finite time at which the tank empties, and the ODE stops applying.
Worked Example: 100-Litre Brine Tank
Here is the canonical mixing problem. A tank holds L of water with kg of dissolved salt. Brine of concentration kg/L flows in at L/min; the well-stirred mixture flows out at the same rate. Find , the equilibrium, and the time at which the salt mass reaches 45 kg. Try it on paper first, then expand the panel.
Show step-by-step solution
Step 1. Write the ODE.
, with .
Step 2. Identify equilibrium and time constant.
Setting : kg. min.
Step 3. Write the closed-form solution.
.
Step 4. Evaluate at several times.
| t (min) | Computation | y(t) (kg) |
|---|---|---|
| 0 | 50 − 40·e^{0} = 50 − 40·1.0000 | 10.0000 |
| 10 | 50 − 40·e^{-0.5} = 50 − 40·0.6065 | 25.7388 |
| 20 | 50 − 40·e^{-1} = 50 − 40·0.3679 | 35.2848 |
| 40 | 50 − 40·e^{-2} = 50 − 40·0.1353 | 44.5866 |
| 60 | 50 − 40·e^{-3} = 50 − 40·0.0498 | 48.0085 |
| 100 | 50 − 40·e^{-5} = 50 − 40·0.0067 | 49.7305 |
Notice the entries don't step by a constant amount — the gap to 50 shrinks by a factor every 10 minutes. After three time constants (60 min) the tank is within 2 kg of equilibrium.
Step 5. When does ?
Solve . Take logs: , so minutes.
That is about — a useful rule for engineers: to close 90% of the gap to equilibrium takes a little over two time constants.
Step 6. Concentration in the tank.
At minutes, the salt concentration is kg/L. Compare with the inflow kg/L — we are 90% of the way there.
Step 7. Verify on the explorer.
Set , , , , marker at min. The readout should show kg and Rate IN 2.5 kg/min versus Rate OUT 2.25 kg/min — a small positive net flow still nudging the tank toward 50.
Application: IV Drug Infusion
Same equation, different costume. A drug is infused intravenously at constant rate (mg/min). The kidneys clear it at a rate proportional to the current blood concentration. Let be the drug mass in the body and the apparent volume of distribution (a pharmacokinetic constant, typically tens of litres).
where is the elimination rate constant. The equilibrium drug mass is and the steady-state plasma concentration is . The time constant is , and the elimination half-life is .
Clinical rule of thumb. A continuous IV reaches roughly 94% of its steady-state plasma concentration after . For a drug with a 6-hour half-life, that is 24 hours of infusion before the plasma level stops climbing — which is exactly the kind of question this ODE answers.
Application: Pollutant in a Lake
A lake of volume is fed by an inflow stream and drained by an outflow river, both at the same volumetric rate . Upstream activity contaminates the inflow at concentration . The ODE looks identical to the salt tank — only the names change:
Lake Erie has volume m³ and an outflow of m³/s, which works out to a time constant of about 2.6 years. So even if the polluting source is shut off completely, the lake takes roughly years to fall to 5% of its peak concentration — a sobering policy timeline that falls straight out of this one equation.
Python: Building the Simulator
Before trusting the closed form, let us reconstruct it from the ODE itself. The simulator below steps forward many tiny steps and compares the result to the analytic answer at five checkpoints.
Euler is the simplest viable stepper — and exactly what you would derive by replacing with . For production work you would reach for , but the ladder of understanding goes: ODE → discrete update → loop → closed form. Each rung confirms the next.
Python: Closed Form to Predict an Event
Once the analytic solution is in hand, every timing question becomes a one-line calculation — no loop required. Below we answer "at what time does the tank reach 90% of equilibrium?" for the canonical 100-litre setup.
The logarithm is the inverse of the exponential
Every timing question on this ODE reduces to the same template: . Plug in any target salt mass; if it lies between and , the answer is a real positive number.
PyTorch: Recovering r/V from Noisy Data
In the lab you measure with noise and want to fit the model. With a single ODE parameter the log trick works, but the moment the model gets more complex you want autograd to do the bookkeeping. Below: synthetic noisy samples, two free parameters ( and ), MSE loss, gradient descent.
Why this generalises
Real reactors and biological compartments rarely fit a single-tank model. They might have time-varying inflow, multiple coupled compartments, or unknown delays. Closed-form solutions vanish, but the PyTorch fit template stays identical — define a model in forward(), pick a loss, call backward, step the optimizer. The mixing problem is the simplest possible instance of an ODE-based model-fitting pipeline that scales to entire pharmacokinetic and environmental models.
Common Pitfalls
- Mixing units silently. If is in L/min and is in g/L, then is in g/min — so must be in grams, not kilograms. Pick a unit system at the top of the problem and never change it.
- Forgetting the well-mixed assumption. The whole derivation needs the outflow concentration to equal at every instant. Real tanks stratify; a layered tank obeys a different (much harder) PDE.
- Confusing equilibrium with steady-state concentration. The equilibrium mass is — proportional to volume. The equilibrium concentration is — independent of volume. Watch which one the question is asking about.
- Treating variable-volume as constant-volume. When , the formula no longer applies — there is no equilibrium because never stops changing.
- Sign errors when . The closed form still works — the transient is now negative and the curve falls toward equilibrium. The same sets the speed of falling and rising.
Summary
- A well-mixed tank with rate-in and rate-out obeys the linear first-order ODE .
- When inflow equals outflow, the closed-form solution is , with time constant and equilibrium .
- After one time constant the gap to equilibrium has shrunk by ; after three time constants the tank is within 5% of steady state.
- When inflow and outflow rates differ, and the integrating factor is — still solvable in closed form for any constant flows.
- The same equation governs IV drug infusion, lake pollution, continuous reactors, building HVAC loads, and many more. Only the names of , , and change.
- Computationally, Euler gives the simplest discrete update; the closed form turns every timing question into a one-line log; and autograd plus gradient descent fits the model to real noisy measurements without ever writing a derivative by hand.