简体   繁体   English

未定义的函数或变量“ w”

[英]Undefined function or variable 'w'

I'm trying to solve an ODE for a school project, and I'm running into a problem with one variable. 我正在尝试为一个学校项目解决ODE,但遇到一个变量问题。 Wondering if I could get some help. 想知道我是否能得到一些帮助。 I'm using ODE15s solver. 我正在使用ODE15s求解器。

options = odeset('RelTol',2.22045e-14, 'AbsTol', 1e-6);
[T,Y] = ode15s(@odeHMCase5,[0,200],[4.3,5.1,3,0,422],options);

The variable in question is part of a block of conditional statements, when debugging it does show up in the workspace. 有问题的变量是条件语句块的一部分,调试时它确实会显示在工作空间中。

%define G as a global variable
global G

%Define ec
if z(5) < 453
    ec = 0;
else
    ec = 1;
end

if (ec == 1) & (G == false)
    G = true;
elseif (ec == 1) & (G == true)
    G = true;
elseif (ec == 0) & (G == true)
    G = true;
elseif (ec == 0) & (G == false)
    G = false;
end

if G == false
    w = 0;
elseif (G == true) & (z(5) >= 433)
    w = 1;
elseif (G == true) & (z(5) < 433)
    w = 0;
    G = false;
end

In addition the conditional operators &&, when running, MATLAB throws up this error , 此外,条件运算符&&在运行时会引发此错误,

Operands to the || and && operators must be convertible to logical scalar values.

Changing to & seems to be the only way around it. 更改为&似乎是解决此问题的唯一方法。

Any help would be greatly appreciated. 任何帮助将不胜感激。

The first problem (" Operands to the || and && operators must be convertible to logical scalar values. ") is pretty easy to solve. 第一个问题(“ Operands to the || and && operators must be convertible to logical scalar values. ”)很容易解决。 In the scope of if statement conditions, you must use the double logical operators ( && or || ): if语句条件的范围内,必须使用双逻辑运算符( &&|| ):

if (ec == 1) & (G == false)  % WRONG
if (ec == 1) && (G == false) % CORRECT

The undefined variable w error is due to the fact that your last if statement ends with an elseif and no else clause is defined. 未定义变量w错误的原因是,您的最后一个if语句以elseif结尾,并且未定义else子句。 Therefore, when none of the specified conditions is evaluated to true , no value is assigned variable w : 因此,当所有指定条件都不被评估为true ,则不会为变量w赋值:

if G == false % THIS CAN ALSO BE REWRITTEN AS: if (~G)
    w = 0;
elseif (G == true) && (z(5) >= 433) % FIX THE LOGICAL OPERATOR
    w = 1;
elseif (G == true) && (z(5) < 433) % FIX THE LOGICAL OPERATOR
    w = 0;
    G = false;
else
    w = ?; % WRITE SOMETHING HERE
end

The statement above could be also simplified as follows, and this should fix the issue unless I misunderstood what you're trying to accomplish: 上面的陈述也可以简化如下,除非我误解了您要完成的工作,否则这应该可以解决问题:

if (G)
    if (z(5) >= 433)
        w = 1;
    else
        w = 0;
        G = false;
    end
else
    w = 0;
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM