Mathematical Functions

From SysCAD Documentation
Jump to: navigation, search

Navigation: PGMs

PGM Main Page Defining a Class Defining a Function Tag Functions Mathematical Functions Defining a Subroutine
Example PGM Files Predefined Classes Predefined Functions Tag Functions (9.3) String Functions Trigger Subroutines

Maths Functions

The PGM Language supports the maths functions in the Table below.

Notes:

  1. All Function names are NOT case sensitive.
  2. To use the negative of a value in any of these functions (where allowable), users must enter -1 * value;
    For example y = exp(-x) is NOT allowed, use y = exp(-1 * x)
  3. Angles in trigonometric functions are specified in units of radians, not degrees. 1 radian is approximately 57.3 degrees, 1 degree is approximately 0.01745 radians.
    [math]\mathbf{\mathit{radians = \frac {180^{o}}{\pi}}}[/math] (Please see Example 4 for sample conversion functions.)
Mathematical Functions See Also: Math Function Examples
Function Syntax Description
Sin variable = Sin(variable1) The Sin function, returns the sine of the argument in radians. Variable1 = an angle in radians
aSin
Only available in Build 137.
variable1 = aSin(variable) The aSin function, is the inverse of the sine function. Returns Variable1, an angle in radians.
NOTE: Valid range: -1 <= Variable <= 1, for out of range variables, variable1 will be limited by the range, math error will be given in the GControl.
Cos variable = Cos(variable1) The Cos function, returns the cosine of the argument in radians. Variable1 = an angle in radians
aCos
Only available in Build 137.
variable1 = aCos(variable) The aCos function, is the inverse of the cosine function. Returns Variable1, an angle in radians.
NOTE: Valid range: -1 <= Variable <= 1, for out of range variables, variable1 will be limited by the range, math error will be given in the GControl.
Tan variable = Tan(variable1) The Tan function, returns the tangent of the argument in radians. Variable1 = an angle in radians
aTan
Only available in Build 137.
variable1 = aTan(variable) The aTan function, is the inverse of the tangent function. Returns Variable1, an angle in radians.
ATan2 variable = ATan2(y1, y2, x1, x2) The ATan2 function, returns the angle, in radians, given by the two points.
Degrees
Only available in Build 137.
variable = Degrees(variable1) This function is used to convert radians to degrees. Can be used to interpret results from the trigonometric functions above. Returns Variable, an angle in degrees from Variable1, an angle in radians.
Radians
Only available in Build 137.
variable = Radians(variable1) This function is used to convert degrees to radians. Can be used as input for the trigonometric functions above. Returns Variable, an angle in radians from Variable1, an angle in degrees.
Abs variable = Abs(variable1) The Abs function, returns the absolute value of variable1
Sqrt variable = Sqrt(variable1) The Sqrt function, returns the square root of variable1.
Note: If variable1 is negative an error occurs as the operation is undefined.
Exp variable = Exp(variable1) The Exp function, returns the exponential of variable1.
Note: If the user wishes to use the negative of variable1, the correct syntax is: variable = Exp(-1 * variable1), NOT variable = Exp(-variable1)
Ln variable = Ln(variable1) The Ln function, returns the natural logarithm of variable1.
Note: If variable1 is zero or negative an error occurs as the operation is undefined and the function returns 0.
Log variable = Log(variable1) The Log function, returns the logarithm of variable1.
Note: If variable1 is zero or negative an error occurs as the operation is undefined and the function returns 0.
Pow variable = Pow(variable1, variable2) OR
variable = variable1^variable2
The Pow function, raises variable1 to the power of variable2. Alternatively, the ^ operator can be used.
Notes:
  1. If variable1 is negative and variable2 is not an integer an error occurs as the operation is undefined.
  2. If the user wishes to use the negative of variable2, the correct syntax is either variable = Pow(variable1, -1 * variable2) OR variable = variable1^(-1 * variable2), NOT variable = Pow(variable1, -variable2)
Max variable = Max(variable1, variable2) The Max function, returns the maximum of variable1 and variable2.
Min variable = Min(variable1, variable2) The Min function, returns the minimum of variable1 and variable2.
Range variable = Range(low_limit, variable1, hi_limit) The Range function, ranges variable1 between the two limits.
Trunc variable = Trunc(variable1) The Trunc function, returns the integer part of variable1.
Round variable = Round(variable1) The Round function, rounds variable1 to the closest integer variable.
Roundup variable = RoundUp(variable1, decimals) Variable1: is any real number that you want rounded up; decimals - is the number of digits to which you want to round number.
If decimals is 0, then number is rounded up to the nearest integer.
If decimals is less than 0, then number is rounded up to the left of the decimal point.
Mod variable = Mod(numerator, denominator) The Mod function, returns the remainder of numerator divided by denominator. The result and the variables may be double variable types. See also Div.
Note: Division by zero will result in an error as it is undefined.
Div variable = Div(numerator, denominator) The Div function, returns the quotient of numerator divided by denominator. The result is a long data type and the parameters may be double variable types. See also Mod.
Note: Division by zero will result in an error as it is undefined.
erf variable = erf(variable1) The erf function returns the the error function integrated between 0 and variable1.

The form of the function is: [math]\operatorname{erf}(z) = \frac{2}{\sqrt{\Pi}}\int\limits_{0}^{z}e^{-t^2} dt\,[/math]

IsNAN variable = IsNAN(variable1) variable1 = double to be tested; The IsNAN function, returns true if variable1 is NAN (not a number) otherwise it returns false. Variable1 should be a double.
See also NAN in Predefined Constants and Variables.

Examples

Example 1: Math Functions

REAL         Sin_a@, Cos_b@, Tan_c@, aSin_a@, aCos_b@, aTan_c@, f@
INTEGER      w@
BIT          i*
BYTE         z*
CONST DOUBLE g = 2.5
CONST DOUBLE h = -3.4
CONST DOUBLE a = 0.6
CONST DOUBLE b = 2
CONST DOUBLE c = -0.5

Sin_a  = sin(a) 	     ;Sin_a= 0.56464 	
Cos_b  = cos(b) 	     ;Cos_b=-0.41615	
Tan_c  = tan(c) 	     ;Tan_c=-0.54630	
aSin_a = aSin(Sin_a)         ;aSin_a= 0.6, implemented in Build137 	
aCos_b = aCos(Cos_b)         ;aCos_b=2.0,  implemented in Build137	
aTan_c = aTan(Tan_c)         ;aTan_c=-0.5, implemented in Build137
f = atan2(1,2,1,2) 	     ;f=0.785	
f = abs(g) 	             ;f=2.5	
f = abs(h) 	             ;f=3.4	
f = sqrt(g) 	             ;f=1.581	
f = sqrt(h) 		     ;f=* (NAN)	(error)
f = exp(-1 * g)              ;f=0.0821	
f = exp(h) 	             ;f=0.0334	
f = ln(g) 	             ;f=0.916	
f = ln(h) 	             ;f=* (NAN)	(error)
f = ln(0) 	             ;f=0	(error)
f = log(g) 	             ;f=0.398	
w = log(h) 	             ;f=-2147483648 (error)
f = pow(g,h) 	             ;f=0.044	
f = pow(h,g) 	             ;f=* (NAN)	(error)
f = trunc(g) 	             ;f=2.0	
f = trunc(h) 	             ;f=-3.0	
f = round(g) 	             ;f=3.0	
f = round(h) 	             ;f=-3.0	
w = mod(7,3) 	             ;w=1	
w = div(7,3) 	             ;w=2	
f = mod(3.4,1.1) 	     ;f=0.1	
f = div(3.4,1.1) 	     ;f=3.0	
f = mod(g,h) 	             ;f=2.5	
f = mod(h,g) 	             ;f=-0.9	
f = mod(h,0) 	             ;f=* (NAN)	(error)
w = div(g,h)	             ;w=0	
w = div(h,g)	             ;w=1	
f = erf(1)   	             ;f=0.84279	
f = min(g,h) 	             ;f=-3.4	
f = max(g,h) 	             ;f=2.5	
f = range(0,h,4) 	     ;f=0.0	
f = range(0,g,4) 	     ;f=2.5	
f = range(h,-4.2,g) 	     ;f=-3.4	
f = range(g,-4.2,h) 	     ;f=2.5	
f = Round(2.5)               ;rounds to 3
f = Round(0.1)               ;rounds to 0
f = Round(-3.7)              ;rounds to -4
f = Round(-3.4)              ;rounds to -3
f = ROUNDUP(3.2,0)           ;Rounds 3.2 up to zero decimal places (4)
f = ROUNDUP(76.9,0)          ;Rounds 76.9 up to zero decimal places (77)
f = ROUNDUP(3.14159, 3)      ;Rounds 3.14159 up to three decimal places (3.142) 
f = ROUNDUP(-3.14159, 1)     ;Rounds -3.14159 up to one decimal place (-3.2) 
f = ROUNDUP(31415.92654, -2) ;Rounds 31415.92654 up to 2 decimal places to the left of the decimal (31500)
f = ROUNDUP(3.2,-3)          ;Rounds 3.2 up to 3 decimal places to the left of the decimal (1000)

Example 2: Calculating pH

Real Tank1_pH@, H_Concentration@("ConcMl", "mol/L")

H_Concentration = GetTag("P_001.Qo.CMlC:LPh.HCl (mol/L)") * 1
Tank1_pH        = -1 * Log(H_Concentration)

$

Example 3: Inverse Trigonometric Functions

Inverse trigonometric functions have been implemented in SysCAD 9.3 Build 137, see functions aSin, aCos and aTan.

For users using older versions of SysCAD, where Inverse trigonometric functions were not available, the Atan2 function can be used:

Double Z<0,1>*, ArcSinZ("Ang","rad")@, ArcCosZ("Ang","rad")@
 ArcSinZ = Atan2(0, Z, 0, sqrt(1.0 - Z^2))
 ArcCosZ = Atan2(0,sqrt(1.0 - Z^2) , 0, Z)

Double ZZ*, ArcTanZ("Ang","rad")@
 ArcTanZ = Atan2(0, ZZ, 0, 1.0)

Example 4: Radians and Degrees Conversion

Radians and Degrees Conversion functions have been implemented in SysCAD 9.3 Build 137, see functions Radians("Degree") and Degrees("Radians").

For users using older versions of SysCAD, where angle conversion functions were not available, the following user defined functions can be used (note that PI is a predefined function in SysCAD):

;--- variable declarations ---
TextLabel(, "Convert Radians to Degrees")
Real	Angle_Radians_Input*<<1>>{Comment("Radians")}
Real	Angle_Degrees_Result@{Comment("Degrees")}
TextLabel(, "Convert Degrees to Radians")
Real	Angle_Degrees_Input*<<180>>{Comment("Degress")}
Real	Angle_Radians_Result@{Comment("Radians")}

;--- Define Functions ---
Function RadToDeg(Real UserDefined_Radians)
   Return UserDefined_Radians * 180 / PI
EndFunct
	
Function DegToRad(Real UserDefined_Degrees)
   Return UserDefined_Degrees / 180 * PI
EndFunct

;--- Convert angles ---
Angle_Radians_Result = DegToRad(Angle_Degrees_Input)
Angle_Degrees_Result = RadToDeg(Angle_Radians_Input)

$  ; --- end of file ---

AngleFunction.png