Piecewise linear function / function expression

Symbol

image-20221202-084004.png

Usage & working

Both function expressions calculate a defined mathematical function.

  • In the piecewise function you can define a function based on points on the graph

  • the function expressions allow you to define a specific function to calculate

Syntax function expression

The function expression evaluates the input variable “x” using the single line function expression. The syntax is similar to the syntax of the programmeable controller, with the additional constraint that the expression must be one line.

The following syntax can be used:

  • operators (note you do not need to put x = … in front of the expression) :

    • increment operators (++ and --)

    • unary operators (- and !) 

    • binary operators : power (^), multiply (*), modulo (%), divide (/), sum (+), min (-)

    • ternary operator (a = x ? b : x)  see https://www.freecodecamp.org/news/c-ternary-operator/

      • Example: Switch command. If x is one, output = a, else output = b

        • (x==1)? a : b

          (x==1)? a : b
          
    • conditional operators (|| and &&)

    • comparison (<=', >=, >, <, <=, ==, '!=)

    • mathematical functions : exp, ln, sin, cos, tan, sqrt, min(x1, x2), max(x1, x2), abs, floor, ceil, sgn (signum)

Common errors

  • It not allowed to use a comma (,) as decimal separator in numbers.

    • Example: 0,5 will yield a syntax error. The correct way to write decimal numbers is using dot as decimal separator, so the number should be written as 0.5

  • Contrary to the programmeable controller, the function expression should not be closed with a semicolon. Adding a semicolon to the function expression would yield a syntax error

  • When using min(x1 , x2) or max (x1, x2) functions, you should use a “comma” inbetween x1 and x2

  • You are not allowed to place x = … or y = … in front of the expression as this will result in a syntax error. You should only write the expression itself, meaning x = 7*x would just become 7*x