| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6544 人关注过本帖
标题:matlab中fmincon命令中的函数fun要求有变量传递怎么办?
只看楼主 加入收藏
rongxianger
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-10-24
收藏
 问题点数:0 回复次数:2 
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
meteora1106
Rank: 2
等 级:新手上路
威 望:5
帖 子:126
专家分:0
注 册:2008-9-15
收藏
得分:0 
这个fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon);中的fun是要单独建立一个函数文件的,或者写成子函数也行。
2008-10-24 23:14
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
快速回复:matlab中fmincon命令中的函数fun要求有变量传递怎么办?
数据加载中...
 
   



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

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