问题描述
我是 MATLAB 新手,我正在尝试使用 heaviside()
绘制阶跃函数.我首先尝试了以下代码:
I'm new to MATLAB and I'm trying to plot a step function with heaviside()
. I tried at first the following code:
f = @(x)heaviside(x+2) - heaviside(x-2);
fplot(f, [-10 10])
结果:
使用ezplot
,结果和我想的一样:
With ezplot
, the result is as I thought:
f = @(x)heaviside(x+2) - heaviside(x-2);
ezplot(f, [-10 10])
结果:
fplot
和 ezplot
有什么区别?提前致谢!
What is the difference between fplot
and ezplot
? Thanks in advance!
推荐答案
这与 fplot
文档的以下部分有关:
This pertains to the following bit of fplot
documentation:
fplot
使用自适应步长控制来生成代表性图形,将其评估集中在函数变化率最大的区域.
它看到您的函数几乎在任何地方都是常数,并且不在 [-2 2]
之间求值.解决方案是指定最少数量的评估点:
It sees that your function is constant just about everywhere and doesn't evaluate between [-2 2]
. The solution is to specify a minimum number of evaluation points:
n = 1e3;
fplot(f, [-10 10],n)
例如,如果我们从 fplot
得到输出坐标:
For example, if we get the output coordinates from 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
您可以看到正在运行的自适应评估.它从 -10 开始,越来越快地前进,直到它从 -2.36 跳到 +2.76!在数据提示上看到:
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! Seen on datatips:
如果我们使用 n=1e3
评估点:
If we use n=1e3
evaluation points:
这篇关于heaviside 函数在 ezplot 和 fplot 中产生不同的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!