Adding new circle to plot at constant frequency

5 Ansichten (letzte 30 Tage)
Ali Kareem
Ali Kareem am 17 Mai 2016
Bearbeitet: Robert am 18 Mai 2016
Hello,
I am trying to plot figures that have a circle inside it. I want to add a new circle to the figure at a constant frequency. let us say each 20 steps. For example, if I have 60 steps then I should have 60 figures. Figures from 1 to 20 will have one circle, figures from 21 to 40 will have 2 circles, and figures from 41 to 60 will have three circles. I did that in below code and it is working properly. However, I used if condition in my code. The problem if I will have 1000 steps this mean I need to use (1000/20= 50) if condition and that does not make sense. Is there any sufficient way to do that. This is an example.In my real case, I will have from step 1-20 circle moving at each step from step 21-40 I will have two circles moving and so on. I gave this example to make the situation easy. I have the location (x,y) for all steps stored in one matrix.
function h = circle(x,y,r)
clc;
for i=1:60
figure
if i>=1 && i<20
x=10;
r=0.4;
y=18;
axis ([0 20 0 20])
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
elseif i>=20 && i<40
x=10;
r=0.4;
y=14;
axis ([0 20 0 20])
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y+2;
h = plot(xunit, yunit);
hold off
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
else i>=40 && i<60;
x=10;
r=0.4;
y=10;
axis ([0 20 0 20])
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y+4;
h = plot(xunit, yunit);
hold off
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y+2;
h = plot(xunit, yunit);
hold off
hold on
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit);
hold off
end
end
end
Figures that I got is like
and
and
%

Akzeptierte Antwort

Robert
Robert am 17 Mai 2016
Bearbeitet: Robert am 18 Mai 2016
You should put your circle plotting code in a for loop and iterate from 0 to floor(i/20) (or the reverse).
function h = circle % (x,y,r) % shouldn't have these inputs if we don't use them
x = 10;
r = 0.4;
y = 18;
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
for ii=1:60 % using ii avoids confusion with complex numbers
figure
axis([0 20 0 20])
for jj = floor(ii/20):-1:0 % backwards to keep colors same as in your example
h = plot(xunit, yunit+2*(jj-1));
hold on
end
hold off
end
end
  2 Kommentare
Ali Kareem
Ali Kareem am 17 Mai 2016
Hi,
Thank you for your reply.
I run code but unfortunately, it does not gave me same results as above and it does not plot figures from 1 to 20
Robert
Robert am 18 Mai 2016
Whoops! floor(ii/20) returns 0 for ii in [0,19]. I should have added 1 or iterated down to zero. I've edited my answer.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by