Search code examples
gnuplot

GNUplot samples and isosamples for ackermann function


So I've been learning GNUplot and one thing that's been bugging me is the sample and isosample rate (esp when computing Ackermann functions). I know that isosample rate is supposed to control the number of isolines while the other does the same thing for sampling along each isoline.

This code plots the ackermann function (source: gnuplot sourceforge). Why do only some specific values of samples and isosamples generate the plot? The only other values which worked were (4,2) and (2,4). For the remaining values I got a stack overflow or recursive depth limit exceeded.

ack(m,n) = (m == 0) ? n + 1 : (n == 0) ? ack(m-1,1) : ack(m-1,ack(m,n-1))
set xrange [0:3]
set yrange [0:3]
set isosamples 4
set samples 4
set title "Plot of the ackermann function"
splot ack(x, y)

Can somebody please explain why only (4,4) , (4,2), (2,4) work?


Solution

  • There are two issues here:

    1. As mentioned in the comments above, you cannot evaluate the Ackermann function for non-integer values. If your samples/isosamples value do not exactly match the length of your xrange/yrange then gnuplot will try to evaluate the function at non-integer values. You can avoid that by explicit conversion to integers:

      ack(m,n) = (m == 0) ? n + 1 : (n == 0) ? ack(m-1,1) : ack(m-1,ack(m,n-1))
      ack_protected(x,y) = ack(int(x),int(y))
      set xrange [0:3]
      set yrange [0:3]
      set isosamples 50
      set samples 50
      set title "Plot of the ackermann function"
      splot ack_protected(x, y)
      

    gives

    enter image description here

    1. You also cannot evaluate the Ackermann function for integer values of (m,n) that would exceed the recursion limit of gnuplot, which is 250. The combination m=4, n=3 already exceeds that limit.