hello i want to ask a question how to make a circle in matlab and mark its center and g
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
Here's another option:
%// Set parameters
R = 0.5; %// radius
C = [3 4]; %// center [x y]
N = 50; %// number of points inside circle
%// generate circle boundary
t = linspace(0, 2*pi, 100);
x = R*cos(t) + C(1);
y = R*sin(t) + C(2);
%// generate random points inside it
th = 2*pi*rand(N,1);
r = R*rand(N,1);
xR = r.*cos(th) + C(1);
yR = r.*sin(th) + C(2);
%// Plot everything
figure(1), clf, hold on
plot(x,y,'b')
plot(C(1),C(2),'r.', 'MarkerSize', 100)
plot(xR,yR,'k.')
axis equal
Here's why that could be useful:
%// Set parameters
R = 0.5; N = 50;
C = [3 4]; M = 100; %// points on boundary
%// generate all points at once
t = linspace(0, 2*pi, M)';
th = 2*pi*rand(N,1);
r = R*rand(N,1);
xR = [R*ones(M,1); r] .* cos([t; th]) + C(1);
yR = [R*ones(M,1); r] .* sin([t; th]) + C(2);
%// Plot everything
figure(1), clf, hold on
plot(xR(1:M),yR(1:M),'b') %// circle boundary
plot(C(1),C(2),'r.', 'MarkerSize', 100) %// center
plot(xR(M+1:end),yR(M+1:end),'k.') %// random points
axis equal
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.