gpt4 book ai didi

awk - 直接从文本文件绘制函数

转载 作者:行者123 更新时间:2023-12-04 12:33:37 24 4
gpt4 key购买 nike

有没有办法根据文本文件中的值绘制函数?

我知道如何在 gnuplot 中定义一个函数然后绘制它,但这不是我需要的。
我有一个表,其中包含定期更新的函数的常量。当这个更新发生时,我希望能够运行一个脚本,用这条新曲线绘制一个图形。由于要绘制的图形很少,我想使该过程自动化。

这是一个带有常量的示例表:

location a  b  c
1 1 3 4
2

我认为有两种方法可以解决问题,但我不知道它们是否以及如何实现。
  • 然后我可以使用 awk 来生成字符串:f(x)=1(x)**2+3(x)+4 ,将其写入文件并以某种方式让 gnuplot 读取这个新文件并在某个 x 上绘图范围。
  • 或者在 gnuplot 中使用 awk 之类的东西 f(x) = awk /1/ {print "f(x)="$2等等,或者直接在 plot 命令中使用 awk。

  • 我无论如何,我被卡住了,还没有在网上找到解决这个问题的方法,你有什么建议吗?

    最佳答案

    另一种可能有一个通用版本,您可以执行以下操作:

    假设参数存储在文件 parameters.dat 中第一行包含变量名称和所有其他参数集,例如

    location a b c
    1 1 3 4

    脚本文件如下所示:
    file = 'parameters.dat'
    par_names = system('head -1 '.file)
    par_cnt = words(par_names)

    # which parameter set to choose
    par_line_num = 2
    # select the respective string
    par_line = system(sprintf('head -%d ', par_line_num).file.' | tail -1')
    par_string = ''
    do for [i=1:par_cnt] {
    eval(word(par_names, i).' = '.word(par_line, i))
    }
    f(x) = a*x**2 + b*x + c

    plot f(x) title sprintf('location = %d', location)

    关于awk - 直接从文本文件绘制函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15007620/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com