[英]Matlab not plotting function interval
It is not possible for the following conditional to ever be true since any number can't be simultaneously greater than and less than 2 以下条件不可能为真,因为任何数字不能同时大于和小于2
x > 2 & x < 2
As a result, x1
is an empty vector and x2
is just going to be 2
, so your plotting command will just yield a single point at (2,2). 结果, x1
是一个空向量, x2
刚好是2
,因此您的绘图命令只会在(2,2)处产生一个点。
You want to use a logical or ( |
) instead 您想改用逻辑或 ( |
)
x1 = x(x > 2 | x < 2);
Also a better way to plot this would be the following 还有一个更好的方法来绘制此将是以下
y = x + 1; % Set all values to x + 1
y(x == 2) = 2; % Replace those that meet your criteria
plot(x, y)
As a side note, it's generally a bad idea to use ==
to compare floating point numbers. 附带说明一下,使用==
比较浮点数通常是个坏主意。 You should instead use eps
to check if two floating point numbers are equal: 您应该改用eps
检查两个浮点数是否相等:
y(abs(x - 2) < eps) = 2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.