变量undefined在简单的matlab函数中

时间:2018-01-16 23:22:49

标签: matlab octave ode45

我无法理解为什么我收到以下错误:

error: 'y' undefined near line 7 column 22
error: execution exception in odefun.m

对于此功能:

function s = odefun(t, y)
        global K = [ 0.5; 3; 1; 4; 1; 5 ];
        function ret = k(n)
                global K;
                ret = K(n+1);
        end
        s = zeros(6,1);
        s(1) = k(0) -k(1) * y(1) * y(2);
        s(2) = k(2) - k(1) * y(1) * y(2);
        s(3) = k(1) * y(1) * y(2) - k(3) * y(3);
        s(4) = k(1) * y(1) * y(2) - k(2) * y(4);
        s(5) = k(3) * y(3) - k(4) * y(5);
        s(6) = k(3) * y(3) - k(5) * y(6);
end

y0 = [1; 1; 1/2; 0; 0; 0]
[t, y] = ode45(odefun, [0 10], y0)

显然我是Matlab的初学者,非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

当你这样做时

[t, y] = ode45(odefun, [0 10], y0)

你在没有参数的情况下调用odefun。您需要传递函数句柄:

[t, y] = ode45(@odefun, [0 10], y0)