Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872# -*- coding: utf-8 -*-123"""4Computes the trajectory of a stitched baseball with air drag.5Takes altitude into account (higher altitude means further travel) and the6stitching on the baseball influencing drag.78This is based on the book Computational Physics by N. Giordano.910The takeaway point is that the drag coefficient on a stitched baseball is11LOWER the higher its velocity, which is somewhat counterintuitive.12"""131415from __future__ import division16import math17import matplotlib.pyplot as plt18from numpy.random import randn19import numpy as np202122class BaseballPath(object):23def __init__(self, x0, y0, launch_angle_deg, velocity_ms, noise=(1.0,1.0)):24""" Create baseball path object in 2D (y=height above ground)2526x0,y0 initial position27launch_angle_deg angle ball is travelling respective to ground plane28velocity_ms speeed of ball in meters/second29noise amount of noise to add to each reported position in (x,y)30"""3132omega = radians(launch_angle_deg)33self.v_x = velocity_ms * cos(omega)34self.v_y = velocity_ms * sin(omega)3536self.x = x037self.y = y03839self.noise = noise404142def drag_force (self, velocity):43""" Returns the force on a baseball due to air drag at44the specified velocity. Units are SI45"""46B_m = 0.0039 + 0.0058 / (1. + exp((velocity-35.)/5.))47return B_m * velocity484950def update(self, dt, vel_wind=0.):51""" compute the ball position based on the specified time step and52wind velocity. Returns (x,y) position tuple.53"""5455# Euler equations for x and y56self.x += self.v_x*dt57self.y += self.v_y*dt5859# force due to air drag60v_x_wind = self.v_x - vel_wind6162v = sqrt (v_x_wind**2 + self.v_y**2)63F = self.drag_force(v)6465# Euler's equations for velocity66self.v_x = self.v_x - F*v_x_wind*dt67self.v_y = self.v_y - 9.81*dt - F*self.v_y*dt6869return (self.x + random.randn()*self.noise[0],70self.y + random.randn()*self.noise[1])71727374def a_drag (vel, altitude):75""" returns the drag coefficient of a baseball at a given velocity (m/s)76and altitude (m).77"""7879v_d = 3580delta = 581y_0 = 1.0e48283val = 0.0039 + 0.0058 / (1 + math.exp((vel - v_d)/delta))84val *= math.exp(-altitude / y_0)8586return val8788def compute_trajectory_vacuum (v_0_mph,89theta,90dt=0.01,91noise_scale=0.0,92x0=0., y0 = 0.):93theta = math.radians(theta)9495x = x096y = y09798v0_y = v_0_mph * math.sin(theta)99v0_x = v_0_mph * math.cos(theta)100101xs = []102ys = []103104t = dt105while y >= 0:106x = v0_x*t + x0107y = -0.5*9.8*t**2 + v0_y*t + y0108109xs.append (x + randn() * noise_scale)110ys.append (y + randn() * noise_scale)111112t += dt113114return (xs, ys)115116117118def compute_trajectory(v_0_mph, theta, v_wind_mph=0, alt_ft = 0.0, dt = 0.01):119g = 9.8120121### comput122theta = math.radians(theta)123# initial velocity in direction of travel124v_0 = v_0_mph * 0.447 # mph to m/s125126# velocity components in x and y127v_x = v_0 * math.cos(theta)128v_y = v_0 * math.sin(theta)129130v_wind = v_wind_mph * 0.447 # mph to m/s131altitude = alt_ft / 3.28 # to m/s132133ground_level = altitude134135x = [0.0]136y = [altitude]137138i = 0139while y[i] >= ground_level:140141v = math.sqrt((v_x - v_wind) **2+ v_y**2)142143x.append(x[i] + v_x * dt)144y.append(y[i] + v_y * dt)145146v_x = v_x - a_drag(v, altitude) * v * (v_x - v_wind) * dt147v_y = v_y - a_drag(v, altitude) * v * v_y * dt - g * dt148i += 1149150# meters to yards151np.multiply (x, 1.09361)152np.multiply (y, 1.09361)153154return (x,y)155156157def predict (x0, y0, x1, y1, dt, time):158g = 10.724*dt*dt159160v_x = x1-x0161v_y = y1-y0162v = math.sqrt(v_x**2 + v_y**2)163164x = x1165y = y1166167168while time > 0:169v_x = v_x - a_drag(v, y) * v * v_x170v_y = v_y - a_drag(v, y) * v * v_y - g171172x = x + v_x173y = y + v_y174175time -= dt176177return (x,y)178179180181182if __name__ == "__main__":183x,y = compute_trajectory(v_0_mph = 110., theta=35., v_wind_mph = 0., alt_ft=5000.)184185plt.plot (x, y)186plt.show()187188189190191192193