Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Industries Academia Support User Community Company

 

Student Center

The Physics of Baseball


This example illustrates how to use MATLAB and Simulink to understand the physics of baseball. A pitcher throws a baseball at 50 m/s at an angle of 36.7 degrees in the air. We can use MATLAB and Simulink to answer the following questions:

  • How far will the ball go before it hits the ground?
  • How high will the ball go?
  • How long does it take to hit the ground?

We can use several different methods to solve this problem:

  1. Using calculus
  2. Using MATLAB ODE solvers
  3. Solving it symbolically
  4. Using Simulink

Let’s look at each approach.

Using Calculus


We can use Equations 1, 2, and 3 to answer the questions.

Using MATLAB ODE Solvers

function dxdt = trajectoryode(t , x)
dxdt(1)= x(2);
dxdt(2,1)= 0;
dxdt(3,1) = x(4);
dxdt(4,1) = -9.8;

MATLAB Script

v0=20;
theta = deg2rad(36.7);
v0x = v0*cos(theta);
v0y = v0*sin(theta);
x0=0;
y0=0;
[t,xsol]=ode45(@trajectoryode,[0 2.5], ...
[x0 v0x y0 v0y],[]);

x=xsol(:,1);
vx=xsol(:,2);
y=xsol(:,3);
vy=xsol(:,4);

plot(x,y)

axis([0 300 -40 50])
hold on
for i= 1:size(x)
plot(x,y,'+') pause(0.2)
end

axis([0 300 -40 50])
plot(t,y)
plot(t,vy)

Solving the Questions Symbolically

x0=0;
y0=0;
v0= 20;
theta = deg2rad(36.7);
v0x = v0*cos(theta);
v0y = v0*sin(theta);

syms t

ay=-9.8;
vy=int(ay,t)+v0y;
y= int(vy,t)+y0;

ax=0;
vx=int(ax,t)+v0x;
x= int(vx,t)+x0;

To reduce number of digits, we'll use the vpa function:

Y=vpa(y,3);
X=vpa(x,3);

pretty(X)
pretty(Y)

16.0 t

2
-4.90 t + 12.0 t

The results are as expected:

ezplot(X,[0 2.5])
ezplot(Y,[0 2.5])

To calculate the time it takes for the ball to reach the ground:

solve(Y) ans =
[0]
[2.4489795918367346938775510204082]

Range=subs(X,t, 2.44897959183673469387755102040)

The following code shows the maximum height:

solve(vy)

ans = 1.2196431570929004168164635989435

MaxHeight=subs(Y,t, 1.2196431570929004168164635989435)

Using Simulink

Now, let’s solve the baseball questions using Simulink.

  1. Open Simulink by typing “simulink” at the MATLAB prompt.
  2. The two given values are the initial velocity and the initial angle of the ball’s trajectory with respect to the ground.
    1. Insert two Constant blocks into your Simulink model (Sources library), and label them “velocity” and “angle”.
    2. Double-click each block and enter 50 for velocity and 36.7*(pi/180) for angle.
  3. We will need the horizontal and vertical components of the initial velocity, so we will need to compute the sine and cosine of the initial angle.
    1. Insert two Trigonometric Function blocks into your Simulink model (Math Operations library).
    2. Double-click one block and choose cos as the function.
    3. Double-click the other block and choose sin as the function.
    4. Connect the output of the angle block to the cosine block. Now we have a signal that connects one block to another block
    5. Branch off this signal by right-clicking it and dragging the signal to the sine block. The model should now look like this:


    6. Figure 1: Calculating the sine and cosine of the initial angle.
  4. Find the horizontal and vertical components of the initial velocity by multiplying velocity by cos(angle) and by sin(angle).
    1. Insert two Product blocks into the model.
    2. Multiply velocity by cos(angle) by connecting the velocity block to one of the input ports of one of the Product blocks, and by connecting the cosine block to the other input port of that same Product block.
    3. Follow the above step to find the vertical velocity component. (Remember to right-click and drag a signal to create a branch.) The model now looks like this:


    4. Figure 2: Calculating the horizontal and vertical components of velocity.
  5. When the ball travels through the air, we need to take gravity into account.
    1. Insert a Constant block into your model and label it “gravity”.
    2. Double-click on the block and enter -9.81 as the value.
    3. Insert an Integrator block into the model (Continuous library).
    4. Connect the gravity block to the input port of the Integrator block. The output signal of the Integrator port is the velocity of the ball due to gravity.
    5. Insert a Sum block into the model (Math Operations library).
    6. Add the gravity velocity signal and the vertical initial velocity signals together by routing these signals to the input ports of the Sum block. The output of the Sum block is now the total vertical velocity of the ball. The model now looks like this:

      Figure 3: Taking gravity into account.
  6. Now that we have the horizontal and vertical components of the ball’s velocity, we need to find the horizontal and vertical components of the ball’s position.
    1. Insert two Integrator blocks into the model.
    2. Connect the horizontal component of velocity signal to one of the Integrator blocks. Connect the vertical component of velocity signal to the other Integrator block.
    3. Insert two To Workspace blocks into the model (Sinks library).
    4. Connect the horizontal component of position signal to one of the To Workspace blocks. Connect the vertical component of position signal to the other To Workspace block. Double-click each To Workspace block to change the variable names (for example, “x” and “y”). This outputs the position of the ball to the MATLAB workspace once the simulation is completed.
    5. Insert an XY Graph block into the model (Sinks library). Let’s use this block to view the ball’s position as the simulation is running.
    6. Connect the position signal’s horizontal component to the first input port of the XY Graph block. Connect the position signal’s vertical component to the second input port of the XY Graph block.
    7. Double-click the XY Graph block and change the axes limits so you can visualize the ball’s trajectory. (We can use x-min = 0, x-max = 300, y-min = -100, y-max = 60.) The model now looks like this:


    8. Figure 4: Calculating the position of the ball.
  7. At this point, the model will run. Press the Play button to see the ball’s trajectory. (To see the plot being created, go to the Simulation > Configuration Parameters menu, change the solver to a fixed-step solver, and change the step time to 0.01. Be sure to change back to the variable-step solver for a more precise answer.) However, there is a problem with the model. Simulink does not know to stop the simulation when the ball hits the ground. As you can see in the XY Graph, the ball travels past y = 0 and keeps going until the simulation stops. To keep this from happening, let’s use an If-Action Subsystem to stop the simulation once the ball reaches the ground.
    1. Insert an If block and an If-Action Subsystem block in the model (Ports & Subsystems library).
    2. Connect the position signal’s vertical component to the input port of the If block.
    3. Double-click the If block and change the If expression to “u1 < 0”. Also, uncheck Show else condition because we won’t be performing a specific action if the ball hasn’t reached the ground yet.
    4. Connect the output port of the If block to the input port of the If-Action Subsystem block. If the ball has reached the ground, this subsystem will be enabled.
    5. Double-click the If-Action Subsystem block.
    6. Insert a Constant block and a Stop Simulation block (Sinks library) and connect the two together. When this subsystem is enabled – meaning, when the ball reaches the ground – the value 1 is fed into the Stop Simulation block, which then stops the simulation. The model now looks like this:


    7. Main Model


      If-Action Subsystem



      Figure 5: Complete Simulink model for baseball example.
  8. We now have a visual of the ball’s trajectory, and the position of the ball at each time step. The last step is to answer the questions posed. We can do this by using MATLAB code:
    1. Go to the File > Model properties window and click the Callbacks tab. We can run custom MATLAB code whenever the model is loaded, initialized, started, stopped, saved, or closed.
    2. In the Simulation Stop Function edit box, insert the following code:


    3. disp(['The ball travels ', num2str(x(end)) ,' meters before it hits the ground.'])

      disp(['The ball reaches a maximum height of ', num2str(max(y)) ,' meters.'])

      disp(['It takes ', num2str(t(end)) ,' seconds before the ball hits the ground.'])
    4. Now we’ll run the model to answer these questions. Of course, we can change the initial velocity and angle easily by changing the contained values in the model’s angle and velocity blocks. Be sure to change the limits of the XY Graph block accordingly so you can see the ball’s complete trajectory. You might also want to set the Stop time to inf so the simulation always runs to completion. (The simulation always stops when the ball hits the ground, anyway.) The following output is displayed to the MATLAB window when the model is executed:

      The ball travels 244.2208 meters before it hits the ground.

      The ball reaches a maximum height of 45.4959 meters.

      It takes 6.092 seconds before the ball hits the ground.

      Let’s compare these answers to those derived by using calculus:

        Calculus Simulink
      Distance Ball Travels 244.2 m 244.2 m
      Maximum Height 45.51 m 45.50 m
      Total Time in Air 6.092 s 6.092 s

      Thus, the numerical solution to this problem using Simulink is almost identical to the theoretical solution using calculus.

Contact sales
Software evaluation for instructors
Free technical kit
E-mail this page

MATLAB & Simulink Student Version

Includes full-featured versions of both MATLAB and Simulink

From School To Industry

"The only limit to MATLAB is your imagination."
Carl Klopstein
Radar Systems Engineer