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.

 채택된 답변

KSSV
KSSV 2017년 5월 4일

0 개 추천

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

Mamuna Anwar
Mamuna Anwar 2017년 5월 4일
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개)

질문:

2017년 5월 4일

댓글:

2017년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by