How do I plot step functions in gnuplot?

AI Thread Summary
The discussion focuses on plotting step functions using gnuplot from the command line, specifically the floor and ceiling functions. Users express confusion over examples that seem to require external data files, but solutions are provided for plotting directly. A key suggestion is to increase the sampling resolution with the command "set samples 10000" to improve the appearance of the step functions, which initially displayed slanted lines connecting horizontal segments. Additionally, a method to create step functions using piecewise-defined functions is mentioned, along with the use of the signum function to define steps. The conversation emphasizes the importance of adjusting settings to achieve the desired visual output in gnuplot.
neutrino
Messages
2,091
Reaction score
2
Among the demos at the gnuplot website, I saw this - http://gnuplot.sourceforge.net/demo/steps.1.gnu, but it does not make much sense to me. :(

Could someone tell me how to plot step functions from the command line, just like you would the usual functions? Specifically, I'd like to plot floor/greatest integer functions.

Thanks
 
Physics news on Phys.org
plot [-5:5][-5:5] floor(3*sin(x))

plot [-5:5][-5:5] ceil(x**2)

Your examples seem to require external data files.
 
robphy said:
plot [-5:5][-5:5] floor(3*sin(x))

plot [-5:5][-5:5] ceil(x**2)

Your examples seem to require external data files.

Thanks a lot, robphy! :-)

EDIT: I just tried an example. While it does plot the lines of constancy, there are slanted lines that "connect" the horizontal ones. Is there a way I could get rid of them, i.e., the slanted ones?
 
Last edited:
Try to increasing the sampling resolution before plotting:

set samples 10000
 
robphy said:
Try to increasing the sampling resolution before plotting:

set samples 10000

Nice. Although the slants have only become vertical, they at least look like steps. :biggrin: Thanks, again. :)
 
http://www6.uniovi.es/gptug/node5.html
makes a suggestion to plot two piecewise-defined functions with illegal parts:

f1(x)=(x<1) ? 0 : sqrt(-1)
f2(x)=(x<1) ? sqrt(-1): 1

plot [-5:5][-2:2] f1(x),f2(x)

you'll have to poke around to set the color of each function to be the same
 
You can declare a step function by using signum function by entering the command:

u(x,t)=(1+sgn(x-t))/2
 
Back
Top