Search code examples
outputmaximawxmaxima

Maxima add current loop iteration to filename


I have code similar to the one below, where a function with a parameter depending on the loop iteration is plotted after every iteration. I would like to save the plot with the name trigplot_i.ps where i is the iteration number, but don't know how.

I have tried trigplot_"i".ps but didn't work, and have not been able to find how to cast i to a string either.

I'm a beginner so any help is very welcome.

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"])
);

code after edits gives an error:

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"])
);

error: "declare: argument must be a symbol; found "./trigplot_1.ps -- an error. To debug this try: debugmode(true);"


Solution

  • Looks good. To construct a file name, try this: sconcat("./trigplot_", i, ".ps") or also you can try: printf(false, "./trigplot_~d.ps", i). My advice is to make that a separate step in the loop, and then you can use it in the call to plot2d, e.g.:

    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>));
    

    EDIT: Fixed a bug in printf (omitted argument i).