标题:matlab中fmincon命令中的函数fun要求有变量传递怎么办?
取消只看楼主
rongxianger
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-10-24
 问题点数:0 回复次数:1 
matlab中fmincon命令中的函数fun要求有变量传递怎么办?
问题:
一般函数的调用中,如:
function y=f12(x,a,b)
y=5*x+a+b;
调用时我只要写f12(x,3,4)就表示a=3,b=4;
但是在优化工具箱中的,fmincon命令,x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon);
若函数fun也要有参数传递怎么办啊????????例如:
A=[1,1,0,0;
   0,0,1,1];
b=[10;20];
lb=zeros(4,1);
x0=[1;1;1;1];
x1=[1;1;1;1];
y=fmincon('(1/2)*dot(x1-y,x1-y)',x0,A,b,[],[],lb,[]),这里x1是参加循环的一个变量,在循环中它的值是不断变化的,现在我给它赋一个值,它运行都有错误,循环中肯定运行不了.显示错误如下:
Warning: Large-scale (trust region) method does not currently solve this type of problem,
switching to medium-scale (line search).
> In C:\MATLAB6p5p1\toolbox\optim\fmincon.m at line 213
??? Error using ==> fmincon
FMINCON cannot continue because user supplied objective function
 failed with the following error:

Error using ==> inlineeval
Error in inline expression ==> (1/2)*dot(x0-y,x0-y)
??? Undefined function or variable 'x0'.
请大家帮帮忙!实在不知道怎么办啦!
搜索更多相关主题的帖子: matlab fun 变量 fmincon 函数 
2008-10-24 21:43
rongxianger
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-10-24
得分:0 
fun也可以直接写成函数的形式,
例如:b=[10;20];
lb=zeros(4,1);
x0=[1;1;1;1];
y=fmincon('(1/2)*x(1)^2+x(2)+x(3)+x(4)',x0,A,b,[],[],lb,[])
Warning: Large-scale (trust region) method does not currently solve this type of problem,
switching to medium-scale (line search).
> In C:\MATLAB6p5p1\toolbox\optim\fmincon.m at line 213
Optimization terminated successfully:
 First-order optimality measure less than options.TolFun and
  maximum constraint violation is less than options.TolCon
Active inequalities (to within options.TolCon = 1e-006):
  lower      upper     ineqlin   ineqnonlin
    1                                 
    2                                 
    3                                 
    4                                 

y =

     0
     0
     0
     0
2008-10-25 15:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-240401-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.177494 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved