Skip to Main Content Skip to Search
Product Documentation

Sqrt, Signed Sqrt, Reciprocal Sqrt - Calculate square root, signed square root, or reciprocal of square root

Library

Math Operations

Description

You can select one of the following functions from the Function parameter list.

FunctionDescriptionMathematical ExpressionMATLAB Equivalent
sqrt

Square root of the input

u0.5

sqrt
signedSqrt

Square root of the absolute value of the input, multiplied by the sign of the input

sign(u)*|u|0.5

rSqrt

Reciprocal of the square root of the input

u-0.5

The block output is the result of applying the function to the input. Each function supports:

Data Type Support

The block accepts input signals of the following data types:

FunctionInput Data TypesRestrictions

sqrt

  • Floating point

  • Built-in integer

  • Fixed point

None

signedSqrt

  • Floating point

  • Built-in integer

  • Fixed point

When the input is an integer or fixed-point type, the output must be floating point.

rSqrt

  • Floating point

  • Built-in integer

  • Fixed point

None

The block accepts real and complex inputs of the following types:

FunctionTypes of Real InputsTypes of Complex Inputs

sqrt

Any, except for fixed-point inputs that are negative or have nontrivial slope and nonzero bias

Any, except for fixed-point inputs

signedSqrt

None

rSqrt

None

The block output:

Parameters and Dialog Box

The Main pane of the block dialog box appears as follows:

Function

Specify the mathematical function. The block icon changes to match the function you select.

FunctionBlock Icon
sqrt
signedSqrt
rSqrt

Output signal type

Specify the output signal type of the block as auto, real, or complex.

FunctionInput Signal TypeOutput Signal Type
AutoRealComplex

sqrt

real

real for nonnegative inputs

nan for negative inputs

real for nonnegative inputs

nan for negative inputs

complex

complex

complex

error

complex

signedSqrt

real

real

real

complex

complex

error

error

error

rSqrt

real

real

real

error

complex

error

error

error

Sample time (-1 for inherited)

Specify the time interval between samples. To inherit the sample time, set this parameter to -1. See How to Specify the Sample Time in the online documentation for more information.

The Algorithm pane of the block dialog box appears as follows:

Method

Specify the method for computing the reciprocal of a square root.

MethodData Types SupportedWhen to Use This Method
Exact

Floating point

If you use a fixed-point or built-in integer type, an upcast to a floating-point type occurs.

You do not want an approximation.

    Note   The input or output must be floating point.

Newton-Raphson

Floating-point, fixed-point, and built-in integer types

You want a fast, approximate calculation.

The Exact method provides results that are consistent with MATLAB computations.

    Note   The algorithms for sqrt and signedSqrt are always of Exact type, no matter what selection appears on the block dialog box.

Number of iterations

Specify the number of iterations to perform the Newton-Raphson algorithm. The default value is 3.

This parameter is not available when you select Exact for Method.

    Note   If you enter 0, the block output is the initial guess of the Newton-Raphson algorithm.

The Data Types pane of the block dialog box appears as follows:

Intermediate results data type

Specify the data type for intermediate results (available only when you set Function to sqrt or rSqrt on the Main pane). You can set the data type to:

  • A rule that inherits a data type, for example, Inherit:Inherit via internal rule

  • The name of a built-in data type, for example, single

  • The name of a data type object, for example, a Simulink.NumericType object

  • An expression that evaluates to a data type, for example, fixdt(1,16,0)

Follow these guidelines on setting an intermediate data type explicitly for the square root function, sqrt:

Input and Output Data TypesIntermediate Data Type
Input or output is double.Use double.
Input or output is single, and any non-single data type is not double.Use single or double.
Input and output are fixed point.Use fixed point.

Follow these guidelines on setting an intermediate data type explicitly for the reciprocal square root function, rSqrt:

Input and Output Data TypesIntermediate Data Type
Input is double and output is not single.Use double.
Input is not single and output is double.Use double.
Input and output are fixed point.Use fixed point.

    Caution   Do not set Intermediate results data type to Inherit:Inherit from output when:

    • You select Newton-Raphson to compute the reciprocal of a square root.

    • The input data type is floating point.

    • The output data type is fixed point.

    Under these conditions, selecting Inherit:Inherit from output yields suboptimal performance and produces an error.

    To avoid this error, convert the input signal from a floating-point to fixed-point data type. For example, insert a Data Type Conversion block in front of the Sqrt block to perform the conversion.

Output data type

Specify the output data type. You can set the data type to:

  • A rule that inherits a data type, for example, Inherit:Inherit via back propagation

  • The name of a built-in data type, for example, single

  • The name of a data type object, for example, a Simulink.NumericType object

  • An expression that evaluates to a data type, for example, fixdt(1,16,0)

Click the Show data type assistant button to display the Data Type Assistant, which helps you set the Output data type parameter.

See Specifying Block Output Data Types for more information.

Output minimum

Specify the minimum value that the block can output. The default value is [] (unspecified). Simulink uses this value to perform:

  • Simulation range checking (see Signal Ranges)

  • Automatic scaling of fixed-point data types

Output maximum

Specify the maximum value that the block can output. The default value is [] (unspecified). Simulink uses this value to perform:

  • Simulation range checking (see Signal Ranges)

  • Automatic scaling of fixed-point data types

Lock output data type setting against changes by the fixed-point tools

Select to lock the output data type setting of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor. For more information, see Fixed-Point Tool and Fixed-Point Advisor in the Simulink Fixed Point documentation.

Integer rounding mode

Specify the rounding mode for fixed-point operations. For more information, see Rounding in the Simulink Fixed Point User's Guide.

Saturate on integer overflow
ActionReasons for Taking This ActionWhat Happens for OverflowsExample

Select this check box.

Your model has possible overflow and you want explicit saturation protection in the generated code.

Overflows saturate to either the minimum or maximum value that the data type can represent.

An overflow associated with a signed 8-bit integer can saturate to -128 or 127.

Do not select this check box.

You want to optimize efficiency of your generated code.

You want to avoid overspecifying how a block handles out-of-range signals. For more information, see Checking for Signal Range Errors.

Overflows wrap to the appropriate value that is representable by the data type.

The number 130 does not fit in a signed 8-bit integer and wraps to -126.

When you select this check box, saturation applies to every internal operation on the block, not just the output or result. In general, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

Examples

sqrt Function

Suppose that you have the following model:

When the input to the Sqrt block is negative and the Output signal type is auto or real, the sqrt function outputs nan. However, setting Output signal type to complex produces the correct answer.

signedSqrt Function

Suppose that you have the following model:

When the input to the Sqrt block is negative, the block output is the same for any Output signal type setting. If you change the first Display block format from short to decimal (Stored Integer), you see the value of the imaginary part for the complex output.

rSqrt Function with Floating-Point Inputs

Suppose that you have the following model:

In the Sqrt block dialog box, assume that the following parameter settings apply:

ParameterSetting
MethodNewton-Raphson
Number of iterations1
Intermediate results data typeInherit:Inherit from input

After one iteration of the Newton-Raphson algorithm, the block output is within 0.0004 of the final value (0.4834).

rSqrt Function with Fixed-Point Inputs

Suppose that you have the following model:

In the Sqrt block dialog box, assume that the following parameter settings apply:

ParameterSetting
MethodNewton-Raphson
Number of iterations1
Intermediate results data typeInherit:Inherit from input

After one iteration of the Newton-Raphson algorithm, the block output is within 0.0459 of the final value (0.4834).

Characteristics

Direct Feedthrough

Yes

Sample Time

Specified in the Sample time parameter

Dimensionalized

Yes

Multidimensionalized

Yes

Zero-Crossing Detection

No

See Also

Math Function, Trigonometric Function

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS