chenbinok 发表于 2010-10-26 15:33:10

调用子函数出现错误

下面这个程序附件是关于通过引用子函数来求解29随机变量的均值 但是计算总是出现错误 我是初学者,不晓得怎么改 请大家帮我看看 谢谢

messenger 发表于 2010-10-26 21:13:22

本帖最后由 messenger 于 2010-10-26 21:24 编辑

大致看了一下,
1、你的第47行的input函数调用中,x,style,muX,sigmaX之前没有被赋值;
2、input是Matlab的内置函数,不要用input这个函数名作为你自编函数的函数名,否则得不到执行;
3、第86、92行的函数参数,1+1/sigmaX、1-1/muX,这种型式是被Matlab所不允许的(好象也不被其他语言允许);

因为不知道你的编程意图,所以只能看这么多错误了。建议你找一本基础的Matlab书,看看函数调用方面的内容。


function XBLMATLAB
clear;clc;
    style(1)=3;      
    style(2)=3;
    style(3)=3;      
    style(4)=3;
    style(5)=3;      
    style(6)=3;
    style(7)=3;      
    style(8)=3;
    style(9)=3 ;      
    style(10)=3;
    style(11)=3;
    style(12)=3;
    style(13)=3;
    style(14)=3;
    style(15)=3;
    style(16)=3;
    style(17)=3;

    style(18)=1;
    style(19)=1;
    style(20)=1;
    style(21)=1;

    style(22)=2;
    style(23)=2;
    style(24)=2;
    style(25)=2;
   
   
    style(26)=4;
    style(27)=4;
    style(28)=4;
    style(29)=4;
for i=1:21
    muX(i)=5.2e-3;
    sigmaX(i)=0.26e-3;
end
for i=22:25
    muX(i)=5.2e-3;
    sigmaX(i)=0.2e-3;
end
muX(26)=0;muX(27)=2.124e6;muX(28)=0;muX(29)=1.2344e6;
sigmaX(26)=1;sigmaX(27)=2.324e6;sigmaX(28)=2*pi;sigmaX(29)=1.4344e6;
for i=1:29
    m=input(x,style,muX,sigmaX)
end
%==========================================================================
function m=input(x,style,muX,sigmaX)
            if (style==1)            %对数正态分布
               if (muX<0)
                     return
               end
               x=muX
            end
            if (style==2)            %极值1型分布
                x=muX+sigmaX*0.5772156649015328606
            end
             if (style==3)            %正态分布
                x=muX
             end
             if (style==4)            %均匀分布
                x=(muX+sigmaX)/2
             end
             if (style==5)            %指数分布
                x=1/muX
             end
             if (style==6)            %威布尔分布
                   if (muX<0)
                        return
                   end
                   if (sigmaX<0)
                        return
                   end
                   n=gammafun(gam,1+1/sigmaX)
                   x=muX*gam
             end
            if (style==7)            %极值2型分布
               if (muX<1)
                      return
               end
               o=gammafun(x,1-1/muX)
            end
    %=====================================================================
    function n=gammafun(gam,1+1/sigmaX)
             gam=1/(1+1/sigmaX)
          for i=1:1000
            gam=gam*((1+1/i)^(1+1/sigmaX))/(1+(1+1/sigmaX)/i)
          end
   %=====================================================================
   function o=gammafun(x,1-1/muX)
             x=1/(1-1/muX)
         for i=1:1000
            x=x*((1+1/i)^(1-1/muX))/(1+(1-1/muX)/i)
          end

qibbxxt 发表于 2010-10-26 21:28:07

我以前没有见过这样的子函数的写法
建议你看看基础的教材

bainhome 发表于 2010-10-26 23:21:26

本帖最后由 bainhome 于 2010-10-26 23:23 编辑

style(1)=...
...
style(i)=...这还真是一个强悍的style啊!幸亏不多,弄个一二十万行,你给数列没赋完值就阵亡了,再让子孙后代接着赋,赋到天荒地老,赋到愚公移山,赋到国足夺取世界杯,家祭无忘告乃翁,呵呵。
ps:统计工具箱概率相关函数好好看看,兴许能有意外收获

chenbinok 发表于 2010-10-27 10:09:14

谢谢版主提示
页: [1]
查看完整版本: 调用子函数出现错误