Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make circle in matlab and generate random points inside it

enter image description herehello i want to ask a question how to make a circle in matlab and mark its center and generate a certain number of random points inside it for example 50 ? i know this code to make a circle

x = linspace(-sqrt(10),sqrt(10));
y1 = sqrt(10-x.^2);
y2 = -sqrt(10-x.^2);
plot(x,y1,x,y2)
axis equal
hold on

but i don't know how to generate 50 random points inside it then i thought of this pseudo code but i don't know how to write it in matlab

01: FOR all nodes j
 02: FOR all nodes i except node j 
03: IF distance(j to center) < distance(j to  i) AND
 04: distance(i to cell center) < distance(j to  i) 
05: THEN there's a red link from node i to node j 
06: ELSEIF distance(j to cell center) < distance(j to  i)
 07: THEN there's a blue link from node i to node j 
08: ELSE there's no D2D link from node i and j;
 09: node i has a green link with the base station 
10: END if 
11: END inner for-loop
like image 219
user3482135 Avatar asked Mar 31 '14 17:03

user3482135


2 Answers

Think this is what you need -

%%// Plot the circle
x = linspace(-sqrt(10),sqrt(10));
y1 = sqrt(10-x.^2);
y2 = -sqrt(10-x.^2);
plot(x,y1,x,y2)
axis equal

%%// Choose from 1000 random point pairs
N = 1000; 
%%// Radius of circle
radius = sqrt(10); 

%%// Create a random point matrix Nx2
points_mat = [ radius*2*(rand(N,1)-0.5) radius*2*(rand(N,1)-0.5)];

%%// Select the first 50 pairs that lies inside circle
ind1 = find(sqrt( points_mat(:,1).^2 + points_mat(:,2).^2 )<radius);
points_mat=points_mat(ind1(1:50),:);

%%// Plot the 50 points on the circle
hold on
text(0,0,'x Center') %%// Center
text(points_mat(:,1),points_mat(:,2),'o') %%// 50 points

Plot

enter image description here

like image 145
Divakar Avatar answered Nov 15 '22 06:11

Divakar


I don't know matlab, so I can't help you there, but if you want to do this without rejection you can generate the points in polar coordinates. If rand() returns a Uniform(0,1) random number, then:

r = radius * sqrt(rand())
theta = 2 * Pi * rand()
x = r * cos(theta)
y = r * sin(theta)

will yield values which are uniformly distributed within a circle of radius radius. Note the square root on the calculation of r, which adjusts the distribution of distance from the center of the circle so that the number of points at a given distance is always proportional to the area and hence is uniform. For spherical uniformity you'd take the cube root to keep proportionality to the volume, and in general the kth root for a k-dimensional hypersphere.

like image 29
pjs Avatar answered Nov 15 '22 05:11

pjs