I'm trying to animate the graph of a function but I cant get the program to graph the correct points. I want to plot points between time 0 and 10 and animate this graph. How do I get the plot as a function of time?
clear;
w = 2*pi;
t = 0:.01:10;
y = sin(w*t);
x = cos(w*t);
for j=1:10
plot(x(6*j),y(6*j),'*');
axis square;
grid on;
F(j) = getframe;
end
movie(F,1,1);
I refined the code to:
clear;
w = 2*pi;
for j=2:11
t=j-1;
y = sin(w*t);
x = cos(w*t);
plot(x(t),y(t),'*');
axis square;
grid on;
F(j) = getframe;
end
movie(F);
This should do what I'm trying however now I'm getting an "Index exceeds matrix dimension." How can I solve this?
Here is an example that show an animated point along a circular path, while recording an AVI movie.
To learn more about doing animations and recording movies in MATLAB, check out this guide.
It's doing exactly what you ask it to do. You're subsampling the
x
andy
, so it looks kind of funny.Try
I would also use
t = 1 : 0.1 : 10;
so that it plots at 10 FPS instead of 100. Slowing the frequency down to, say,w = pi;
will be smoother as well.At the end of the day, Matlab is just not a great animation solution.
Answer to refined code question
You'd need to use
plot(x,y);
, but this will reveal another error - your frame index does not start at 1. It will choke onF(j)
in the first iteration, wherej = 2
. Why not just loop overt = 1 : 10
?