Rick T
Rick T

Reputation: 3389

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

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

Upvotes: 0

Views: 1113

Answers (3)

Rick T
Rick T

Reputation: 3389

This will work with 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

Upvotes: 0

Nras
Nras

Reputation: 4311

Another solution would be to use the handle of a plot and then only update the 'YData'-property of a plot. 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:

% // 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

Upvotes: 0

rayryeng
rayryeng

Reputation: 104464

Put your plotting code in a for loop with b as the iterating variable, then place a pause for a small amount of time. After, plot your graph, then use drawnow to refresh the plot. In other words, try this code. I've placed %// Change comments in your code where I have introduced new lines:

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. BTW, I don't know why normalize_y is in your code when you aren't using it. Do you mean to plot normalize_y instead of y? Just an after thought. Anyway, try that out and see how it looks. Good luck!

Upvotes: 1

Related Questions