fmincon:Supplied objective function must return a scalar value.

%constraints c and ceq
for h=linspace(h_min,h_max,69);
x=zeros(N,1);
c=h-x(1)'*D*x(2);
end
function [c,ceq]= contr (x)
ceq=[];
c=c;
end
%function to optimize
[T,N]=size(RR)
629 69
f=@(x)x(1)'*sigma*x(2);
nonlcon=@contr;
Aeq=ones(1,N);
beq=1;
l_b=[];
u_b=[];
x0=ones(N,1);
A=[];
b=[];
sigma_2rao = zeros(1,length(h));
sigma_2rao(1)=var_min;
x_rao=zeros(N,length(h));
for h=linspace(h_min,h_max,69)
[x_rao(:,h),sigma_2rao(h)] = fmincon(f,x0,A,b,Aeq,beq,l_b,u_b,nonlcon);
end

4 Kommentare

You do not tell us anything about the size of the sigma that is being used in f
You are repeating the same fmincon every iteration of the for loop.
The c you use inside of contr does not appear to be connected to the c you assign in the code at the top. Also, if you were trying to use nested functions with shared variables, then because c is an output variable for contr then inside the function, c would only refer to the output variable, not to any shared variable.
size(sigma)
69 69
N=69
%the problem to optimize is:
min x'*sigma*x
s.t h-x'*D*x 0
0 x 1
%with
h_min=0.639064969481681
h_max=0.968289685083315
h=linspace(h_min,h_max,69)
So for each h, it should be a couple of x. so x_rao will be a matrix 69x69?
f = @(x)x*sigma*x';
x0 = ones(1,N);
I changed the codes like you said.
Now I have this error:
Unrecognized function or variable 'contr'.
Error in fmincon (line 649)
[ctmp,ceqtmp] =
feval(confcn{3},X,varargin{:});
Error in funzionerao_14 (line 15)
[x_rao(:,h),sigma_2rao(h)] =
fmincon(f,x0,A,b,Aeq,beq,l_b,u_b,nonlcon);

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Gefragt:

am 15 Nov. 2020

Kommentiert:

am 15 Nov. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by