声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1822|回复: 6

[综合讨论] fimincon在做含多参数分段函数优化、积分和嵌套的复杂应用时报错

[复制链接]
发表于 2012-10-8 15:22 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 motepasserby 于 2012-10-8 15:47 编辑

各位大侠:
             在用fimincon在做含多参数分段函数优化、积分和嵌套的复杂应用时报错,麻烦各位大侠看看,
多谢!

1.程序简述:funquantum1是关于x的分段函数,同时含有k(1),k(2),k(3),k(4)四个参数需要确定,接着对x进行积分并计算cramer-von mises统计量最终得到一个关于k(1),k(2),k(3),k(4)的函数wcvm,进而通过最优化方法,确定使得函数wcvm最小的k(1),k(2),k(3),k(4)估计值。

2.程序代码:
--------------------------------------------------------------------------------------------------------------------------------------------------
function funalltest4tohelp2%函数主要是通过优化的方法得到k(1),k(2),k(3),k(4)的估计值.
    function fqx=funquantum1(x,k)%该函数是x的分段函数,且含有k(1),k(2),k(3),k(4)
    %syms x;
    fqx=((1./2.*k(1).^(-1./2)+1./4.*(k(2)+k(1)).*(k(3).^(-3./2))+1./2.*(k(2)+k(1)).*((k(2).^(-1./2)).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2))))./k(2)+...
    1./4.*(k(2)-k(1)).*(k(2).^(-3./2)).*sin(2.*(k(2).^(1./2)).*((k(2).^(-1./2)).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2)))))+1./2.*(k(1).^(1./2)).*(k(2).^(-1))...
    .*(1-cos(2.*(k(2).^(1./2)).*((k(2).^(-1./2)).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2))))))+1./4.*(k(1)-k(2)).*(k(3).^(-3./2)).*cos(2.*(k(2).^(1./2))...
    .*((k(2).^(-1./2)).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2)))))...
    -1./2.*((k(2).*k(1)).^(1./2)).*(k(3).^(-3./2)).*sin(2.*(k(2).^(1./2)).*((k(2).^(-1./2)).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2)))))).^(-1))...%M
    .*(exp(2.*(k(1).^(1./2)).*(x-k(4))).*(x<=k(4))+(1./2.*k(1).*(1-cos(2.*(k(2).^(1./2)).*(x-k(4))))./k(2)+1./2.*(1+cos(2.*(k(2).^(1./2)).*(x-k(4))))...
    +((k(1)./k(2)).^(1./2)).*sin(2.*(k(2).^(1./2)).*(x-k(4)))).*((x>k(4))&(x<=(k(4)...
    +(k(2).^(-1./2).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2)))))))...
    +(1./2.*k(2).*(1-cos(2.*(k(2).^(1./2)).*((k(2).^(-1./2)).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2))))))./k(3)...
    +1./2.*k(1).*(1+cos(2.*(k(2).^(1./2)).*((k(2).^(-1./2)).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2))))))./k(3)...
    -((k(1).*k(2)).^(1./2)).*sin(2.*(k(2).^(1./2)).*((k(2).^(-1./2)).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2)))))./k(3))...
    .*exp(-2.*(k(3).^(1./2)).*(x-(k(4)+(k(2).^(-1./2).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2))))))).*(x>(k(4)...
    +(k(2).^(-1./2).*atan(((k(2)./k(1)).^(1./2)).*(k(1)+(k(1).*k(3)).^(1./2))./(k(2)-(k(1).*k(3)).^(1./2)))))));
    end
shr=[0.0262921165536936,-0.0282095913259344,-0.00406052786892364,-0.0165677985850552,0.00909696533667326,-0.00956507143812324,-0.0112331445613503,-0.00259071889150775,-0.00613521028358299,0.00158949687334520,0.0299821976458539,0.00773447449728781,-0.00304301727373790,0.00370296290341532,-0.0235961601524215,-0.00251207861573199,-0.0195976439113545,-0.00606630895922145,0.0226394173034787,0.0164606227059787,0.00736387355417545,0.00334355628788341,0.0154005268270083,-0.00208836782593291,0.000716850263673408,0.0137071342123347,0.00158812530885122,0.00802167760091876,-0.00734011620112973,-0.00237751526134566,0.00838382562435935,-0.0181363199554214,0.000620890150615061,0.0190153420304906,0.000415145296929431,-0.0251378131890583,-0.00158620401168008,-0.0190553780856275,-0.000591925409847427,-0.00103567709391683,-0.00730915604842330,0.00103909987419915,-0.00725446222920390,0.00117986682453353,0.0122451701258859,-0.0332872403462829,0.0226479776199851,-0.0110374536536462,-0.0116876672643382,-0.00314221337965304,0.00292789521065284,-0.00124824414873537,-0.00626469996762502,-0.0103022307772571,-0.0189207275176244,-0.00896566510120955,-0.0216000523697817,0.0199473672791441,-0.00297091425519280,-0.00104190872653653,-0.0112456597789885,-0.00221590285876516,0.00841711337779305,-0.00667595980098490,-0.0109726740897562,0.00175267904892440,0.00163460047400754,0.0118268582663461,-0.0137476734923228,-0.00969937462481063,0.00693440951867341,0.0284757904023563,0.0265329992450827,-0.00424834059543233,-0.000457036591536841,-0.0134660326700145,-0.0172513831770056,0.0409374992583667,-0.0140206881551253,0.0130194818356291,0.00998448664799767,-0.0148038425154837,0.00330694070156579,-0.0107572526546056,0.0194214858821233,0.00768908506107824,0.000313200681819836,-0.0169762577714723,0.0239825540276835,0.000877133651772333,0.00101668218171813,-5.10221653149401e-05,-0.00301918967640002,0.00930926767428986,-0.00416635690045109,0.000135764709784603,0.00271989233886050,0.00751527026551901,0.00926230932715377,0.00247655606226971,0.0124063855033310,0.00304091676224960,0.00195961701585468,-0.00957725909382123,-0.000976391992220549,0.0141485307734426,-0.00639667810084266,-0.0142316767252950,-0.00651791043044270,0.0105876960064384,0.00789347063033397,-0.00188703339470120,0.00856331372914543,-0.0266446958333777,-0.00732846931493469,0.0129623810095501,0.00225964390466160,-0.0139295591351392,0.000572024646305458,-0.00102230355329702,-0.0111020311533183,0.000451050401890640,-0.00145600713706795,-0.0269011010237943,-0.0144237508447258,0.00470880946255203,0.0172839968593896,0.00187033961470462,-0.00904557887648050,0.00874638604446965,0.00133050494297127,0.0179970048981470,0.00352440481844507,-0.000903271558110319,-0.00939474346067293,0.0194499583237526,-0.000932875087613638,0.0117983017275396,-0.00761975972948914,0.000100472640128579,0.00749851291913206,-0.000877479449981955,-0.00349092984458732,0.0174242705214489,0.000672335353391951,0.00487727271014649,-2.44700301283984e-05,-0.00125284968236539,-0.0165892699623678,0.000680664869815217,-0.00634730117805216,-0.00596771925386186,-0.00247667650715400,-0.0121377571500645,0.0138412623263839,-0.0145528818578589,0.00161097200297444,0.0105939431291384,-0.00416784350997901,-0.00529017814414434,-0.00743687037354501,0.0118512458631272,0.0119003494274850,-0.00208197786789643,-0.00523031116462924,0.000509938759399198,-0.0277203364202503,0.00145829377799078,-0.00102564821730322,-0.00713500690662983,-0.00510649304958566,0.0106425077170860,-0.00699360155486595,0.0126414552316962,-0.00995486314766314,0.00473625841318981,0.00398019257421556,-0.00661060906706545,-0.00344387440184768,-0.0140545574650623,-0.0163972897417803,-0.000917642138102082,-0.00231583897861576,-0.00955824361410240,0.0133854941252727,0.000305512252070095,0.00138262316371751,-0.000843711482787946,-0.0117242250428689,0.0100477056446548,-0.0240181507724513,-0.00293870366090498,0.00504169675934740,0.00463669990143600,0.000187583440867556,-0.0175090966350746,0.00614044426870386,0.00365334074677204,0.00723026766556512,-0.00744235977519647,-0.0126404280893082,0.00242071691170498,-0.00487539545003290,-0.00476286566662054,0.00129737126629959,-0.00889436222320583,-0.00298087006697134,0.00933532022611061,-0.00575270954708795,0.0101886423670763,0.0103179342673609,0.00125216304309905,0.00156068839035139,0.00604833856486753,-0.00243615688671461,-0.0152062635211987,0.00301033416242014,-0.0110620285552256,-0.00319062614983068,0.00127274396138510,-0.00375665316513985,0.00535357003821257,-0.00499767011541152,0.00253981748450194,-0.00997352487493064,-0.0175470676080911,0.00844790676749074,-0.00965016111459605,-0.000321495013020368,-0.00246823493919912,0.00566397434194155,-0.00755585517122892,-0.00292552044723493,0.00696403781041699,0.0362943303189418,0.00334533994087938,-0.00673963410601353,0.00282545925059896,-0.00763292219002754,0.00636245335736696,-0.0215839997063974,-0.00916382281531281,0.00401709134290851,-0.0210090631469670,0.000913235310401106,0.00320206782162824,-0.00192001004218678,-0.0124559682169760,0.0256879675412520,0.0144118714382078]';
    function vp=cvmtest(shr0,shr)
        function px = edfx(xd,D) %返回在点x处的经验概率,经验概率函数由D决定
            j = 1; m = 0;
            l = length(D); %Compute length of D once
            S = sort(D); %Puts data in ascending order
            while S(j) <= xd
                m = m + 1;
                j = j + 1;
                if j == l + 1
                    break
                end
            end
            px = m/l;
        end
        for si=1:length(shr0)
    fp(si) = quad(@(x)funquantum1(x,k),-0.09,shr0(si));%对funquantum1中的x进行积分,得到在shr0(si)处的累积概率
    fpe(si) = edfx(shr0(si),shr);%调用嵌套函数edfx,得到在shr0(si)处的经验概率
    ff(si) = funquantum1(shr0(si));%得到在shr0(si)处的概率密度
    end
    vp = (fp-fpe).^2.*ff;
    end
    function ww=wcvm()
    ww=length(shr)*quad(@cvmtest,-0.09,0.09,[],[],shr)%对cvmtest中的shr0进行积分,最后得到关于k(1),k(2),k(3),k(4)的函数,积分限从-0.1到0.1
    end
k0=[0.05;0.05;0.05;-0.05];
A2=[0,0,0,-1];
B2=0.09;
[kk,kfval]=fmincon(@wcvm,k0,A2,B2)%通过优化方法求使得函数wcvm最小的k(1),k(2),k(3),k(4)估计值
end
--------------------------------------------------------------------------------------------------------------------------------------------------

3.报错信息
--------------------------------------------------------------------------------------------------------------------------------------------------
??? Error using ==> funalltest4tohelp2>wcvm
Too many input arguments.
Error in ==> fmincon at 540
      initVals.f = feval(funfcn{3},X,varargin{:});
Error in ==> funalltest4tohelp2 at 47
[kk,kfval]=fmincon(@wcvm,k0,A2,B2)%通过优化方法求使得函数wcvm最小的k(1),k(2),k(3),k(4)估计值
Caused by:
    Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
--------------------------------------------------------------------------------------------------------------------------------------------------

4.系统及版本

matlab版本:2009b(64位)
windows7 64位

本帖被以下淘专辑推荐:

回复
分享到:

使用道具 举报

发表于 2012-10-19 09:41 | 显示全部楼层
function里边定义function?
说实话,第一次见到这种编程方式的
  1. function ww=wcvm()
复制代码
连优化变量都没有

总而言之,是一段让人无语的代码
发表于 2012-10-19 21:57 | 显示全部楼层
function里边定义function?

好像较新版些很早就有!
不过, 个人不太喜欢/习惯如此用
发表于 2012-10-21 16:03 | 显示全部楼层

嗯,这个是MATLAB 7.0开始增加的嵌套函数功能,很方便参数传递和内存共享。不过楼主没有掌握好,用得很别扭,那个wcvm函数是个没有输入变量得函数,怎么用fmincon来优化?
建议楼主还是理清变量传递逻辑关系,再好好研究下嵌套函数的用法。

评分

2

查看全部评分

发表于 2012-10-22 19:39 | 显示全部楼层
rocwoods 发表于 2012-10-21 16:03
嗯,这个是MATLAB 7.0开始增加的嵌套函数功能,很方便参数传递和内存共享。不过楼主没有掌握好,用得很别 ...

好几年没太接触matlab了,个人一致停留在2009的阶段
不过个人一致非常反感这种所谓编程方式的改进
虽然在某些方面是方便了,但是不利于良好的编程习惯的形成
很难产出高质量的代码?

一直以来还是比较欣赏Fortran77的格式,虽然麻烦点,不过逻辑性很好
一段没有goto的Fortran77代码,堪称完美
发表于 2012-10-22 22:27 | 显示全部楼层
本帖最后由 rocwoods 于 2012-10-22 22:41 编辑
happy 发表于 2012-10-22 19:39
好几年没太接触matlab了,个人一致停留在2009的阶段
不过个人一致非常反感这种所谓编程方式的改进
虽然 ...


个人觉得MATLAB本身是一个灵活性和高效性结合得很好的语言。但这是一把双刃剑,上手简单让很多人浅尝辄止,似懂非懂的时候便照猫画虎,有时虽然能做出来,但肯定不是很好的,这也是一些朋友更喜欢C/C++等语法检查严格的语言的原因之一。
不过个人觉得还是要肯定新技术的发展的,MATLAB 7.0以后的嵌套函数、匿名函数等大大方便了程序开发,面向对象编程等全面支持也使得实际工程中用MATLAB和C/C++混合开发大型软件的架构更清晰。如果还是6.5以前的MATLAB,实际比较大的工程中用MATLAB还是比较不方便的,很多工具箱不能编译。那时候更多是把MATLAB当成数学库来用,适合小型应用。
现在的MATLAB反过来了,小型应用不适合,一个MCR的初始化时间太长,反而较大型的应用适合。

评分

1

查看全部评分

 楼主| 发表于 2012-11-2 22:14 | 显示全部楼层
rocwoods 发表于 2012-10-21 16:03
嗯,这个是MATLAB 7.0开始增加的嵌套函数功能,很方便参数传递和内存共享。不过楼主没有掌握好,用得很别 ...

感谢rocwoods的回答和各位的讨论,确实是变量传递时出了问题。
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

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

GMT+8, 2024-11-17 20:45 , Processed in 0.064349 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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