Skip to main content
Engineering LibreTexts

13.5: Animation

  • Page ID
    84754
  • \( \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}}\)

    Animation is a useful tool for checking the results of a physical model. If something is wrong, animation can make it obvious. There are two ways to do animation in MATLAB. One is to use getframe to capture a series of images and then use movie to play them back.

    The more informal way is to draw a series of plots. Listing 13.2 is a function that animates the results of a baseball simulation:

    Listing 13.2: A function that animates the results of a baseball simulation

    function animate(T,M)
        X = M(:,1);
        Y = M(:,2);
    
        minmax = [min([X]), max([X]), min([Y]), max([Y])];
    
        for i=1:length(T)
            clf; hold on
            axis(minmax)
            plot(X(i), Y(i), 'o')
            drawnow;
    
            if i < length(T)
                dt = T(i+1) - T(i);
                pause(dt);
            end
        end
    end

    The input variables are the output from ode45: T, which contains the time values, and M, which contains the position and velocity of the baseball.

    A vector of four elements, minmax is used inside the loop to set the axes of the figure. This is necessary because otherwise MATLAB scales the figure each time through the loop, so the axes keep changing, which makes the animation hard to watch.

    Each time through the loop, animate uses clf to clear the figure and axis to reset the axes. Then it plots a circle to represent the position of the .

    We have to call drawnow after plot so that MATLAB actually displays each plot. Otherwise it waits until you finish drawing all the figures and then updates the display.

    We can call animate like this:

        tspan = [0 10];
        W = [0 1 30 40];
        [T, M] = ode45(@rate_func, tspan, W);
        animate(T, M)

    One limitation of this kind of animation is that the speed of the animation depends on how fast your computer can generate the plots. Since the results from ode45 are usually not equally spaced in time, your animation might slow down where ode45 takes small time steps and speed up where the time steps are larger.

    One way to fix this problem is to change the way we specify tspan. Here’s an example:

        tspan = 0:0.1:10;

    The result is a vector that goes from 0 to 10 with a step size of 0.1. Passing tspan to ode45 in this form doesn’t affect the accuracy of the results; ode45 still uses variable time steps to generate the estimates, but then it interpolates them before returning the results.

    With equal time steps, the animation should be smoother.

    Another option is to use pause to play the animation in real time. After drawing each frame and calling drawnow, you can compute the time until the next frame and use pause to wait:

        dt = T(i+1) - T(i);
        pause(dt);

    A limitation of this method is that it ignores the time required to draw the figure, so it tends to run slow, especially if the figure is complex or the time step is small.


    This page titled 13.5: Animation 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?