How to plot 10 figures on same plot, and How do i find radius inbetween 0 and 1.

So basically, Is there a more cleaner way to do ten random circles on a plot with the x cord and y cordinates with there cordinates between randi([-,8]). Like i have a code doing a function:
function [h] = Circle(x,y,r)
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
and i have 10 lines of that function being used to plot 10 different circles. Any way it can be condensed into less that ten lines? Also How can i get my radius to be inbetween [0,1] and not 0 or 1. (It can be one i just want it to be able to pick any number inbetween them.)

 Accepted Answer

Try calling it like this where you pass in the x and y values from your arrays for the centers, and a random number between 0 and 1 for the radius:
for k = 1 : 10
hold on;
h(k) = Circle(x(k), y(k),rand(1))
end
hold off;
and take the holds out of Circle if you want 2 fewer lines.

5 Comments

So I tried what you said, it only resulted in One circle in the plot, needs to be ten different circles in the same plot.
x = randi([-4,8]);
y = randi([-4,8]);
for k = 1 : 10
hold on;
h(k) = CircPlot(x(k), y(k),rand(1))
end
hold off;
oh sorry the function is actually called CircPlot
I don't think you tried what I said because when I did it worked fine. Since your x and y are just a single number, not a vector of 10 values, I put them into the loop. I also renamed the circle plotting routine to your new name. The code below works just fine. What did you do differently?
hFig = figure;
hFig.WindowState = 'maximized';
for k = 1 : 10
x = randi([-4,8]);
y = randi([-4,8]);
hold on;
h(k) = CircPlot(x, y, rand(1));
end
hold off;
grid on;
axis 'equal'
% Put lines along the axes
yline(0);
xline(0);
function [h] = CircPlot(x,y,r)
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
h = plot(xunit, yunit, 'LineWidth', 2);
end
I put my x and Y randi commands before the for loop. but Thank you this really helped!
You can do that if you got all 10 values, and it would be more efficient. Here is how a skilled MATLAB programmer would do it:
hFig = figure;
hFig.WindowState = 'maximized';
numberOfCircles = 10;
% Get all 10 center coordinates in advance. Range is -4 to +8.
x = randi([-4, 8], 1, numberOfCircles);
y = randi([-4, 8], 1, numberOfCircles);
radius = rand(1, numberOfCircles); % Range is 0 to 1.
% Now loop through plotting each circle one at a time.
for k = 1 : 10
hold on;
CircPlot(x(k), y(k), radius(k));
end
hold off;
grid on;
axis 'equal'
caption = sprintf('%d Circles', numberOfCircles);
title(caption, 'FontSize', 15);
xlabel('X', 'FontSize', 15);
ylabel('Y', 'FontSize', 15);
% Put lines along the axes
yline(0);
xline(0);
function CircPlot(xCenter, yCenter, radius)
anglesInRadians = 0 : (pi / 50) : (2 * pi);
xCoordinates = radius * cos(anglesInRadians) + xCenter;
yCoordinates = radius * sin(anglesInRadians) + yCenter;
plot(xCoordinates, yCoordinates, '-', 'LineWidth', 2);
end

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!