Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Matlab function

Please login with a confirmed email address before reporting spam

I have a couple of question;

1-Is it necessary that the input arguments of MATLAB function should be equal in size? and also output argument when using Livelink Matlab

In this case, the sizes of the arguments are ;

c = 100

cavg=1

MI =1

.................................................................

function MI = MixingIndex(c,cavg)

sumc=0;

for n=1: length(c)

a=(((c(n)-cavg/cavg)^2));

sumc=sumc+a;

end

MI=(1-sqrt((1/length(c))sumc))100;

...................................................................

2- How can I use this external function as a stop condition?

for example;

MixingIndex(c,aeop1(c))<0.5

aeop1(c) is the surface average on a boundry .

what is your suggestion to seek these goals?**


2 Replies Last Post Jun 20, 2019, 6:27 a.m. EDT
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 5 years ago Jun 17, 2019, 4:58 a.m. EDT
Updated: 5 years ago Jun 17, 2019, 4:57 a.m. EDT

Hi Ehsan

Yes, if you are using a MATLAB node in your Comsol model your function must be able to take arguments that are vectors of equal size and your function must return a vector of the same size as the inputs. You can test this using the Matlab command by passing vectors that you hace created yourself. The reason is that each call to Matlab are rather slow and hence we need to batch the calls to speed things up.

You can insert a stop condition in your solver setup. The conditions used for stopping can depend on a Matlab function (and any other valid Comsol expression/function).

-------------------
Lars Gregersen
Comsol Denmark
Hi Ehsan Yes, if you are using a MATLAB node in your Comsol model your function must be able to take arguments that are vectors of equal size and your function must return a vector of the same size as the inputs. You can test this using the Matlab command by passing vectors that you hace created yourself. The reason is that each call to Matlab are rather slow and hence we need to batch the calls to speed things up. You can insert a stop condition in your solver setup. The conditions used for stopping can depend on a Matlab function (and any other valid Comsol expression/function).

Please login with a confirmed email address before reporting spam

Posted: 5 years ago Jun 20, 2019, 6:27 a.m. EDT
Updated: 5 years ago Jun 20, 2019, 6:29 a.m. EDT

Hi Ehsan

Yes, if you are using a MATLAB node in your Comsol model your function must be able to take arguments that are vectors of equal size and your function must return a vector of the same size as the inputs. You can test this using the Matlab command by passing vectors that you hace created yourself. The reason is that each call to Matlab are rather slow and hence we need to batch the calls to speed things up.

You can insert a stop condition in your solver setup. The conditions used for stopping can depend on a Matlab function (and any other valid Comsol expression/function).

Thank you for your attention! I face with a problem. In fact, I wanna use this function as a stop condition.

this is my Matlab function: function MI = MixingIndex(c) sumc=0; cavg=0; for i=1:length(c) cavg= cavg+c(i); end cavg=(cavg/length(c)); for n=1: length(c) a=(((c(n)-cavg/cavg)^2)); sumc=sumc+a; end MIpre=(1-sqrt((1/length(c))sumc))100; X = zeros(1,length(c)) for i=1:length(c) X(i)=MIpre end MI=X;

=============================================

model.func.create('extm1', 'MATLAB'); model.func('extm1').setIndex('funcs', 'MixingIndex', 0, 0); model.func('extm1').setIndex('funcs', 'ehs', 0, 1);

model.component('comp1').variable.create('var1'); model.component('comp1').variable('var1').selection.geom('geom1', 2); model.component('comp1').variable('var1').selection.named('geom1_cad1_Face06Part8_bnd'); model.component('comp1').variable('var1').set('c_outlet', 'c');

model.component('comp1').probe.create('bnd1', 'Boundary'); model.component('comp1').probe('bnd1').set('intsurface', true); model.component('comp1').probe('bnd1').selection.named('geom1_cad1_Face06Part8_bnd'); model.component('comp1').probe('bnd1').set('expr', 'MixingIndex(comp1.c_outlet[m^3/mol])'); model.component('comp1').probe.create('var1', 'GlobalVariable'); model.component('comp1').probe('var1').set('expr', ''); model.component('comp1').probe.remove('var1'); model.component('comp1').probe.create('var1', 'GlobalVariable'); model.component('comp1').probe('var1').set('expr', 'if(comp1.bnd1>1,(at(t,comp1.bnd1)-at(t-timestep,comp1.bnd1))/timestep,10[1/s])');

model.sol('sol1').feature('t1').create('st1', 'StopCondition'); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', '', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondterminateon', 'true', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondActive', true, 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopconddesc', 'Stop expression 1', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', '', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondterminateon', 'true', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondActive', true, 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopconddesc', 'Stop expression 1', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', 'comp1.var1<=0.2', 0);

%%%%%%% but the error is "could not evaluate stop condition %%%%%%%% although all of the variables and probes evaluate during solution, It seems the stop condition could not access to the variable in previous time step! what is your idea and suggestion?

>Hi Ehsan > >Yes, if you are using a MATLAB node in your Comsol model your function must be able to take arguments that are vectors of equal size and your function must return a vector of the same size as the inputs. You can test this using the Matlab command by passing vectors that you hace created yourself. >The reason is that each call to Matlab are rather slow and hence we need to batch the calls to speed things up. > >You can insert a stop condition in your solver setup. The conditions used for stopping can depend on a Matlab function (and any other valid Comsol expression/function). Thank you for your attention! I face with a problem. In fact, I wanna use this function as a stop condition. this is my Matlab function: function MI = MixingIndex(c) sumc=0; cavg=0; for i=1:length(c) cavg= cavg+c(i); end cavg=(cavg/length(c)); for n=1: length(c) a=(((c(n)-cavg/cavg)^2)); sumc=sumc+a; end MIpre=(1-sqrt((1/length(c))*sumc))*100; X = zeros(1,length(c)) for i=1:length(c) X(i)=MIpre end MI=X; ============================================= model.func.create('extm1', 'MATLAB'); model.func('extm1').setIndex('funcs', 'MixingIndex', 0, 0); model.func('extm1').setIndex('funcs', 'ehs', 0, 1); model.component('comp1').variable.create('var1'); model.component('comp1').variable('var1').selection.geom('geom1', 2); model.component('comp1').variable('var1').selection.named('geom1_cad1_Face06Part8_bnd'); model.component('comp1').variable('var1').set('c_outlet', 'c'); model.component('comp1').probe.create('bnd1', 'Boundary'); model.component('comp1').probe('bnd1').set('intsurface', true); model.component('comp1').probe('bnd1').selection.named('geom1_cad1_Face06Part8_bnd'); model.component('comp1').probe('bnd1').set('expr', 'MixingIndex(comp1.c_outlet[m^3/mol])'); model.component('comp1').probe.create('var1', 'GlobalVariable'); model.component('comp1').probe('var1').set('expr', ''); model.component('comp1').probe.remove('var1'); model.component('comp1').probe.create('var1', 'GlobalVariable'); model.component('comp1').probe('var1').set('expr', 'if(comp1.bnd1>1,(at(t,comp1.bnd1)-at(t-timestep,comp1.bnd1))/timestep,10[1/s])'); model.sol('sol1').feature('t1').create('st1', 'StopCondition'); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', '', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondterminateon', 'true', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondActive', true, 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopconddesc', 'Stop expression 1', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', '', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondterminateon', 'true', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondActive', true, 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopconddesc', 'Stop expression 1', 0); model.sol('sol1').feature('t1').feature('st1').setIndex('stopcondarr', 'comp1.var1

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.