Azzera filtri
Azzera filtri

Solving quadratic optimization problem in Simulink and code generation - Different behavior in Matlab?

7 visualizzazioni (ultimi 30 giorni)
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

Risposte (1)

Matt J
Matt J il 5 Ago 2021
Modificato: Matt J il 5 Ago 2021
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.
  1 Commento
Clemens Troll
Clemens Troll il 5 Ago 2021
Thank you for your answer!
Meanwhile I found the problem: The algorithm creating the equality constraint vector acted different in Matlab and in Simulink due to a equality comparison of two doubles, which I now fixed. Hence, also Simulink solves the linear equation system correctly. And more importantly: With the same result as in Matlab.

Accedi per commentare.

Categorie

Scopri di più su DSP Algorithm Acceleration in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by