简体   繁体   English

Matlab不绘制函数间隔

[英]Matlab not plotting function interval

I want to plot the following mathemathical function: 我想绘制以下数学函数:

功能

I tried using the following code: 我尝试使用以下代码:

x= -3:0.1:3;
x1 = x(x>2 & x<2);
x2 = x(x==2)
y1 = (x1)+1
y2 = 2
plot([x1 x2], [y1 y2])

Why is it giving me an empty window? 为什么给我一个空的窗口?

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.

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