Calculate square root, signed square root, or reciprocal of square root (2024)

Table of Contents
Description Examples Square Root of Negative Values Signed Square Root of Negative Values rSqrt of Floating-Point Inputs rSqrt of Fixed-Point Inputs Ports Input Port_1 — Input signalscalar | vector | matrix Output Port_1 — Output signalscalar | vector | matrix Parameters Main Function — Function the block performssqrt (default) | signedSqrt | rSqrt Output signal type — Output signal type auto (default) | real | complex Sample time (-1 for inherited) — Interval between samples -1 (default) | scalar | vector Algorithm Method — Method to compute reciprocal of square rootExact (default) | Newton-Raphson Number of iterations — Number of iterations used for Newton Raphson algorithm 3 (default) | integer Data Types Intermediate results data type — Data type of intermediate results Inherit:Inherit via internalrule (default) | Inherit: Inherit from input | Inherit: Inherit from output | double | single | int8 | uint8 | int16 | uint16 | int32 | uint32 | int64 | uint64 | fixdt(1,16,,0) | fixdt(1,16,2^0,0) | <data type expression> Output — Output data type Inherit: Same as firstinput (default) | Inherit: Inherit via internal rule | Inherit: Inherit via backpropagation | double | single | half | int8 | int32 | uint32 | int64 | uint64 | fixdt(1,16,2^0,0) | <data type expression> | ... Minimum — Minimum output value for range checking [] (default) | scalar Maximum — Maximum output value for range checking [] (default) | scalar Integer rounding mode — Rounding mode for fixed-point operations Floor (default) | Ceiling | Convergent | Nearest | Round | Simplest | Zero Lock output data type setting against changes by the fixed-point tools — Prevent fixed-point tools from overriding data types off (default) | on Saturate on integer overflow — Choose the behavior when integer overflow occurs off (default) | on Block Characteristics Extended Capabilities C/C++ Code Generation Generate C and C++ code using Simulink® Coder™. HDL Code GenerationGenerate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™. PLC Code Generation Generate Structured Text code using Simulink® PLC Coder™. Fixed-Point ConversionDesign and simulate fixed-point systems using Fixed-Point Designer™. Version History See Also MATLAB Command Americas Europe Asia Pacific

Calculate square root, signed square root, or reciprocal of squareroot

expand all in page

  • Calculate square root, signed square root, or reciprocal of squareroot (1)

Libraries:
Simulink / Math Operations
HDL Coder / HDL Floating Point Operations
HDL Coder / Math Operations

Description

The Sqrt block calculates the square root, signed square root, orreciprocal of square root on the input signal. Select one of the following functionsfrom 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 bythe sign of the input

sign(u)*|u|0.5

rSqrt

Reciprocal of the square root of the input

u-0.5

The block icon changes to match the function.

Examples

expand all

Open Model

This example shows how to compute the square root of a negative-valued input signal as complex-valued output.

Calculate square root, signed square root, or reciprocal of squareroot (2)

By setting the Function to sqrt and Output signal type to complex, the block produces the correct result of 0 + 10i for an input of -100. If you change the Output signal type to auto or real, the block outputs NaN.

Open Model

This example shows how to compute the signed square root of a negative-valued input signal.

Calculate square root, signed square root, or reciprocal of squareroot (3)

When the block input is negative and you set the Function to signedSqrt, the Sqrt block output is the same for any setting of the Output signal type parameter. By setting the Numerica display format of the first Display block to decimal (Stored Integer), you can see the value of the imaginary part for the complex output.

Open Model

This example shows how to compute the rSqrt of a floating-point input signal. The Sqrt block has the following settings:

  • 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).

Calculate square root, signed square root, or reciprocal of squareroot (4)

This example uses:

  • Fixed-Point DesignerFixed-Point Designer
  • SimulinkSimulink

Open Model

This example shows how to compute the rSqrt of a fixed-point input signal. The Sqrt block has the following settings:

  • 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).

Calculate square root, signed square root, or reciprocal of squareroot (5)

Ports

Input

expand all

Input signal to the block to calculate the square root, signed square root, orreciprocal of square root. The sqrt function acceptsreal or complex inputs, except for complex fixed-point signals.signedSqrt and rSqrt do notaccept complex inputs. The input signal must be a floating pointnumber.

This table summarizes the support for complex types and negativevalues for floating point, integer, and fixed-point data types forsqrt, rSqrt, andsignedSqrt functions.

FunctionData TypeComplexNegative Values
InputOutput
sqrtFloating pointYesYesYes
Integer and fixed-pointNoNoNo

rSqrt

Floating pointNoNoYes
Integer and fixed-pointNoNoNo
signedSqrtFloating pointNoYesYes
Integer and fixed-pointNoNoNo

If the input is negative, set the Output signal to complex for allfunctions except signedSqrt.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fixed point

Output

expand all

Output signal that is the square root, signed square root, orreciprocal of square root of the input signal. When the input is aninteger or fixed-point type, the output must be floating point.

Data Types: single | double | half | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | fixed point

Parameters

expand all

Main

Specify the mathematical function that the block calculates. The blockicon changes to match the function you select.

FunctionBlock Icon
sqrtCalculate square root, signed square root, or reciprocal of squareroot (6)
signedSqrtCalculate square root, signed square root, or reciprocal of squareroot (7)
rSqrtCalculate square root, signed square root, or reciprocal of squareroot (8)

Dependency

When this parameter is set tosignedSqrt, theIntermediate results data type parameter isdisabled.

Programmatic Use

Block Parameter:Operator
Type: charactervector
Values:'sqrt' | 'signedSqrt'| 'rSqrt'
Default:'sqrt'

Specify the output signal type of the block.

FunctionInput SignalTypeOutput Signal Type
AutoRealComplex

sqrt

real

real for nonnegativeinputs

NaN fornegative inputs

real for nonnegativeinputs

NaN fornegative inputs

complex

complex

complex

error

complex

signedSqrt

real

real

real

complex

complex

error

error

error

rSqrt

real

real

real

error

complex

error

error

error

Programmatic Use

Block Parameter:OutputSignalType
Type: charactervector
Values:'auto' | 'real' |'complex'
Default:'auto'

Specify the time interval between samples. To inherit the sample time, set this parameter to -1. For more information, see Specify Sample Time.

Dependencies

This parameter is visible only if you set it to a value other than -1. To learn more, see Blocks for Which Sample Time Is Not Recommended.

Programmatic Use

Block Parameter: SampleTime
Type: string scalar or character vector
Default: "-1"

Algorithm

Specify the method for computing the reciprocal of a square root. Thisparameter is only valid for the rSqrtfunction.

MethodData Types SupportedWhen to Use This Method
Exact

Floating point

You do not want an approximation.

Note

The input or output must be floatingpoint.

Newton-Raphson

Floating-point, fixed-point, and built-ininteger types

You want a fast, approximatecalculation.

The Exact method provides results that areconsistent with MATLAB computations.

Note

The algorithms for sqrt andsignedSqrt are always ofExact type, no matter what selectionappears on the block dialog box.

Programmatic Use

Block Parameter:AlgorithmType
Type: charactervector
Values:'Exact' |'Newton-Raphson'
Default:'Exact'

Specify the number of iterations to perform the Newton-Raphsonalgorithm. This parameter is valid with the rSqrtfunction and the Newton-Raphson value forMethod.

Note

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

Programmatic Use

Block Parameter:Iterations
Type: charactervector
Values: integer
Default:'3'

Data Types

The Data Type Assistant helps you set dataattributes. To use the Data Type Assistant, click Calculate square root, signed square root, or reciprocal of squareroot (9). For more information, see Specify Data Types Using Data Type Assistant.

Specify the data type for intermediate results when you setFunction to sqrt orrSqrt on the Mainpane.

The type can be inherited, specified directly, or expressed as a datatype object such as Simulink.NumericType.

Note

To avoid overflow, the intermediate data type must be larger thanor equal to a data type that can contain the square of the outputdata type.

Follow these guidelines on setting an intermediate data typeexplicitly 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 datatype is not double. Use single or double.
Input and output are fixed point.Use fixed point.

Follow these guidelines on setting an intermediate data typeexplicitly 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 toInherit:Inherit from outputwhen:

  • You select Newton-Raphson tocompute 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:Inheritfrom output yields suboptimal performance andproduces an error.

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

Dependencies

This parameter is disabled when the Functionparameter is set to signedSqrt.

Programmatic Use

Block Parameter:IntermediateResultsDataTypeStr
Type: charactervector
Values: 'Inherit: Inherit via internalrule' | 'Inherit: Inherit frominput' | 'Inherit: Inherit fromoutput' | 'double' |'single', 'int8','uint8', int16,'uint16', 'int32','uint32', 'int64','uint64',fixdt(1,16,0),fixdt(1,16,2^0,0). '<datatype expression>'
Default: 'Inherit:Inherit via internal rule'

Specify the output data type. The type can be inherited, specifieddirectly, or expressed as a data type object such asSimulink.NumericType.

Dependencies

When input is a floating-point data type smaller than singleprecision, the Inherit: Inherit via internalrule output data type depends on the setting ofthe Inherit floating-point output type smaller than single precision configuration parameter. Data types are smaller than singleprecision when the number of bits needed to encode the data type isless than the 32 bits needed to encode the single-precision datatype. For example, half andint16 are smaller than singleprecision.

Programmatic Use

Block Parameter:OutDataTypeStr
Type: charactervector
Values: 'Inherit: Inherit via internalrule' | 'Inherit: Inherit via backpropagation' | 'Inherit: Same as firstinput' | 'double' |'single' | 'half' |'int8' | 'uint8' |int16 | 'uint16' |'int32' | 'uint32' |'int64' | 'uint64' |fixdt(1,16,0) |fixdt(1,16,2^0,0) |fixdt(1,16,2^0,0) | '<datatype expression>'
Default: 'Inherit:Same as first input'

Specify the lower value of the output range that Simulink® checks as a finite, real, double, scalar value.

Note

If you specify a bus object as the data type for this block, donot set the minimum value for bus data on the block. Simulink ignores this setting. Instead, set the minimum valuesfor bus elements of the bus object specified as the data type. Forinformation on the Minimum parameter for a bus element, see Simulink.BusElement.

Simulink uses the minimum to perform:

  • Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters) for someblocks.

  • Simulation range checking (see Specify Signal Ranges and Enable Simulation Range Checking).

  • Automatic scaling of fixed-point data types.

  • Optimization of the code that you generate from the model.This optimization can remove algorithmic code and affect theresults of some simulation modes such as SIL or externalmode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).

Note

Output minimum does not saturate or clipthe actual output signal. Use the Saturation blockinstead.

Programmatic Use

Block Parameter:OutMin
Type: charactervector
Values: scalar
Default: '[]'

Specify the upper value of the output range that Simulink checks as a finite, real, double, scalar value.

Note

If you specify a bus object as the data type for this block, donot set the maximum value for bus data on the block. Simulink ignores this setting. Instead, set the maximum valuesfor bus elements of the bus object specified as the data type. Forinformation on the Maximum parameter for a bus element, see Simulink.BusElement.

Simulink uses the maximum value to perform:

  • Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters) for someblocks.

  • Simulation range checking (see Specify Signal Ranges and Enable Simulation Range Checking).

  • Automatic scaling of fixed-point data types.

  • Optimization of the code that you generate from the model.This optimization can remove algorithmic code and affect theresults of some simulation modes such as SIL or externalmode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).

Note

Output maximum does not saturate or clipthe actual output signal. Use the Saturation blockinstead.

Programmatic Use

Block Parameter:OutMax
Type: charactervector
Values: scalar
Default: '[]'

Specify the rounding mode for fixed-point operations. For more information, see Rounding (Fixed-Point Designer).

Programmatic Use

Block Parameter: RndMeth
Type: character vector
Values: 'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' | 'Zero'
Default: 'Floor'

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 (Fixed-Point Designer).

Programmatic Use

Block Parameter: LockScale
Type: character vector
Values: 'off' | 'on'
Default: 'off'
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.

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 Troubleshoot 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.

Programmatic Use

Block Parameter: DoSatur
Type: character vector
Value: 'off' | 'on'
Default: 'off'

Block Characteristics

Data Types

double | fixed point | half | integer | single

Direct Feedthrough

yes

Multidimensional Signals

yes

Variable-Size Signals

yes

Zero-Crossing Detection

no

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.

PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.

Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.

Version History

Introduced in R2010a

See Also

Math Function | Trigonometric Function

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

 

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Calculate square root, signed square root, or reciprocal of squareroot (10)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

Contact your local office

Calculate square root, signed square root, or reciprocal of square
root (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6003

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.