How to avoid redefining optimoptions in Simulink for real-time nonlinear optimization with fmincon (SQP)?

조회 수: 7 (최근 30일)
Hello guys, I'm trying to implement a real-time nonlinear optimization solver using fmincon with the SQP algorithm in Simulink. I'm currently using a Function (Fcn) block and wrote the below optimization code inside it:
function y = fcn(u)
options = optimoptions('fmincon','Display','None','Algorithm','sqp');
x0=0.1;
lb=-10;
ub=10;
Cost_Function = @(x) f1(x,u);
y = fmincon(Cost_Function,x0,[],[],[],[],lb,ub,[],options);
end
function cost = f1(x,u)
cost=abs(x^2-u^2);
end
The code works correctly, but it's too time-consuming for my application, as the optimization needs to be performed frequently. One of the performance bottlenecks seems to be the repeated definition of "optimoptions" inside the block.
To address this, I tried defining the "options" object just once using "persistent" and "global" variables. However, Simulink throws an error, likely because options is not recognized as a Simulink parameter.
I also experimented with a "MATLAB System block" instead of the Fcn block, but the same issue persists — I can't define "optimoptions" only once at the start.
Question:
Is there a way to define optimoptions once (e.g., during initialization) and reuse it in Simulink for real-time execution, without redefining it at every simulation step?
Any suggestions or best practices for improving the performance of fmincon-based optimization in Simulink would be appreciated.
  댓글 수: 1
Matt J
Matt J 2025년 7월 25일
편집: Matt J 2025년 7월 25일
Is your optimization problem just a fake placeholder example? I don't see why you would be using fmincon for such a thing.The solution is obviously x=±u.
In any case, for a 1D problem, you should just use fminbnd which won't require optimoptions, if all you're trying to do is to suppress termination messages.
u=6;
lb=-10;
ub=10;
y = fminbnd(@(x) f1(x,u),lb,ub)
y = -6.0000
function cost = f1(x,u)
cost=abs(x^2-u^2);
end

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

답변 (2개)

Paul
Paul 2025년 7월 26일
편집: Paul 2025년 7월 26일
Are you really using a Matlab Function block?
If so have you tried:
function y = fcn(u)
persistent options
if isempty(options)
options = optimoptions('fmincon','Display','None','Algorithm','sqp');
end
x0=0.1;
lb=-10;
ub=10;
Cost_Function = @(x) f1(x,u);
y = fmincon(Cost_Function,x0,[],[],[],[],lb,ub,[],options);
end
function cost = f1(x,u)
cost=abs(x^2-u^2);
end
If that code in the Matlab Function block throws an error, please show the full text of the error message.
If not, please provide a link to the specific block called "Matlab (fcn)"

Matt J
Matt J 2025년 7월 25일
You can use global variables (but with care).
function y = fcn(u)
global lb ub options
y = fminbnd(@(x) f1(x,u),lb,ub,options)
end
function cost = f1(x,u)
cost=abs(x^2-u^2);
end

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by