Documentation |
Calculate square root, signed square root, or reciprocal of square root
You can select one of the following functions from the Function parameter list.
Function | Description | Mathematical Expression | MATLAB^{®} Equivalent |
---|---|---|---|
sqrt | Square root of the input | u^{0.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:
Scalar operations
Element-wise vector and matrix operations
The block accepts input signals of the following data types:
Function | Input Data Types | Restrictions |
---|---|---|
sqrt |
| None |
signedSqrt |
| When the input is an integer or fixed-point type, the output must be floating point. |
rSqrt |
| None |
The block accepts real and complex inputs of the following types:
Function | Types of Real Inputs | Types 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:
Uses the data type that you specify for Output data type
Is real or complex, depending on your selection for Output signal type
The Main pane of the block dialog box appears as follows:
Specify the mathematical function. The block icon changes to match the function you select.
Function | Block Icon |
---|---|
sqrt | |
signedSqrt | |
rSqrt |
Specify the output signal type of the block as auto, real, or complex.
Function | Input Signal Type | Output Signal Type | ||
---|---|---|---|---|
Auto | Real | Complex | ||
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 |
Specify the time interval between samples. To inherit the sample time, set this parameter to -1. See Specify Sample Time in the online documentation for more information.
The Algorithm pane of the block dialog box appears as follows:
Note: The parameters in the Algorithm pane are available only when you set Function to rSqrt on the Main pane. |
Specify the method for computing the reciprocal of a square root.
Method | Data Types Supported | When 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. |
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.
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.
The Data Types pane of the block dialog box appears as follows:
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 Types | Intermediate 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 Types | Intermediate 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:
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. |
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 Specify Block Output Data Types for more information.
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
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
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 Use Lock Output Data Type Setting.
Specify the rounding mode for fixed-point operations. For more information, see Rounding. in the Fixed-Point Designer™ documentation.
Action | Reasons for Taking This Action | What Happens for Overflows | Example |
---|---|---|---|
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. | The maximum value that the int8 (signed, 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box selected, the block output saturates at 127. Similarly, the block output saturates at a minimum output value of -128. |
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 maximum value that the int8 (signed, 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box cleared, the software interprets the overflow-causing value as int8, which can produce an unintended result. For example, a block result of 130 (binary 1000 0010) expressed as int8, is -126. |
When you select this check box, saturation applies to every internal operation on the block, not just the output or result. Usually, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.
Suppose that you have the following modelmodel:
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.
Suppose that you have the following modelmodel:
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.
Suppose that you have the following modelmodel:
In the Sqrt block dialog box, assume that the following parameter settings apply:
Parameter | Setting |
---|---|
Method | Newton-Raphson |
Number of iterations | 1 |
Intermediate results data type | Inherit:Inherit from input |
After one iteration of the Newton-Raphson algorithm, the block output is within 0.0004 of the final value (0.4834).
Suppose that you have the following modelmodel:
In the Sqrt block dialog box, assume that the following parameter settings apply:
Parameter | Setting |
---|---|
Method | Newton-Raphson |
Number of iterations | 1 |
Intermediate results data type | Inherit:Inherit from input |
After one iteration of the Newton-Raphson algorithm, the block output is within 0.0459 of the final value (0.4834).