C-PLOT

Scientific Graphics and Data Analysis

calc.4

data calculator user function

SYNOPSIS

fn calc.4 [ expression [; expression...]]

DESCRIPTION

The user function calc.4 is a general-purpose calculator for manipulating data. The function uses a grammar consisting of vector names ( x , y, etc.), variable names (upper-case letters), arithmetic operators ( + , -, =, etc.), and function names ( sin() , sqrt(), log(), etc.). The arithmetic expressions you enter are performed for each point in the current data.

For instance, if you enter an expression such as y=2*x,the value of each y in your data set will be replaced with twice the value of the corresponding x.

LOWER-CASE LETTERS

The letters n and i have special meaning. By assigning to n,you set the number of points. When i is used on the right side of an equation, its value is the index number of the data point. (Index numbers start at 0.)

Valid vector names are x, y, z, r, s, t, t0, t1 and t2.Both r and z refer to the same data vector, representing x error bars in two-dimensional mode and z data in three-dimensional mode. The vector t refers to a data vector internal to calc.4 that can be used to hold intermediate results. This data vector retains its values through subsequent calls to calc.4. The vector t0 refers to the same storage as t,while t1 and t2 are additional temporary vectors.

You can assign to vectors by placing them on the left side of an equals sign, and you can use their current value by placing them on the right side of the equals sign.

UPPER-CASE LETTERS

The upper-case letters A to Z represent single-valued variables. They retain their values on subsequent calls to calc.4.

Variables can appear on either side of an equation. For instance, you may have::

fn calc.4  A = 1.4; B = 4.4e-2; C = 3.22e-4
fn .  y = Ax + Bxx + Cxxx

or:

fn calc.4 n=101; W=i*pi/50; x=cos(W); y=sin(W)

Assignment to variables only occurs if the number of current points is non-zero. If you have no current points, you may use n = 1 to force evaluation of of your expressions.

OPERATORS

The arithmetic operators are, in order of precedence::

                   (  )  Parenthesis for grouping
                   +  -  Unary plus and minus
                      ^  Exponentiation
                *  /  %  Multiply, Divide, Modulus
                   +  -  Addition and Subtraction
                      =  Assignment
+=  -=  *=  /=  %=  ^=  Assign with operator

Note, x += 1 is the same as x = x + 1, etc.

FUNCTIONS:

  exp() : exponent base e
  log() : logarithm base e
exp10() : exponent base 10
log10() : logarithm base 10
 sqrt() : square root
 fabs() : absolute value
 step() : 0 if argument < 0, otherwise 1
  sin() : sine
  cos() : cosine
  tan() : tangent
 asin() : inverse sine
 acos() : inverse cosine
 atan() : inverse tangent
 sinh() : hyperbolic sine
 cosh() : hyperbolic cosine
 tanh() : hyperbolic tangent
  rad() : converts from degrees to radians
  deg() : converts from radians to degrees
  int() : returns the integer part of argument
 rand() : returns random number between 0 and arg,
          uses the C-library \f*(#Crand()\f1 routine.
   j0() : Bessel function of the first kind, order 0
   j1() : Bessel function of the first kind, order 1
   y0() : Bessel function of the second kind, order 1
   y1() : Bessel function of the second kind, order 1
  erf() : error function
 erfc() : 1 - erf()

OTHER THINGS

The name pi is special and stands for the constant 3.14159...

Only one line of input is allowed, but you can have multiple expressions on that line separated by semicolons. When you have multiple expressions, the whole group is evaluated for the first data point ( i = 0 ), then for the next data point ( i = 1 ) and so on. If you have no points, the expressions won't be evaluated at all.

An expression preceded by the word once will only be evaluated for the first point ( i = 0 ).

You can enter the expressions on the command line from plot or when prompted.

If you just enter a dot as the expression, as in fn . ., the same input line is used as the last time.

Spaces aren't significant in the expressions.

ERROR HANDLING Syntax errors from bad grammar are noted with the offending character indicated.

On these error conditions:
dividing by zero,
taking the square root of a negative number,
taking the logarithm of 0 or a negative number,
raising 0 to a non-positive exponent,
raising a negative number to a non-integral exponent,

an error message is printed showing the index number. The offending operation is not performed, but calculations continue.

The errors:

Math library domain error,
Math library range error,

may be produced if certain math library functions have problems with your numbers.

EXAMPLES

Create a circle of 101 points, radius 3:

fn . n=101; x=3cos(pii/50); y=3sin(pii/50)

Invert x, set error bars to the square root of y:

fn . x ^= -1; s = y^.5

or:

fn . x = 1 / x ; s = sqrt(y)

Assign x and y in one expression, 201 points, x runs from -10 to 10:

fn . n=201; y = cos(x = -10 + 20 * i / 200)

Increment the variable A,and increment each y by 0.2 times A

fn . once A += 1; y += A * 0.2

(Note the once means A only gets incremented once. You can now enter fn .. to increase the y values in steps, perhaps to plot a series of curves offset by constant increments.)

SEE ALSO

fn