matlab的非线性优化问题,我自己编了一下,有问题,我把我的程序发上来,希望大虾帮我看下,谢了

%2jiyouhua.m
vlb=[0.25;18;1.5];%x1,x2,x3的下限值
vub=[1;100;6];%x1,x2,x3的上限值
x0=[0.9;50;2.5];%x1,x2,x3的初始值

[x,fval,exitflag,output,lambda,grad,hessian] = fmincon('funn',x0,[],[],[],[],vlb,vub,'nonlcon')

%目标函数
function y=funn(x)
y=x(2)*x(3)+(x(2)*x(3))/x(1)

%约束条件
function[c,ceq]=nonlcon(x)
c(1)=(863.4*x(1))/x(2)-x(3).^3;
c(2)=x(2)+x(2)/x(1)-120;
ceq=[];

运行时出现了问题,显示是
Warning: Large-scale (trust region) method does not currently solve this type of problem,
switching to medium-scale (line search).
> In fmincon at 274
??? Error using ==> fmincon
FMINCON cannot continue because user supplied objective function failed with the following error:
Error using ==> feval
Undefined command/function 'funn'.

我也不知道为什么,该怎么办

%约束条件
function[c,ceq]=nonlcon(x)
c(1)=(863.4*x(1))/x(2)-x(3).^3;
c(2)=x(2)+x(2)/x(1)-120;
ceq=[];
---------------
>> funn=@(x)x(2)*x(3)+(x(2)*x(3))/x(1);
>> vlb=[0.25;18;1.5];%x1,x2,x3的下限值
vub=[1;100;6];%x1,x2,x3的上限值
x0=[0.9;50;2.5];%x1,x2,x3的初始值
[x,fval,exitflagn] = fmincon(funn,x0,[],[],[],[],vlb,vub,@nonlcon)
Warning: Trust-region-reflective algorithm does not solve this type of problem,
using active-set algorithm. You could also try the interior-point or sqp
algorithms: set the Algorithm option to 'interior-point' or 'sqp' and rerun.
For more help, see Choosing the Algorithm in the documentation.
> In fmincon at 472

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in
feasible directions, to within the default value of the function tolerance,
and constraints were satisfied to within the default value of the constraint tolerance.

<stopping criteria details>

Active inequalities (to within options.TolCon = 1e-006):
lower upper ineqlin ineqnonlin
2 1 1

x =

1.0000
18.0000
3.6334

fval =

130.8024

exitflagn =

1

>>追问

1.为什么我已经写了funn函数了,会出现Undefined command/function 'funn',而你这样写就能用啊
2.我自己的程序有些问题,就是约束条件里x1是只能小于1的,不能等于1,但我又不知道怎么修改,而你的优化出来的结果刚好又是等于1,这不是我想要的,能否再帮我看下

追答

我这么跟你说吧,整个计算其实和x1关系不大,你只要取一个你在精度范围类接近于1就可以了,x1是做分母的,当然允许时越大越好

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-07
换用这个函数进行求解:
[x,fval,attainfactor,exitflag,output]=fgoalattain(@goalfunction,x0,goal,weight,A,b,Aeq,beq,lb,ub,@nonlcon);