7
votes

I need to plot a function f(x), where x is discrete set of values (in my case positive integers). I couldn't find a way to specify a step-size when using the range option and samples doesn't seem to be the right solution. Finally, I would like to approximate f(x) with a smooth function.

3

3 Answers

8
votes

I don't quite understand why samples is not the solution to your problem.

If I want to plot sin(x) on an interval between 0 and 10 with a point at every integer I use

set xrange [0:10]
set sample 11
plot sin(x) w p

Obviously the number of samples is xmax-xmin+1 (10 - 0 + 1 = 11).

Finally to tackle the approximation problem have a look at this website which discusses linear least squares fitting. For simple linear interpolation use lp instead of p.

1
votes

Or alternatively, play around with the ceil(x) or floor(x) functions.

Maybe have a look at this example: http://gnuplot.sourceforge.net/demo/prob2.html

0
votes

You can do:

plot [1:12] '+' u ($0):(f($0))

Where, $0 will be replaced by 1, 2, ..., 12. You can even do a smooth on this. For instance:

f(x)=sin(2*x)
plot [1:12] f(x) t 'the function'\
          , '+' u ($0):(f($0)) t 'the points'\
          , '+' u ($0):(f($0)) smooth cspline t 'the smooth'