√

Square Root

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

The SquareRoot block calculates the nn-th root of an input signal. Although commonly used to compute standard square roots (n=2n = 2), the block supports cube roots (n=3n = 3) and arbitrary numeric root orders (n≥1n \ge 1). To maintain numerical stability in dynamic control loops and physical simulations where signals can momentarily dip negative due to solver steps or noise, the block provides configurable negative-input handling modes (return zero, take absolute value, or return NaN) and optional console warning reporting.

Mathematical Model

The block evaluates the real nn-th root of the incoming scalar signal u(t)u(t):

y(t)=u(t)n=(u(t))1/ny(t) = \sqrt[n]{u(t)} = (u(t))^{1/n}

where nn represents the configured rootOrder (n≥1n \ge 1).

When the input signal falls below zero (u(t)<0u(t) < 0), the mathematical response is determined by the negativeHandling parameter:

y(t)={0,negativeHandling=zero∣u(t)∣n,negativeHandling=absNaN,negativeHandling=nany(t) = \begin{cases} 0, & \text{negativeHandling} = \text{zero} \\ \sqrt[n]{|u(t)|}, & \text{negativeHandling} = \text{abs} \\ \text{NaN}, & \text{negativeHandling} = \text{nan} \end{cases}

When reportWarning is enabled, the simulation engine registers a non-fatal warning on the first occurrence of a negative input, alerting the user to potential model boundary violations without crashing the solver.

Inputs & Outputs

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

Parameters

Parameters Label Type Default Description
rootOrder Root Order (n) number 2
negativeHandling Negative Input select zero
reportWarning Report Warning boolean true

Usage Examples

Example 1: 2D Euclidean Distance and Vector Magnitude

Calculating planar distance r=x2+y2r = \sqrt{x^2 + y^2} from orthogonal Cartesian coordinates. Squaring each component, summing them with a Sum block, and routing the sum into SquareRoot with rootOrder = 2.0 and negativeHandling = "zero" provides accurate vector magnitude for missile trajectory or mobile robot tracking.

Example 2: Fluid Flow Measurement via Differential Pressure (Bernoulli Law)

In industrial flowmeters (such as orifice plates and Venturi tubes), fluid volume flow rate QQ is proportional to the square root of differential pressure across the restriction: Q=CdA2ΔP/ρQ = C_d A \sqrt{2 \Delta P / \rho}. Routing the differential pressure sensor signal ΔP\Delta P into SquareRoot with rootOrder = 2.0 and negativeHandling = "zero" prevents solver failure when line pressure fluctuates near zero during valve closure.

Remarks & Best Practices

  • Numerical Loop Stability: Selecting negativeHandling = "zero" is strongly recommended in closed-loop feedback systems where momentary numerical overshoot during step responses could dip an otherwise positive physical quantity (such as pressure, fluid level, or kinetic energy) slightly negative.
  • Odd vs. Even Root Semantics: While odd integer roots (n=3,5n = 3, 5) are mathematically defined for negative real numbers (−83=−2\sqrt[3]{-8} = -2), setting negativeHandling = "abs" and combining with a downstream sign multiplication preserves signed odd-root behavior.
  • Derivative Singularity at Zero: The analytical derivative dduu=12u\frac{d}{du}\sqrt{u} = \frac{1}{2\sqrt{u}} approaches infinity as u→0+u \to 0^+. In stiff variable-step continuous ODE solvers (such as TR-BDF2), crossing zero with a square root operation can force the solver to adaptively reduce its time step.
  • Native Hardware Acceleration: Mapped directly to native WebAssembly hardware instructions (f64.sqrt when n=2n = 2), providing single-instruction execution speed on modern microprocessors.

Related Components

  • MathFunction: General power (ucu^c), exponential, and logarithmic functions.
  • Product: Multiplication and squaring (u2u^2) operations.
  • Abs: Absolute value and sign extraction.
  • Gain: Constant scalar multiplication.