optimproblem does not solve an analytical equation correct

2 views (last 30 days)
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

Accepted Answer

Torsten
Torsten on 10 May 2023
Moved: Torsten on 10 May 2023
Works for me (see above).
Your objective must return the norm of A*x (squared), not A*x.

More Answers (0)

Categories

Find more on Quadratic Programming and Cone Programming in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!