Matlab for 循环动画

发布于 2024-12-05 05:39:05 字数 529 浏览 1 评论 0原文

我正在尝试对函数的图形进行动画处理,但无法让程序绘制正确的点。我想绘制时间 0 到 10 之间的点并为该图设置动画。如何获得随时间变化的图?

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);

我将代码改进为:

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);

这应该可以完成我正在尝试的操作,但是现在我得到“索引超出矩阵维度”。我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

古镇旧梦 2024-12-12 05:39:05

下面的示例显示了录制 AVI 影片时沿圆形路径的动画点。

要了解有关在 MATLAB 中制作动画和录制电影的更多信息,请查看此指南

%# some parameters
ERASEMODE = 'normal';        %# normal,xor
RENDERER = 'painters';       %# painters,zbuffer,opengl

%# data
t = linspace(0,2*pi,100)';   %'# adjust number of points here
D = [cos(t) -sin(t)];

%# plot circluar path
figure('DoubleBuffer','on', 'Renderer',RENDERER)
plot(D(:,1), D(:,2), 'Color','b', 'LineWidth',2)
grid on, axis([-1.5 1.5 -1.5 1.5]), axis square
xlabel('x'), ylabel('y'), title('Circle Animation')
%#set(gca, 'DrawMode','fast')

%# moving point
hPoint = line('XData',D(1,1), 'YData',D(1,2), 'EraseMode',ERASEMODE,  ...
        'Color','r', 'Marker','.', 'MarkerSize',30);

%# moving coordinates text
hTxtCoords = text(D(1,1), D(1,2), sprintf('(%.2f,%.2f)',D(1,:)), ...
    'Color',[0.2 0.2 0.2], 'FontSize',8, 'EraseMode',ERASEMODE, ...
    'HorizontalAlignment','left', 'VerticalAlignment','top');

%# angle text
hTxtAngle = text(0, 0, num2str(t(1),'%.02f'), ...
    'FontSize',15, 'EraseMode',ERASEMODE, ...
    'HorizontalAlignment','center', 'VerticalAlignment','middle');

%# prepare video output
useVideoWriter = ~verLessThan('matlab','7.11');
if useVideoWriter
    vid = VideoWriter('vid.avi');
    vidObj.Quality = 100;
    vid.FrameRate = 30;
    open(vid);
else
    vid = avifile('vid.avi', 'fps',30, 'quality',100);
end

%# loop
for i=1:numel(t)
    set(hPoint, 'XData',D(i,1), 'YData',D(i,2))    %# update point location
    set(hTxtAngle, 'String',num2str(t(i),'%.02f')) %# update angle text
    set(hTxtCoords, 'Position',D(i,:), ...         %# update angle text
        'String',sprintf('(%.3f,%.3f)',D(i,:)))
    drawnow                                        %# force refresh
    if ~ishandle(hPoint), break; end               %# if you close the figure

    %# capture frame
    if useVideoWriter
        writeVideo(vid,getframe);
    else
        vid = addframe(vid, getframe(gcf));
    end 
end

%# close and save video output
if useVideoWriter
    close(vid);
else
    vid = close(vid);
end

%# open AVI file using system default player
winopen('vid.avi')

screenshot_animation

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.

%# some parameters
ERASEMODE = 'normal';        %# normal,xor
RENDERER = 'painters';       %# painters,zbuffer,opengl

%# data
t = linspace(0,2*pi,100)';   %'# adjust number of points here
D = [cos(t) -sin(t)];

%# plot circluar path
figure('DoubleBuffer','on', 'Renderer',RENDERER)
plot(D(:,1), D(:,2), 'Color','b', 'LineWidth',2)
grid on, axis([-1.5 1.5 -1.5 1.5]), axis square
xlabel('x'), ylabel('y'), title('Circle Animation')
%#set(gca, 'DrawMode','fast')

%# moving point
hPoint = line('XData',D(1,1), 'YData',D(1,2), 'EraseMode',ERASEMODE,  ...
        'Color','r', 'Marker','.', 'MarkerSize',30);

%# moving coordinates text
hTxtCoords = text(D(1,1), D(1,2), sprintf('(%.2f,%.2f)',D(1,:)), ...
    'Color',[0.2 0.2 0.2], 'FontSize',8, 'EraseMode',ERASEMODE, ...
    'HorizontalAlignment','left', 'VerticalAlignment','top');

%# angle text
hTxtAngle = text(0, 0, num2str(t(1),'%.02f'), ...
    'FontSize',15, 'EraseMode',ERASEMODE, ...
    'HorizontalAlignment','center', 'VerticalAlignment','middle');

%# prepare video output
useVideoWriter = ~verLessThan('matlab','7.11');
if useVideoWriter
    vid = VideoWriter('vid.avi');
    vidObj.Quality = 100;
    vid.FrameRate = 30;
    open(vid);
else
    vid = avifile('vid.avi', 'fps',30, 'quality',100);
end

%# loop
for i=1:numel(t)
    set(hPoint, 'XData',D(i,1), 'YData',D(i,2))    %# update point location
    set(hTxtAngle, 'String',num2str(t(i),'%.02f')) %# update angle text
    set(hTxtCoords, 'Position',D(i,:), ...         %# update angle text
        'String',sprintf('(%.3f,%.3f)',D(i,:)))
    drawnow                                        %# force refresh
    if ~ishandle(hPoint), break; end               %# if you close the figure

    %# capture frame
    if useVideoWriter
        writeVideo(vid,getframe);
    else
        vid = addframe(vid, getframe(gcf));
    end 
end

%# close and save video output
if useVideoWriter
    close(vid);
else
    vid = close(vid);
end

%# open AVI file using system default player
winopen('vid.avi')

screenshot_animation

能怎样 2024-12-12 05:39:05

它正在完全按照您的要求进行操作。您正在对 xy 进行二次采样,因此看起来有点有趣。

尝试

plot(x,y);
axis square;
ax = axis;  

for jx = 1 : length(t),
    plot(x(ix), y(ix), '*');
    axis(ax);  grid on;
    F(ix) = getframe;
end

movie(F, 1, 1/(t(2)-t(1)))

我也会使用 t = 1 : 0.1 : 10; 以便它以 10 FPS 而不是 100 FPS 绘制。将频率降低到,比如说,w = pi;也会更加顺利。

归根结底,Matlab 并不是一个很好的动画解决方案。

回答精炼代码问题

您需要使用plot(x,y);,但这会显示另一个错误 - 您的帧索引不是从 1 开始。将在第一次迭代中因 F(j) 而阻塞,其中 j = 2。为什么不直接循环t = 1 : 10

It's doing exactly what you ask it to do. You're subsampling the x and y, so it looks kind of funny.

Try

plot(x,y);
axis square;
ax = axis;  

for jx = 1 : length(t),
    plot(x(ix), y(ix), '*');
    axis(ax);  grid on;
    F(ix) = getframe;
end

movie(F, 1, 1/(t(2)-t(1)))

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 on F(j) in the first iteration, where j = 2. Why not just loop over t = 1 : 10?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文