During a break at a music festival, the crew is launching T-shirts into the audience using a T-shirt cannon. And you’re in luck — your seat happens to be in the line of flight for one of the T-shirts! In other words, if the cannon is strong enough and the shirt is launched at the right angle, it will land in your arms.
The rows of seats in the audience are all on the same level (i.e., there is no incline), they are numbered 1, 2, 3, etc., and the T-shirts are being launched from directly in front of Row 1. Assume also that there is no air resistance (yes, I know, that’s a big assumption). You also happen to know quite a bit about the particular model of T-shirt cannon being used — with no air resistance, it can launch T-shirts to the very back of Row 100 in the audience, but no farther.
The crew member aiming in your direction is still figuring out the angle for the launch, which you figure will be a random angle between zero degrees (straight at the unfortunate person seated in Row 1) and 90 degrees (straight up). Which row should you be sitting in to maximize your chances of nabbing the T-shirt?
In [4]:
from matplotlib import pyplot as plt
import random
import math
import numpy as np
import itertools
In [158]:
LAST_ROW = 100
NUM_SAMPLES = 200_000
TIME_SPACING = 0.001
TINY_MIN = -0.5
G = 9.81
In [69]:
def find_exit_velocity(max_distance):
return math.sqrt(max_distance * G)
In [125]:
def create_x_linspace(max_distance):
return np.linspace(0, max_distance, max_distance*10 + 1)
In [123]:
def create_points(exit_velocity, angle, x_linspace):
vx0 = exit_velocity * math.cos(angle)
vy0 = exit_velocity * math.sin(angle)
y_points = (vy0*x_linspace)/vx0 - (((G/2) * x_linspace**2) / (vx0**2))
return y_points
In [72]:
def create_overlay_plot(points_series):
fig, ax = plt.subplots(figsize=(10,10))
for x,y in points_series:
ax.plot(x,y, color='k', alpha=0.01)
ax.set_xlim(0,LAST_ROW)
ax.set_ylim(bottom=0)
plt.show()
In [152]:
def create_overlay_plot_stacked(points_series, x_lispace, max_distance):
fig, ax = plt.subplots(figsize=(14, 14))
fig.patch.set_facecolor('white')
plt.subplots_adjust(hspace=0)
ax1 = plt.subplot2grid((4, 1), (0, 0), rowspan=3)
ax2 = plt.subplot2grid((4, 1), (3, 0))
for p in points_series:
ax1.plot(x_linspace,p, color='k', alpha=.002)
ax2.hist([x_linspace[np.max(np.where(p >= 0))] for p in points_series], color='k', bins=LAST_ROW, density=True)
ax2.invert_yaxis()
# limits
ax1.set_xlim(0,LAST_ROW)
ax1.set_ylim(bottom=0, top=np.max(points_series))
ax2.set_xlim(ax1.get_xlim())
#ticks
ax1.xaxis.tick_top()
ax2.yaxis.tick_right()
ax1.set_yticks([])
#labels
ax1.set_xlabel('row')
ax2.set_ylabel('probability')
ax2.set_xlabel(ax1.get_xlabel())
ax2.yaxis.set_label_position('right')
ax1.xaxis.set_label_position('top')
plt.show()
In [126]:
exit_velocity = find_exit_velocity(LAST_ROW)
x_linspace = create_x_linspace(LAST_ROW)
In [159]:
random_angles = [random.uniform(0, (math.pi)/2) for _ in range(NUM_SAMPLES * 4)]
point_cloud = np.array([create_points(exit_velocity, a, x_linspace) for a in random_angles])
In [ ]:
create_overlay_plot_stacked(point_cloud, x_linspace, LAST_ROW)
In [ ]: