Motion simulation of a point and a circular object
19 views (last 30 days)
Show older comments
Matlab beginner checking in. I've written some code for a point moving in a straight line with a constant velocity (for a given initial position and velocity). Could anyone tell me how to get rid of the trace created as the point moves?
pC = [4; 10]; % initial position of A
vC = [2.3100; -0.5500]; % initial velocity of A
dt = 0.1; % time step for animation
clf; % Clear the figure
axis(50*[0 1 0 1],'square'); % Set axis limits
grid on;
hold on; % Keep plot from erasing
for t=0:dt:15 % t is time variable
plot(pC(1),pC(2),'ro') % Plot circle
pC = pC + vC*dt; % update position
pause(dt) % pause for animation
end
hold off;
title('Motion animation');
xlabel('x (m)');
ylabel('y (m)');
In the same plot, I need to simulate a circular object undergoing uniform circular motion with constant speed (given position of center of circular path, radius of circular path, initial position of center of circular object, radius and tangential velocity of circular object). Any advice?
pA = [22; 8]; % position of center of circular path
rA = 4; % radius of circular path
pB = [25.8637; 9.0353]; % initial position of B
vB = [-0.5176; 1.9319]; % tangential velocity of B
rB = 1; % radius of B
I've attached a picture for clarity.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/165654/image.jpeg)
0 Comments
Answers (3)
KSSV
on 30 Jun 2017
Edited: KSSV
on 30 Jun 2017
vA = [2.31; -0.55]; % initial velocity
dt = 0.1; % time step for animation.
% get points trajectory
t = 0:dt:10 ;
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
end
% plot
figure
title('Motion animation');
xlabel('x (m)');
ylabel('y (m)');
h = plot(pA(1,1),pA(1,2),'Or') ;
axis([min(pA(1,:)) max(pA(1,:)) min(pA(2,:)) max(pA(2,:)) ])
for i = 1:length(pA)
set(h,'XData',pA(1,i),'YData',pA(2,i))
pause(0.05)
end
Veronica Maria Rodriguez Betancourtt
on 29 May 2021
clear
dt = 0.1; % time step
t = 0:dt:12 ; % time
pR=zeros(2,length(t));pB=zeros(2,length(t));
pR(:,1) = [4 ;10] ; % initial position of R ("Red dot")
vR = [2.31; -0.55]; % initial velocity of R
pB(:,1) = [25.8637; 9.0353] ; % initial position of B ("Blue dot")
vB = [-0.5176; 1.9319]; % tangential velocity of B
pC = [22; 8]; % position of circular path's center
rC = 4; % circular path's radius
th=zeros;th(1)=atan(pB(2)/pB(1));
for i = 2:length(t)
pR(:,i) = pR(:,i-1) + vR*dt; % R's position update ([x y])
th(i)=th(i-1)+norm(vB)*dt/rC; % th: angle,
% norm(vB)/rC = angular velocity
pB(:,i) = pC + rC*[cos(th(i));sin(th(i))]; % B's position update
end
% Plotting:
h1 = plot(pR(1,1),pR(2,1),'Or') ;grid on, hold on
h2 = plot(pB(1,1),pB(2,1),'Ob');
title('Motion animation');xlabel('x (m)');ylabel('y (m)');
% axis([min(pA(1,:)) max(pA(1,:)) min(pA(2,:)) max(pA(2,:)) ])
axis([4 26 4 12])
plotcircle(rC,pC(1),pC(2)) % B's path
for i = 1:length(pR)
set(h1,'XData',pR(1,i),'YData',pR(2,i))
set(h2,'XData',pB(1,i),'YData',pB(2,i))
pause(0.05)
end
function plotcircle(r,x,y)
% https://www.mathworks.com/matlabcentral/answers/...
% 98665-how-do-i-plot-a-circle-with-a-given-radius-and-center
plot(r*exp(1i*(0:pi/100:2*pi))+x+1i*y,'--');
end
0 Comments
Sandy Narsale
on 2 Oct 2018
MATLAB Answers™ Theo James 0 Flag Motion simulation of a point and a circular object Asked by Theo James on 30 Jun 2017 Latest activity Edited by KSSV on 30 Jun 2017 113 views (last 30 days) Matlab beginner checking in. I've written some code for a point moving in a straight line with a constant velocity (for a given initial position and velocity). Could anyone tell me how to get rid of the trace created as the point moves?
pC = [4; 10]; % initial position of A
vC = [2.3100; -0.5500]; % initial velocity of A
dt = 0.1; % time step for animation
clf; % Clear the figure
axis(50*[0 1 0 1],'square'); % Set axis limits
grid on;
hold on; % Keep plot from erasing
for t=0:dt:15 % t is time variable
plot(pC(1),pC(2),'ro') % Plot circle
pC = pC + vC*dt; % update position
pause(dt) % pause for animation
end
hold off;
title('Motion animation');
xlabel('x (m)');
ylabel('y (m)');
In the same plot, I need to simulate a circular object undergoing uniform circular motion with constant speed (given position of center of circular path, radius of circular path, initial position of center of circular object, radius and tangential velocity of circular object). Any advice?
pA = [22; 8]; % position of center of circular path rA = 4; % radius of circular path pB = [25.8637; 9.0353]; % initial position of B vB = [-0.5176; 1.9319]; % tangential velocity of B rB = 1; % radius of B I've attached a picture for clarity.
0 Comments
Comment on this Question
Tags
simulationanimationmotionconstant velocityuniform circular m...
Related Content
1 Answer
KSSV
1
Flag Link
Answer by KSSV on 30 Jun 2017
Edited by KSSV on 30 Jun 2017
vA = [2.31; -0.55]; % initial velocity dt = 0.1; % time step for animation. % get points trajectory t = 0:dt:10 ; 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 end % plot figure title('Motion animation'); xlabel('x (m)'); ylabel('y (m)'); h = plot(pA(1,1),pA(1,2),'Or') ; axis([min(pA(1,:)) max(pA(1,:)) min(pA(2,:)) max(pA(2,:)) ]) for i = 1:length(pA) set(h,'XData',pA(1,i),'YData',pA(2,i)) pause(0.05) end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!