2016-06-14 56 views
0

定义的多变量函数我已经在Matlab中定义的多变量函数为:集成在一起的逻辑运算

function f=test(x,y) 

if x>=0 && y>0 
f=x+y; 
end 

if x<0 && y>0 
f=-x+y; 
end 

end 

现在我想要的功能集成在Xý固定为1,所以在命令窗口中我写道:

[email protected](x) test(x,1); 
integrate(f,-1,1); 

然后,我得到一个错误说:

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

Error in test (line 3) 
if x>=0 && y>0 

如果我更换由&所有& &与其他一切不变,但另一个错误:

Output argument "f" (and maybe others) not assigned during call to "test". 

Error in @(x)test(x,1) 

我很感激,如果有人能帮助我做这个积分数值。

顺便说一句,我注意到这里出现的问题似乎是独特的,当在函数的定义中有一些逻辑操作。如果我定义

function f=test(x,y) 
    f=x+y; 
end 

,做整体的,因为它正确地返回2

回答

0

[email protected](x) test(x,1); 
integral(f,-1,1) 

试试这个:

function f=test(x,y) 
f = (x>=0).*(y>0).*(x+y) - (x<0).*(y>0).*(x+y); 
end