Circular moving point at a set velocity
4 次查看(过去 30 天)
显示 更早的评论
The function I am looking to create is one that takes multiple circular plots, of different radius, and have a point follow the graph at its own distinct velocity, for as long as possible. The code I have so far is:
radius=[10 15 20 30]
velocity=[5 2.5 15 20]
colors=['y','m','c','r'] % This is to just make the points different colors
hold on
for i=1:4 %Loop to create multiple circles
th=0:pi/50:2*pi;
xunit=radius(i)*cos(th);
yunit=radius(i)*sin(th);
h=plot(xunit,yunit);
p=plot(xunit(1),yunit(1),'o','MarkerFaceColor',colors(i)); %creates a point on each graph
end
hold off
0 个评论
采纳的回答
Michelangelo Ricciulli
2017-3-25
Assuming that your velocities are already angular velocities (number of radians per time unit), adding a for loop and a vector to store the points handler should work. I also modified the plot function so to have always the same color of the border and filling of the points. Don't know what you're trying to do, but is pretty hypnotic :)
radius=[10 15 20 30]
velocity=[5 2.5 15 20]
colors=['y','m','c','r'] % This is to just make the points different colors
p=zeros(4,1); %here we will store the handles to delete the point
hold on
for i=1:4 %Loop to create multiple circles
th=0:pi/50:2*pi;
xunit=radius(i)*cos(th);
yunit=radius(i)*sin(th);
h=plot(xunit,yunit);
p(i)=plot(xunit(1),yunit(1),'o','MarkerFaceColor',colors(i),'Color',colors(i));
%creates a point on each graph
end
time=[0:0.001:100]; %time vector in seconds
for t=1:length(time)
for i=1:4 %Loop to create multiple circles
delete(p(i)); %delete the old point
%computes the new angle for each point as velocity*time
xunit=radius(i)*cos(velocity(i)*time(t));
yunit=radius(i)*sin(velocity(i)*time(t));
%creates a point on each graph
p(i)=plot(xunit(1),yunit(1),'o','MarkerFaceColor',colors(i),'Color',colors(i));
end
pause(0.01); %wait 0.01 seconds so the plot is displayed
end
hold off
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!