2

我有类似于下面的代码,其中在每次迭代后绘制一个参数取决于循环迭代的函数。我想用名称 trigplot_i.ps 保存绘图,其中 i 是迭代次数,但不知道如何。

我试过 trigplot_"i".ps 但没有用,也找不到如何将 i 转换为字符串。

我是初学者,所以非常欢迎任何帮助。

f(x) := sin(x);
g(x) := cos(x);
for i:1 thru 10 do
    (plot2d([i*f(x), i*g(x)], [x,-5,5],[legend,"sin(x)","cos(x)"],
    [xlabel,"x"],[ylabel,"y"],
    [ps_file,"./trigplot_i.ps"],
    [gnuplot_preamble,"set key box spacing 1.3 top right"])
);

编辑后的代码给出错误:

f(x) := sin(x);
g(x) := cos(x);
for i:1 thru 10
    do block([myfile],
        myfile: sconcat("./trigplot_", i, ".ps"),
        printf (true, "iteration ~d, myfile = ~a~%", myfile),
        plot2d([i*f(x), i*g(x)], [x,-5,5],[legend,"sin(x)","cos(x)"],
        [xlabel,"x"],[ylabel,"y"],
        [ps_file, myfile],
        [gnuplot_preamble,"set key box spacing 1.3 top right"])
);

错误:“声明:参数必须是一个符号;找到”./trigplot_1.ps -- 一个错误。要调试此尝试:debugmode(true);"

4

1 回答 1

1

看起来不错。要构造文件名,试试这个:sconcat("./trigplot_", i, ".ps")或者你也可以试试:printf(false, "./trigplot_~d.ps", i)。我的建议是在循环中将其作为一个单独的步骤,然后您可以在对 的调用中使用它plot2d,例如:

for i:1 thru 10
  do block ([myfile],
            myfile: sconcat("./trigplot_", i, ".ps"),
            printf (true, "iteration ~d, myfile = ~a~%", i, myfile),
            plot2d (<stuff goes here>, [ps_file, myfile], <more stuff>));

编辑:修复了printf(省略的参数i)中的错误。

于 2020-12-29T17:54:27.967 回答