In this notebook, we will graph the function for $v_0(\phi)$ in order to observe graphically the angle at which the initial speed required to putt the ball into the hole is a minimum. The function is
$$ v_0 = \sqrt{\frac{4g\sin(\theta)}{\cos(\phi)\sin(\phi)}} $$and its derivative, which equals 0 at the minimum $v_0$, is
$$ \frac{dv_0}{dt}=\frac{\frac{g\sin(\theta)}{cos(\phi)^2}-\frac{g\sin(\theta)}{\sin(\phi)^2}}{\sqrt{\frac{g\sin(\theta)}{\cos(\phi)\sin(\phi)}}} $$We can graph either function. If we graph $v_0(\phi)$ then we will look for $\phi$ when $v_0$ is a minimum. If we graph $\frac{dv_0}{dt}$ then we will look for $\phi$ for $\frac{dv_0}{dt}=0$. Let's do both!
We will have to use values of $\theta$ and $g$ since we will calculate a numerical value of $v_0$ for each value of $\phi$. Let's use $\theta=5^\circ$ and $g=9.8$ m/s/s.
In [4]:
from __future__ import division, print_function
from math import *
from numpy import *
#constants
theta_in_deg=5 #degree
theta=theta_in_deg*pi/180 #radian
g=9.8
#define functions which take an argument of phi and returns the calculation of the function
def f(phi): #angle must be in rad
return sqrt(4*g*sin(theta)/(cos(phi)*sin(phi)))
def dfdphi(phi): #angle must be in rad
return (g*sin(theta)/cos(phi)**2-g*sin(theta)/sin(phi)**2)/sqrt(g*sin(theta)/(cos(phi)*sin(phi)))
#print a heading for the data
print('phi(deg)', 'v0(m/s)', 'dv0/dphi (m/s/rad)')
#phi cannot be zero or 90deg or it will give an unphysical result.
#loop through values of phi from 2deg to 88deg.
for phi_deg in arange(2,88,1):
phi_rad=phi_deg*pi/180 #convert angle to rad
v=f(phi_rad) #get v0
dvdphi=dfdphi(phi_rad) #get dv0/dphi
print(phi_deg,v,dvdphi) #print results
You can see from the data that the minimum value of $v_0$ occurs for $\phi=45^\circ$. However, let's graph the data. To graph data, we will have to store data in a list. I'm going to rewrite the program for clarity.
We will need matplotlib.
In [5]:
%matplotlib inline
In [6]:
import matplotlib.pyplot as plt
In [7]:
#constants
theta_in_deg=5 #degree
theta=theta_in_deg*pi/180 #radian
g=9.8
vlist=[]
dvlist=[]
philist=[]
#define functions which take an argument of phi and returns the calculation of the function
def f(phi): #angle must be in rad
return sqrt(4*g*sin(theta)/(cos(phi)*sin(phi)))
def dfdphi(phi): #angle must be in rad
return (g*sin(theta)/cos(phi)**2-g*sin(theta)/sin(phi)**2)/sqrt(g*sin(theta)/(cos(phi)*sin(phi)))
#print a heading for the data
print('phi(deg)', 'v0(m/s)', 'dv0/dphi (m/s/rad)')
#phi cannot be zero or 90deg or it will give an unphysical result.
#loop through values of phi from 2deg to 88deg.
for phi_deg in arange(2,88,1):
phi_rad=phi_deg*pi/180 #convert angle to rad
v=f(phi_rad) #get v0
dvdphi=dfdphi(phi_rad) #get dv0/dphi
#add data to the list
vlist.append(v)
dvlist.append(dvdphi)
philist.append(phi_deg)
print(vlist)
print(dvlist)
print(philist)
Now that we see data and are confident in our code, let's create the graphs.
In [8]:
plt.title('v0 as a function of phi')
plt.xlabel('phi (deg)')
plt.ylabel('v0 (m/s)')
plt.plot(philist, vlist, 'b.')
Out[8]:
You can see that $v_0$ is a minimum at 45 degrees.
In [11]:
plt.title('dv0/dphi as a function of phi')
plt.xlabel('phi (deg)')
plt.ylabel('dv0/dphi (m/s/rad)')
plt.plot(philist, dvlist, 'r.')
Out[11]:
In [ ]: