i am new to matlab and i am required to plot a circle in matlab and mark its center and generate a random coordinate inside the circle and the negative of this coordinate and measure the distance between these two points any idea ? thanks in advance

6 visualizzazioni (ultimi 30 giorni)
i am new to matlab and i am required to plot a circle in matlab and mark its center and generate a random coordinate inside the circle and the negative of this coordinate and measure the distance between these two points any idea ? thanks in advance

Risposte (2)

Roger Stafford
Roger Stafford il 22 Mar 2014
To place a single point randomly within a circle with a statistically uniform distribution area-wise, it is most convenient to use polar coordinates, but a square root operation is needed on the random radius to achieve true uniformity. Let the circle have its center at (xc,yc) and radius R.
r = R*sqrt(rand);
t = 2*pi*rand;
x = xc + r*cos(t);
y = yc + r*sin(t);
plot(x,y,'yo')
In case you feel adventurous and want to place a large number of such random points within the circle just to test their uniformity, do this:
% The random points
n = 8192;
r = R*sqrt(rand(n,1));
t = 2*pi*rand(n,1);
x = xc + r.*cos(t);
y = yc + r.*sin(t);
% The circle
t2 = linspace(0,2*pi);
X = xc + R*cos(t2);
Y = yc + R*sin(t2);
plot(xc,yc,'w*',X,Y,'r-',x,y,'y.')
axis equal
The circle is in red, its center is at the white asterisk, and the random points are the yellow dots.

Joseph Cheng
Joseph Cheng il 21 Mar 2014
Modificato: Joseph Cheng il 21 Mar 2014
to draw a circle you can use
R = 1;
rectangle('position',[0-R,0-R,R*2,R*2],...
'curvature',[1,1],'linestyle','-','edgecolor','k');
Where you will get a circle with radius R centered around zero.
to get a random point within this circle perhaps use the rand() function to get x and y points and use some scheme to get +/- values( ex: 2*rand(1,2)-1). Mirror across the origin or single axis and use the pythagorean theorem. If the circle is larger than radius 1 just scale everything by the desired radius.

Categorie

Scopri di più su Random Number Generation in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by