Solving quadratic optimization problem in Simulink and code generation - Different behavior in Matlab?
4 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm experimenting with solving quadratic optimization problems (about 400 variables and only equality constraints) in Simulink using Matlab function blocks. Basically, I tried 2 approaches:
First, I formulated the matrix and vector of the resulting linear equation system and solved it:
%H: Weighting matrix of the problem
%f: Weighting vector of the problem
%Aeq: Matrix of equality constraints
%beq: Vector of equality constraints
Atmp = [H,Aeq';Aeq,zeros(size(Aeq,1),size(Aeq,1))];
btmp = [-f;beq];
Solution = Atmp\btmp;
Executing this code in matlab delivers a warning of a bad conidtioned matrix but I get the right solution of the problem. Executing this code in Simulink with putting it into a Matlab function block delivers no solution (only NaN's) and also a warning of a bad condition.
Second, I tried to solve the problem using quadprog():
options = optimoptions('quadprog','algorithm','active-set');
[Solution,fval,exitflag,output] = quadprog(H,f,[],[],Aeq,beq,[],[],zeros(length(H),1),options);
Again, I first executed this in Matlab where I got the right result in 2 iterations and an acceptable computation time. Executing this code inside a function block in Simulink I also get a result (exitflag = 1), but the solution is nonsense and it's not solving my original problem.
Does anyone here has a guess for explaining the observed behavior? What is there to consider when solving numerical problems in Matlab function blocks in Simulink? Could it have something to do with the automatic code generation (missing libraries etc.) or is it a execution time problem? I execute the Simulink model with a fixed time step of 1 s, the problem solving should not need more than some ms.
Thank you very in advance!
Cheers,
Clemens
0 个评论
回答(1 个)
Matt J
2021-8-5
编辑:Matt J
2021-8-5
Well, the problem is what the warning message told you - the conditioning of the problem is poor needs and needs to be improved if you hope to have a unique solution. It is likely that both quadprog solutions are valid, which you can verify by looking at the fval output in both cases. You should see that both are approximately the same, meaning that both solutions are solving the minimization problem that you are providing.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Quadratic Programming and Cone Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!