Anonymous function alternative for MATLAB Coder?

조회 수: 3 (최근 30일)
Mike Wa
Mike Wa 2015년 7월 28일
답변: Ryan Livingston 2015년 7월 29일
I'm using fmincon and would like to export my code to C++ via MATLAB Coder. However, my objective function uses more arguments than just the variable being optimised. To allow this, I use an anonymous function handle for my objective function:
idealx=fmincon(@(x)obj_fun(x,y,z,...),...);
MATLAB Coder does not support anonymous functions. What alternative can I use to pass additional objective function arguments that MATLAB Coder supports?
  댓글 수: 1
Ryan Livingston
Ryan Livingston 2015년 7월 29일
MATLAB Coder unfortunately does not support fmincon either since it isn't in the list of supported functions.

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

답변 (2개)

Muthu Annamalai
Muthu Annamalai 2015년 7월 28일
편집: Muthu Annamalai 2015년 7월 28일
You may write out your function with extra arguments as object.
First define this class,
classdef FcnWithArgs
properties
a
b
c
end
methods
function obj = FcnWithArgs(a,b,c)
obj.a = a;
% etc.
end
function yval = invoke(obj,xval)
yval = original_function( xval, obj.a, obj.b, obj.c )
end
end
end
Next you can make an instance of this class and use it with fmincon This could be codegen compatible. Worth a try.

Ryan Livingston
Ryan Livingston 2015년 7월 29일
For the general issue of replacing an anonymous function for code generation, see this question on fminsearch that describes modelling an anonymous function using persistent variables.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by