简体   繁体   English

用Matlab /八度创建动画图

[英]Animating plot with matlab / octave

I'm trying to animate a plot for this equation see below I'm trying to animate it for b when 300>= b <= 486 我正在为该方程式的动画制作动画,请参见下文,当300> = b <= 486时,我正在为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)

I'm using octave 3.8.1 which is a clone of matlab 我正在使用octave 3.8.1,这是matlab的克隆

Put your plotting code in a for loop with b as the iterating variable, then place a pause for a small amount of time. 将绘图代码放入带有b作为迭代变量的for循环中,然后pause一小段时间。 After, plot your graph, then use drawnow to refresh the plot. 之后,绘制图形,然后使用drawnow刷新图形。 In other words, try this code. 换句话说,请尝试此代码。 I've placed %// Change comments in your code where I have introduced new lines: 我已经在您的代码中引入%// Change地方添加了%// Change注释,并在其中添加了新行:

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

As a bonus, I've put what the current value of b is at each drawing of the plot. 作为奖励,我将b的当前值放在图的每个图上。 BTW, I don't know why normalize_y is in your code when you aren't using it. 顺便说一句,我不知道为什么当您不使用normalize_y在您的代码中。 Do you mean to plot normalize_y instead of y ? 您是要绘制normalize_y而不是y吗? Just an after thought. 只是经过一番思考。 Anyway, try that out and see how it looks. 无论如何,尝试一下,看看外观如何。 Good luck! 祝好运!

Another solution would be to use the handle of a plot and then only update the 'YData' -property of a plot. 另一种解决方案是使用图的句柄,然后仅更新图的'YData'属性。 That is especially useful for more complex plots where there are more than 1 line but you only want to change one line. 这对于多于一条线但您只想更改一条线的更复杂的图尤为有用。 Also Axis-labels are not overwritten then, which generally prevents alot of overhead. 轴标签也不会被覆盖,这通常可以避免很多开销。 In Matlabcode it could look like this: 在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

This will work with octave 3.8.1 这将适用于八度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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM