对于gnuplot

发布于 2025-01-27 00:49:36 字数 310 浏览 2 评论 0原文

我有一个小问题,我需要加入for循环,以迅速绘制不同的函数,在这里我留下原型代码

reset
set terminal png
plot for [i=1:2]\
    set output "Graph".i.".png"\
    if (i==1)\
        x title 'Graph'.i;\
    if (i==2)\
        2x+3x**2 title 'Graph'.i;\
    if (i==3)\
        3x+8+4x**2-15x**3 title 'Graph'.i
unset output 

I have a little problem, I need to join the for loop with the if condition to graph different functions quickly, here I leave the prototype code

reset
set terminal png
plot for [i=1:2]\
    set output "Graph".i.".png"\
    if (i==1)\
        x title 'Graph'.i;\
    if (i==2)\
        2x+3x**2 title 'Graph'.i;\
    if (i==3)\
        3x+8+4x**2-15x**3 title 'Graph'.i
unset output 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕梦 2025-02-03 00:49:36

我不确定您是否正在寻找以下示例。如果没有,请重新提出您的问题。
例如,您可以定义几个功能,然后由三元运营商选择它们(检查help ternary)。在for循环中,您还可以将项目定义为字符串。这使您可以随意更改数值顺序。

脚本:

### select function to plot via number
reset session

f1(x) = x
f2(x) = 2*x + 3*x**2
f3(x) = 3*x +8 +4*x**2 - 15*x**3
f4(x) = x**2
f5(x) = x**3

myFunction(n) = n==1 ? f1(x) : n==2 ? f2(x) : n==3 ? f3(x) : n==4 ? f4(x) : n==5 ? f5(x) : NaN

set xrange[-1:1]

plot for [n in '1 2 5 4'] '+' u (x):(myFunction(n)) w l ti sprintf("Graph %s",n)
### end of script

结果:

”在此处输入图像描述”

I am not sure if you are looking for something like the following example. If not, please rephrase your question.
You could for example define several functions and select them by the ternary operator (check help ternary). In a for loop you can also define items as strings. This allows you to change the numerical order as you like.

Script:

### select function to plot via number
reset session

f1(x) = x
f2(x) = 2*x + 3*x**2
f3(x) = 3*x +8 +4*x**2 - 15*x**3
f4(x) = x**2
f5(x) = x**3

myFunction(n) = n==1 ? f1(x) : n==2 ? f2(x) : n==3 ? f3(x) : n==4 ? f4(x) : n==5 ? f5(x) : NaN

set xrange[-1:1]

plot for [n in '1 2 5 4'] '+' u (x):(myFunction(n)) w l ti sprintf("Graph %s",n)
### end of script

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文