2

I am looking to create 2 dots which move along 2 graphs within MATLAB in which the model shows the point moving along the curve with time. I know there has been ask a similar question here, but it doesn't help me that much :-( I already tried to implement this solution: link I do not know how to implement the

b = linspace(x1,10,numPoints);

into my solution.

the 2 graphs

Here is my code of 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

And here is my solution, which does not work properly right now:

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

1 Answer 1

0

There are a few things unclear to me:

  • According to your definition of hLine and hText the vector/array b should contain the y-values for the ball at the specific time points. Instead you define b=linspace(x1,10,numPoints). That gives you specific time points.

  • What is ball? It's used in the while-loop but nowhere defined.

  • First you define x=[0;10] and a few lines later you redefine x=g/2 * t.^2;.

Assuming I understood your needs correctly, here's a way to do it:

%% 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
2
  • THIS IS AWESOME!!! :-D Thanks a lot! ;-) You are right, I had some unlogic in my code. Is it possible to let the balls stop at v = -10m/s (equals t=1)?
    – Mirabella
    Oct 22, 2018 at 19:27
  • @ChristineHümmer Sure, you just need an if-statement in the while-loop, see my edited answer. 🙂 If you want the animation to start from the beginning after t=1, then just remove the break statement and uncomment the line i=1; continue; above.
    – joni
    Oct 22, 2018 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.