issue with fmincon function.
I want to maximize rate using fmincon function. the problem is I have an error as "Arrays have incompatible sizes for this operation." and "Caused by: Failure in initial objective function evaluation. FMINCON cannot continue.". I provide a part of my code and the objective function here. I guess the problem is in the reshape part and it might be wrong. I would really appreciate it if someone could help me to write the objective function correctly.
when . I should mention that is a vector and is a matrix for .
here is my code.
%initial guess for P2 and W2
P2_0 = ones(Ns,1) * (tilde_P2 / Ns) / 2; % initial guess for P2
W2_0 = ones(Ms,Ns); %initial guess for W2
%combine initial guess in a single vector
x0 = [P2_0 ; W2_0(:)];
% Define the linear constraint (C2)
A = zeros(Np, Ns + Ms * Ns);
for l = 1:Np
A(l, 1:Ns) = xi’; % Place xi’ in the first Ns columns of each row
end
% Define the bounds(C1)
lb = [zeros(Ns, 1); -Inf(Ms * Ns, 1)]; % Lower bound (P2 >= 0, no lower bound for W2)
ub = [repmat(tilde_P2 / Ns, Ns, 1); Inf(Ms * Ns, 1)]; % Upper bound (P2 <= tilde_P2_max / Ns, no upper bound for W2)
%%%objective function
obj_fun = @(x) -((Nup – Nt) / Nup * sum(log(1 + x(1:Ns) ./ (sigma2 * reshape(x(Ns+1:end), Ms, Ns).^2))));
% Set options for fmincon
options = optimoptions(‘fmincon’, ‘Display’, ‘iter’, ‘Algorithm’, ‘interior-point’);
% Run the optimization
[x_opt, fval] = fmincon(obj_fun, x0, A, hat_zeta1, [], [], lb, ub, [], options);
% Extract optimized values for P2 and W2
P2_opt = x_opt(1:Ns);
W2_opt = reshape(x_opt(Ns+1:end), Ms, Ns);I want to maximize rate using fmincon function. the problem is I have an error as "Arrays have incompatible sizes for this operation." and "Caused by: Failure in initial objective function evaluation. FMINCON cannot continue.". I provide a part of my code and the objective function here. I guess the problem is in the reshape part and it might be wrong. I would really appreciate it if someone could help me to write the objective function correctly.
when . I should mention that is a vector and is a matrix for .
here is my code.
%initial guess for P2 and W2
P2_0 = ones(Ns,1) * (tilde_P2 / Ns) / 2; % initial guess for P2
W2_0 = ones(Ms,Ns); %initial guess for W2
%combine initial guess in a single vector
x0 = [P2_0 ; W2_0(:)];
% Define the linear constraint (C2)
A = zeros(Np, Ns + Ms * Ns);
for l = 1:Np
A(l, 1:Ns) = xi’; % Place xi’ in the first Ns columns of each row
end
% Define the bounds(C1)
lb = [zeros(Ns, 1); -Inf(Ms * Ns, 1)]; % Lower bound (P2 >= 0, no lower bound for W2)
ub = [repmat(tilde_P2 / Ns, Ns, 1); Inf(Ms * Ns, 1)]; % Upper bound (P2 <= tilde_P2_max / Ns, no upper bound for W2)
%%%objective function
obj_fun = @(x) -((Nup – Nt) / Nup * sum(log(1 + x(1:Ns) ./ (sigma2 * reshape(x(Ns+1:end), Ms, Ns).^2))));
% Set options for fmincon
options = optimoptions(‘fmincon’, ‘Display’, ‘iter’, ‘Algorithm’, ‘interior-point’);
% Run the optimization
[x_opt, fval] = fmincon(obj_fun, x0, A, hat_zeta1, [], [], lb, ub, [], options);
% Extract optimized values for P2 and W2
P2_opt = x_opt(1:Ns);
W2_opt = reshape(x_opt(Ns+1:end), Ms, Ns); I want to maximize rate using fmincon function. the problem is I have an error as "Arrays have incompatible sizes for this operation." and "Caused by: Failure in initial objective function evaluation. FMINCON cannot continue.". I provide a part of my code and the objective function here. I guess the problem is in the reshape part and it might be wrong. I would really appreciate it if someone could help me to write the objective function correctly.
when . I should mention that is a vector and is a matrix for .
here is my code.
%initial guess for P2 and W2
P2_0 = ones(Ns,1) * (tilde_P2 / Ns) / 2; % initial guess for P2
W2_0 = ones(Ms,Ns); %initial guess for W2
%combine initial guess in a single vector
x0 = [P2_0 ; W2_0(:)];
% Define the linear constraint (C2)
A = zeros(Np, Ns + Ms * Ns);
for l = 1:Np
A(l, 1:Ns) = xi’; % Place xi’ in the first Ns columns of each row
end
% Define the bounds(C1)
lb = [zeros(Ns, 1); -Inf(Ms * Ns, 1)]; % Lower bound (P2 >= 0, no lower bound for W2)
ub = [repmat(tilde_P2 / Ns, Ns, 1); Inf(Ms * Ns, 1)]; % Upper bound (P2 <= tilde_P2_max / Ns, no upper bound for W2)
%%%objective function
obj_fun = @(x) -((Nup – Nt) / Nup * sum(log(1 + x(1:Ns) ./ (sigma2 * reshape(x(Ns+1:end), Ms, Ns).^2))));
% Set options for fmincon
options = optimoptions(‘fmincon’, ‘Display’, ‘iter’, ‘Algorithm’, ‘interior-point’);
% Run the optimization
[x_opt, fval] = fmincon(obj_fun, x0, A, hat_zeta1, [], [], lb, ub, [], options);
% Extract optimized values for P2 and W2
P2_opt = x_opt(1:Ns);
W2_opt = reshape(x_opt(Ns+1:end), Ms, Ns); fmincon, error MATLAB Answers — New Questions