MATLAB Answers

Damonyr
0

How can I create a handle-function with a double summation on 201 variables (40401 terms)?

Damonyr
さんによって質問されました 2019 年 6 月 11 日
最新アクティビティ Damonyr
さんによって コメントされました 2019 年 6 月 11 日
Hello everyone!
I have to use Lagrangian multipliers with two linear constrains to minimize the function
where W represent all 201 variables that I want to determine, C are correlation coefficients between two vectors and sigma are standard deviations of the vectors (obviously I have 201 vectors).
To use Lagrangian multipliers, I use the function fmincon.
The problem is that I don't know how create the function .
I think the input function should be a handle-function (and fmincon works with a handle-function), but I don't know how create a so complicated handle funcion.
I used
W = sym('W',[1 201]);
to create my symbolic variables.
Then, I tried to use several methods to create , like
FUN=@(W) symsum(symsum(W('i')*W('j')*S('i','j')*std(L(:,'i'))*std(L(:,'j')), j, 1, 201), i, 1, 201);
When I try to minimize this FUN with fmincon, this error appears:
"Undefined function 'symsum' for input arguments of type 'double'."
Another very complex attempt consists of creating an ausiliar array in which write every term of summations,
FUNau =cell(201*201, 1);
nit=0;
for i=1:201
for j=1:201
nit=nit+1;
FUNau(nit,1) = (W(i)*W(j)*S(i,j)*std(L(:,i))*std(L(:,j)));
end
end
% high computation time
then I tried several methods to compute the right FUN, like
FUN =@(W) sum(FUNau);
or
FUN =@(W) symsum(FUNau);
but every attemp has failed when I used fmincon.
How can I create the function so that fmincon works?
Thanks.

  3 件のコメント

Torsten
2019 年 6 月 11 日
W and sigma are assumed as column vectors of size 201x1 and C is 201x201.
Damonyr
2019 年 6 月 11 日
Yes, I've just corrected and that was the point, but now, unfortunately, there is another error:
Error using fmincon (line 609)
Supplied objective function must return a scalar value.
I've already found it yesterday while I was making my attempts and I failed to solve it.
Do you have any idea how to solve it?
Damonyr
2019 年 6 月 11 日
Ok, I solved it. Thank you so much.

サインイン to comment.

1 件の回答

回答者: Torsten
2019 年 6 月 11 日
 採用された回答

FUN = @(W) (W.*sigma).'*C*(W.*sigma)

  1 件のコメント

Damonyr
2019 年 6 月 11 日
Hi Torsten,
Thanks for the reply.
I tried your function, but when I use fmincon this error appears:
"Error using *
Inner matrix dimensions must agree.
Error in @(W)(W.*sigma).'*S*(W.*sigma)
Error in fmincon (line 536)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue."
I thought you forgot a dot after C, but the error persists.
For completeness I write the code for fmincon
lb=zeros(1,201);
ub=ones(1,201);
Aeq=ones(2, 201);
Aeq(2,:)=Gain(1,:);
beq=[1; 0.05];
w0=zeros(1,201);
options = optimoptions('fmincon','Display','off');
[x,fval,exitflag,output,lambda] = fmincon(FUN,w0,[],[],Aeq,beq,lb,ub,[],options);

サインイン to comment.



Translated by