首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的heaviside函数图形与gnuplot?

简单的heaviside函数图形与gnuplot?
EN

Stack Overflow用户
提问于 2018-04-16 06:50:33
回答 2查看 415关注 0票数 0

我基本上想要用gnuplot完成this (第一张图)。我没有找到任何与此完全相同的东西。我可以做一个很好的heaviside没有在两行的结尾和开始的小圆圈,但我似乎不能得到它与小圆圈。实际上,第二张图也很好知道。第三个也是,但我不贪心。

EN

回答 2

Stack Overflow用户

发布于 2020-11-19 22:52:39

只是为了记录和完整性。尽管您可以定义一个函数

代码语言:javascript
复制
H(x) = x<0 ? 0 : 1

如果你绘制了

代码语言:javascript
复制
plot H(x) w l

这条线将在零点连续,当然也不会有点。因此,另一个只有两列x,y和变量pointtype的建议如下所示。

代码:

代码语言:javascript
复制
### Heaviside function
reset session

$Heaviside <<EOD
-2 0
 0 0
 
 0 0.5
 
 0 1
 2 1
EOD

set yrange [-1:2]
set ytics 1
unset key
set multiplot layout 3,1
    plot $Heaviside u 1:2 w l lc 0, \
         '' u 1:($0==1||$0==3?$2:NaN):($0==3?7:6) w p pt var lc 0

    plot $Heaviside u 1:2 w l lc 0, \
        '' u 1:($0==1||$0==2||$0==3?$2:NaN):($0==2?7:6) w p pt var lc 0

    set xrange [0:4]
    a = 3
    plot $Heaviside u ($1+a):2 w l lc 0, \
        '' u ($1+a):($0==1||$0==3?$2:NaN):($0==3?7:6) w p pt var lc 0
unset multiplot
### end of code

结果:

添加:

一个带有更短且不那么令人困惑的plot命令的变体,但使用了4列和变量pointtype。这将产生与上面相同的结果。

代码:

代码语言:javascript
复制
### Heaviside function
reset session

$Heaviside <<EOD
-2 0    NaN  NaN
 0 0    6    6
 
 0 0.5  NaN  7
 
 0 1    7    6
 2 1    NaN  NaN
EOD

set yrange [-1:2]
set ytics 1
unset key
set multiplot layout 3,1
    plot $Heaviside u 1:2 w l lc 0, \
         '' u 1:2:3 w p pt var lc 0

    plot $Heaviside u 1:2 w l lc 0, \
         '' u 1:2:4 w p pt var lc 0

    set xrange [0:4]
    a = 3
    plot $Heaviside u ($1+a):2 w l lc 0, \
         '' u ($1+a):2:3 w p pt var lc 0
unset multiplot
### end of code 

添加2:

为了最终确定答案,这里提供了一种绘制包含Heaviside函数的函数的方法。它使用当前的x范围,而不是从具有固定x值的数据块(如上面的两个示例)绘制。注意,例如,语法plot '+' u 1:(sin($1))plot sin(x)基本相同。

显然,通过lc rgb -1设置线条颜色不会绘制线条,这里可以使用线条来中断线条。您可能想要增加样本,例如set samples 300,以避免点和函数之间的间隙。

代码:

代码语言:javascript
复制
### plotting Heaviside function and functions containing Heaviside function 
#   including line interruption and inclusion/exclusion points
reset session

Heaviside(x,a) = x<a ? 0 : 1                              # definition of Heaviside function
array Hpoints[2] = [6,7]                                  # array for plotting "Heaviside points"
Hcolor(x) = (x0=x1, x1=x, x0<a && x1>=a ? -1 : 0xff0000)  # set color -1 for line interruption
dx(n) = 1e-3*(2*n-1)                                      # small dx to get y-value of points close to break

f(x,a) = 50/(x**2+2)*cos(4*x) * Heaviside(x,a)

unset key
set multiplot layout 2,1
    a = 2.0
    set yrange[-1:2]
    plot x1=NaN '+' u 1:(Heaviside(x,a)):(Hcolor(x)) w l lc rgb var, \
         Hpoints u (a):(Heaviside($1,a)):(Hpoints[$0+1]) w p pt var lc rgb Hcolor(NaN)
    
    a= 0
    set samples 300
    set yrange[-25:35]
    plot x1=NaN '+' u 1:(f(x,a)):(Hcolor(x)) w l lc rgb var, \
         Hpoints u (a):(f(a+dx($0),a)):(Hpoints[$0+1]) w p pt var lc rgb Hcolor(NaN)
unset multiplot
### end of code

结果:

票数 2
EN

Stack Overflow用户

发布于 2018-04-16 06:53:36

我创建了以下数据文件(请注意这两行空行):

代码语言:javascript
复制
-2 0 0 1
0 0 2 1


0 0 0 1

并运行以下gnuplot命令:

代码语言:javascript
复制
set yrange [-2:2]
plot "file"     using 1:2 with lines,\
     ""         using 3:4 with lines, \
     "" index 1 using 1:2 with points pointtype 6, \
     "" index 1 using 3:4 with points pointtype 7

把颜色改成你喜欢的颜色。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49847539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档