next up previous contents index
Next: Glossary Up: Expressions Previous: Gnuplot-defined variables   Contents   Index


User-defined variables and functions

New user-defined variables and functions of one through five variables may be declared and used anywhere, including on the plot command itself.

User-defined function syntax:

     <func-name>( <dummy1> {,<dummy2>} ... {,<dummy5>} ) = <expression>

where 5#5expression6#6 is defined in terms of 5#5dummy16#6 through 5#5dummy56#6.

User-defined variable syntax:

     <variable-name> = <constant-expression>

Examples:

     w = 2
     q = floor(tan(pi/2 - 0.1))
     f(x) = sin(w*x)
     sinc(x) = sin(pi*x)/(pi*x)
     delta(t) = (t == 0)
     ramp(t) = (t > 0) ? t : 0
     min(a,b) = (a < b) ? a : b
     comb(n,k) = n!/(k!*(n-k)!)
     len3d(x,y,z) = sqrt(x*x+y*y+z*z)
     plot f(x) = sin(x*a), a = 0.2, f(x), a = 0.4, f(x)


     file = "mydata.inp"
     file(n) = sprintf("run_%d.dat",n)

The final two examples illustrate a user-defined string variable and a user-defined string function.

Note that the variable pi is already defined. But it is in no way magic; you may redefine it to be whatever you like. Some other variables may be defined under various gnuplot operations like mousing in interactive terminals or fitting; see gnuplot-defined variables (p. [*]) for details.

You can check for existence of a given variable V by the exists("V") expression. For example

     a = 10
     if (exists("a")) print "a is defined"
     if (!exists("b")) print "b is not defined"

Valid names are the same as in most programming languages: they must begin with a letter, but subsequent characters may be letters, digits, "$", or "_".

See show functions (p. [*]), functions (p. [*]), gnuplot-defined variables (p. [*]), macros (p. [*]).


next up previous contents index
Next: Glossary Up: Expressions Previous: Gnuplot-defined variables   Contents   Index
Ethan Merritt 2007-03-03