Store midpoint of all the squares inside the square of width 1cm.

1 view (last 30 days)
There is a square of width 1cm.Discretize it in n squares and find the midpoint of all the square.Plot it
Donot use any pde tools or inbuilt function.
  3 Comments
Sidharth Sanjeev Wazir
Sidharth Sanjeev Wazir on 25 Nov 2022
NN=150;
x1=0; y1=0;
x2=1; y2=0;
x3=1; y3=1;
x4=0; y4=1;
% For Bottom Line
L1 = [0,0,0;5,0,0]
R1 = pdist(L1,'euclidean')
for N = 1:NN
delL1 = R1/N; %small increment on loop1
for m = 1:N
R12 = delL1*m
end
end
%For Top Line
L2 = [5,5,0;0,5,0];
R2 = pdist(L2,'euclidean')
for N = 1:NN
delL2 = R2/N; %small increment on loop1
for m = 1:N
R34 = delL2*m
end
end
%Mapping top and bottom line
%for i=2:NN-1;
% for j=2:NN-1;
%L3=[R34(i+1);R12(j+1)]
% for N=1:NN
% R1234 = pdist(L3,'euclidean')
% delL3=R1234/N
% for m=1:N
% R1234=delL3*m
% end
% end
% end
%end
Sidharth Sanjeev Wazir
Sidharth Sanjeev Wazir on 25 Nov 2022
Edited: Sidharth Sanjeev Wazir on 25 Nov 2022
I divided the lne how to find all the midpoints between the two lines

Sign in to comment.

Answers (1)

Aditya
Aditya on 29 Nov 2022
Hi,
I understand that you are trying to divide a square of side 1 into n squares and find mid points of all these smaller squares.
A basic approach is to divide the larger square into rows and columns and proceed as follows:
  1. The number of squares in each column/row is sqrt(n) (=4 in above figure).
  2. The side of a smaller square is of length 1/sqrt(n) (=0.25 in above figure).
  3. Find the mid-point of first square (0.25/2, 0.25/2) and keep incrementing by side of square (0.25) to get mid points along an axis.
  4. Once, we have mid points along a side, we can use meshgrid to generate all points.
Here is a simple function that does that:
function discretize_square(n)
% n has to be a perfect square.
assert(mod(sqrt(n), 1) == 0, "n not a perfect square");
side_divs = sqrt(n);
delta = 1/side_divs;
midpts = delta/2: delta:1;
[x,y]= meshgrid(midpts, midpts);
scatter(x,y);
xlim([0, 1]);
ylim([0,1]);
end
A few points for above code:
  1. This will work only is n is a perfect square.
  2. Notice the colon operator for creating array of mid points along a side.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!