Search code examples
gnuplot

gnuplot: plotting custom function at given discrete points with linespoints


I want to plot a function, let say, f(x) = 1-0.5^x. And I want to plot it at given discrete points: x = 4,8,12,16...32, with points connected with the linespoints style. No external file is supposed to be needed. I know there's a way to do it in matlab/octave but I want to do it in gnuplot (for consistent style, because I have many other figures drawn in gnuplot). I have searched for results about plotting functions over a continuous interval. But I can't find how I can explicitly set some x points to plot. Pre-generating the x's and y's into a file is a viable option but sounds stupid. Is there a way to do it on the fly in gnuplot?


Solution

  • For equidistant x-values like you mention you must only set xrange, xtics and the number of samples properly:

    set xrange [4:32]
    set samples (32 - 4)/4 + 1
    set xtics 4
    f(x) = 1 - 0.5**x
    plot f(x) with linespoints pt 7 ps 2
    

    In general you cannot define an array of value where you want the numbers to be computed. If you need a more sophisticated selection of sampling values, you could use the + special file name.

    enter image description here