Skip to main content
Engineering LibreTexts

13.1: Optimal Baseball

  • Page ID
    84548
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In the previous chapter we wrote functions to simulate the flight of a baseball with a known initial velocity. Now we’ll use that code to find the launch angle that maximizes range, that is, the distance the ball travels before landing.

    First, we need an event function to stop the simulation when the ball lands.

    function [value, isterminal, direction] = event_func(t, W)
        value = W(2);
        isterminal = 1;
        direction = -1;
    end

    This is similar to the event function we saw in Chapter 11.3, except that it uses W(2) as the event value, which is the y-coordinate. This event function stops the simulation when the altitude of the ball is 0 and falling.

    Now we can call ode45 like this:

        P = [0; 1];       % initial position in m
        V = [40; 30];     % initial velocity in m/s
        W = [P; V];       % initial condition
    
        tspan = [0 10];
        options = odeset('Events', @event_func);
        [T, M] = ode45(@rate_func, tspan, W, options);

    The initial position of the ball is 1 m above home plate. The ball’s initial velocity is 40 m/s in the \(x\)-direction and 30 m/s in the \(y\)-direction.

    The maximum duration of the simulation is 10 s, but we expect an event to stop the simulation first. We can get the final values of the simulation like this:

        T(end)
        M(end, :)

    The final time is 5.1 s. The final \(x\)-position is 131 m; the final \(y\)-position is 0, as expected.


    This page titled 13.1: Optimal Baseball is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?