简体   繁体   English

heavyseide函数在ezplot和fplot中产生不同的输出

[英]heaviside function procudes different outputs in ezplot and fplot

I'm new to MATLAB and I'm trying to plot a step function with heaviside() . 我是MATLAB的新手,我正在尝试使用heaviside()绘制步进函数。 I tried at first the following code: 我首先尝试了以下代码:

f = @(x)heaviside(x+2) - heaviside(x-2);
fplot(f, [-10 10])

The result: 结果:

在此输入图像描述

With ezplot , the result is as I thought: 使用ezplot ,结果如我所想:

f = @(x)heaviside(x+2) - heaviside(x-2);
ezplot(f, [-10 10])

The result: 结果:

在此输入图像描述

What is the difference between fplot and ezplot ? fplotezplot什么区别? Thanks in advance! 提前致谢!

This pertains to the following bit of fplot documentation: 这与fplot文档的以下内容有关:

fplot uses adaptive step control to produce a representative graph, concentrating its evaluation in regions where the function's rate of change is the greatest. fplot使用自适应步长控制来生成代表图,将其评估集中在函数变化率最大的区域。

It sees that your function is constant just about everywhere and doesn't evaluate between [-2 2] . 它看到你的函数几乎无处不在,并且不在[-2 2]之间进行评估。 The solution is to specify a minimum number of evaluation points: 解决方案是指定最少数量的评估点:

n = 1e3;
fplot(f, [-10 10],n)

For example, if we get the output coordinates from fplot : 例如,如果我们从fplot获取输出坐标:

>> [x,y] = fplot(f, [-10 10]);
>> [x y]

ans =

  -10.0000         0
   -9.9600         0
   -9.8800         0
   -9.7200         0
   -9.4000         0
   -8.7600         0
   -7.4800         0
   -4.9200         0
   -2.3600         0
    2.7600         0
   10.0000         0

You can see the adaptive evaluation in action. 您可以看到适应性评估的实际效果。 It starts at -10, steps forward faster and faster until it skips right from -2.36 to +2.76! 它从-10开始,越来越快地向前迈进,直到它从-2.36跳到+2.76! Seen on datatips: 看到数据提示:

在此输入图像描述

If we use n=1e3 evaluation points: 如果我们使用n=1e3评估点:

在此输入图像描述

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

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