声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2213|回复: 5

[综合讨论] 请教一个使用fmincon求解非线性规划结合符号函数编程的问题

[复制链接]
发表于 2008-1-19 18:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
在matlab中使用fmincon函数作非线性优化理论推导时,由于做的是理论推导,中间有几个常数(也是别的地方的理论推导的公式,在这里可以看成不变量常数)打算用字母表示(希望求解推导的优化求解结果中仍然用这几个字母表示),这样我就打算把它们设置成符号常量(M, pe0, C),这样在用fmincon推导求解的结果表达式中也是含有字母的理论结果。可是matlab报错了,错误信息如下:
       ??? Error using ==> fmincon FMINCON cannot continue because user supplied objective function failed with the following error: Undefined function or variable 'M'. 我查了很多相关信息,也进行了尝试,都没有解决,请大家帮忙,谢谢! 相关代码如下:

主函数:
    clear,clc;
%syms M pe0 C;
%subs(M),subs(pe0),subs(C);
A = [-1 1 2;-1 1 0;-1 0 1];  
b = [0 0 0];
x0 = [7 4 1];  
lb = ones(3,1);
ub = 100.*lb;   
Aeq = [];
beq = [];
options=optimset('LargeScale','off');   

[x, fval] = fmincon(@myfun, x0, A, b, [], [], lb, ub, @mycon, options);
%[x, fval] = fmincon(@myfun, x0, A, b, [], [], lb, ub, @mycon, options, M, pe0, C);
%[x, fval] = fmincon(@myfun, x0, A, b, [], [], lb, ub, @mycon, options, pe0, C);
%[x, fval, exitflag, output, lambda] = fmincon(@opt_f, x0, A, b, [], [], lb, [], @opt_c);



两个调用函数:
function f = myfun(x, M)
%   M = inline(M);
%f = M^(-1)*x(1)*x(2)^(-1);
f = sym('M^(-1)*x(1)*x(2)^(-1)');

f = subs(f);
%f = inline(f);



function [c, ceq] = mycon(x, pe0, C)
%c(1) = pe0 - x(1)^(-1)*x(3);
%c(2) = -C + x(1)^(-1)*x(2);
c(1) = sym('pe0 - x(1)^(-1)*x(3)');
c(2) = sym('-C + x(1)^(-1)*x(2)');
%c(1) = subs(c(1));
%c(2) = subs(c(2));
c = [c(1) c(2)];
c = subs(c);
%c = inline(c);
ceq=[];


[ 本帖最后由 yanxuehu 于 2008-1-19 18:14 编辑 ]
回复
分享到:

使用道具 举报

发表于 2008-1-19 19:05 | 显示全部楼层
[x, fval] = fmincon(@myfun, x0, A, b, [], [], lb, ub, @mycon, options);
myfun光这样行吗
是不是要写成myfun(x,M)

@mycon这个你本来要返回2个变量,直接就这样的话,应该只返回默认的第一个变量
 楼主| 发表于 2008-1-21 14:37 | 显示全部楼层
试了,参数放上去[x, fval] = fmincon(@myfun(x, M), x0, A, b, [], [], lb, ub, @mycon(x, pe0, C), options, Mmax, pe0, C);就提示如下错误信息:
??? Error: File: opt.m Line: 23 Column: 27
Unbalanced or misused parentheses or brackets.

不放上去:[x, fval] = fmincon(@myfun, x0, A, b, [], [], lb, ub, @mycon, options);就提示如下错误信息:
??? Error using ==> fmincon
FMINCON cannot continue because user supplied objective function failed with the following error:
Input argument "M" is undefined.

[ 本帖最后由 yanxuehu 于 2008-1-21 14:40 编辑 ]
发表于 2008-1-21 17:57 | 显示全部楼层
1.请先讲一下M是什么.
2. 将问题的公式帖一下。
你的这个问题本身并不难解,只是你没有讲清楚问题。

评分

1

查看全部评分

发表于 2012-10-3 10:57 | 显示全部楼层
楼主,你的问题解决了吗?我也遇到了类似问题,我的myfunc里含有进行迭代得到的变量值,其结果每次都不一样呢,所以无法直接给出其具体数值。楼主怎么解决的呢?谢谢指点!
发表于 2012-10-19 10:03 | 显示全部楼层
myfunc里含有进行迭代得到的变量值,其结果每次都不一样
这句话是什么意思?能否详细说明一下
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 05:41 , Processed in 0.057737 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表