【问题标题】:Creating a point moving along a graph in MATLAB在 MATLAB 中创建沿图移动的点
【发布时间】:2011-02-10 16:06:48
【问题描述】:

我希望在 MATLAB 中创建一个简单的 log(x) 图,其中模型显示点随时间沿曲线移动。

总体目标是将这些图中的两个并排放置,并对它们应用一种算法。我真的不确定从哪里开始。

我在 MATLAB 编码方面相对较新,所以任何帮助都会非常有用!

谢谢 卢克

【问题讨论】:

    标签: matlab animation graph model plot


    【解决方案1】:

    这是@Jacob 解决方案的一个变体。我们无需在每一帧重绘所有内容 (clf),而是简单地更新点的位置:

    %# control animation speed
    DELAY = 0.01;
    numPoints = 600;
    
    %# create data
    x = linspace(0,10,numPoints);
    y = log(x);
    
    %# plot graph
    figure('DoubleBuffer','on')                  %# no flickering
    plot(x,y, 'LineWidth',2), grid on
    xlabel('x'), ylabel('y'), title('y = log(x)')
    
    %# create moving point + coords text
    hLine = line('XData',x(1), 'YData',y(1), 'Color','r', ...
        'Marker','o', 'MarkerSize',6, 'LineWidth',2);
    hTxt = text(x(1), y(1), sprintf('(%.3f,%.3f)',x(1),y(1)), ...
        'Color',[0.2 0.2 0.2], 'FontSize',8, ...
        'HorizontalAlignment','left', 'VerticalAlignment','top');
    
    %# infinite loop
    i = 1;                                       %# index
    while true      
        %# update point & text
        set(hLine, 'XData',x(i), 'YData',y(i))   
        set(hTxt, 'Position',[x(i) y(i)], ...
            'String',sprintf('(%.3f,%.3f)',[x(i) y(i)]))        
        drawnow                                  %# force refresh
        %#pause(DELAY)                           %# slow down animation
    
        i = rem(i+1,numPoints)+1;                %# circular increment
        if ~ishandle(hLine), break; end          %# in case you close the figure
    end
    

    【讨论】:

      【解决方案2】:

      一个简单的解决方案是:

      x = 1:100;
      y = log(x);
      DELAY = 0.05;
      for i = 1:numel(x)
          clf;
          plot(x,y);
          hold on;
          plot(x(i),y(i),'r*');
          pause(DELAY);
      end
      

      【讨论】:

      • 这不是一个好的解决方案,因为每次重绘所有内容时都会清除整个图形。所以速度、扩展空间等并不是最好的方式。
      【解决方案3】:

      您可能想看看COMET 函数,它会制作曲线动画。

      例如(使用与@Jacob 相同的数字)

      x = 1:100;
      y = log(x);
      comet(x,y)
      

      如果您想显示在​​线上移动的点(而不是“绘制”它),您只需在之前绘制线

      x = 1:100;
      y = log(x);
      plot(x,y,'r')
      hold on %# to keep the previous plot
      comet(x,y,0) %# 0 hides the green tail
      

      【讨论】:

      • +1 我不知道彗星。顺便说一句,如果彗星似乎什么都不做,请尝试更多点(例如 x=1:.01:100;)
      【解决方案4】:

      与@Jacob 类似的更复杂的解决方案。这里我添加了一些优化,使用句柄图形和一个 MATLAB 电影对象进行播放。

      x=1:100;
      y=log(x);
      figure
      plot(x,y);
      hold on; % hold on so that the figure is not cleared
      h=plot(x(1),y(1),'r*'); % plot the first point
      DELAY=.05;
      
      
      for i=1:length(x)
          set(h,'xdata',x(i),'ydata',y(i)); % move the point using set
                                            % to change the cooridinates.
          M(i)=getframe(gcf);
          pause(DELAY)
      end
      
      %% Play the movie back
      
      % create figure and axes for playback
      figure
      hh=axes;
      set(hh,'units','normalized','pos',[0 0 1 1]); 
      axis off
      
      movie(M) % play the movie created in the first part
      

      【讨论】:

        【解决方案5】:

        可以这样解决

        x = .01:.01:3;
        comet(x,log(x))
        

        【讨论】:

        • Ben 请描述得更详细些。
        • 这并没有增加 Jonas 的回答中尚未涵盖的任何新内容。
        猜你喜欢
        • 2011-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-31
        • 1970-01-01
        相关资源
        最近更新 更多