Simulink Matlabfunction does not support Global Optimization toolbox

Hi there,
I recently use Global Optimization toolbox in Simulink Matlabfunction to solve global optimization problem. But it always report a bug. Does Simulink Matlabfunction support Global Optimization toolbox ?
Best regards,

5 comentarios

I assume when you say "report a bug" you mean that you receive an error when you try to run your Simulink model. If so please show us the full and exact text of all error messages you receive, whether they're displayed in red in the Command Window or displayed in a dialog box brought up by Simulink during execution of the model.
If I had to guess I'd say you're trying to generate C or C++ code from your model but the Global Optimization Toolbox functions you're calling don't have that extended capability. I think the only Global Optimization Toolbox function that does is optimoptions, as it's the only one a search of the alphabetical list linked to by this page for "global optimization" found.
When I solve the following problem using GlobalSearch in Simulink matlab function, the dialog box in Simulink reports
The 'GlobalSearch' class does not support code generation.
Function 'MATLAB Function' (#1386.1710.1722), row 49, column 6.
“GlobalSearch”
Starts a diagnostic report.
========================================= Code
```
opts = optimoptions(@fmincon,'Algorithm','sqp');
problem = createOptimProblem('fmincon','objective',fun,'x0',3,'lb',-5,'ub',5,'options',opts);
gs = GlobalSearch;
[x,f] = run(gs,problem)
```
tianyuan wang
tianyuan wang el 20 de Mzo. de 2025
Editada: tianyuan wang el 20 de Mzo. de 2025
Actually I don't need to generate C or C++ code. Could I use GlobalSearch by getting around the C or C++ code generation?
If not, how can I achieve global optimization solution in the matlab function
Thank you very much for your help
Turn Rapid Acceleration off for the model.
I have set “Rapid Acceleration” to “Normal”.
But the problem still exists.
Here I share my model for reference
Thank you very much for your help
Best regards,
Tianyuan

Iniciar sesión para comentar.

 Respuesta aceptada

Paul
Paul el 20 de Mzo. de 2025
The MatlabFunction block always generates code, even in Normal mode.
One option that may be workable is to implement your optimization function in an ordinary, Matlab, .m-function file with inputs that you'd expect to get from Simulink and outputs to send back to Simulink. Then declare that function with coder.extrinsic inside a Simulink MatlabFunction that serves as the wrapper to your function that does the actual work.

10 comentarios

coder.extrinsic works well. It seems the disadvantage of coder.extrinsic is the relatively slow simulation speed. A rough estimate of the difference is about 50 times.
Glad to hear that it works well for you.
There will be some overhead with the interface from Simulink to Matlab and back, and then all the code in the extrinsic function is being run in Matlab, as opposed to compiled code, whatever that might entail as far as execution speed.
How are you estimating the reduction in simulation speed given that the simulation wasn't working at all to begin with (as I understand the situation)?
I try some codes that can be run in both Matlab function and coder.extrinsic, like
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
nonlcon = [];
x0 = [0,0];
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
Running the codes through coder.extrinsic is about 50 times slower than using Matlab Function directly
Fair enough. I guess something is better than nothing.
In your case, if you're running the global search multiple times in the simulation, maybe it would be beneficial to save the results from one global search and use them to initialize the global search on the next step? Don't know if that makes sense or not for your problem; it's just a thought.
Thank you very much for your suggestions. I will try.
Best regards,
Tianyuan
I encounter another problem. Matlab Function allows the matrix transposition:
y = fun(x) % x = [6,1]
x = x';
x
end
For my codes:
bPosGlobal = zeros(6,1)
function [y] = OWScalcBodyWave(bPosGlobal)
y = zeros(6,1);
rowNum = size(bPosGlobal,1)
columnNum = size(bPosGlobal,2)
if (rowNum < columnNum && rowNum > 1) || (rowNum > columnNum && columnNum == 1)
bPosGlobal = bPosGlobal'; % Must be row vector
end
bPosGlobal
end
It reports:
Size mismatch between two arrays
Size mismatch (size [6][1] ~= size [1][6])
But when I comment out some codes, the error disappear:
function [y] = OWScalcBodyWave(bPosGlobal)
y = zeros(6,1);
rowNum = size(bPosGlobal,1)
columnNum = size(bPosGlobal,2)
% if (rowNum < columnNum && rowNum > 1) || (rowNum > columnNum && columnNum == 1)
bPosGlobal = bPosGlobal'; % Must be row vector
% end
bPosGlobal
end
Do you have any idea on it?
No, because I don't know what line is generating the error message, nor if it's coming from the MatlabFunction or the extinsic function. The full error message, which I think includes more information, would be helpful.
Also, I don't really understand the code. Is bPosGobal always a vector and the purpose of the logic is to ensure that it's a row vector (based on comment "% Must be row vector")? If so, then
if iscolumn(bPosGlobal)
bPosGlobal = bPosGlobal';
end
Or is it possible that bPosGlobal is a matrix and you want to ensure that the matrix is square or has more rows than columns?
Yes, bPosGlobal is a vector (1×6 or 6×1) or matrix (timeLength×6 or 6×timeLength). I want to set it to 1×6 or timeLength×6.
When I use
if (rowNum < columnNum && rowNum > 1) || (rowNum > columnNum && columnNum == 1)
, the error reports
Simulink does not have enough information to determine the size of the output of this module.If you think the following errors are inaccurate, try specifying the type of module input and/or the size of the module output.
Component:MATLAB Function | Category:Coder Error
Size mismatch (size [6 x 1] ~= size [1 x 6]).
The size of the left side is the size of the left side of the assignment.
Detailed information
Function 'OWScalcBodyWave.m' (#33.1182.1183), row 24, column 9.
bPosGlobal
Starts a diagnostic report.
Component:MATLAB Function | Category:Coder Errors
But the error disapper when I comment out the if condition. I don't know why, as the method works in Matlab environment but the error is in Simulink (matlab function) environment
Paul
Paul el 21 de Mzo. de 2025
Editada: Paul el 21 de Mzo. de 2025
Simulink generates code for the MatlabFunction block and that code is statically typed, which means the code generator has to be able to determine the class and size of each variable. Also, a Simulink model is static (by and large) and so Simulink can determine the class and size of all signals in the simulation when the model is compiled (I might be being a bit loose with terminlogy). For the MatlabFunction block, therefore, the size and class of the inputs to the function are known and then the code analysis tries to figure out the rest (sometimes also by back propagating information on the output side of the block). In this case, the code analysis isn't smart enough to figure out, at code generation time, if the if statement is going to be true or not, and therefore cannot know how to dimension bPosGlobal in the generated code. Matlab, of course, is much more flexible about this kind of stuff.
Having said all of that, I don't understand why it's a problem. It seems to me that you should know (or can force) the dimensions of bPosGlobal in Simulink, and therefore the dimension check shouldn't be needed in the MatlabFunction.

Thank you very much for your kindly reply. Now I understand why the error happens.

Best regards,

Tianyuan

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2024b

Preguntada:

el 20 de Mzo. de 2025

Editada:

el 22 de Mzo. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by