Square Root
MathThe SquareRoot block calculates the -th root of an input signal. Although commonly used to compute standard square roots (), the block supports cube roots () and arbitrary numeric root orders (). 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 -th root of the incoming scalar signal :
where represents the configured rootOrder ().
When the input signal falls below zero (), the mathematical response is determined by the negativeHandling parameter:
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 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 is proportional to the square root of differential pressure across the restriction: . Routing the differential pressure sensor signal 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 () are mathematically defined for negative real numbers (), setting
negativeHandling = "abs"and combining with a downstream sign multiplication preserves signed odd-root behavior. - Derivative Singularity at Zero: The analytical derivative approaches infinity as . 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.sqrtwhen ), providing single-instruction execution speed on modern microprocessors.
Related Components
- MathFunction: General power (), exponential, and logarithmic functions.
- Product: Multiplication and squaring () operations.
- Abs: Absolute value and sign extraction.
- Gain: Constant scalar multiplication.