Question

I'm plotting an animation of a function dx/dt and I've set the axes, but when the animation runs, the axes change dynamically, according to the plot. How do I fix this problem?

clear all;

%Equation variables:
s = 0;
r = 0.4;

%axes limits:
xmin = 0;
xmax = 2;
ymin = -.05;
ymax = .2;

%s limits:
smin = 0;
smax = 1;
s_steps = 100;

%our x-space:
x = linspace(xmin, xmax, 100);

%Let's try different s-values and plot as an animation:
for s=linspace(smin, smax, s_steps)
    counter = counter + 1;

    %dx/dt:
    dxdt = s - r.*x + (x.^2)./(1 + x.^2);

    figure(1),    
    subplot(2,1,1)
    axis([xmin xmax ymin ymax]);    
    plot(x, dxdt);


    title(strcat('S-value:', num2str(s)));

    hold on;
    y1 = line([0 0], [ymin ymax], 'linewidth', 1, 'color', [0.5, 0.5, 0.5], 'linestyle', '--');
    x1 = line([xmin xmax], [0 0], 'linewidth', 1, 'color', [0.5, 0.5, 0.5], 'linestyle', '--');
    hold off;
end
Was it helpful?

Solution

Simply reverse the order of the "axis" command and the "plot" command. When you use "axis" before "plot", "plot" overrides the "axis" command with default axes. Switching these two lines will fix the problem.

However, if you want to animation individual points, there is also a "set" command which works wonders for neat animations. Check this out:

% data (Lissajous curves)
t = linspace(0,2*pi,50) ;
x = 2 * sin(3*t) ;
y = 3 * sin(4*t) ;

figure % force view
h = plot(x(1),y(1),'b-',x(1),y(1),'ro') ;
pause(0.5) ;
axis([-3 3 -4 14]) ; % specify a strange axis, that is not changed

for ii=2:numel(x),
  % update plots
  set(h(1),'xdata',x(1:ii),'ydata',y(1:ii)) ;
  set(h(2),'xdata',x(ii),'ydata',y(ii)) ;
  pause(0.1) ; drawnow ; % visibility
end

http://www.mathworks.com/matlabcentral/newsreader/view_thread/270439

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top