1

The issue

Assume the explicit solution to a differential equation to be:

(%i1) x(t):=((x[0]*%e^(k*t))/(x[0]*%e^(k*t)-x[0]+1))

Obviously the parameter k affects the slope of the solutions, as can be seen by graphing them together for different values of k (in this example, varying from k=0.25 to k=1.75 with step 0.25).

Trying to plot the graphs this way

(%i6) plot2d(
        makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
        [t,0,15],
        cons(legend, makelist("k=d*0.25", d,1,7)),
        [gnuplot_preamble,"set key right bottom"]
      )$

however yields a plot with "static" labels for each solution: enter image description here

Next we returned to a solution offered here on stackoverflow (maxima-plotting-in-a-loop) and adapted it to this case:

(%i7) makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7);
          plot2d(%, [t,0,15],
          [gnuplot_preamble,"set key nobox spacing 1.1 bottom right"]
      )$

This is an improvement over the first attempt, but this code prints the entire function names x(t)=(x_0*e^(k*t))/(x_0*e^(k*t)-x_0+1) with k=d×0.25,d=1,…,7 as legend labels and thus it is not very reader-friendly.

enter image description here

What we actually want to have in the legend are just the k-values from this list:

(%i8) kV: makelist(k=d*0.25, d,1,7);
(%o8) [k=0.25,k=0.5,k=0.75,k=1.0,k=1.25,k=1.5,k=1.75]

So we tried this to get the following error message:

(%i11) plot2d(
           makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
           [t,0,15],
           cons(legend, makelist(kV)),
           [gnuplot_preamble,"set key right bottom"]
       )$

Value of option legend. should be a string or false, not "
                [k = 0.25,k = 0.5,k = 0.75,k = 1.0,k = 1.25,k = 1.5,k = 1.75]
                ".
-- an error. To debug this try: debugmode(true);

Does anyone know how to get these k values into the legend?

Any help/suggestions appreciated.

Cheers, Tilda

The issue solved

Robert's suggestion helped. So

plot2d(
    makelist(
      subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
    [t,0,15],
    cons(legend, map(string, kV)),
    [gnuplot_preamble,"set key right bottom"]
)$

yields the correct plot now: enter image description here

Thanks to Raymond's answer and his clarifications on the intricacies of Maxima's printf(), we now have the icing on the cake:

plot2d(
  makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
  [t,0,15],
  cons(legend, makelist(printf(false,"k = ~,2f", 0.25*d), d,1,7)),
  [gnuplot_preamble, "set key nobox spacing 1.1 right bottom"]
)$

enter image description here

A workaround to typesetting titles in a typographically correct way is to use captions, as can be seen from a screenshot of a TeXmacs session. This is not really the jewel to the crown, but pretty close.

enter image description here

1 Answers1

2

An alternative:

plot2d(
        makelist(subst([k=d*0.25,x[0]=0.1],x(t)), d,1,7),
        [t,0,15],
        cons(legend, makelist(printf(false, "k=~2f", 0.25*d), d,1,7)),
        [gnuplot_preamble,"set key right bottom"]
      );

This is kind of nice because then all the legends line up nicely.

Raymond Toy
  • 5,490
  • 10
  • 13
  • Ah, trusty old `printf()`. The problem with `"k=~2f"` and `"k=~3f"`is that they round and do not yield correct numerical results in any case; `"k=~4f"` does, but then the nice alignment is gone as `0.5` or `1.5` is not printed as `0.50` or `1.50` respectively . – tilda-a-steiner Dec 08 '22 at 16:58
  • 1
    It's actually Common Lisp `format`. You have to choose the right option. I think "~,2f" works better. 0.5 and 1.5 get printed at "0.50" and "1.50" so it lines up nicely again. The user has to decide what the right format specifier should be. – Raymond Toy Dec 09 '22 at 02:55
  • Dear Raymond, thanks for reminding us that maxima `printf()` is in fact Clisp `format`. The comma in `"~,2f"` is more than the icing on the cake. Now, the crown to the jewel would be to have the title `x(t)=((x[0]*%e^(k*t))/(x[0]*%e^(k*t)-x[0]+1))` typeset in TeX, but I guess that's then another ticket. – tilda-a-steiner Dec 09 '22 at 16:42
  • I think that's an issue with gnuplot, the default plotting program for maxima. I don't know if gnuplot can do that, but I didn't check. – Raymond Toy Dec 16 '22 at 05:46
  • Not in a straightforward way. See Janert, _gnuplot in Action_, 2nd ed., Ch.10.4, "Using gnuplot with LaTeX". The bottom line is that setting the terminal to `cairolatex` will split the gnuplot graph into its textual (*.tex) and graphical components (*pdf) that will then be post-processed by LaTeX. Fine, if you still use LaTeX, yet I've moved to TeXmacs for various reasons some time ago. So, instead of titles to plots, I decided to use figure captions and typeset the formulae in a typographically correct way there. – tilda-a-steiner Dec 16 '22 at 16:16
  • I haven't figured out if the method de bribed above also works from within Maxima and its interfaced to gnuplot. Maybe somebody else knows. – tilda-a-steiner Dec 17 '22 at 17:58
  • 1
    If you don't mind using an HTML canvas, then https://stackoverflow.com/questions/20852529/rendering-mathjax-output-on-canvas suggests ways on doing this. While gnuplot supports a canvas output, I don't know how to hook up the suggestions in the link to the gnuplot canvas output. Would be cool if gnuplot has a terminal that could do that. – Raymond Toy Feb 11 '23 at 20:27