is it possible to optimize a function that have a matrix output with fmincon ?

4 views (last 30 days)
I want to minimize a function which the output is a matrix with an equality constraint. Matlab displays the following error message:
Error using fmincon (line 289) Aeq must have 440 column(s).
Error in objective2 (line 52) x = fmincon(fun,x0,A,b,Aeq,beq)
Below I write down my the example to solve:
A1=[-3 0;
0 -4];
B=[2;
1];
C=[1 0;
0.9 1;];
Ed=[0.7 0.7;
0.3 0.3];
Ef=[1;
0];
x0=zeros(22,20);
Hf=[zeros(2,11);
C*Ef zeros(2,10);
C*A1*Ef C*Ef zeros(2,9);
C*A1^2*Ef C*A1*Ef C*Ef zeros(2,8);
C*A1^3*Ef C*A1^2*Ef C*A1*Ef C*Ef zeros(2,7);
C*A1^4*Ef C*A1^3*Ef C*A1^2*Ef C*A1*Ef C*Ef zeros(2,6);
C*A1^5*Ef C*A1^4*Ef C*A1^3*Ef C*A1^2*Ef C*A1*Ef C*Ef zeros(2,5);
C*A1^6*Ef C*A1^5*Ef C*A1^4*Ef C*A1^3*Ef C*A1^2*Ef C*A1*Ef C*Ef zeros(2,4);
C*A1^7*Ef C*A1^6*Ef C*A1^5*Ef C*A1^4*Ef C*A1^3*Ef C*A1^2*Ef C*A1*Ef C*Ef zeros(2,3);
C*A1^8*Ef C*A1^7*Ef C*A1^6*Ef C*A1^5*Ef C*A1^4*Ef C*A1^3*Ef C*A1^2*Ef C*A1*Ef C*Ef zeros(2,2);
C*A1^9*Ef C*A1^8*Ef C*A1^7*Ef C*A1^6*Ef C*A1^5*Ef C*A1^4*Ef C*A1^3*Ef C*A1^2*Ef C*A1*Ef C*Ef zeros(2,1);
];
Hd=[zeros(2,22);
C*Ed zeros(2,20);
C*A1*Ed C*Ed zeros(2,18);
C*A1^2*Ed C*A1*Ed C*Ed zeros(2,16);
C*A1^3*Ed C*A1^2*Ed C*A1*Ed C*Ed zeros(2,14);
C*A1^4*Ed C*A1^3*Ed C*A1^2*Ed C*A1*Ed C*Ed zeros(2,12);
C*A1^5*Ed C*A1^4*Ed C*A1^3*Ed C*A1^2*Ed C*A1*Ed C*Ed zeros(2,10);
C*A1^6*Ed C*A1^5*Ed C*A1^4*Ed C*A1^3*Ed C*A1^2*Ed C*A1*Ed C*Ed zeros(2,8);
C*A1^7*Ed C*A1^6*Ed C*A1^5*Ed C*A1^4*Ed C*A1^3*Ed C*A1^2*Ed C*A1*Ed C*Ed zeros(2,6);
C*A1^8*Ed C*A1^7*Ed C*A1^6*Ed C*A1^5*Ed C*A1^4*Ed C*A1^3*Ed C*A1^2*Ed C*A1*Ed C*Ed zeros(2,4);
C*A1^9*Ed C*A1^8*Ed C*A1^7*Ed C*A1^6*Ed C*A1^5*Ed C*A1^4*Ed C*A1^3*Ed C*A1^2*Ed C*A1*Ed C*Ed zeros(2,2);
];
Ho=[C;C*A1;C*A1^2;C*A1^3;C*A1^4;C*A1^5;C*A1^6;C*A1^7;C*A1^8;C*A1^9;C*A1^10];
%--------------------------------------------------------------------------
fun = @(x)(x*Hd*Hd'*x')/(x*Hf*Hf'*x');
A = [];
b = [];
Aeq = Ho';
beq = 0;
x = fmincon(fun,x0,A,b,Aeq,beq)
x
  3 Comments
Marya
Marya on 25 Jul 2016
Edited: Walter Roberson on 25 Jul 2016
Yes, the variable to optimise is a matrix of size (20x22), so we have 440 unknowns. But here we have the following constaint: x.Ho=0 (x and Ho are orthogonal) or in fmincon the equality constraint must be in the form Aeq.x=beq, I apply the transpose for (x.Ho=0)' ,we obtain Ho'.x'=0 and we return to the form Aeq.y=beq with Aeq=Ho' y=x' .
we find enclosed the code :
A1=[-3 0;
0 -4];
B=[2;
1];
C=[1 0;
0.9 1;];
Ed=[0.7 0.7;
0.3 0.3];
Ef=[1;
0];
x0=zeros(22,20);
Hf=[zeros(2,11);
C*Ef zeros(2,10);
C*A1*Ef C*Ef zeros(2,9);
C*A1^2*Ef C*A1*Ef C*Ef zeros(2,8);
C*A1^3*Ef C*A1^2*Ef C*A1*Ef C*Ef zeros(2,7);
C*A1^4*Ef C*A1^3*Ef C*A1^2*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Ho=[C;C*A1;C*A1^2;C*A1^3;C*A1^4;C*A1^5;C*A1^6;C*A1^7;C*A1^8;C*A1^9;C*A1^10];
%----------------------------------
fun = @(x)(x*Hd*Hd'*x')/(x*Hf*Hf'*x');
A = [];
b = [];
Aeq = Ho';
beq = zeros(2,20);
x = fmincon(fun,x0,A,b,Aeq,beq)
x
Torsten
Torsten on 25 Jul 2016
Edited: Torsten on 25 Jul 2016
x must be defined as a vector, not as a matrix.
How else can you expect that "fun" makes sense for x being a matrix ?
Best wishes
Torsten.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!