修复动画轴

fix axes for animation

我正在使用 matlab 进行小型模拟。为此,我想创建模拟对象(倒立摆)的动画。不幸的是,轴不断重新缩放。 我尝试了一切。有类似的 questions,但我无法让它工作。我得到的最好的是下面的代码。在我同时获得两个轴的地方,从 -5 缩放到 5 以及由 matlab 缩放的那些。

%init visualisation
visualisation = figure();
axis([-5 5 -5 5]);
xlim([-5 5]);
ylim([-5 5]);
ax_hand = axes;

for i = 1:N
    k1 = h * feval ( 'RHS', t0, x0, u );
    k2 = h * feval ( 'RHS', t0 + (h/2), x0 + (k1/2), u);
    k3 = h * feval ( 'RHS', t0 + h/2, x0 + k2/2, u);
    k4 = h * feval ( 'RHS', t0 + h, x0 + k3, u);
    x0 = x0 + ( k1 + 2*k2 + 2*k3 + k4 ) / 6;
    t0 = t0 + h;
    % model output
    wi(1:neqn,i+1) = x0';

    % model visualisation
    %plotting cart
    figure(visualisation);
    plot(x0(3), 0, 'ro', 'LineWidth', 3);
    %plotting pendulum
    l = 2;
    %plot(sin(x0(1))*l, cos(x0(1))*l, 'b*' , 'LineWidth', 2);
    % regulator

end;

这是一种方法:

  • 做第一个情节(可能是空的,无所谓)。得到它的句柄,比如 h.
  • 设置轴限制并包含语句 axis manual 以冻结它们。
  • 通过 h'XData''YData' 属性更新绘图(循环)。

示例:

h = plot(NaN, NaN, 'o'); %// empty plot
axis([0 10 0 5])
axis manual %// this line freezes the axes
for n = 1:10
    x = 1:n;
    y = sqrt(x);
    set(h, 'XData', x, 'YData', y)
    pause(.2)
end

包含两个图的示例:

h1 = plot(NaN, NaN, 'bo'); %// empty plot
hold on
h2 = plot(NaN, NaN, 'r*'); %// empty plot
axis([0 10 0 5])
axis manual %// this line freezes the axes
for n = 1:10
    x1 = 1:n;
    y1 = sqrt(x1);
    set(h1, 'XData', x1, 'YData', y1)
    x2 = 4.5;
    y2 = n/2-.5;
    set(h2, 'XData', x2, 'YData', y2)
    pause(.2)
end