使用 fmincon() 求解矩阵形式的非线性程序

Solving a nonlinear program in matrix form using fmincon()

我想在matlab中求解这个非线性程序

min J=X' EX + U' EU
subject to X'-X0'-G'Z-X'DP+X'DZ-U'P=0

其中 X 和 U 是未知的 2*r+1 by 1 矩阵,其他矩阵是已知的给定矩阵。

如何使用 fmincon() 找到矩阵 X 和 U?

更新

Known matrices: X0, G, Z, D, P

    r=2;
     % X0
    X0(1)=1;
    for i=2:2*r+1
        X0(i)=0;
    end
    X0=X0';

    % P
    P1=[1/2];
    P2=zeros(1,r);
    for i=1:r
        P3(i)=(-1)/(i*pi);
    end
    P4=zeros(r,1);
    P5=zeros(r,r);
    for i=1:r
        V1(i)=1/(2*i*pi);
    end
    P6=diag(V1);
    for i=1:r
        W(i)=1/(2*i*pi);
    end
    P7=W';
    for i=1:r
        V2(i)=(-1)/(2*i*pi);
    end
    P8=diag(V2);
    P9=zeros(r,r);
    P=[P1 P2 P3 ; P4 P5 P6 ; P7 P8 P9];
    
    % D
    D1=[1];
    D2=zeros(1,r);
    D3=zeros(1,r);
    D4=zeros(r,1);
    for i=1:r
        V4(i)=cos((2*i*pi)/2);
    end
    D5=diag(V4);
    for i=1:r
        V5(i)=sin((2*i*pi)/2);
    end
    D6=diag(V5);
    D7=zeros(r,1);
    for i=1:r
        V6(i)=-sin((2*i*pi)/2);
    end
    D8=diag(V6);
    for i=1:r
        V7(i)=cos((2*i*pi)/2);
    end
    D9=diag(V7);
    D=[D1 D2 D3 ; D4 D5 D6 ; D7 D8 D9];

    % G
    G(1)=1;
    for i=2:2*r+1
        G(i)=0;
    end
    G=G';

    % Z
    Z1=[1];
    Z2=zeros(1,2*r);
    for i=1:r
        Z3(i)=(1/(i*pi))*sin(i*pi);
    end
    Z3=Z3';
    Z4=zeros(r,2*r);
    for i=1:r
        Z5(i)=(1/(i*pi))*(1-cos(i*pi));
    end
    Z5=Z5';
    Z6=zeros(r,2*r);
    Z=[Z1 Z2 ; Z3 Z4 ;Z5 Z6];
    V3(1)=2;
    for i=2:2*r+1
        V3(i)=1;
    end

    % E
    E=diag(V3);

提前致谢。

主要思想

将决策变量定义为长度的列向量decision_variable 4*r + 2

  • decision_variable中第2*r + 1个元素一共代表X
  • decision_variable 中的下一个 2*r + 1 元素从 2*r + 2 开始 索引一共代表U

说明代码

% Given data 
r = 
X0 =  
E =  
C =
G = 
Z = 
D 
P  = 
% default fmincon setting 
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub =[];

% Starting guess solution to be optimized 
initial = ones(4*r + 2, 1);


%% All function should have a unique vector as input
% Objective function
J =@(decision_variable)objective_function(decision_variable, r, E);

% Constraint
equality = @(decision_variable)constraint(decision_variable, r, X0, C, G, Z, P, D);



solution = fmincon(J,initial,A,b,Aeq,beq,lb,ub,equality);
 
% X and U extraction 
 X_sol = solution(1:2*r+1);
 U_sol = solution(2*r+2:end);

function objective = objective_function(input, r, E)
% first r elements are X columns, followed by U  
X = input(1:2*r+1);
U = input(2*r+2:end);
objective = (X')*E*X + (U')*E*U;
end

function [inequality, equality] = constraint(input, r, X0, C, G, Z, P, D)

X = input(1:2*r+1);
U = input(2*r+2:end);
% No inequality constraint 
inequality = [];
equality = X'- X0'-(G')*Z - (X')*D*P + (X')*D*Z - (U')*P;

end

更新

OP face function definition not allow in script file error



Ojective function file name: objective_function.m

function objective = objective_function(input,r, E)
% first r elements are X columns, followed by U  
X = input(1:2*r+1);
U = input(2*r+2:end);
objective = (X')*E*X + (U')*E*U;
end

Constraint function file name: constraint.m

function [inequality, equality] = constraint(input, r,  X0, G, Z, P, D)

X = input(1:2*r+1);
U = input(2*r+2:end);
% No inequality constraint 
inequality = [];
equality = X'- X0'-(G')*Z - (X')*D*P + (X')*D*Z - (U')*P;

end

Optimization file name: main.m

    clear all
    clc
    r=2;
    X0(1)=1;
    for i=2:2*r+1
        X0(i)=0;
    end
    X0=X0';
    P1=[1/2];
    P2=zeros(1,r);
    for i=1:r
        P3(i)=(-1)/(i*pi);
    end
    P4=zeros(r,1);
    P5=zeros(r,r);
    for i=1:r
        V1(i)=1/(2*i*pi);
    end
    P6=diag(V1);
    for i=1:r
        W(i)=1/(2*i*pi);
    end
    P7=W';
    for i=1:r
        V2(i)=(-1)/(2*i*pi);
    end
    P8=diag(V2);
    P9=zeros(r,r);
    P=[P1 P2 P3 ; P4 P5 P6 ; P7 P8 P9];
    D1=[1];
    D2=zeros(1,r);
    D3=zeros(1,r);
    D4=zeros(r,1);
    for i=1:r
        V4(i)=cos((2*i*pi)/2);
    end
    D5=diag(V4);
    for i=1:r
        V5(i)=sin((2*i*pi)/2);
    end
    D6=diag(V5);
    D7=zeros(r,1);
    for i=1:r
        V6(i)=-sin((2*i*pi)/2);
    end
    D8=diag(V6);
    for i=1:r
        V7(i)=cos((2*i*pi)/2);
    end
    D9=diag(V7);
    D=[D1 D2 D3 ; D4 D5 D6 ; D7 D8 D9];
    G(1)=1;
    for i=2:2*r+1
        G(i)=0;
    end
    G=G';
    Z1=[1];
    Z2=zeros(1,2*r);
    for i=1:r
        Z3(i)=(1/(i*pi))*sin(i*pi);
    end
    Z3=Z3';
    Z4=zeros(r,2*r);
    for i=1:r
        Z5(i)=(1/(i*pi))*(1-cos(i*pi));
    end
    Z5=Z5';
    Z6=zeros(r,2*r);
    Z=[Z1 Z2 ; Z3 Z4 ;Z5 Z6];
    V3(1)=2;
    for i=2:2*r+1
        V3(i)=1;
    end
    E=diag(V3);

    DP=D*P;
    DZ=D*Z;
     A=[];
    b=[];
    Aeq=[];
    beq=[];
    lb=[];
    ub=[];

    initial=ones(4*r+2,1);
   %% All function should have a unique vector as input
% Objective function
J =@(decision_variable)objective_function(decision_variable, r, E);

% Constraint
equality = @(decision_variable)constraint(decision_variable, r, X0, G, Z, P, D);



solution = fmincon(J,initial,A,b,Aeq,beq,lb,ub,equality);

% X and U extraction 
 X_sol = solution(1:2*r+1);
 U_sol = solution(2*r + 2:end);

Result

X_sol =

   1.112801908938543
  -0.005818430474721
   0.019059770457710
  -0.270668041221971
  -0.129238710353755


U_sol =

  -0.290486339639219
  -0.061844751604623
   0.001509836114210
  -0.234109764352394
  -0.110273429042447

NB: All 3 files should be located in same directory