Circular moving point at a set velocity

6 vues (au cours des 30 derniers jours)
Meeechhhr
Meeechhhr le 25 Mar 2017
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

Réponse acceptée

Michelangelo Ricciulli
Michelangelo Ricciulli le 25 Mar 2017
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

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Aucun tag saisi pour le moment.

Community Treasure Hunt

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

Start Hunting!

Translated by