Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“MATLAB错误”;在此上下文中不允许使用函数定义。”;_Matlab - Fatal编程技术网

“MATLAB错误”;在此上下文中不允许使用函数定义。”;

“MATLAB错误”;在此上下文中不允许使用函数定义。”;,matlab,Matlab,可能重复: 毫不奇怪,如果我尝试在MATLAB中运行以下M脚本,我会得到错误 ???错误:文件:kalmanmle.m行:47列:1 在此上下文中不允许使用函数定义 我不确定这是否可以像我这样运行。或者,我如何在MATLAB命令行上运行它 clear all; % State space reprsentation to be forcasted by kalman filter % zhi(t+1) = F*zhi(t) + v(t+1) --> unbobserved v

可能重复:

毫不奇怪,如果我尝试在MATLAB中运行以下M脚本,我会得到错误

???错误:文件:kalmanmle.m行:47列:1 在此上下文中不允许使用函数定义

我不确定这是否可以像我这样运行。或者,我如何在MATLAB命令行上运行它

clear all;

%  State space reprsentation to be forcasted by kalman filter
%   zhi(t+1) = F*zhi(t) + v(t+1)   --> unbobserved varaibles
%   v~N(0,Q)
%   y(t) = A'*x(t) + H'*zhi(t) + w(t)
%   w~N(0,R)

global y;
global x;
global Hvec;
%%----    Enter  Input parameters
F = 0.9;
Q = 0.1;
A = 2;
n = 100;
Hvec = zeros(n,1); %index returns process
indexshock = normal_rnd(0,0.1,n,1);
Hvec(1) = 0;
for i = 2:n,
    Hvec(i) = 0.95*Hvec(i-1) + indexshock(i);
end

%H = 0.3;
R = 0.05;

x = ones(n,1);
zhi = zeros(n,1);
y = zeros(n,1);
zhi(1) = 0;
v = normal_rnd(0,Q,n,1);
w = normal_rnd(0,R,n,1);

H = Hvec(1);
y(1) = A'*x(1) + H'*zhi(1) + w(1);
for i = 2:n,
    H = Hvec(i);
    zhi(i) = F*zhi(i-1) + v(i);
    y(i) = A'*x(i) + H'*zhi(i) + w(i);
end
%% ------------------
%test = [zhi y]

function ret = MyLikelihoodFn(p)
    global y;
    global x;
    global Hvec;
    F = p(1);
    Q = p(2)^2;
    A = p(3);
    R = p(4)^2;
    n = size(y,1);
    P = Q;
    Ezhi = 0;
    Ezhivec = zeros(n,1);
    Ezhivec(1) = Ezhi;
    tmpsum = 0;
    tmp1 = -(n/2)*log(2*pi);
    for i = 2:n,
        yt = y(i);
        xt = x(i);
        H = Hvec(i);
        Ezhi = F*Ezhi + F*P*H*inv(H'*P*H+R)*(yt-A'*xt-H'*Ezhi);
        P = F*P*F' - F*P*H*inv(H'*P*H+R)*H'*P*F' + Q;
        Ezhivec(i) = Ezhi;
        tmpmat = H'*P*H + R;
        tmp2 = -0.5*log(det(tmpmat));
        tmpmat2 = yt - A'*xt - H'*Ezhi;
        tmp3 = -0.5*tmpmat2'*inv(tmpmat)*tmpmat2;
        tmpsum = tmp1+tmp2+tmp3;
    end
    ret = -tmpsum;
endfunction

param = zeros(4,1);
param(1) = 0.2;
param(2) = 0.2;
param(3) = 1;
param(4) = 0.2;

resultparam = fmins('MyLikelihoodFn',param)

actualF = F
F = resultparam(1)
actualQ = Q
Q = resultparam(2)^2
actualA = A
A = resultparam(3)
actualR = R
R = resultparam(4)^2

n = size(y,1);
P = Q;
Ezhi = 0;
Ezhivec = zeros(n,1);
Ezhivec(1) = Ezhi;

for i = 2:n,
    yt = y(i);
    xt = x(i);
    H = Hvec(i);
    Ezhi = F*Ezhi + F*P*H*inv(H'*P*H+R)*(yt-A'*xt-H'*Ezhi);
    P = F*P*F' - F*P*H*inv(H'*P*H+R)*H'*P*F' + Q;
    Ezhivec(i) = Ezhi;
end
test = [zhi Ezhivec Hvec y];
tmp = 1:n;
%plot(tmp,zhi,'-',tmp,Ezhivec,'-',tmp,Hvec,'-',tmp,y,'-');
plot(tmp,zhi,'-',tmp,Ezhivec,'-');

不能在脚本文件(不是函数定义的.m文件)中定义函数。它们必须在自己的.m文件中。这很烦人,但事实就是这样。对于非常短的函数,您可以动态生成匿名函数,但这些函数的内容有限:

 fun = @(params) STATEMENT;

 fun = @(x,y) x*y+sum(x^2-y^2);

你上面的例子有点太复杂了。

要扩展Alex的答案,你需要将你的函数
MyLikelihoodFn(p)
放入一个新文件,该文件必须名为
MyLikelihoodFn.m
。另外,MATLAB中没有
endfunction
关键字,它只是
end


如果你想把所有的东西都放在一个文件中,你必须把你的脚本变成一个函数本身(通过添加<代码>函数函数名,MatthScript文件名作为第一行),并移动<代码>函数Rt= MyRealHOODFFN(P)< /代码>到文件的末尾(现在它似乎在脚本的代码中间). 在这种情况下,您也不需要清除所有,因为函数总是以其自己的干净工作区启动。

请尝试为代码使用代码块。它看起来像一堆,读起来非常困难。同样相关:,