Circular moving point at a set velocity

조회 수: 5 (최근 30일)
Meeechhhr
Meeechhhr 2017년 3월 25일
답변: Michelangelo Ricciulli 2017년 3월 25일
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

채택된 답변

Michelangelo Ricciulli
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개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by