Mathematical modelling and simulation of a leaky bucket in Python


A water leak had occurred in the heating system of a person’s home. The whole kitchen had been flooded and the water was seeping into his neighbor’s flat downstairs. To stop the water from escaping into the flat below, a small bucket/container and a piece of tubing was used. The tube had been inserted into the bucket through a small hole near its bottom and the bucket was placed directly below the source of the leak. The idea was that the bucket would collect all the water and safely divert it into the kitchen sink which was within reach of the tube.

While this arrangement had worked on this occasion, it would be worthwhile thinking about a related problem. Given the cross section of the bucket, the cross section of the hole, and the rate of water entering the bucket, would the water have overflowed?

Mathematical Modelling

The math for the problem turned out to be simpler than expected. But, real data that is needed, namely, cross-section of the bucket & tube plus the flow rate of water entering the bucket were not available. So, best estimates were used instead.

Simulation using Python

Once we have the ordinary differential equation (ODE), we can program it into the software and integrate the equation with respect to time to find out the curve of the variable in question. So, here that means the equation for the derivative of height is going to be integrated to find out the height of the water level at a time ‘t’. ODE solvers are available in programming languages like Python, GNU Octave, MATLAB and others. Here, Python was used and the additional libraries of NumPy, SciPy and matplotlib were needed so that we could work with Arrays, solve the ODE and plot the results respectively.

To solve the required equations, it needs to be first expressed in the form of

Luckily, this has already been done in the last section where we find the derivative of the height ‘h’. The below picture shows the first few Python commands. The conversion of units into metric units are done inline in the code itself. ‘t_span’ is a python list with the initial and final time values. It starts at 0 and ends at 8 hours. This is an initial value problem, so the initial value ’y0' is set to 0 meters as it is the initial height of the water in the bucket.

The first line of code in the picture below sets the tolerances (absolute and relative tolerances) which are going to be used by the ode solver. The solve_ivp (solve initial value problem) function performs the integration up to the time specified in the ‘t_span’ array. The next line after that plots the graph with y(t) in the y-axis and the t in the x-axis. The last line prints the last two values of y(t).

As we can see, the final value of the height in the bucket after 8 hours is 9.3e-5 meters or approximately 0.1 millimeters. This is also the theoretical height of the bucket needed to prevent an overflow.

If you look at the shape of the curve in the previous figure, it looks like there is a sharp change in value at t=100. But, a closer look needs to be taken. That’s not a problem as we can adjust the graph settings and plot again.

The curve now looks different. It looks like there is a smooth curve instead of a sharp one.


A mathematical model of a system was solved for the height of the water level in a bucket. It was then plotted. The height of the water level in the bucket after 8 hours was determined. It was noticed that there was a gradual smooth curve. With the estimates available for the model parameters, the minimum height needed to prevent an overflow was also determined.


[1] “An introduction to NumPy and matplotlib”
[2] “NumPy quickstart”
[3] “Integration (scipy.integrate)”
[4] “Torricelli’s Law Formula and Derivation — Torricelli’s Theorem (”
[5] “Discharge of liquids (Torricelli’s law)”

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store