在MATLAB动画中创建沿图形移动的点

时间:2018-10-22 13:13:40

标签: matlab animation point

我正在寻找创建2个点,这些点在MATLAB中沿着2个图形移动,其中模型显示点随时间沿曲线移动。我知道这里有人问过类似的问题,但这对我没有太大帮助:-(我已经尝试实现此解决方案:link 我不知道如何实现

b = linspace(x1,10,numPoints);

进入我的解决方案。

the 2 graphs

这是我在两张图中的代码:

%% definitions

% initial velocity is assumed to be zero
g = -9.81; % [m/s^2]
m = 1; % [kg]
v = -10; % velocity [m/s]
x = [0:10];

t = [0:0.01:2]; % Vector with 10 Elements, step size: 0.01

%%equations
x = g/2 * t.^2 % distance

x1 = 1/2 * v^2/g % distance at v=-10m/s

dx = g * t % velocity 

%%plotting
clf
yyaxis left
plot (t,x)
hold on
xlabel('t [s]')
ylabel('x [m]')

yyaxis right
plot (t, dx)
hold on
xlabel('t [s]')
ylabel('dx [m/s]')
title('Trajectory of the fallen ball')

lgd = legend({'x(t)','v(t)'},'FontSize',12, 'location', 'NorthEast')

set(gca, 'Fontsize', 18)
grid on

这是我的解决方案,目前无法正常运行:

%% definitions
%# control animation speed
DELAY = 0.01
numPoints = 600;

% initial velocity is assumed to be zero
g = -9.81; % [m/s^2]
m = 1; % [kg]
v = -10; % velocity [m/s]
x = [0:10];

t = [0:0.01:2]; % Vector with 10 Elements, step size: 0.01

%%equations
x = g/2 * t.^2 % distance

x1 = 1/2 * v^2/g % distance at v=-10m/s

dx = g * t % velocity 

b = linspace(x1,10,numPoints); %ball --> way x

%%plotting
clf
figure('DoubleBuffer','on')
yyaxis left
plot (t,x)
hold on
xlabel('t [s]')
ylabel('x [m]')

yyaxis right
plot (t, dx)
hold on
xlabel('t [s]')
ylabel('dx [m/s]')
title('Trajectory of the fallen ball')

lgd = legend({'x(t)','v(t)'},'FontSize',12, 'location', 'NorthEast')

set(gca, 'Fontsize', 18)
grid on

%# create moving point # coords text
hLine = line('XData',t(1), 'YData',b(1), 'Color','r', ...
    'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt = text(t(1), ball(1), sprintf('(%.3f,%.3f)',t(1),b(1)), ...
    'Color',[0.2 0.2 0.2], 'FontSize',8, ...
    'HorizontalAlignment','left', 'VerticalAlignment','top');

%# infinite loop
i=1;
while true
    %# update point & text
    set(hLine, 'XData', t(i), 'YData',b(i))
    set(hTxt, 'Position', [t(i) ball(i)], ...
        'String', sprintf('%.3f,%3f)',[t(i) b(i)]))
    drawnow                                  %# force refresh
    %#pause(DELAY)                           %# slow down animation

    i = rem(i+1,numPoints)+1;                %# circular increment
    if ~ishandle(hLine), break; end          %# in case you close the figure
end

1 个答案:

答案 0 :(得分:0)

有些事情我不清楚:

  • 根据您对hLinehText的定义,向量/数组b应包含特定时间点球的y值。而是定义b=linspace(x1,10,numPoints)。这样可以为您提供特定的时间点。

  • ball是什么?它在while循环中使用,但未定义。

  • 首先定义x=[0;10],几行后重新定义x=g/2 * t.^2;

假设我正确理解了您的需求,这是一种解决方法:

%% definitions
%# control animation speed
DELAY = 0.01;
%numPoints = 600;

% initial velocity is assumed to be zero
g = -9.81;               % [m/s^2]
m = 1;                   % [kg]
v = -10;                 % velocity [m/s]
tball = [0:DELAY:2];     % Vector with 10 Elements, step size: 0.01
t = linspace(0, 2, 500); % for plotting the two curves 

%%equations
x = g/2 * t.^2;         % distance
x1 = 1/2 * v^2/g;       % distance at v=-10m/s
dx = g * t;             % velocity 
ball1 = g/2 * tball.^2; % Position of Ball 1
ball2 = g * tball;      % Position of Ball 2
%b = linspace(x1,10,numPoints); %ball --> way x

%%plotting
clf
figure('DoubleBuffer','on')
yyaxis left
plot (t,x)
hold on
xlabel('t [s]')
ylabel('x [m]')

yyaxis right
plot (t, dx)
hold on
xlabel('t [s]')
ylabel('dx [m/s]')
title('Trajectory of the fallen ball')

lgd = legend({'x(t)','v(t)'},'FontSize',12, 'location', 'NorthEast');

set(gca, 'Fontsize', 18)
grid on

% create moving point coords text for ball 1 and ball 2
hLine1 = line('XData',tball(1), 'YData',ball1(1), 'Color','r', ...
    'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt1 = text(tball(1), ball1(1), sprintf('(%.3f,%.3f)',tball(1),ball1(1)), ...
    'Color',[0.2 0.2 0.2], 'FontSize',8, ...
    'HorizontalAlignment','left', 'VerticalAlignment','top');
hLine2 = line('XData',tball(1), 'YData',ball2(1), 'Color','r', ...
    'Marker','o', 'MarkerSize',6, 'LineWidth',2);
hTxt2 = text(tball(1), ball2(1), sprintf('(%.3f,%.3f)',tball(1),ball2(1)), ...
    'Color',[0.2 0.2 0.2], 'FontSize',8, ...
    'HorizontalAlignment','left', 'VerticalAlignment','top');


i=1;
while true
    % update point & text for ball 1
    set(hLine1, 'XData', tball(i), 'YData',ball1(i))
    set(hTxt1, 'Position', [tball(i) ball1(i)], ...
        'String', sprintf('%.3f,%3f)',[tball(i) ball1(i)]))
    % ... and the same for ball 2
    set(hLine2, 'XData', tball(i), 'YData',ball2(i))
    set(hTxt2, 'Position', [tball(i) ball2(i)], ...
        'String', sprintf('%.3f,%3f)',[tball(i) ball2(i)]))
    drawnow                                  % force refresh
    %#pause(DELAY)                           % slow down animation

    % To stop the balls at t=1
    if tball(i) == 1
        %i = 1; continue;
        break;
    end

    i = rem(i+1,length(tball))+1;            % circular increment
    if ~ishandle(hLine1), break; end         % in case you close the figure
end