Graphing a Function

Finding the minimum in Example Taylor 1.38

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


phi(deg) v0(m/s) dv0/dphi (m/s/rad)
2 9.89723258408 -141.53702005
3 8.08516249393 -76.9251826388
4 7.00693975533 -49.8569669816
5 6.27293692234 -35.5755931233
6 5.73279722195 -26.9706904219
7 5.31457386022 -21.3155915084
8 4.97894120646 -17.3636314785
9 4.70234894492 -14.4723419338
10 4.46971871385 -12.2804512376
11 4.27088941957 -10.5708222548
12 4.09873070802 -9.20589989654
13 3.94807191412 -8.09474701236
14 3.81506015347 -7.17508459752
15 3.69675810175 -6.40297285552
16 3.59088239369 -5.74661308435
17 3.49562800381 -5.18248163889
18 3.40954708923 -4.69283897061
19 3.33146339811 -4.26407869937
20 3.26041053032 -3.88560596284
21 3.19558657461 -3.54905844198
22 3.13632022663 -3.24775466843
23 3.08204510687 -2.97629636315
24 3.03228003475 -2.73027720673
25 2.98661369517 -2.50606645009
26 2.94469258963 -2.30064599476
27 2.90621147492 -2.11148623191
28 2.87090570786 -1.93645035101
29 2.83854506655 -1.7737198161
30 2.80892872693 -1.62173575663
31 2.78188115205 -1.47915244631
32 2.75724870891 -1.34480005011
33 2.73489687021 -1.21765453798
34 2.71470789067 -1.09681318331
35 2.69657887097 -0.981474443385
36 2.68042014173 -0.870921298226
37 2.66615391322 -0.764507332339
38 2.6537131484 -0.661644999409
39 2.64304062487 -0.561795627576
40 2.63408815879 -0.464460811959
41 2.626815969 -0.369174908893
42 2.62119216427 -0.27549839791
43 2.61719234027 -0.183011916598
44 2.61479927601 -0.0913108027849
45 2.61400272215 -6.79554318879e-16
46 2.61479927601 0.0913108027849
47 2.61719234027 0.183011916598
48 2.62119216427 0.27549839791
49 2.626815969 0.369174908893
50 2.63408815879 0.464460811959
51 2.64304062487 0.561795627576
52 2.6537131484 0.661644999409
53 2.66615391322 0.764507332339
54 2.68042014173 0.870921298226
55 2.69657887097 0.981474443385
56 2.71470789067 1.09681318331
57 2.73489687021 1.21765453798
58 2.75724870891 1.34480005011
59 2.78188115205 1.47915244631
60 2.80892872693 1.62173575663
61 2.83854506655 1.7737198161
62 2.87090570786 1.93645035101
63 2.90621147492 2.11148623191
64 2.94469258963 2.30064599476
65 2.98661369517 2.50606645009
66 3.03228003475 2.73027720673
67 3.08204510687 2.97629636315
68 3.13632022663 3.24775466843
69 3.19558657461 3.54905844198
70 3.26041053032 3.88560596284
71 3.33146339811 4.26407869937
72 3.40954708923 4.69283897061
73 3.49562800381 5.18248163889
74 3.59088239369 5.74661308435
75 3.69675810175 6.40297285552
76 3.81506015347 7.17508459752
77 3.94807191412 8.09474701236
78 4.09873070802 9.20589989654
79 4.27088941957 10.5708222548
80 4.46971871385 12.2804512376
81 4.70234894492 14.4723419338
82 4.97894120646 17.3636314785
83 5.31457386022 21.3155915084
84 5.73279722195 26.9706904219
85 6.27293692234 35.5755931233
86 7.00693975533 49.8569669816
87 8.08516249393 76.9251826388

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.

Graphing Data

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)


phi(deg) v0(m/s) dv0/dphi (m/s/rad)
[9.8972325840817703, 8.0851624939251714, 7.0069397553298627, 6.272936922343411, 5.7327972219529544, 5.3145738602196291, 4.978941206462224, 4.7023489449226368, 4.4697187138537782, 4.2708894195661031, 4.0987307080205877, 3.9480719141238105, 3.81506015347244, 3.6967581017470974, 3.5908823936920884, 3.4956280038086285, 3.4095470892281639, 3.3314633981117949, 3.260410530316002, 3.1955865746114207, 3.1363202266294459, 3.0820451068664672, 3.03228003474764, 2.9866136951693316, 2.9446925896338838, 2.9062114749243975, 2.8709057078638591, 2.8385450665505489, 2.8089287269290852, 2.7818811520541269, 2.7572487089054385, 2.7348968702149246, 2.7147078906677939, 2.696578870972222, 2.6804201417255329, 2.6661539132201346, 2.6537131483988703, 2.6430406248684628, 2.6340881587881197, 2.6268159689986099, 2.6211921642701834, 2.6171923402740438, 2.6147992760146526, 2.6140027221516813, 2.6147992760146526, 2.6171923402740438, 2.6211921642701834, 2.6268159689986095, 2.6340881587881193, 2.6430406248684628, 2.6537131483988698, 2.6661539132201342, 2.6804201417255329, 2.696578870972222, 2.7147078906677939, 2.7348968702149246, 2.7572487089054385, 2.7818811520541264, 2.8089287269290852, 2.8385450665505485, 2.8709057078638591, 2.9062114749243975, 2.9446925896338838, 2.9866136951693316, 3.03228003474764, 3.0820451068664663, 3.1363202266294459, 3.1955865746114203, 3.2604105303160016, 3.3314633981117945, 3.4095470892281634, 3.4956280038086285, 3.5908823936920884, 3.6967581017470974, 3.8150601534724387, 3.9480719141238114, 4.0987307080205868, 4.2708894195661022, 4.4697187138537773, 4.7023489449226368, 4.9789412064622196, 5.3145738602196291, 5.7327972219529544, 6.2729369223434119, 7.0069397553298547, 8.0851624939251607]
[-141.53702005000801, -76.925182638815258, -49.856966981644312, -35.575593123294851, -26.970690421934307, -21.315591508436885, -17.36363147845104, -14.472341933841824, -12.280451237627011, -10.570822254762819, -9.2058998965447039, -8.0947470123593739, -7.175084597523858, -6.4029728555178504, -5.7466130843510284, -5.1824816388867756, -4.6928389706087597, -4.264078699370712, -3.8856059628360895, -3.5490584419835964, -3.2477546684292999, -2.9762963631500163, -2.7302772067303693, -2.5060664500855991, -2.3006459947617972, -2.11148623190976, -1.9364503510057087, -1.77371981610086, -1.621735756626981, -1.47915244630848, -1.3448000501143291, -1.2176545379802208, -1.0968131833114274, -0.98147444338505119, -0.87092129822602393, -0.76450733233866286, -0.66164499940897992, -0.56179562757622747, -0.46446081195902927, -0.36917490889263627, -0.27549839791000091, -0.18301191659833962, -0.091310802784896741, -6.7955431887923443e-16, 0.091310802784895728, 0.18301191659833912, 0.27549839790999991, 0.36917490889263516, 0.46446081195902889, 0.56179562757622614, 0.66164499940897958, 0.7645073323386623, 0.87092129822602304, 0.98147444338504997, 1.0968131833114274, 1.2176545379802199, 1.3448000501143291, 1.4791524463084775, 1.6217357566269794, 1.7737198161008592, 1.9364503510057078, 2.1114862319097596, 2.3006459947617972, 2.5060664500855991, 2.7302772067303693, 2.9762963631500141, 3.2477546684293008, 3.5490584419835942, 3.8856059628360868, 4.2640786993707094, 4.6928389706087588, 5.1824816388867756, 5.7466130843510284, 6.4029728555178504, 7.17508459752385, 8.0947470123593792, 9.2058998965446932, 10.57082225476281, 12.280451237627, 14.472341933841815, 17.363631478450991, 21.315591508436885, 26.970690421934314, 35.57559312329488, 49.856966981644149, 76.92518263881496]
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87]

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]:
[<matplotlib.lines.Line2D at 0x107533210>]

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]:
[<matplotlib.lines.Line2D at 0x107837310>]

In [ ]: