How to make random voids with for loop?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Mehrdad Nasirshoaibi
el 23 de Feb. de 2023
Comentada: Stephen23
el 23 de Feb. de 2023
Hi eveyone, I have a question regarding MATLAB and COMSOL. I have a membrane in COMSOL and I wanna add voids in the MATLAB code which is linked to COMSOL. The following code gives me random locatoin as 0 and 1, which, 1 means the location of a void.
x = linspace(-2,2,10) ; y = linspace(-2,5,10) ; [X,Y] = meshgrid(x,y) ; c = randi([0 1], size(Y))==1 P = [X(c) Y(c)] figure plot(X(c),Y(c), '.r') hold on plot(X(~c), Y(~c), '.b') hold off axis('padded') legend('1','0', 'Location','best')
I need a code that can give me the number of 1's (the number of voids in the membrane) and their locations.
Then, I know how write the code for one, two voids and so on in MATLAB linked to COMSOL. However, adding them by hands is very time consuming. The code is as follows:
model.component('comp1').geom('geom1').feature('wp1').geom.create('e1', 'Ellipse'); model.component('comp1').geom('geom1').feature('wp1').geom.feature('e1').set('semiaxes', {'R/10' 'R/15'}); model.component('comp1').geom('geom1').feature('wp1').geom.feature('e1').set('pos', {'0' '0'}); model.component('comp1').geom('geom1').feature('wp1').geom.create('e2', 'Ellipse'); model.component('comp1').geom('geom1').feature('wp1').geom.feature('e2').set('semiaxes', {'R/15' 'R/15'}); model.component('comp1').geom('geom1').feature('wp1').geom.feature('e2').set('pos', {'1.5e-4' '0'}); model.component('comp1').geom('geom1').feature('wp1').geom.create('dif1', 'Difference'); model.component('comp1').geom('geom1').feature('wp1').geom.feature('dif1').selection('input').set({'r1'}); model.component('comp1').geom('geom1').feature('wp1').geom.feature('dif1').selection('input2').set({'e1' 'e2'}); model.component('comp1').geom('geom1').run('wp1'); model.component('comp1').geom('geom1').run('fin');
In this code 'comp1' , 'geom1', 'wp1', 'dif1', selection('input') and selection('input2') aren't going to change. In a nutshel, How can I write a code which can generate a new membrane with random voids? More details: For instance if I have 10 voids, then I have e1 to e10 and for each one I should add their location in .set('pos', {'0' '0'}). Also I should add e1 to e 10 in selection('input2').set({'e1' 'e2'}).
Thank you all in advance.
0 comentarios
Respuesta aceptada
Stephen23
el 23 de Feb. de 2023
Editada: Stephen23
el 23 de Feb. de 2023
Why not just use a loop, something like this (aircode):
% ... other model settings
S = "e" + (1:10);
for k = 1:numel(S)
F = S{k};
model.component('comp1').geom('geom1').feature('wp1').geom.feature(F).set('semiaxes', {'R/10' 'R/15'});
model.component('comp1').geom('geom1').feature('wp1').geom.feature(F).set('pos', {'0' '0'});
end % ^
% ... more model settings
model.component('comp1').geom('geom1').feature('wp1').geom.feature('dif1').selection('input2').set(cellstr(S));
% ^^^^^^^^^^
I don't see how this is related to "How to loop through variables names?" at all. Nothing in your description is related to variable names.
2 comentarios
Stephen23
el 23 de Feb. de 2023
"how can I insert the position of each location to your code in geom.feature(F).set('pos', {'0' '0'}) instead of {'0' '0'}."
I guess you would have to convert the required values to character and place them in a 2x1 cell array: from your code it is unclear which (pairs of) values you want to convert.
x = linspace(-2,2,10);
y = linspace(-2,5,10);
[X,Y] = meshgrid(x,y);
c = randi(0:1,size(Y))==1;
P = [X(c),Y(c)];
plot(X(c),Y(c),'.r')
hold on
plot(X(~c), Y(~c),'.b')
axis('padded')
legend('1','0', 'Location','best')
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!