How can I generate code using Simulink PLC Coder for fminsearch function?

Hi, I'm trying to generate code for a MATLAB Function Block where I use the method fminsearch in Simulink, but I get some errors.
I decided to create a model with just the MATLAB Function Block and a code using fminsearch (as simple as possible) like this:
function y = myfminsearch(u)
%#codegen
y = fminsearch(@myfun,u);
function f = myfun(x)
f = (x+5)^2+5;
Even if the method is supposed to work for code generation I get this error:
Call to unsupported function 'frexp' detected.
Function 'eps' (#42.910.941), line 31, column 17
Function 'fminsearch' (#41.14645.14653), line 451, column 25
Function 'fminsearch' (#41.14645.14653), line 451, column 25
Function 'fminsearch' (#41.6622.6656), line 195, column 53
Function 'fminsearch' (#41.6622.6656), line 195, column 53
Function 'MATLAB Function1' (#25.34.54), line 3, column 5
Function 'MATLAB Function1' (#25.34.54), line 3, column 5
Component: stateflow | Category: PLC Coder errorOpen
Errors occurred during codegenIR Construction failed
Component: simulink | Category: PLC Coder error
Can you see what the problem is? I don't really understand the error... Do you think I can't use fminsearch for code generation?
Thank you guys!

6 Comments

frexp() however is not a Matlab function. It is C++ function from the cmath library.
It is hard to help you or venture conjectures without looking at the code that actually generates the error.
Hi, i just put this code:
function y = fcn(u)
%#codegen
y = fminsearch(@myfun,u);
where myfun is:
function f = myfun(x)
f = (x+5)^2+5;
into the MATLAB Function Block with input u and output y, and generate the code for the block. That's all I'm doing, so this is the code that generates the error...
I am not a Simulink expert, but have you tried to define the function inside the block?
function y = fcn(u)
f = @(x) (x+5).^2 + 5;
y = fminsearch(f,u);
I defined it into the block as:
function y = fcn(u)
%#codegen
y = fminsearch(@myfun,u);
function f = myfun(x)
f = (x+5)^2+5;
Since anonymous functions are not supported for code generation.
Sorry about that:
Here is an example of how it work with lsqnonlin(). It should be the same for fminsearch().
I have the same problem. the @ character is not supported I thing you have to write fminsearch('myfun',x,options)

Sign in to comment.

Answers (0)

Categories

Find more on Simulink PLC Coder in Help Center and File Exchange

Asked:

on 14 Oct 2014

Commented:

on 7 Jun 2016

Community Treasure Hunt

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

Start Hunting!