【问题标题】:Maxima add current loop iteration to filenameMaxima 将当前循环迭代添加到文件名
【发布时间】:2021-04-06 07:21:42
【问题描述】:

我的代码类似于下面的代码,其中一个函数的参数取决于循环迭代,在每次迭代后绘制。我想用名称 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);"

【问题讨论】:

    标签: output maxima wxmaxima


    【解决方案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)。

    【讨论】:

    • 谢谢!编辑后,我仍然收到错误“声明:参数必须是符号;发现“./trigplot_1.ps” - 错误。要调试此尝试:debugmode(true);”。我在帖子中发布了编辑后的代码。
    • 哦,抱歉,printf 中有一个错误。我已经更新了代码。
    • 完美运行。谢谢!我的代表很低,所以我的投票没有显示,但我将答案标记为已接受并投票赞成。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多