Opening the book…
Sometimes we want to show how a graph is drawn over time. MATLAB provides several ways to create animations. Two beginner-friendly options are comet and movie.
Using comet
The comet function animates a curve as if a point is moving along the path.
Using comet to create an animated plot.
clear; clc; clf;
x = -2*pi:1/5:2*pi;
y = sin(x);
comet(x, y)
xlabel('x')
ylabel('sin(x)')
title('Animated sin(x)')
Solution
Run the code in MATLAB to see how the plot is animated.

Using movie
MATLAB plots one point at a time, captures each frame using getframe, and then plays the frames as a movie.
Using comet to create an animated plot.
clear; clc; clf;
x = -2*pi:1/5:2*pi;
y = sin(x);
n = length(x);
for i = 1:n
plot(x(i), y(i), 'r*')
axis([min(x)-1 max(x)+1 min(y)-1 max(y)+1])
M(i) = getframe;
end
movie(M)
Solution
Run the code in MATLAB to see how the plot is animated by moving the a red star.
This page titled 10.12: Animated Plots was last modified on Tue, 30 Jun 2026 02:11:46 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.
The book could not be loaded just now.