【问题标题】:Animating plot with matlab / octave用matlab / octave制作动画图
【发布时间】:2014-11-04 04:06:52
【问题描述】:

我正在尝试为这个等式绘制动画,见下文我正在尝试为 b 动画它,当 300>= b

clear all, clc,clf,tic
m=3.73;
a=480;
b=486;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
y=m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
normalize_y=(y/max(abs(y))*0.8);
plot(x,y)

我使用的是 octave 3.8.1,它是 matlab 的克隆版

【问题讨论】:

    标签: matlab octave


    【解决方案1】:

    将您的绘图代码放入以b 作为迭代变量的for 循环中,然后将pause 放置一小段时间。之后,绘制图表,然后使用drawnow 刷新图表。换句话说,试试这个代码。我在你的代码中放置了%// Change cmets 我已经引入了新行:

    m=3.73;
    a=480;
    r=1;
    fs=44100;
    x=linspace(0,2*pi,fs)';
    figure;
    for b = 300 : 486 %// Change
        y=m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
        normalize_y=(y/max(abs(y))*0.8);
        pause(0.1); %// Change
        plot(x,y);
        title(['b = ' num2str(b)]); %// Change
        drawnow; %// Change
    end
    

    作为奖励,我在每张绘图中都标注了b 的当前值。顺便说一句,我不知道为什么 normalize_y 在您不使用它时会出现在您的代码中。你的意思是绘制normalize_y 而不是y?只是事后的想法。无论如何,尝试一下,看看它的外观。祝你好运!

    【讨论】:

      【解决方案2】:

      另一种解决方案是使用绘图的句柄,然后只更新绘图的'YData'-property。这对于有超过 1 行但您只想更改一行的更复杂的绘图特别有用。轴标签也不会被覆盖,这通常可以防止大量开销。 在 Matlabcode 中可能如下所示:

      % // Parameter and x-range
      m=3.73;
      a=480;
      r=1;
      fs=44100;
      x=linspace(0,2*pi,fs)';
      % // function to compute y for given x and parameter b
      f = @(x, b) m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
      
      % // first plot out of loop (to get plot handle)
      figure;
      b = 300;
      y = f(x, b);
      handle = plot(x, y);
      xlabel('x') % // only set once
      ylabel('y=f(x,b)') % // only set once
      title(['b = ' num2str(b)]);
      pause(0.1);
      
      % // animate for b = 301 to 86
      for b = 301:486 %// Change
          set(handle, 'YData', f(x, b)) % set new y-data in plot handle
          pause(0.1); %// update plot
          title(['b = ' num2str(b)]); %// update title
      end
      

      【讨论】:

      • 我尝试运行你的代码,但出现错误:octave_base_value::array_value(): wrong type argument 'complex matrix' in line set(handle, 'YData', f(x, b)) % 在绘图句柄中设置新的 y 数据
      • @RickT 好吧,这很不幸,在 Matlab 中这段代码运行良好并生成了动画情节。它只是抛出这个警告:Warning: Imaginary parts of complex X and/or Y arguments ignored,但是当我运行其他建议的解决方案来解决您的问题时,我得到了同样的警告。
      • 我找到了一个可以使用 set(handle, 'YData', real (f(x, b))) 的解决方法,这会起作用
      【解决方案3】:

      这适用于 octave 3.8.1

      % // Parameter and x-range
      m=3.73;
      a=480;
      r=1;
      fs=44100;
      x=linspace(0,2*pi,fs)';
      % // function to compute y for given x and parameter b
      f = @(x, b) m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
      
      % // first plot out of loop (to get plot handle)
      figure;
      b = 300;
      y = f(x, b);
      handle = plot(x, y);
      xlabel('x') % // only set once
      ylabel('y=f(x,b)') % // only set once
      title(['b = ' num2str(b)]);
      pause(0.1);
      
      % // animate for b = 301 to 86
      for b = 301:486 %// Change
          %set(handle, 'YData', f(x, b)) % set new y-data in plot handle
      
          %To work with octave 3.8.1 use the line below
          set(handle, 'YData', real (f(x, b)))
      
          pause(0.1); %// update plot
          title(['b = ' num2str(b)]); %// update title
      end
      

      【讨论】:

        猜你喜欢
        • 2013-05-10
        • 1970-01-01
        • 2014-12-30
        • 2018-11-15
        • 2012-11-24
        • 2022-01-25
        • 2014-12-26
        • 1970-01-01
        • 2017-09-21
        相关资源
        最近更新 更多