Creation of circles of x radius in a matrix

5 views (last 30 days)
JdC
JdC on 13 Jan 2022
Edited: JdC on 13 Jan 2022
Hello, I am still new to MatLab, and surely making a lot of mistakes and increasing the length of my code for no reason, sorry about it.
I wanted to make a program to have a grid of N_grid size (500 right now) with circles inside. the circles are associated to a High index value and can have possible diameter size. those are calculated from the first part of the program.
Now, I created a high and low index values alternating matrix (1 0 1 0 1 0 (n) matrix with 1 corresponding to high index and 0 to low), and randomly associated a possible diameter to high index value position.
From this point, I would have to create the circles with their respective radius by giving them position and showing them on a graph. I tried to associate a horizontal count and a vertical one, then associate them to circles but I am stucked on how to write this part.
%Circle size at first
r_rod = 7.5*2/3;
%max size of a circle
max_size = 15;
%increment of the circle size
size_increment = 0.1;
%Wavelength
lamda = 1.550;
%random variable
x = (r_rod:size_increment:max_size);
% Diameter of circles matrix
D= zeros(size(x));
H= zeros(size(x));
% High index
n_high = 2.05829192;
%Low index
n_low = 1.963340563;
%High index ratio in the matrix
R_hi=0.5;
% grid size
N_grid = 500;
%Calculate the possible circle diameters
for ii = 1:length(x)
D(ii)=r_rod+0.1*ii;
H(ii)= D(ii)/lamda;
if H(ii)<0.5
D(ii) = r_rod;
else
D(ii)=r_rod+0.1*ii;
end
if H(ii)>5
D(ii) = r_rod;
else
D(ii)=r_rod+0.1*ii;
end
end
r_average = mean(D);
N_rod=fix(N_grid/r_average); %Possible number of circles in the grid
% Number of High index points
high=0;
%Number of low index points
low=0;
% Create a 1 0 1 0 1 matrix and associate a random circle radius to the 1 part of the matrix
M = toeplitz(mod(1:length(x),2));
for iii=1:length(x)
if M(iii) == 1
s(iii) = n_high;
high = high+iii;
R1(iii) = D(randi(numel(D)))*M(iii);
else
R1(iii) = 0*M(iii);
s(iii)=n_low;
low = low+iii;
end
end
hexcount = sum(R1~=0);
whitecount = sum(R1==0);
%Now is the problem, Create coordinates for
%the circles with their associated radius
%And show the now created grid with circles
m = N_rod+1; % horizontal count
n = fix(N_rod*2/sqrt(3))+2; % vertical count
% parametric definition of a circle
radius=R1/2;
t = (1/12:1/6:1)' * 2 * pi;
x = cos(t);
y = sin(t);
circle_area = pi*radius.^2;
Thank you in advance, JdC

Accepted Answer

KSSV
KSSV on 13 Jan 2022
% Make grid
[X,Y] = meshgrid(1:10) ;
% Make random radii for circle
R = randi(5,size(X))/10 ;
% Make circle coordinates
th = linspace(0,2*pi) ;
xc = cos(th) ; yc = sin(th) ;
figure
plot(X,Y,'.r') ;
hold on
for i = 1:size(X,1)
for j = 1:size(X,2)
plot(X(i,j)+R(i,j)*xc,Y(i,j)+R(i,j)*yc)
end
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!