How to make random voids with for loop?
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Stephen23
2023 年 2 月 23 日
編集済み: Stephen23
2023 年 2 月 23 日
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 件のコメント
Stephen23
2023 年 2 月 23 日
"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')
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!