© MatCalc (2008)
 
 
 
 
 
 
 
 
   
 
   

Syntax for user-defined functions and variables

Introduction

MatCalc provides the possibility for the user to define functions and variables, using numerical expressions, built-in variables (F$*, T, X$*$* etc.), built-in constants and the functions listed below. See the priority rules section for information on how algebraic expressions are interpreted.

Use

Functions and variables can be defined using the 'Functions' and 'Variables' tabs of 'Global > Variables & functions' or using the following commands in the console:

SET_FUNCTION_EXPRESSION <name> <expression> for functions
SET_VARIABLE_VALUE <name> <expression> for variables

Interpretation: functions and variables

In the case of a function, the expression is given as a function of the step-value 'x' or of a function which varies with x. When the step-value is varied, for example during a stepped equilibrium calculation, the user-defined function is evaluated at each value of x. A simple example to illustrate this is as follows:

SET_FUNCTION_EXPRESSION f1 T-273.16 : this defines a function f1 which gives the same values as the built-in function T$C.

Variables, by contrast, can only take numerical values. The expression assigned to the variable is evaluated using the current values of all quantities.

Thus,

SET_VARIABLE_VALUE v1 T-273.16

or

SET_VARIABLE_VALUE v1 f1

assign the current value of T-273.16 to v1.

When an equilibrium calculation is performed at a different temperature, the value of f1 will change but v1 will remain the same.

Examples

Tutorial 6 provides several examples of the use of variables and functions in MatCalc.

Syntax list

Basic arithmetic

+
plus
-
minus
*
multiply
/
divide
()
parantheses
**
exponentiation
^
exponentiation

Available constants

Name Meaning Value
NA Avogadro's number 6.02214199e+023
PI pi 3.14159265359
R Gas constant 8.31451

Functions

Name Meaning Example
ABS(a) absolute value of a abs(-1) = 1
SQRT(a) square root of a sqrt(4) = 2
SQR(a) square of a sqr(4) = 16
INV(a) 1/a inv(2) = 0.5
EXP(a) e raised to the power of a exp(1) = 2.71828182846
LN(a) natural log of a ln(1) = 0
POW10(a) 10 raised to power of a pow10(2) = 100
LOG(a) log to base 10 of a log(100) = 2
SIN(a) sine of a (a in radians) sin(pi/2) = 1
COS(a) cosine of a (a in radians) cos(0) = 1
TAN(a) tangent of a (a in radians) tan(pi/4) = 1
COT(a) cotangent of a (a in radians) cot(pi/4) = 1
ASIN(a) arcsin of a (in radians) asin(1) = 1.57079632679
ACOS(a) arccos of a (in radians) acos(1) = 0
ATAN(a) arctan of a (in radians) atan(1) = 0.785398163397
ACOT(a) arccot of a (in radians) acot(0) = 1.57079632679
SINH(a) hyperbolic sine of a sinh(0) = 0
COSH(a) hyperbolic cosine of a cosh(0) = 1
TANH(a) hyperbolic tangent of a tanh(0) = 0
COTH(a) hyperbolic cotangent of a coth(1) = 1.3130352855
ERF(a) error function of a erf(0) = 1
ERFC(a) error function complement of a erfc(0) = 0
POW(a)(b) a raised to the power of b pow(3)(2) = 9
MAX(a)(b) maximum of a and b max(3)(2) = 3
MIN(a)(b) minimum of a and b min(3)(2) = 2

Priority rules

The usual priority rules apply:

  1. expressions in parentheses are evaluated first
  2. exponentiation
  3. multiplication and division
  4. addition and subtraction
  5. in the case of operators with equal precedence, the expression is evaluated from left to right

Some examples:

4*5**2 = 100

(4*5)**2 = 400

4+5*2 = 14

(4+5)*2 = 18

4-5-2 = -3

4-(5-2) = 1

4/5*2 = 1.6

4/(5*2) = 0.4

4**5**2 = 1048576

4**(5**2) = 1.12589990684e+015

‹‹ to the Top ››  ‹‹ to the Reference files ››