trig

Trigonometric

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

The Trigonometric block evaluates analytical trigonometric and inverse trigonometric functions on input signals. It provides seven standard functions—sine (sin), cosine (cos), tangent (tan), arcsine (asin), arccosine (acos), arctangent (atan), and two-argument four-quadrant arctangent (atan2). In accordance with standard scientific and engineering conventions, all angular inputs and outputs are expressed in radians. When atan2 is selected, the block dynamically provides a second input port to enable full 360∘360^\circ four-quadrant vector phase angle recovery.

Mathematical Model

The block computes the output signal y(t)y(t) as a function of the primary input u(t)u(t) (and optional secondary input u2(t)u_2(t) for atan2):

y(t)=ftrig(u(t))y(t) = f_{\text{trig}}(u(t))

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

  • Sine (sin):

```latex

y(t) = \sin(u(t)), \quad u \in \mathbb{R}, \quad y \in [-1, 1]

```

  • Cosine (cos):

```latex

y(t) = \cos(u(t)), \quad u \in \mathbb{R}, \quad y \in [-1, 1]

```

  • Tangent (tan):

```latex

y(t) = \tan(u(t)) = \frac{\sin(u(t))}{\cos(u(t))}, \quad u \ne \pm \frac{\pi}{2} + k\pi

```

Exhibits vertical asymptotes where cos⁡(u)=0\cos(u) = 0.

  • Arcsine (asin):

```latex

y(t) = \arcsin(\operatorname{clamp}(u(t), -1, 1)) \in \left[-\frac{\pi}{2}, \frac{\pi}{2}\right]

```

Input is automatically clamped to [−1,1][-1, 1] to prevent NaN generation.

  • Arccosine (acos):

```latex

y(t) = \arccos(\operatorname{clamp}(u(t), -1, 1)) \in [0, \pi]

```

Input is automatically clamped to [−1,1][-1, 1] to prevent NaN generation.

  • Arctangent (atan):

```latex

y(t) = \arctan(u(t)) \in \left(-\frac{\pi}{2}, \frac{\pi}{2}\right), \quad u \in \mathbb{R}

```

  • Four-Quadrant Arctangent (atan2):

```latex

y(t) = \operatorname{atan2}(u(t), u_2(t)) \in (-\pi, \pi]

```

where u(t)u(t) is the yy-coordinate (in) and u2(t)u_2(t) is the xx-coordinate (in2). Correctly resolves the angle of the vector (x,y)(x, y) in all four quadrants without division-by-zero singularities when x=0x = 0.

Inputs & Outputs

Direction ID Label Type Status
→ In in Input number Required
→ In in2 X (atan2) number Required
← Out out Output number Output

Parameters

Parameters Label Type Default Description
function Function select sin

Usage Examples

Example 1: Direct-Quadrature (d-q) Park Transformation for AC Motors

In Field-Oriented Control (FOC) of permanent magnet synchronous motors, stationary stator currents (iα,iβi_\alpha, i_\beta) are rotated into rotor coordinates (id,iqi_d, i_q) using rotor electrical angle θe\theta_e. Feeding θe\theta_e into two Trigonometric blocks configured for sin and cos provides the harmonic projection matrices: id=iαcos⁡θe+iβsin⁡θei_d = i_\alpha \cos \theta_e + i_\beta \sin \theta_e and iq=−iαsin⁡θe+iβcos⁡θei_q = -i_\alpha \sin \theta_e + i_\beta \cos \theta_e.

Example 2: Full 360∘360^\circ Phase Angle Recovery via atan2

Computing true heading angle ψ\psi from 2-axis magnetometer flux density measurements (By,BxB_y, B_x). Selecting function = "atan2" activates the secondary in2 port. Connecting ByB_y to in and BxB_x to in2 recovers the true continuous orientation angle across all four quadrants over the range (−π,π](-\pi, \pi] radians without quadrant ambiguity or divide-by-zero singularities when Bx=0B_x = 0.

Remarks & Best Practices

  • Radian Angular Convention: All functions strictly utilize radians for angular arguments and outputs. To interface with degrees, multiply incoming degree values by π180\frac{\pi}{180} using a Gain block (≈0.017453\approx 0.017453), and scale outgoing radians by 180π\frac{180}{\pi} (≈57.2958\approx 57.2958).
  • Automatic Domain Clamping in Inverse Functions: For asin and acos, the simulation engine automatically clamps input values to the valid mathematical domain [−1.0,1.0][-1.0, 1.0]. This prevents floating-point domain errors when numerical overshoot momentarily yields values like 1.0000011.000001.
  • Tangent Singularity Protection: The tan function approaches ±∞\pm \infty at odd multiples of π/2\pi/2. For physical models where input angles pass through these asymptotes, cascade a Saturation block after the Trigonometric block to prevent downstream solver step-size collapse.
  • Hardware-Accelerated WASM Execution: Mapped directly to native WebAssembly floating-point math instructions (f64.sin, f64.cos), delivering <5 ns< 5\text{ ns} execution latency per step.

Related Components

  • SignalGenerator: Generates continuous time-varying sinusoidal waveforms directly.
  • MathFunction: General mathematical functions (exp⁡\exp, ln⁡\ln, pow\text{pow}, round\text{round}).
  • Gain: Scales angular signals between degrees and radians.
  • Saturation: Clamps signals to protect against tangent singularities.