MATLAB: Could you please tell me how to plot 2 objects with different velocity

velocity

clear all;
close all;
clc;
vA = [2.31; -0.55]; % initial velocity OF 1
dt = 0.1; % time step for animation.
t = 0:dt:10 ;
% get points trajectory
pA = zeros(2,length(t)+1) ;
pA(:,1) = [4 ;10] ; % initial position
for i = 2:length(t)+1 % t is time variable
% plot(pA(1),pA(2),'ro') % Plot circle
pA(:,i) = pA(:,i-1) + vA*dt; % update position
%pB(:,i) = pB(:,i-1) + vB*dt; % update position
end
%plot
figure
title('Motion animation');
%xlabel('x (m)');
%ylabel('y (m)');
h = plot(pA(1,2),pA(1,1),'cs') ;
%h = plot(pB(1,1),pB(1,2),'gp') ;
axis([min(pA(2,:)) max(pA(2,:)) min(pA(1,:)) max(pA(1,:)) ])
for i = 1:length(pA)
set(h,'XData',pA(1,i),'YData',pA(2,i))
%set(h,'XData',pB(1,i),'YData',pB(2,i))
pause(.5)
end
this is the code for a single object moving with constant velocity,anyone explain how to create two objects with same velocity?

Best Answer

close all;
clc;
vA = [2.31; -0.55]; % initial velocity OF 1

vB = [2.31; -0.55]; % initial velocity OF 1
dt = 0.1; % time step for animation.
t = 0:dt:10 ;
% get points trajectory
pA = zeros(2,length(t)+1) ;
pA(:,1) = [4 ;10] ; % initial position

pB = zeros(2,length(t)+1) ;
pB(:,1) = [2 ;8] ; % initial position
for i = 2:length(t)+1 % t is time variable
% plot(pA(1),pA(2),'ro') % Plot circle
pA(:,i) = pA(:,i-1) + vA*dt; % update position

pB(:,i) = pB(:,i-1) + vB*dt; % update position
end
%plot
figure
title('Motion animation');
%xlabel('x (m)');
%ylabel('y (m)');
x = [pA(1,2) ; pB(1,2)] ;
y = [pA(1,1) ; pB(1,1)] ;
h = plot(x,y,'s') ;
%h = plot(pB(1,1),pB(1,2),'gp') ;
axis([min(pA(2,:)) max(pA(2,:)) min(pA(1,:)) max(pA(1,:)) ])
for i = 1:length(pA)
x = [pA(1,i) ; pB(1,i)] ;
y = [pA(1,i) ; pB(1,i)] ;
set(h,'XData',x,'YData',y)
%set(h,'XData',pB(1,i),'YData',pB(2,i))
pause(.5)
end
Related Question