2

I'm trying to iterate in parametric mode to plot several concentric arcs of circles with the parameter t ranging according to a function. I've tried, among others,

a=sqrt(2)
plot [-pi/2:pi/2] a*cos(t), a*sin(t)
do for [i=2:10] {
  a=sqrt(2)/i
  set trange [-1./2*acos(-(a**2)/2.):1./2*acos(-(a**2)/2.)]
  replot a*cos(t), a*sin(t)
}

what I see is a plot of 10 identical overlapped arcs. I also replaced replot with plot and only the last arc is retained.

I know "that iteration does not work for plots in parametric mode" (ref. "plot for" in the manual), but this is using a do for construct. There must be a way to do this! How?

System: gnuplot Version 5.2 patchlevel 2, windows 10.

the_eraser
  • 381
  • 1
  • 3
  • 14

3 Answers3

3

Today I developed my own solution, which is

a(i)=sqrt(2)/30*(31-i)
s(t, i)=t*(1./2*acos(-(a(i)**2)/2.))/(pi/2)
set trange [-pi/2:pi/2]
plot [-pi/2:pi/2] for [j=1:30] a(j)*cos(s(t,j)), a(j)*sin(s(t,j)) lw 2

Notice that in the meanwhile I made a little math adjustment from a=sqrt(2)/i to a(i)=sqrt(2)/30*(31-i).

Output:

arcs stopping at half lemniscate

The settings used to output that picture are

set term wxt size 800,800
set grid
set size ratio -1
set parametric
set xrange [-1.6:1.6]
set yrange [-1.6:1.6]

The rationale behind this is that in this way I set trange only once, and then with a variable substitution I map [0:pi/2] to [0:s(pi/2,i)].

the_eraser
  • 381
  • 1
  • 3
  • 14
2

You can use the for loop inside your plot statement. Try this:

set term png
set out "tmp.png"

unset key
set parametric

plot for [i=2:10]  (sqrt(2)/i)*cos(t), (sqrt(2)/i)*sin(t)

exit

Output:

first example

Update: the solution above won't take care of the trange requirement of the question. For that, one possible solution is to create a series of tables with proper ranges, and then loop through the files created for the plot. Something like the following:

set term png
set out "tmp.png"

unset key
set parametric

do for [i=2:10] {
  a=sqrt(2)/i
  set trange [-1./2*acos(-(a**2)/2.):1./2*acos(-(a**2)/2.)]
  set table 'data'.i.'.txt'
    plot a*cos(t), a*sin(t)
  unset table
}

plot for [i=2:10] 'data'.i.'.txt' w l

exit

Output:

second example

Hope this solution works! With a little help from this post.

Vinicius Placco
  • 1,683
  • 2
  • 14
  • 24
  • Thanks for your answer, but in your example the parameter `t` is not ranging accorging to a function. I mean that the `trange` should vary with the iteration variable `i`, like in `set trange [-1./2*acos(-(a**2)/2.):1./2*acos(-(a**2)/2.)]` (where `a=sqrt(2)/i`) – the_eraser Feb 26 '18 at 19:09
  • OK, I updated the answer with something that (I think) works. Have a look. – Vinicius Placco Feb 26 '18 at 19:33
  • Do you have any idea *why* your first script works? According to `help plot for` (on gnuplot 5.0) as the OP mentioned the iteration should stop at the comma and therefore should not work in parametric mode. The docs for 5.2 also state something similar. But you use it, and I have copied it, and it works. Why? – maij Feb 26 '18 at 20:22
  • Yes, I noticed that as well. I just made two tests, and if you do `plot for [i=2:10] (sqrt(2)/i)*cos(x), (sqrt(2)/i)*sin(x)` you'll end up with 9 `cos(x)` curves and one `sin(x)`, as expected from the `help plot for`. However, if you do `set parametric ; plot for [i=2:10] (sqrt(2)/i)*cos(t), (sqrt(2)/i)*sin(t)` you end up with the first plot above. It may be the case that the `for` loop behaves differently for parametric. – Vinicius Placco Feb 26 '18 at 20:31
  • Found some useful info here: https://stackoverflow.com/questions/20895415/for-loop-with-parametric-plots-in-gnuplot (for older versions) – Vinicius Placco Feb 26 '18 at 20:33
  • Yes, the `for` loop behaves differently for parametric. And it works where it should not work - good news. – maij Feb 26 '18 at 20:46
  • Thanks, it works. As for the manual, it may be a bug in the manual not being updated. I took my time to answer because today suddenly I got an idea and I developed my own solution, which I'm going to post below. Please check if you like it. – the_eraser Feb 27 '18 at 18:37
2

You can often avoid parametric mode by using the + special filename in conjunction with a using statement:

plot for  [i=2:10] [t=-1./2*acos(-((sqrt(2)/i)**2)/2.):1./2*acos(-((sqrt(2)/i)**2)/2.)] '+' using (sqrt(2)/i)*cos(t):(sqrt(2)/i)*sin(t) notitle with lines

enter image description here

user8153
  • 4,049
  • 1
  • 9
  • 18
  • Thanks, this works. As I said in a comment to @ViniciusPlacco answer, I developed my own solution, which I'm going to post below. – the_eraser Feb 27 '18 at 18:39