3

See my code below:

x = linspace(-2*pi,2*pi,100);
y = linspace(-2*pi,2*pi,100);
[X,Y] = meshgrid(x,y);

for t = linspace(0,2*pi,400);
    Z = sin(sqrt(X.^2 + Y.^2 + t)) ./ (X.^2 + Y.^2 + t);
    figure(1); surf(X,Y,Z);
    pause(0.01)
    surf(X,Y,Z);
    az = 90;
    el = 25;
    figure(2);view(az,el);
    xlim([-10,10]);
    ylim([-10,10]);
    zlim([-1,1]);
end

Why does z limit still keep on changing even though set to [-1 1]?.

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
Amir A.
  • 31
  • 1

2 Answers2

3

Your problem is that you keep recreating the entire plot anew every time you call surf in your loop, thus discarding any previous changes you made to the axes limits, etc. When animating plots, it's much more efficient (and easier to control the axes settings) if you use set to update graphics objects instead of completely reproducing them, as illustrated here.

Here's a better approach to create the animation you want:

% Initialize data:
x = linspace(-2*pi, 2*pi, 100);
y = linspace(-2*pi, 2*pi, 100);
[X, Y] = meshgrid(x, y);
Z = sin(sqrt(X.^2 + Y.^2)) ./ (X.^2 + Y.^2);

% Create plot objects and set axes settings:
hSurf = surf(X, Y, Z);
view(90, 25);
xlim([-10 10]);
ylim([-10 10]);
zlim([-1 1]);

% The animation loop:
for t = linspace(0, 2*pi, 400)
  Z = sin(sqrt(X.^2 + Y.^2 + t)) ./ (X.^2 + Y.^2 + t);  % Recalculate Z
  set(hSurf, 'ZData', Z);  % Update surface Z data
  pause(0.01);  % Pause/refresh plot
end
gnovice
  • 125,304
  • 15
  • 256
  • 359
-1

I believe the view() function overrides/disregards magnitudes of x y and z.

Edit: view([x,y,z]) sets the view direction to the Cartesian coordinates x, y, and z. The magnitude of (x,y,z) is ignored. From here

niCk cAMel
  • 869
  • 1
  • 10
  • 26