matlab程序问题(优化的问题Error: Function definitions are not permitted at the prompt or in scripts

来源:百度知道 编辑:UC知道 时间:2024/05/22 03:51:32
m文件如下
function f =objfun(x)
f =0.1*(3600*x(1)+3600*x(2)+3600*x(3)+3600*x(4)+3600*sqrt(2)*x(5)+3600*sqrt(2)*x(6)+3600*sqrt(2)*x(7)+3600*sqrt(2)*x(8)+3600*x(9)+3600*x(10));
%约束文件
function [c, ceq] = confun(x)
% Nonlinear inequality constraints
c = [195360/(x(1))-2.5e9;
35490/(x(2))-2.5e9;
59875/(x(3))-2.5e9;
84677/(x(4))-2.5e9;
40125/(x(5))-2.5e9;
40125/(x(6))-2.5e9;
56745/(x(7))-2.5e9;
204640/(x(8))-2.5e9;
134870/(x(9))-2.5e9;
147980/(x(10))-2.5e9;
x(1)-12.5;
11.5-x(1);
x(2)-9;
8-x(2);
x(3)-1;
0.1-x(3);
x(4)-6.5;
5.5-x(4);
x(5)-6;
5.5-x(5);
x(6)-9;
8-x(6);
x(7)-9;
8-x(7);
x(8)-1;
0.1-x(8);
x(9)-1;
0.1-x(9);
x(10)-1;
0.1-x(10);];
% Nonlinear equality constraints
c

错误是脚本文件里不能定义函数,更改如下:
%求解文件
function solvefile
x0 = [0,0,0,0,0,0,0,0,0,1]; % Make a starting guess at the solution
options = optimset('LargeScale','off');
[x, fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options)
function f =objfun(x)
f =0.1*(3600*x(1)+3600*x(2)+3600*x(3)+3600*x(4)+3600*sqrt(2)*x(5)+3600*sqrt(2)*x(6)+3600*sqrt(2)*x(7)+3600*sqrt(2)*x(8)+3600*x(9)+3600*x(10));
%约束文件
function [c, ceq] = confun(x)
% Nonlinear inequality constraints
c = [195360/(x(1))-2.5e9;
35490/(x(2))-2.5e9;
59875/(x(3))-2.5e9;
84677/(x(4))-2.5e9;
40125/(x(5))-2.5e9;
40125/(x(6))-2.5e9;
56745/(x(7))-2.5e9;
204640/(x(8))-2.5e9;
134870/(x(9))-2.5e9;
147980/(x(10))-2.5e9;
x(1)-12.5;
11.5-x(1);
x(2)-9;
8-x(2);
x(3)-1;
0.1-x(3);
x(4)-6.5;
5.5-x(4);