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?
Posted 13.01.2010, 12:04 GMT-5 8 Replies
Please login with a confirmed email address before reporting spam
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!
Please login with a confirmed email address before reporting spam
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
[ can you call the function from within the matlab window..? and get an answer then...
Please login with a confirmed email address before reporting spam
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
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.
Please login with a confirmed email address before reporting spam
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...
Please login with a confirmed email address before reporting spam
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
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
Please login with a confirmed email address before reporting spam
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.
Suggested Content
- FORUM How to call an analytical function for a boundary condition
- BLOG Combining COMSOL Multiphysics® and MATLAB®
- FORUM how to import a computed polynomial function in a Matlab script into a Comsol simulation?
- KNOWLEDGE BASE Undefined Function Call
- FORUM How can I define and where can I apply a variable function in space and time?