# Optimization of matrix with constraints

7 views (last 30 days)
Zhenghao Yang on 30 Jul 2022
Answered: Akshat Dalal on 8 Oct 2023
I have written a prototype function to minimize a matrix equation with code below. The input N is an even number, A and H0 are 2Nx2N matrices with real entries but complex eigenvalues.
I found the documentation of Optimization Toolbox not easy to follow, so I listed all the constraints and the equation to be minimized in the code to give my best to anyone whom would help me.
function H=extractor(N,A,H0)
A=real(A);
T0=A(1:2,1:2);
LHP = optimvar('LHP',N-1,4,'LowerBound',-2,'UpperBound',2);
RHP = optimvar('RHP',N-1,4,'LowerBound',-2,'UpperBound',2);
for q=1:N/2
if q~=N/2
UPPER(1:2,2*q-1:2*q)=[LHP(q,2),LHP(q,4);LHP(q,1),LHP(q,3)]+[RHP(N-q,2),RHP(N-q,4);RHP(N-q,1),RHP(N-q,3)];
LOWER(2*q-1:2*q,1:2)=[RHP(q,2),RHP(q,1);RHP(q,4),RHP(q,3)]+[LHP(N-q,2),LHP(N-q,1);LHP(N-q,4),LHP(N-q,3)];
elseif q==N/2
UPPER(1:2,2*q-1:2*q)=[LHP(q,2),LHP(q,4);LHP(q,1),LHP(q,3)];
LOWER(2*q-1:2*q,1:2)=[RHP(q,2),RHP(q,1);RHP(q,4),RHP(q,3)];
end
end
for q=1:N/2-1
DECAYL(q,1:4)=LHP(q,1:4)-LHP(N-q,1:4);
DECAYR(q,1:4)=RHP(q,1:4)-RHP(N-q,1:4);
end
for i=1:N
for j=1:N
d=abs(j-i);
if i<j %upper triangle
if d>N/2 % higher order
H(2*i-1:2*i,2*j-1:2*j)=[LHP(d,2),LHP(d,1);LHP(d,4),LHP(d,3)];
elseif d==N/2 % central order
H(2*i-1:2*i,2*j-1:2*j)=[LHP(d,2),LHP(d,4);LHP(d,1),LHP(d,3)];
elseif d<N/2 % lower order
H(2*i-1:2*i,2*j-1:2*j)=[LHP(d,2),LHP(d,4);LHP(d,1),LHP(d,3)];
end
elseif i>j %lower triangle
if d>N/2 % higher order
H(2*i-1:2*i,2*j-1:2*j)=[RHP(d,2),RHP(d,4);RHP(d,1),RHP(d,3)];
elseif d==N/2 % central order
H(2*i-1:2*i,2*j-1:2*j)=[RHP(d,2),RHP(d,1);RHP(d,4),RHP(d,3)];
elseif d<N/2 % lower order
H(2*i-1:2*i,2*j-1:2*j)=[RHP(d,2),RHP(d,1);RHP(d,4),RHP(d,3)];
end
elseif i==j %diagonal
H(2*i-1:2*i,2*j-1:2*j)=T0;
end
end
end
%constraint1: A(1:2,3:N+2)==UPPER && A(3:N+2,1:2)==LOWER;
%constraint2: abs(DECAYL)>0 && abs(DECAYR)>0
%minimize this equation: sum(abs(sort(real(eig(H0)))-sort(real(eig(H)))))+sum(abs(sort(imag(eig(H0)))-sort(imag(eig(H)))))

Akshat Dalal on 8 Oct 2023
Hi Zhenghao,
I understand that you want to solve an optimization problem. You can use the ‘Optimize’ function provided in the Optimization Toolbox. You can use the following approach to solve your problem:
• In your script, insert an ‘Optimize Live Editor task’ by clicking on the Insert tab and then, in the Code section, selecting Task > Optimize.
• Select the objective and Constraints type as per your data and then select the recommended solver.
• Define the objective function for your problem. The inputs would be ‘H’ and ‘H0’ and the output would the value of the equation you want to minimize. You can refer the attached examples to see how to pass multiple inputs.
• Define the constraints function for your problem. The inputs would be ‘A’, ‘N’, ‘UPPER’, ‘LOWER’, ‘DECAYL’, and ‘DECAYR’. Declare the inequality constraints in the ‘c’ structure and the equality constraints in the ‘ceq’ structure. For example,
c(1) = -abs(DECAYL);
ceq = [A(1:2,3:N+2) - UPPER, A(3:N+2,1:2) - LOWER];
• Select the objective function and constraint function you wrote in previous steps as inputs to the problem data, and then run the solver to get the desired results.
You could also refer to the following example on using the ‘Optimize’ function –

### Categories

Find more on Choose a Solver in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!