24小时热门版块排行榜    

CyRhmU.jpeg
查看: 644  |  回复: 1

Justiceall

新虫 (初入文坛)

[求助] 关于matlab的一个非线性约束求解的问题

具体如下: 目标方程:y=(x1-2.0).^2.0+(x2-1.0).^2.0;
                  约束条件:x1-2.0x2+1>=0;
                                   ((x1).^2.0)/4.0-(x2).^2.0+1>=0
                  目标方程M文件fitness:function [ y ] = fitness( x )
                                            y=(x1-2.0).^2.0+(x2-1.0).^2.0
                                            end
                 非线性约束条件M文件nonl:function [ c,ce ] = nonl( x )
                                                      ce=[];
                                                      c=-((x1).^2.0)/4.0+(x2).^2.0-1.0;
                                                      end
                 主程序:A=[-1,2];
                               b=1;
                               [x,fval]=ga(@fitness,2,A,b,[],[],[],[],@nonl)
结果运行错误了,修改了好多次还是不行啊。具体错误如下:
Undefined function or variable 'x1'.

Error in nonl (line 5)
cineq(1)=-((x1).^2.0)/4.0+(x2).^2.0-1.0;

Error in validate>@(x)nonlcon(x,NonconFcnArgs{:}) (line 142)
    nonlcon = @(x) nonlcon(x,NonconFcnArgs{:});

Error in constrValidate (line 20)
        [cineq,ceq] = nonlcon(Iterate.x');

Error in gacommon (line 132)
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...

Error in ga (line 322)
[x,fval,exitFlag,output,population,scores,FitnessFcn,nvars,Aineq,bineq,Aeq,beq,lb,ub, ...

Caused by:
    Failure in initial user-supplied nonlinear constraint function evaluation.
请各位大侠帮忙看看到底是怎么回事啊,因为刚刚注册的木虫只有这么多金币了,谢谢各位好心人。
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dbb627

荣誉版主 (著名写手)

【答案】应助回帖

★ ★ ★ ★ ★
感谢参与,应助指数 +1
Justiceall: 金币+5, ★★★很有帮助, 谢谢这位仁兄 2012-04-18 18:12:12
CODE:
function aa
[x,fval]=ga(@fitness,2,[],[],[],[],[],[],@nonl)

function y= fitness( x )
y=(x(1)-2.0).^2.0+(x(2)-1.0).^2.0;
                                       
function [ c,ce] = nonl( x )
     c(1)=-((x(1)).^2.0)/4.0+(x(2)).^2.0-1.0;
     c(2)=-(x(1)-2.0*x(2)+1);
      ce=[];                              
            

代码拷到editor中保存为m文件,点击run运行
x =

    2.0025    0.9970


fval =

  1.5006e-005
The more you learn, the more you know, the more you know, and the more you forget. The more you forget, the less you know. So why bother to learn.
2楼2012-04-18 10:56:47
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 Justiceall 的主题更新
信息提示
请填处理意见