User Tools

Site Tools


gibson:teaching:summer-2017:techcamp:numerical-baseball

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gibson:teaching:summer-2017:techcamp:numerical-baseball [2017/07/17 19:38]
gibson [Running the Matlab code to see the results]
gibson:teaching:summer-2017:techcamp:numerical-baseball [2017/07/18 09:09] (current)
gibson [Running the Matlab code to see the results]
Line 20: Line 20:
  
 Here $x$ and $y$ are the horizontal and vertical distances from home plate, $v_x$ and $v_y$ are the components of velocity in the $x$ and $y$ directions, and $g$ is the acceleration due to gravity. Here $x$ and $y$ are the horizontal and vertical distances from home plate, $v_x$ and $v_y$ are the components of velocity in the $x$ and $y$ directions, and $g$ is the acceleration due to gravity.
- 
 $dx/dt$ means the rate of change of $x$, and $dv_x/dt$ means the rate of change of the $x$ velocity. ​ $dx/dt$ means the rate of change of $x$, and $dv_x/dt$ means the rate of change of the $x$ velocity. ​
 +
 +Note that these equations relate //the rates of change// of a set of variables to the //values// of those variables. This kind of system is called an Ordinary Differential Equation, or ODE.
  
 We'll now implement some Matlab code to solve these equations numerically. We write a function ''​f_nodrag''​ in file ''​fnodrag.m''​ which defines the above equations of motion as a function mapping $x,​y,​v_x,​v_y$ into $dx/dt, dy/dt, dv_x/dt, dv_y/​dt$. ​ We'll now implement some Matlab code to solve these equations numerically. We write a function ''​f_nodrag''​ in file ''​fnodrag.m''​ which defines the above equations of motion as a function mapping $x,​y,​v_x,​v_y$ into $dx/dt, dy/dt, dv_x/dt, dv_y/​dt$. ​
Line 27: Line 28:
 <code matlab> <code matlab>
 function dudt = f_nodrag(t, u)  function dudt = f_nodrag(t, u) 
 +% equations of motion for baseball in flight without air resistance
  
     g = 9.8;    % acceleration of gravity m/s^2     g = 9.8;    % acceleration of gravity m/s^2
  
 +    % extract components of vector u into variables x, y, vx, vy
     x  = u(1);     x  = u(1);
     y  = u(2);     y  = u(2);
Line 35: Line 38:
     vy = u(4);     vy = u(4);
  
 +    % compute rates of change of those components
     dxdt = vx;     dxdt = vx;
     dydt = vy;     dydt = vy;
Line 40: Line 44:
     dvydt = -g;     dvydt = -g;
  
 +    % pack rates of change back into vector dudt, return value of function
     dudt = [dxdt; dydt; dvxdt; dvydt];     dudt = [dxdt; dydt; dvxdt; dvydt];
 end end
Line 63: Line 68:
  
 <code matlab> <code matlab>
-function dudt = baseballeqns(t,u)  +function dudt = f_withdrag(t,u)  
-    rho_air ​ = 1.196; ​ % kg/m^3, density of dry air, 21 C, sea level   ​(Fenway) +% equations of motion for baseball in flight with turbulent air resistance 
-  + 
-    C = 0.3;       ​% drag coefficient for baseball (rough sphere) +    rho_air ​ = 1.196; ​    ​% kg/m^3, density of dry air, 21 C, sea level   ​(Fenway) 
-    g = 9.81;      % acceleration due to gravity in m/s^2 +    C = 0.3;              % drag coefficient for baseball (rough sphere) 
-    r = 0.0375; ​   % radius of baseball in m (3.75 cm) +    g = 9.81;             ​% acceleration due to gravity in m/s^2 
-    A = pi*r^2; ​   % cross-sectional area of baseball in m^2 +    r = 0.0375; ​          ​% radius of baseball in m (3.75 cm) 
-    m = 0.145; ​    ​% mass of baseball in kg (145 gm +    A = pi*r^2; ​          ​% cross-sectional area of baseball in m^2 
- +    m = 0.145; ​           % mass of baseball in kg (145 gm)
     mu = rho_air*C_D*A/​2;​ % coefficient of nonlinear |v|^2 term, in mks units     mu = rho_air*C_D*A/​2;​ % coefficient of nonlinear |v|^2 term, in mks units
  
 +    % extract components of vector u into variables x, y, vx, vy
     x  = u(1);     x  = u(1);
     y  = u(2);     y  = u(2);
Line 79: Line 85:
     vy = u(4);     vy = u(4);
  
 +    % compute rates of change of those components
     dxdt = vx;     dxdt = vx;
     dydt = vy;     dydt = vy;
Line 84: Line 91:
     dvydt = -mu/m * vy * sqrt(vx^2 + vy^2) - g;     dvydt = -mu/m * vy * sqrt(vx^2 + vy^2) - g;
  
 +    % pack rates of change back into vector dudt, return value of function ​
     dudt = [dxdt; dydt; dvxdt; dvydt]     dudt = [dxdt; dydt; dvxdt; dvydt]
 end end
Line 90: Line 98:
  
 ==== Solving the equations of motion numerically ==== ==== Solving the equations of motion numerically ====
 +
  
 The file ``baseballsolve.m`` solves the above equations with some numerical solution methods, given The file ``baseballsolve.m`` solves the above equations with some numerical solution methods, given
Line 96: Line 105:
  
 <code matlab> <code matlab>
 +% Compute the trajectory of a baseball in flight, with and without ​
 +% air resistance and plot results
 +
 x = 0.0;      % horizontal position of home plate, meters x = 0.0;      % horizontal position of home plate, meters
 y = 1.0;      % height of ball over strike zone y = 1.0;      % height of ball over strike zone
Line 107: Line 119:
 tspan = [0 10];  ​ tspan = [0 10];  ​
  
-% solve the baseball equations numerically+% solve the baseball equations numerically. ode45 is Matlab'​s numerical algorithm for ODEs
 [t, u_withdrag] = ode45(@f_withdrag,​ tspan, u0); [t, u_withdrag] = ode45(@f_withdrag,​ tspan, u0);
 [t, u_nodrag] ​  = ode45(@f_nodrag, ​  ​tspan,​ u0); [t, u_nodrag] ​  = ode45(@f_nodrag, ​  ​tspan,​ u0);
Line 122: Line 134:
 plot(x, y, '​b-'​) plot(x, y, '​b-'​)
  
-% plot outfield fence: 390 ft = 120 m, 17 ft high = 5.2 m+% plot outfield fence at x = 120 m (390 ft) and y 0 to 5.2 m (17 ft)
 plot([120, 120], [0, 5.2], '​k-',​ '​linewidth',​2) ​ plot([120, 120], [0, 5.2], '​k-',​ '​linewidth',​2) ​
  
Line 134: Line 146:
 title("​baseball trajectory"​) title("​baseball trajectory"​)
 </​code>​ </​code>​
- 
 ==== Running the Matlab code to see the results ==== ==== Running the Matlab code to see the results ====
  
Line 147: Line 158:
 {{ :​gibson:​teaching:​summer-2017:​techcamp:​baseballsolve.png?​600 |}} {{ :​gibson:​teaching:​summer-2017:​techcamp:​baseballsolve.png?​600 |}}
  
-**Questions**+The thick black line shows the outfiled fence at 390 ft or 120 m, and 17 ft or 5.2 m high.  
 + 
 +==== Question ====
  
   * How important is the effect of air resistance? ​   * How important is the effect of air resistance? ​
 +
 +  * Air density at Fenway park (sea level) is 1.196 kg/m^3. At one mile high in Denver, it's 0.986 kg/m^3. How much does the change in density affect the path of the ball at different angles and speeds? Try to change the above codes to show the path of the ball for both Fenway and Denver conditions. See if you can find a speed and angle that gives a home run in Denver but not at Fenway. ​
  
   * Play around with the parameters in the functions, like the speed and angle of the ball. Can you figure out the minimum speed and optimal angle to just clear the outfiled fence and hit a home run?   * Play around with the parameters in the functions, like the speed and angle of the ball. Can you figure out the minimum speed and optimal angle to just clear the outfiled fence and hit a home run?
  
-==== Hitting a home run ==== +==== Next ====
- +
-The code as written above is kind of unwieldy for finding the minimum speed and optimal angle for hitting a home run. We can revise it to make it easier to try different values of $v$ and $\theta$. Let's wrap all the code in a function of those two variables and include a numerical test to see if the ball clears the outfield fence. +
- +
  
 +[[gibson:​teaching:​summer-2017:​techcamp:​computer-homerun | Hitting a home run]]
  
  
  
gibson/teaching/summer-2017/techcamp/numerical-baseball.1500345484.txt.gz · Last modified: 2017/07/17 19:38 by gibson