Error using fmincon Supplied objective function must return a scalar value

C1=log(1+(h(1,1).*P(1,1)/n+x1));
C2=log(1+(h(2,2).*P(2,1)/n+x2));
C3=log(1+(h(3,3).*P(3,1)/n+x3));
C4=log(1+(h(4,4).*P(4,1)/n+x4));
C=[C1,C2,C3,C4];
M=4;
w=linspace(0,1,M);
C_sum= C.*w;
Pimax(1:4)= 10^-4;
x0 = ones(64,1);
c_lin = [H',zeros(1,48);zeros(1,16),H',zeros(1,32);zeros(1,32),H',zeros(1,16);zeros(1,48),H'];
b = Pimax;
X = fmincon(@myfunc,x0,c_lin,b);
function C_sum1=myfunc(C_sum)
C_sum1=C_sum;
end

2 个评论

clc;
clear all;
close all;
x1=0;
x2=0;
x3=0;
x4=0;
n=10^-9; %noise
N= 16; % number of channels
h_rayleigh=(1/sqrt(2)).*(randn(N,1)+1i*randn(N,1));
h_abs= abs(h_rayleigh);
H= h_abs;
h=reshape(h_abs,4,4);
syms A [4 16] matrix
P= A*H;
i=1;
for j=1:1:4
if(j~=i)
x1= x1+h(j,i).*P(j,1);
else
x1=x1;
end
end
i=2;
for j=1:1:4
if(j~=i)
x2= x2+h(j,i).*P(j,1);
else
x2=x2;
end
end
i=3;
for j=1:1:4
if(j~=i)
x3= x3+h(j,i).*P(j,1);
else
x3=x3;
end
end
i=4;
for j=1:1:4
if(j~=i)
x4= x4+h(j,i).*P(j,1);
else
x4=x4;
end
end
X=[x1,x2,x3,x4];
size(X);
C1=log(1+(h(1,1).*P(1,1)/n+x1));
C2=log(1+(h(2,2).*P(2,1)/n+x2));
C3=log(1+(h(3,3).*P(3,1)/n+x3));
C4=log(1+(h(4,4).*P(4,1)/n+x4));
C=[C1 C2 C3 C4];
M=4;
w=linspace(0,1,M);
C_sum= C.*w;
Pimax(1:4)= 10^-4;
x0 = ones(64,1);
c_lin = [H',zeros(1,48); zeros(1,16), H',zeros(1,32);zeros(1,32),H',zeros(1,16);zeros(1,48),H'];
b = Pimax;
X = fmincon(@myfunc,x0,c_lin,b);
function C_sum1=myfunc(C_sum)
C_sum1=C_sum;
end
The above is the full code.. please help
thanks in advance

请先登录,再进行评论。

 采纳的回答

The error message says that your objective function "myfunc()" must return a scalar value. Perhaps the trapz() function might work. I'm unfamiliar with the physics of Rayleigh scattering. So, you need to check if the proposed objective function is acceptable.
x1 = 0;
x2 = 0;
x3 = 0;
x4 = 0;
n = 1e-9; % noise
N = 16; % number of channels
h_rayleigh = (1/sqrt(2)).*(randn(N,1) + 1i*randn(N,1));
h_abs = abs(h_rayleigh);
H = h_abs;
h = reshape(h_abs, 4, 4);
syms A [4 16] matrix
P = A*H;
i = 1;
for j = 1:1:4
if(j ~= i)
x1 = x1 + h(j,i).*P(j,1);
else
x1 = x1;
end
end
i = 2;
for j = 1:1:4
if(j ~= i)
x2 = x2 + h(j,i).*P(j,1);
else
x2 = x2;
end
end
i = 3;
for j = 1:1:4
if(j ~= i)
x3 = x3 + h(j,i).*P(j,1);
else
x3 = x3;
end
end
i = 4;
for j = 1:1:4
if(j ~= i)
x4 = x4 + h(j,i).*P(j,1);
else
x4 = x4;
end
end
X = [x1, x2, x3, x4];
size(X);
C1 = log(1 + (h(1,1).*P(1,1)/n + x1));
C2 = log(1 + (h(2,2).*P(2,1)/n + x2));
C3 = log(1 + (h(3,3).*P(3,1)/n + x3));
C4 = log(1 + (h(4,4).*P(4,1)/n + x4));
C = [C1 C2 C3 C4];
M = 4;
w = linspace(0, 1, M);
C_sum = C.*w;
Pimax(1:4) = 10^-4;
x0 = ones(64,1);
c_lin = [H', zeros(1,48); zeros(1,16), H', zeros(1,32); zeros(1,32), H', zeros(1,16); zeros(1,48), H'];
b = Pimax;
options = optimoptions(@fmincon, 'MaxFunctionEvaluations', 3e6);
X = fmincon(@myfunc, x0, c_lin, b, [], [], [], [], [], options);
Problem appears unbounded. fmincon stopped because the objective function value is less than the value of the objective function limit and constraints are satisfied to within the value of the constraint tolerance.
function C_sum1 = myfunc(C_sum)
C_sum1 = trapz(C_sum);
end

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by