程序如下:
x=0.1:0.05:5;
if x<1
y=-atan((2*0.1*x)./(1-x.^2))
else
y=-pi-atan((2*0.1*x)./(1-x.^2))
end
为什么每次运行,即使在x<1段,y的值都是-pi-atan((2*0.1*x)./(1-x.^2)),if语句不起作用?
Nonscalar Expressions
If the evaluated expression yields a nonscalar value, then every element of this value must be true or nonzero for the entire expression to be considered true. For example, the statement if (A < B) is true only if each element of matrix A is less than its corresponding element in matrix B.
明白了,那就再加个循环好了。
x=0:0.05:5;
for i=1:length(x);
if x(i)<1, y(i)=-atan((2*0.1*x(i))/(1-x(i)^2));
elseif x(i)==1, y(i)=-pi/2;
else y(i)=-pi-atan((2*0.1*x(i))/(1-x(i)^2));
end
end