Use of Function Handle and fmincon in Embedded MATLAB

조회 수: 2 (최근 30일)
rach
rach 2012년 3월 1일
Hi,
I would like to perform the following 2 lines of code in the MATLAB Function found in the Simulink library (user-defined function):
F = @(x) eval('x(1)^2 + x(2) - 5'); [x,fval] = fmincon(F,x0,[],[],Aeq,beq,lb,ub);
However, I received a coder error saying that: 'This kind of expression is not supported'. Is there any way I can get around this problem?
Any help or suggestion is greatly appreciated! Thanks alot!

답변 (2개)

Sean de Wolski
Sean de Wolski 2012년 3월 1일
No need for evil-eval:
F = @(x)x(1)^2 + x(2) - 5;
  댓글 수: 2
rach
rach 2012년 3월 1일
Hi Sean,
The code that I typed above is a simplified version of what I actually have in my code. The actual code is:
F = @(x) eval(costFunction);
[x,fval] = fmincon(F,x0,[],[],Aeq,beq,lb,ub);
where costFunction is a string generated from a rather long and complicated function. Hence, 'eval' is necessary in this case.
The problem I am facing now is that embedded matlab does not seem to be able to handle the function handle. Is there any way to get around this limitation?
Thank you!
Sean de Wolski
Sean de Wolski 2012년 3월 1일
web([docroot '/toolbox/eml/ug/bsvqvgq.html'])
I'm sorry I do not have more time to look at this now.

댓글을 달려면 로그인하십시오.


Kaustubha Govind
Kaustubha Govind 2012년 3월 2일
As Sean pointed out, here is the documentation about support for function handles with code generation - Code Generation for Function Handles.
Btw, support for function handles was supported for code-generation only added fairly recently. So you might be using an older release where they are not supported in an Embedded MATLAB Function block. In that case, I would recommend defining a MATLAB-file with your code in it:
F = @(x) eval('x(1)^2 + x(2) - 5');
[x,fval] = fmincon(F,x0,[],[],Aeq,beq,lb,ub);
And call that function with the coder.extrinsic (previously eml.extrinsic) directive. Since eval and fmincon are not amongst the functions supported for code-generation, you will have to declare them as extrinsic functions anyway.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by