particle distributed on a ring

I want to generate a ring distribution of particles. This is my code so far
function [x,y]=cirrdnPJ(x1,y1,rc)
%the function, must be on a folder in matlab path
a=2*pi*rand;
r=sqrt(rand);
x=(rc*r)*cos(a)+x1;
y=(rc*r)*sin(a)+y1;
end
%to test the function
clf
axis equal
hold on
x1=1;
y1=1;
rc=1;
[x,y,~] = cylinder(rc,200);
plot(x(1,:)+x1,y(1,:)+y1,'r');
x1=1;
y1=1;
rc2=2;
[x,y,~] = cylinder(rc2,200);
plot(x(1,:)+x1,y(1,:)+y1,'p');
for t=1:1000 %loop until doing 1000 points inside the circle
[x ,y]=cirrdnPJ(x1,y1,rc2-rc);
plot(x,y,'x')
pause(0.01) %if you want to see the point being drawn
end
however the marks are appearing inside inner circle and not between two circles as I want. Any help or suggestion would be appreciated.

 采纳的回答

clc ; clear all ;
r0 = 0.5 ; % inner radius
r1 = 1. ; % outer radius
% circles
th = linspace(0,2*pi) ;
x0 = r0*sin(th) ; y0 = r0*cos(th) ;
x1 = r1*sin(th) ; y1 = r1*cos(th) ;
plot(x0,y0,'r') ;
hold on
plot(x1,y1,'r') ;
%%generate random numbers
x = [x1 NaN fliplr(x0)] ;
y = [y1 NaN fliplr(y0)] ;
a = -1;
b = 1;
count = 0 ;
n = 0 ; % number of pints lying inside
while count==0
r = (b-a).*rand(1,2) + a;
idx = inpolygon(r(1),r(2),x,y) ;
if idx
plot(r(1),r(2),'.g')
n = n+1 ;
else
plot(r(1),r(2),'.b')
end
drawnow
end

1 个评论

Thanks a lot for a quick response. This seems to work, however my goal is not only to get a plot but a distribution function that I can use as an input for another code. Can you please give any suggestion or help for this?

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by