#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_line_magic('matplotlib', 'inline') # In[2]: from pylab import * # In[3]: x = linspace(0, 10) plot(x, sin(x)) # Let's create an array of 100 runners. Each runner is characterized by his speed, which we draw in the interval from 1 to 20 (km per hour). # In[3]: runners = random_integers(1, 20, size=(100)) # In[10]: runners.sort() # Let's plot this distribution of speeds: # In[12]: bar(range(runners.size), runners) # Each runner propagates at his running speed. So while everyone starts at the same position, soon the faster runners will overtake the slower runners. This can be illustrated by the following figure: # In[13]: def propagate(runners, t): distance = runners * t return distance # In[35]: distance = zeros_like(runners, dtype=float32) total_time = 0 for ind, t in enumerate([0.1, 0.1, 0.2, 0.2]): distance += propagate(runners, t) total_time += t subplot(2, 2, ind + 1) title('time=%.2f' % total_time) bar(range(runners.size), distance) ylim(0, 12) tight_layout() # What does this look like if we let runners start from random points? # In[37]: distance = (rand(runners.size) - 0.5) * 6 total_time = 0 for ind, t in enumerate([0.1, 0.1, 0.2, 0.2]): distance += propagate(runners, t) total_time += t subplot(2, 2, ind + 1) title('time=%.2f' % total_time) bar(range(runners.size), distance) ylim(-3, 14) tight_layout() # In[ ]: