fmincon in matlab function in simulink model
显示 更早的评论
Dear Matlab community
I am facing the following issue. I have a simulink model with three parallel aligned, self programmed MPCs. Those MPCs were designed fmincon and solve an optimization problem with constraints on the amplitude of the input.
However, after the simulation takes a decent amount of time to finally execute the error message that at sec 0.0 the model was ill-defined.
In order to deal with fmincon, the whole function was outsourced to an external file to deal with the compiler.
How do you solve this problem?
%%%Code of the matlab function
function du_llm1 = fcn(Yref,x,dztrack,PhiU1,PhiZ1,F1,H1,Qy1,y2,fuzzy_menge,b,Nc,dUmin,Umin,dUmax,Umax,Ymin,Ymax,M_du,M_u,C1)
nu = 3;
dU1 = zeros(nu*Nc,1);
du_llm1 = zeros(nu,1);
coder.extrinsic('opt_solv1');
if y2 <= fuzzy_menge(1,2)-b
du_llm1 = opt_solv1(dU1,H1,PhiU1,M_u,M_du,dUmin,dUmax,Umin,Umax,C1,du_llm1,Ymin,Ymax,x,F1,PhiZ1,dztrack,Qy1,Yref,nu);
end
end
%%%Code of the opt_solv1 containing fmincon and the defined constraints
function du_llm1 = opt_solv1(dU1,H1,PhiU1,M_u,M_du,dUmin,dUmax,Umin,Umax,C1,du_llm1,Ymin,Ymax,x,F1,PhiZ1,dztrack,Qy1,Yref,nu)
M_y = [-PhiU1; PhiU1];
M1 = [M_u;M_du;M_y];
gamma_du = [-dUmin; dUmax];
gamma_u = [ -Umin + C1*du_llm1; Umax - C1*du_llm1];
gamma_y = [ -Ymin + F1*x + PhiZ1*dztrack; Ymax - F1*x - PhiZ1*dztrack];
gamma1 = [gamma_u;gamma_du;gamma_y];
M1(isnan(gamma1),:) = [];
gamma1(isnan(gamma1)) = [];
f1 = -PhiU1' * Qy1 * ( Yref - F1*x - PhiZ1*dztrack);
options = optimoptions('fmincon', 'Algorithm', 'sqp');
options.MaxIterations = 1000;
options.OptimalityTolerance = 1e-5;
options.ConstraintTolerance = 1e-5;
dU1 = fmincon(@(dU1) ((Yref-F1*x)'*Qy1*(Yref-F1*x)-2*dU1'*f1+dU1'*H1*dU1),dU1,M1,@(x,du_llm1) gamma1,[],[],[],[],[],options);
du_llm1 = dU1(1:nu,1);
end
3 个评论
Pooja Kumari
2023-9-5
Can you provide more infomation on simulink model?
Benjamin Pommer
2023-9-22
Benjamin Pommer
2023-9-22
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Model Predictive Control Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

