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

조회 수: 9 (최근 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 CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by