optimproblem does not solve an analytical equation correct

5 ビュー (過去 30 日間)
Gabriel Waibel
Gabriel Waibel 2023 年 5 月 10 日
コメント済み: Gabriel Waibel 2023 年 5 月 10 日
Dear Matlab,
I am using optimproblem to solve a overdetermined optimization problem. Since the results were completly wrong I tried the follow problem which has a unique solution. Also there the result is extremly wrong.
I have a rectangle, each side determined by two points. I would like to get the constant and the normal vector of each side. Therefore I set up the optimization problem as follows.
% points for each side of the rectangle
Px = [0;1]
Px = 2×1
0 1
Py = [1;1]
Py = 2×1
1 1
Qx = [1;1]
Qx = 2×1
1 1
Qy = [1;0]
Qy = 2×1
1 0
Rx = [1;0]
Rx = 2×1
1 0
Ry = [0;0]
Ry = 2×1
0 0
Sx = [0;0]
Sx = 2×1
0 0
Sy = [0;1]
Sy = 2×1
0 1
zp = zeros(size(Px)); op = ones(size(Px));
zq = zeros(size(Qx)); oq = ones(size(Qx));
zr = zeros(size(Rx)); or = ones(size(Rx));
zs = zeros(size(Sx)); os = ones(size(Sx));
A = [ op zp zp zp Px Py
zq oq zq zq Qy -Qx
zr zr or zr Rx Ry
zs zs zs os Sy -Sx]
A = 8×6
1 0 0 0 0 1 1 0 0 0 1 1 0 1 0 0 1 -1 0 1 0 0 0 -1 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0
c1 = optimvar('c1');
c2 = optimvar('c2');
c3 = optimvar('c3');
c4 = optimvar('c4');
n1 = optimvar('n1');
n2 = optimvar('n2');
optivars = [c1; c2; c3; c4; n1; n2];
prob = optimproblem;
prob.Objective = (A*optivars).'*(A*optivars);
prob.Constraints.con1 = n1*n1 + n2*n2 == 1;
show(prob)
OptimizationProblem : Solve for: c1, c2, c3, c4, n1, n2 minimize : 2*c1^2 + 2*c2^2 + 2*c3^2 + 2*c4^2 + 4*n1^2 + 4*n2^2 + n1*c4 + c4*n1 + n1*c3 + c3*n1 + n1*c2 + c2*n1 + n1*c1 - 2*n2*c2 + c1*n1 - 2*c2*n2 + 2*n2*c1 + 2*c1*n2 subject to con1: ((n1 .* n1) + (n2 .* n2)) == 1
init_value.c1 = -1.0;
init_value.c2 = 1.0;
init_value.c3 = 0.0;
init_value.c4 = 0.0;
init_value.n1 = 0.0;
init_value.n2 = 1.0;
[sol,fval,exitflag] = solve(prob, init_value);
Solving problem using fmincon. Initial point is a local minimum that satisfies the constraints. Optimization completed because at the initial point, the objective function is non-decreasing in feasible directions to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
If I do show(prob) I get the output of the optimization problem. The equations are correct and if I solve them by hand the result is correct. So why does solve(prob,init_value) not find the solution even with the perfect inital condition? I also get 70 values for each variable (c1,c2,c3,c4,n1,n2)?
Thanks

採用された回答

Torsten
Torsten 2023 年 5 月 10 日
移動済み: Torsten 2023 年 5 月 10 日
Works for me (see above).
Your objective must return the norm of A*x (squared), not A*x.
  1 件のコメント
Gabriel Waibel
Gabriel Waibel 2023 年 5 月 10 日
That was the error. Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeQuadratic Programming and Cone Programming についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by