MATLAB Answers

Unable to use for loop in fmincon

16 views (last 30 days)
I am trying to maximize an expression using fmincon, which has 8 parameters, and an extra variable(p) which I want to keep as a parameter not to be used in maximization.
I have defined an objective function as shown below:
function fval = myfun1(K)
%define parameters
syms theta [1 4] real
syms phi [1 4] real
syms p real
theta1 = K(1);
theta2 = K(2);
theta3 = K(3);
theta4 = K(4);
phi1 = K(5);
phi2 = K(6);
phi3 = K(7);
phi4 = K(8);
% define eigenstates
psi_01 = [(1/2)*(1+cos(theta1))*exp(-1i*phi1); sqrt(1/2)*sin(theta1); (1/2)*(1-cos(theta1))*exp(1i*phi1)];
psi_02 = [(1/2)*(1+cos(theta2))*exp(-1i*phi2); sqrt(1/2)*sin(theta2); (1/2)*(1-cos(theta2))*exp(1i*phi2)];
psi_03 = [(1/2)*(1+cos(theta3))*exp(-1i*phi3); sqrt(1/2)*sin(theta3); (1/2)*(1-cos(theta3))*exp(1i*phi3)];
psi_04 = [(1/2)*(1+cos(theta4))*exp(-1i*phi4); sqrt(1/2)*sin(theta4); (1/2)*(1-cos(theta4))*exp(1i*phi4)];
psi_11 = [-sqrt(1/2)*sin(theta1)*exp(-1i*phi1); cos(theta1); sqrt(1/2)*sin(theta1)*exp(1i*phi1)];
psi_12 = [-sqrt(1/2)*sin(theta2)*exp(-1i*phi2); cos(theta2); sqrt(1/2)*sin(theta2)*exp(1i*phi2)];
psi_13 = [-sqrt(1/2)*sin(theta3)*exp(-1i*phi3); cos(theta3); sqrt(1/2)*sin(theta3)*exp(1i*phi3)];
psi_14 = [-sqrt(1/2)*sin(theta4)*exp(-1i*phi4); cos(theta4); sqrt(1/2)*sin(theta4)*exp(1i*phi4)];
psi_21 = [(1/2)*(1-cos(theta1))*exp(-1i*phi1); -sqrt(1/2)*sin(theta1); (1/2)*(1+cos(theta1))*exp(1i*phi1)];
psi_22 = [(1/2)*(1-cos(theta2))*exp(-1i*phi2); -sqrt(1/2)*sin(theta2); (1/2)*(1+cos(theta2))*exp(1i*phi2)];
psi_23 = [(1/2)*(1-cos(theta3))*exp(-1i*phi3); -sqrt(1/2)*sin(theta3); (1/2)*(1+cos(theta3))*exp(1i*phi3)];
psi_24 = [(1/2)*(1-cos(theta4))*exp(-1i*phi4); -sqrt(1/2)*sin(theta4); (1/2)*(1+cos(theta4))*exp(1i*phi4)];
%define projectors
P_01 = psi_01*psi_01';
P_02 = psi_02*psi_02';
P_03 = psi_03*psi_03';
P_04 = psi_04*psi_04';
P_11 = psi_11*psi_11';
P_12 = psi_12*psi_12';
P_13 = psi_13*psi_13';
P_14 = psi_14*psi_14';
P_21 = psi_21*psi_21';
P_22 = psi_22*psi_22';
P_23 = psi_23*psi_23';
P_24 = psi_24*psi_24';
%define the state in question
psi1 = (1/sqrt(3))*[1;0;0;0;1;0;0;0;1];
rho = p*(psi1*psi1') + (1-p)*(1/9)*kron(eye(3),eye(3));
% define the expression to maximize
P1 = trace(rho*(kron(P_01,P_03)))+ trace(rho*(kron(P_11,P_13)))+trace(rho*(kron(P_21,P_23)));
P2 = trace(rho*(kron(P_02,P_13)))+ trace(rho*(kron(P_12,P_23)))+trace(rho*(kron(P_22,P_03)));
P3 = trace(rho*(kron(P_02,P_04)))+ trace(rho*(kron(P_12,P_14)))+trace(rho*(kron(P_22,P_24)));
P4 = trace(rho*(kron(P_01,P_04)))+ trace(rho*(kron(P_11,P_14)))+trace(rho*(kron(P_21,P_24)));
P5 = trace(rho*(kron(P_01,P_13)))+ trace(rho*(kron(P_11,P_23)))+trace(rho*(kron(P_21,P_03)));
P6 = trace(rho*(kron(P_02,P_03)))+ trace(rho*(kron(P_12,P_13)))+trace(rho*(kron(P_22,P_23)));
P7 = trace(rho*(kron(P_02,P_14)))+ trace(rho*(kron(P_12,P_24)))+trace(rho*(kron(P_22,P_04)));
P8 = trace(rho*(kron(P_11,P_04)))+ trace(rho*(kron(P_21,P_14)))+trace(rho*(kron(P_01,P_24)));
fval = -real(P1+P2+P3+P4-P5-P6-P7-P8); % consider minus to maximize this function
end
I want to maximize the objective function fval for different values of p, ranging from 0 to 1. For a fixed value of p, I am able to maximize the function. However, I am unable to add a for loop to vary p.
The code for optimization is:
% variable bounds
lb = [0,0,0,0,0,0,0,0];
ub = [2*pi,2*pi,2*pi,2*pi,pi,pi,pi,pi];
for p = 0:0.2:1
objective = @myfun1;
% initial guess
x0 = [0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5];
% show initial objective
disp(['Initial Objective: ' num2str(-objective(x0))])
x = fmincon (objective,x0,[],[],[],[],lb,ub,[]);
x
% show final objective
num2str(-objective(x))
end
Myfun1 is not considering the p values of the for loop defined above. Any help would be greatly appreciated.

  0 Comments

Sign in to comment.

Accepted Answer

Alan Weiss
Alan Weiss on 8 Jan 2021
You are not passing the value of p into myfun1. What is it supposed to represent in your code? What I mean is, when you write
for p = 0:0.2:1
objective = @myfun1;
your objective does not depend on the value of p.
Alan Weiss
MATLAB mathematical toolbox documentation

  3 Comments

Ijaz Ahamed Mohammad
Ijaz Ahamed Mohammad on 8 Jan 2021
Okay, I get it. How do I pass value of p using a for loop, so that I can maximize for various values of p? When I fix a value for the variable p, say, p = 0.1 in myfun1 and remove the for loop before using fmincon, I am able to maximize without any problems. But I would like maximize my 'fval' of myfun1 (which also depends on p) for different values of p and plot the maximum values for different p. I don't want 'p' to be a parameter which is used for maximization. Can you please guide me how I can achieve this?
Alan Weiss
Alan Weiss on 8 Jan 2021
As explaned in Passing Extra Parameters, write your function as a function of two variables, and then set one variable in the loop:
for p = 0:0.2:1
objective = @(K)myfun1(K,p);
% The rest of your optimization call here
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!