creating multiple holes in a flatwire PDE Modeler

6 views (last 30 days)
Hi,
Matlab suggested me to create a more efficient code: what i am trying to do is to create 12 holes vertically in a strip of wire and repeat it 7x times over a 0.2 distance: i started with this code:
model = createpde;
%Define a circle in a rectangle, place these in one matrix, and create a set formula that subtracts the circle from the rectangle.
rectx=0;
recty=0;
rect_width=1.38;
rect_height=0.2;
x_start=0.1;%starting position x circle
y_start=0.2;%starting position y circle
radius=0.005;%radius holes
num_holes_y=12;%number of repitition in the y
num_rep_x=7;%number of repitition in the x
y_interval=0.0183; %distance between the holes in the y;
x_interval=0.2; % distance between the holes in the x;
pderect([rectx rect_width recty,rect_height] )
%create multiple holes in a strip and draw it into the pde modeler
for j=0:(num_rep_x-1)
for i=0:(num_holes_y-1)
%calculate the x and y position for the current hole
x_center=x_start+j*x_interval;
y_center=y_start-i*y_interval;
pdecirc(x_center, y_center,radius)
hold on;
end
end
axis equal;
hold off;
better code as matlab suggested: but get stuck with an error:
Error in TestSkript2 (line 38)
ns(idx+1)=['C',num2str(idx)];
please advise.
model = createpde;
%Define a circle in a rectangle, place these in one matrix, and create a set formula that subtracts the circle from the rectangle.
rectx=0;
recty=0;
rect_width=1.38;
rect_height=0.2;
x_start=0.1;%starting position x circle
y_start=0.2;%starting position y circle
radius=0.005;%radius holes
num_holes_y=12;%number of repitition in the y
num_rep_x=7;%number of repitition in the x
y_interval=0.0183; %distance between the holes in the y;
x_interval=0.2; % distance between the holes in the x;
R1=[3,4,rectx, rectx+rect_width,rectx+rect_width,rectx,recty,recty,recty+rect_height,recty+rect_height]';
%preallocating the arrays(more efficient for matlab CHAT GPT)
num_circles = num_holes_y * num_rep_x;
gd = zeros(10, 1 + num_circles);
gd(:, 1) = R1;
ns = cell(1+num_circles,1);
ns{ones}='R1';
sf='R1';
%create multiple holes in a strip and draw it into the pde modeler
for j=0:(num_rep_x-1)
for i=0:(num_holes_y-1)
%calculate the x and y position for the current hole
x_center=x_start+j*x_interval;
y_center=y_start-i*y_interval;
C=[1,x_center, y_center,radius]';
idx=j*num_holes_y+i+1;
gd(1:4,idx+1)=C;
ns{idx+1}=['C',num2str(idx)];
sf=[sf,'C',num2str(idx)];
end
end
ns=char(ns);
g=decsg(gd,sf,ns);
axis equal;
hold off;

Answers (1)

Arnav
Arnav on 25 Sep 2024
I understand you are getting the following error while executing the second code:
Error using decsg (line 138)
Geometry description and name-space matrix define different number of shapes.
Error in test2 (line 50)
g = decsg(gd, sf, ns);
This suggests that the dimensions of ns and gd are mismatching. decsg is expecting a matrix which has the same number of columns as gd.
This can be fixed by transposing ns as shown:
g = decsg(gd, sf, ns');
You also need to fix the code that calculates sf as shown below:
sf = [sf, '-C', num2str(idx)];
This is because sf should be R1-C1-C2-…-C84 and not R1C1C2C3…C84.
You may refer to the documentation link of decsg to see a few example usages of the function:
Hope it helps!

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!