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.

Can COMSOL call an external function ?function in Matlab?

Please login with a confirmed email address before reporting spam

In the subdomain setting, can I specify certain property with an expression of a Matlab function.

for example, if I specify this property to be '1*(x<0.5)+2*(x>=0.5)', it works.
while, if I set it to be 'fx', and fx is a function defined in Matlab as
function y=fx(x)
if x<0.5
y=1
else
y=2
end
end
it doesn't work.

Does it mean COMSOL cannot call an external Matlab function in modeling setting? or did I do it in a wrong way?
For my real case, the expression I would use cannot be written in an analytical way, so the user-defined function in COMSOL seems not work for me... Does anybody have the answer or any alternative methods for my problem?
Thanks!

8 Replies Last Post 2010年11月11日 GMT-5 08:13
Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2010年1月13日 GMT-5 14:54
Hi

indeed, from my understanding too, COMSOL cannot call a matlab function, but it can call a COMSOL function, but you have less freedom to elaborate what you will therein.

This is improved in the to come V4, will have to wait and see, and learn again ...

Ivar
Hi indeed, from my understanding too, COMSOL cannot call a matlab function, but it can call a COMSOL function, but you have less freedom to elaborate what you will therein. This is improved in the to come V4, will have to wait and see, and learn again ... Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2010年1月13日 GMT-5 15:15
well from having read the documentation section "using matlab functions" it could [ I dont have matlab currently so no practical experience tough..] but from my prior experience with matlab you have to be careful that the special function you define is in a path matlab knows
[ can you call the function from within the matlab window..? and get an answer then...
well from having read the documentation section "using matlab functions" it could [ I dont have matlab currently so no practical experience tough..] but from my prior experience with matlab you have to be careful that the special function you define is in a path matlab knows [ can you call the function from within the matlab window..? and get an answer then...

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2010年1月13日 GMT-5 16:14
Hi
Matlab and Comsol are close, so you run one from the other, both ways, but mainly comsol from matlab for any secial function or application. but from V4 (to come) I beleive you can do far more without bothe on your PC, just with COMSOL i mean. Have to wait to see.

Good luck
Ivar
Hi Matlab and Comsol are close, so you run one from the other, both ways, but mainly comsol from matlab for any secial function or application. but from V4 (to come) I beleive you can do far more without bothe on your PC, just with COMSOL i mean. Have to wait to see. Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2010年1月13日 GMT-5 16:49

In the subdomain setting, can I specify certain property with an expression of a Matlab function.

for example, if I specify this property to be '1*(x<0.5)+2*(x>=0.5)', it works.
while, if I set it to be 'fx', and fx is a function defined in Matlab as
function y=fx(x)
if x<0.5
y=1
else
y=2
end
end
it doesn't work.

Does it mean COMSOL cannot call an external Matlab function in modeling setting? or did I do it in a wrong way?
For my real case, the expression I would use cannot be written in an analytical way, so the user-defined function in COMSOL seems not work for me... Does anybody have the answer or any alternative methods for my problem?
Thanks!


The first example you gave above was in inline function, whereas the second one was not. Also, to invoke matlab you need to have it running in client/server mode along with comsol, and your function paths specified, else comsol doesn't know where to look to find the code. Finally, if all else fails you can export your application to matlab, at which point it becomes a matlab program and you can embed whatever additional matlab code you want.
[QUOTE] In the subdomain setting, can I specify certain property with an expression of a Matlab function. for example, if I specify this property to be '1*(x=0.5)', it works. while, if I set it to be 'fx', and fx is a function defined in Matlab as function y=fx(x) if x

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2010年1月15日 GMT-5 15:09
I was just running it under Matlab. Here is my code for a test sample:

fem.geom=rect2(1, 0.1);
clear appl
appl.mode='ConductiveMediaDC';
%appl.equ.sigma='1e7*(y<0.05)+1e8*(y>=0.05)';
appl.equ.sigma='fy(y)';
appl.bnd.V0={0, 30, 0};
appl.bnd.type={'V0', 'V', 'nJ0'};
appl.bnd.ind=[3, 1, 3, 2];
fem.appl=appl;
fem=multiphysics(fem);
fem.mesh=meshinit(fem);
fem.xmesh=meshextend(fem);
fem.sol=femstatic(fem);

where fy(y) is a function defined in matlab and saved under the same directory where I ran the code.

function sigma=fy(y)
sigma=1e7;
end

But, it didn't work, and reported an "error in external function..."


well from having read the documentation section "using matlab functions" it could [ I dont have matlab currently so no practical experience tough..] but from my prior experience with matlab you have to be careful that the special function you define is in a path matlab knows
[ can you call the function from within the matlab window..? and get an answer then...


I was just running it under Matlab. Here is my code for a test sample: fem.geom=rect2(1, 0.1); clear appl appl.mode='ConductiveMediaDC'; %appl.equ.sigma='1e7*(y=0.05)'; appl.equ.sigma='fy(y)'; appl.bnd.V0={0, 30, 0}; appl.bnd.type={'V0', 'V', 'nJ0'}; appl.bnd.ind=[3, 1, 3, 2]; fem.appl=appl; fem=multiphysics(fem); fem.mesh=meshinit(fem); fem.xmesh=meshextend(fem); fem.sol=femstatic(fem); where fy(y) is a function defined in matlab and saved under the same directory where I ran the code. function sigma=fy(y) sigma=1e7; end But, it didn't work, and reported an "error in external function..." [QUOTE] well from having read the documentation section "using matlab functions" it could [ I dont have matlab currently so no practical experience tough..] but from my prior experience with matlab you have to be careful that the special function you define is in a path matlab knows [ can you call the function from within the matlab window..? and get an answer then... [/QUOTE]

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2010年11月10日 GMT-5 18:14
Ou,
i am curious to know if you resolved the issue with the latest COMSOL40a? if so can you share, i am interested in the same issue.

--
merci
Ou, i am curious to know if you resolved the issue with the latest COMSOL40a? if so can you share, i am interested in the same issue. -- merci

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2010年11月10日 GMT-5 18:17
Hello Ivar,
please did you find out if this can be done in COMSOL40a? if so, please can you share the knowlegde.

in the meantime i will be comsoling :)

--
merci
Hello Ivar, please did you find out if this can be done in COMSOL40a? if so, please can you share the knowlegde. in the meantime i will be comsoling :) -- merci

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago 2010年11月11日 GMT-5 08:13
Hi

so far I have managed to survive without calling matlab vit 4.0 and 4.1 now, so sorry I cannot telll.

the only thing I know for sure is that your matlab function must be vector input compatible

--
Good luck
Ivar
Hi so far I have managed to survive without calling matlab vit 4.0 and 4.1 now, so sorry I cannot telll. the only thing I know for sure is that your matlab function must be vector input compatible -- Good luck Ivar

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.