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 ビュー (過去 30 日間)
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

回答 (2 件)

Roger Stafford
Roger Stafford 2014 年 3 月 22 日
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 2014 年 3 月 21 日
編集済み: Joseph Cheng 2014 年 3 月 21 日
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.

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by