f(x)

Math Function

Math
Run the live example Free · no sign-up · runs in your browser

The MathFunction block applies a selectable analytical or integer-rounding mathematical operation to the incoming scalar signal u(t)u(t). Supported operations include exponential, logarithmic (natural and base-10), power, remainder, and rounding functions (nearest, floor, ceil). It is commonly used in physical plant modeling (e.g., chemical kinetics, thermodynamics, fluid drag, acoustic decibel scaling) and signal processing without requiring custom code blocks.

Mathematical Model

The block computes the output y(t)y(t) as a function of the input signal u(t)u(t) and the secondary parameter constant (cc):

y(t)=f(u(t),c)y(t) = f(u(t), c)

The mathematical behavior for each selectable function is defined as follows:

  • Exponential (exp):

```latex

y(t) = \exp(u(t)) = e^{u(t)}

```

  • Natural Logarithm (log):

```latex

y(t) = \ln(u(t)), \quad u(t) > 0

```

For non-positive inputs (u≤0u \le 0), the block returns −∞-\infty to avoid unhandled NaN propagation.

  • Common Logarithm (log10):

```latex

y(t) = \log_{10}(u(t)), \quad u(t) > 0

```

Returns −∞-\infty for u≤0u \le 0.

  • Power (pow):

```latex

y(t) = (u(t))^c

```

Raises the input base u(t)u(t) to the exponent specified by constant (cc). Negative bases with non-integer exponents produce NaN.

  • Remainder (rem):

```latex

y(t) = u(t) \bmod c = u(t) - c \cdot \operatorname{trunc}\left(\frac{u(t)}{c}\right)

```

Computes the floating-point remainder with division-by-zero protection (returns 0.00.0 if ∣c∣<10−6|c| < 10^{-6}).

  • Round (round):

```latex

y(t) = \operatorname{round}(u(t))

```

Rounds to the nearest integer value.

  • Floor (floor):

```latex

y(t) = \lfloor u(t) \rfloor

```

Rounds down to the largest integer less than or equal to u(t)u(t).

  • Ceiling (ceil):

```latex

y(t) = \lceil u(t) \rceil

```

Rounds up to the smallest integer greater than or equal to u(t)u(t).

Inputs & Outputs

Direction ID Label Type Status
→ In in Input number Required
← Out out Output number Output

Parameters

Parameters Label Type Default Description
function Function select exp
constant Constant (n) number 2

Usage Examples

Example 1: Arrhenius Temperature-Dependent Reaction Kinetics

In chemical reactor simulation, the reaction rate coefficient varies exponentially with temperature according to the Arrhenius law k(T)=Aexp⁡(−Ea/(RT))k(T) = A \exp(-E_a / (R T)). Feeding the dimensionless normalized activation term −Ea/(RT)-E_a / (R T) into a MathFunction block with function = "exp" and multiplying the output by pre-exponential factor AA models continuous kinetic reaction rates.

Example 2: Sound Pressure Level Decibel (dB SPL) Conversion

In acoustic instrumentation, root-mean-square pressure prmsp_{\text{rms}} is converted to decibels relative to the hearing threshold (p0=20 μPap_0 = 20\ \mu\text{Pa}): Lp=20log⁡10(prms/p0)L_p = 20 \log_{10}(p_{\text{rms}} / p_0). Dividing measured pressure by reference p0p_0, feeding the ratio into MathFunction with function = "log10", and scaling by a Gain block of 2020 yields real-time calibrated decibel sound levels.

Remarks & Best Practices

  • Logarithmic Out-of-Domain Protection: For non-positive inputs (u≤0u \le 0), log and log10 return −∞-\infty rather than producing NaN. This allows downstream Saturation or MinMax blocks to clamp the signal safely without poisoning subsequent calculations.
  • Remainder Divisor Safeguard: When using rem, if the divisor magnitude ∣c∣|c| is smaller than 10−610^{-6}, the block returns 0.00.0 to prevent floating-point division by zero.
  • Fractional Exponents: Non-integer exponents in pow (e.g. c=0.5c = 0.5) with negative bases produce NaN according to real arithmetic. For dedicated root extraction with configurable negative-input fallback modes, use the SquareRoot block.
  • Stateless WASM Execution: Executed directly as compiled WebAssembly arithmetic operations without internal state accumulators, requiring <5 ns< 5\text{ ns} per evaluation.

Related Components

  • SquareRoot: Specialized nth-root computation block with configurable negative-input fallback handling.
  • Trigonometric: Trigonometric and hyperbolic functions (sin⁡\sin, cos⁡\cos, tan⁡\tan, etc.).
  • Abs: Absolute value and sign extraction.
  • Gain: Pure linear scaling by a constant factor.