CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
# -*- coding: utf-8 -*-
2
3
4
"""
5
Computes the trajectory of a stitched baseball with air drag.
6
Takes altitude into account (higher altitude means further travel) and the
7
stitching on the baseball influencing drag.
8
9
This is based on the book Computational Physics by N. Giordano.
10
11
The takeaway point is that the drag coefficient on a stitched baseball is
12
LOWER the higher its velocity, which is somewhat counterintuitive.
13
"""
14
15
16
from __future__ import division
17
import math
18
import matplotlib.pyplot as plt
19
from numpy.random import randn
20
import numpy as np
21
22
23
class BaseballPath(object):
24
def __init__(self, x0, y0, launch_angle_deg, velocity_ms, noise=(1.0,1.0)):
25
""" Create baseball path object in 2D (y=height above ground)
26
27
x0,y0 initial position
28
launch_angle_deg angle ball is travelling respective to ground plane
29
velocity_ms speeed of ball in meters/second
30
noise amount of noise to add to each reported position in (x,y)
31
"""
32
33
omega = radians(launch_angle_deg)
34
self.v_x = velocity_ms * cos(omega)
35
self.v_y = velocity_ms * sin(omega)
36
37
self.x = x0
38
self.y = y0
39
40
self.noise = noise
41
42
43
def drag_force (self, velocity):
44
""" Returns the force on a baseball due to air drag at
45
the specified velocity. Units are SI
46
"""
47
B_m = 0.0039 + 0.0058 / (1. + exp((velocity-35.)/5.))
48
return B_m * velocity
49
50
51
def update(self, dt, vel_wind=0.):
52
""" compute the ball position based on the specified time step and
53
wind velocity. Returns (x,y) position tuple.
54
"""
55
56
# Euler equations for x and y
57
self.x += self.v_x*dt
58
self.y += self.v_y*dt
59
60
# force due to air drag
61
v_x_wind = self.v_x - vel_wind
62
63
v = sqrt (v_x_wind**2 + self.v_y**2)
64
F = self.drag_force(v)
65
66
# Euler's equations for velocity
67
self.v_x = self.v_x - F*v_x_wind*dt
68
self.v_y = self.v_y - 9.81*dt - F*self.v_y*dt
69
70
return (self.x + random.randn()*self.noise[0],
71
self.y + random.randn()*self.noise[1])
72
73
74
75
def a_drag (vel, altitude):
76
""" returns the drag coefficient of a baseball at a given velocity (m/s)
77
and altitude (m).
78
"""
79
80
v_d = 35
81
delta = 5
82
y_0 = 1.0e4
83
84
val = 0.0039 + 0.0058 / (1 + math.exp((vel - v_d)/delta))
85
val *= math.exp(-altitude / y_0)
86
87
return val
88
89
def compute_trajectory_vacuum (v_0_mph,
90
theta,
91
dt=0.01,
92
noise_scale=0.0,
93
x0=0., y0 = 0.):
94
theta = math.radians(theta)
95
96
x = x0
97
y = y0
98
99
v0_y = v_0_mph * math.sin(theta)
100
v0_x = v_0_mph * math.cos(theta)
101
102
xs = []
103
ys = []
104
105
t = dt
106
while y >= 0:
107
x = v0_x*t + x0
108
y = -0.5*9.8*t**2 + v0_y*t + y0
109
110
xs.append (x + randn() * noise_scale)
111
ys.append (y + randn() * noise_scale)
112
113
t += dt
114
115
return (xs, ys)
116
117
118
119
def compute_trajectory(v_0_mph, theta, v_wind_mph=0, alt_ft = 0.0, dt = 0.01):
120
g = 9.8
121
122
### comput
123
theta = math.radians(theta)
124
# initial velocity in direction of travel
125
v_0 = v_0_mph * 0.447 # mph to m/s
126
127
# velocity components in x and y
128
v_x = v_0 * math.cos(theta)
129
v_y = v_0 * math.sin(theta)
130
131
v_wind = v_wind_mph * 0.447 # mph to m/s
132
altitude = alt_ft / 3.28 # to m/s
133
134
ground_level = altitude
135
136
x = [0.0]
137
y = [altitude]
138
139
i = 0
140
while y[i] >= ground_level:
141
142
v = math.sqrt((v_x - v_wind) **2+ v_y**2)
143
144
x.append(x[i] + v_x * dt)
145
y.append(y[i] + v_y * dt)
146
147
v_x = v_x - a_drag(v, altitude) * v * (v_x - v_wind) * dt
148
v_y = v_y - a_drag(v, altitude) * v * v_y * dt - g * dt
149
i += 1
150
151
# meters to yards
152
np.multiply (x, 1.09361)
153
np.multiply (y, 1.09361)
154
155
return (x,y)
156
157
158
def predict (x0, y0, x1, y1, dt, time):
159
g = 10.724*dt*dt
160
161
v_x = x1-x0
162
v_y = y1-y0
163
v = math.sqrt(v_x**2 + v_y**2)
164
165
x = x1
166
y = y1
167
168
169
while time > 0:
170
v_x = v_x - a_drag(v, y) * v * v_x
171
v_y = v_y - a_drag(v, y) * v * v_y - g
172
173
x = x + v_x
174
y = y + v_y
175
176
time -= dt
177
178
return (x,y)
179
180
181
182
183
if __name__ == "__main__":
184
x,y = compute_trajectory(v_0_mph = 110., theta=35., v_wind_mph = 0., alt_ft=5000.)
185
186
plt.plot (x, y)
187
plt.show()
188
189
190
191
192
193