14:440:127– Introduction to Computers for Engineers
Notes for Lecture 07
Rutgers University, Spring 2010
Instructor Blase E. Ur
1
Animation
Taking the power of loops together with Matlab’s
plot
function, we can create animations. The idea behind animated
graphs is that we’ll graph something, pause, graph something slightly different, pause, graph something slightly dif
ferent, and so on. In essence, you’ll write a loop that plots slightly different things in each iteration.
There are two very useful functions you’ll use when creating animations.
One is
pause( )
, which pauses for a
specified number of seconds i.e.
pause(0.5
) stops the program for 0.5 seconds before continuing.
The other useful function is
axis([xmin xmax ymin ymax])
, which specifies a particular axis for a graph/plot. This
allows you to slowly add more data to your plot, but have a consistent axis. Using the
axis
command, which should
almost always directly follow
plot
, the axes won’t change, making it seem like only the graph is moving.
One example of animation is to graph a single point over time. For t = 0 through 20, let’s graph
x
(
t
) =
t
*
cos
(
t
)
and
y
(
t
) = 5
*
sin
(
t
). To do this, we’ll create a
for loop
for t running from 0 to 20, moving up in small increments
so that it looks like the point is moving continuously. We’ll display it as an X rather than as a single point so that
we can actually see it. We’ll define our axes based on the theoretical minima and maxima of the function within our
specified domain. Here’s one way of animating this function:
% X floating through space
for t=0:0.1:20
% choose a small interval
x=t*cos(t);
y=5*sin(t);
pause(0.05) % can come anywhere in the loop
plot(x,y,’X’)
axis([20 20 5 5])
% must follow plot
end
It might be easier to precompute all of the points we’ll display and store them as vectors, and then just loop
through these vectors. Let’s rewrite the above loop using this new method. Note that we can also improve upon our
method of determining the axes since we’ve precomputed all points that will be displayed:
% X floating through space
t = 0:0.1:20;
x=t.*cos(t);
% don’t forget dot operations
y=5*sin(t);
for j = 1:length(x)
pause(0.05) % can come anywhere in the loop
plot(x(j),y(j),’X’)
axis([min(x) max(x) min(y) max(y)])
end
Another use of animation is to show motion over time by leaving a “trail” of where the X has been.
Let’s re
tain our method of precomputing all of the points we’ll want to show. Now, rather than just looping through and
displaying a single point at a time, we’ll keep displaying more points each time we go through the loop. Note that
we’ll use 1:j to signify “the first j points in the vector.”
1
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
% X floating through space, leaving a trail
t = 0:0.1:20;
x=t.*cos(t);
% don’t forget dot operations
y=5*sin(t);
for j = 1:length(x)
pause(0.05) % can come anywhere in the loop
plot(x(1:j),y(1:j),x(j),y(j),’X’)
axis([min(x) max(x) min(y) max(y)])
end
Notice the complicated
plot
command we use.
This will plot the first j points of the x and y vectors normally,
and also plot just the j’th point as an X.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 Finch
 builtin function, Closure, Function Handles

Click to edit the document details