以下为求解非线性规划的代码。为什么目标函数1执行成功,目标函数2会执行错误?目标函数2应该如何修改?
%主程序
clc
options=optimset;
[x,y]=fmincon('f',rand(4,1),[],[],[],[],zeros(4,1),[],'f2',options)
%非线性约束
function [g,h]=f2(x)
    g(1)=x(1)-400;
    g(2)=1.1*x(1)+x(2)-440;
    g(3)=1.21*x(1)+1.1*x(2)+x(3)-484;
    g(4)=1.331*x(1)+1.21*x(2)+1.1*x(3)+x(4)-532.4;
    h=0;
end
%目标函数2(执行错误)
function z=f(x)
for i=1:4
    z=z+(x(i)^0.5*(-1));
end
%目标函数1(执行正确)
function z=f(x)
z=-(sqrt(x(1))+sqrt(x(2))+sqrt(x(3))+sqrt(x(4)));

 Akzeptierte Antwort

nqfyncr
nqfyncr am 22 Mai 2023

0 Stimmen

要写成
z = 0; % 先赋值
for i=1:4
    z=z+(x(i)^0.5*(-1)); % 才能调用
end

Weitere Antworten (0)

Kategorien

Mehr zu 语言基础知识 finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!