How do I plot step functions in gnuplot?

In summary, the conversation discusses the use of gnuplot to plot step functions from the command line, specifically floor/greatest integer functions. The examples provided require external data files and the suggestion is to increase the sampling resolution before plotting. The conversation also mentions a way to declare a step function using the signum function.
  • #1
neutrino
2,094
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
  • #2
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.
 
  • #3
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:
  • #4
Try to increasing the sampling resolution before plotting:

set samples 10000
 
  • #5
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. :)
 
  • #6
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
 
  • #7
You can declare a step function by using signum function by entering the command:

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

1. How do I define a step function in gnuplot?

To define a step function in gnuplot, you can use the "set samples" command to set the number of sample points and the "plot" command to specify the function using the "step" keyword. For example:
set samples 1000
plot step(x)

2. How do I plot a step function with varying step sizes?

You can use the "using" keyword in the "plot" command to specify a variable step size. For example:
plot step(x) using ($1*2)

3. How do I label the x and y axes for a step function plot?

You can use the "set xlabel" and "set ylabel" commands to label the axes. For example:
set xlabel "x"
set ylabel "y"
plot step(x)

4. Can I customize the style of my step function plot?

Yes, you can use the "with" keyword in the "plot" command to specify a particular style, such as "steps", "histeps", or "fsteps". For example:
plot step(x) with steps

5. How do I save my step function plot as an image file?

You can use the "set terminal" and "set output" commands to specify the type of image file and the file name, respectively. For example:
set terminal png
set output "step_function.png"
plot step(x)

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
5
Views
12K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
984
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Replies
13
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
Back
Top