# MATLAB: Unable to use for loop in fmincon

fminconfor loop

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 parameterssyms theta [1 4] realsyms phi [1 4] realsyms p realtheta1 = K(1);theta2 = K(2);theta3 = K(3);theta4 = K(4);phi1 = K(5);phi2 = K(6);phi3 = K(7);phi4 = K(8);% define eigenstatespsi_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 projectorsP_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 questionpsi1 = (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 maximizeP1 =  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 functionend
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 boundslb = [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.

for p = 0:0.2:1    objective = @myfun1;