gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:57:14 26 4
gpt4 key购买 nike

我想在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

  • 第一个2*r + 1 decision_variable 中的元素共代表X
  • 下一篇 2*r + 1 decision_variable 中的元素从 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


  • Including functions in scripts requires MATLAB R2016b or later.
  • 或者在 2 个单独的 m 文件中定义两个函数中的每一个,
    然后第三个(main.m)用于优化。
  • 运行 main.m 文件

  • 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

    关于matlab - 使用 fmincon() 以矩阵形式求解非线性程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59098942/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com