Main Content

# Math Function

Perform mathematical function

• Library:
• Simulink / Math Operations

HDL Coder / Math Operations

## Description

The Math Function block performs many common mathematical functions.

Tip

To perform square root calculations, use the Sqrt block.

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

FunctionDescriptionMathematical Expression MATLAB® Equivalent
`exp`

Exponential

`eu`

`exp`
`log`

Natural logarithm

`ln u`

`log`
`10^u`

Power of base 10

`10u`

`10.^u`
(see `power`)

`log10`

Common (base 10) logarithm

`log u`

`log10`
`magnitude^2`

Complex modulus

`|u|2`

`real(u).^2 + imag(u).^2`
(see `real`, `imag`, and `power`)

`square`

Power 2

`u2`

`u.^2`
(see `power`)

`pow`

Power

`sign(u)*|u|v` (default, applied only for even-order roots) or `uv`

`power`
`conj`

Complex conjugate

`ū`

`conj`
`reciprocal` with Exact method

Reciprocal

`1/u`

`1./u`
(see `rdivide`)

`reciprocal` with Newton-Raphson method

Reciprocal

See Newton-Raphson Reciprocal Algorithm MethodNone
`hypot`

Square root of sum squares

`(u2+v2)0.5`

`hypot`
`rem`

Remainder after division

`rem`
`mod`

Modulus after division

`mod`
`transpose`

Transpose

`uT`

`u.'`
(see Array vs. Matrix Operations)

`hermitian`

Complex conjugate transpose

`uH`

`u'`
(see Array vs. Matrix Operations)

The block output is the result of the operation of the function on the input or inputs. The functions support these types of operations.

FunctionScalar OperationsElement-Wise Vector and Matrix OperationsVector and Matrix Operations
`exp`

Yes

Yes

`log`

Yes

Yes

`10^u`

Yes

Yes

`log10`

Yes

Yes

`magnitude^2`

Yes

Yes

`square`

Yes

Yes

`pow`

Yes

Yes

`conj`

Yes

Yes

`reciprocal` with Exact method

Yes

Yes

`reciprocal` with Newton-Raphson method

Yes

Yes

`hypot`

Yes, on two inputs

Yes, on two inputs (two vectors or two matrices of the same size, a scalar and a vector, or a scalar and a matrix)

`rem`

Yes, on two inputs

Yes, on two inputs (two vectors or two matrices of the same size, a scalar and a vector, or a scalar and a matrix)

`mod`

Yes, on two inputs

Yes, on two inputs (two vectors or two matrices of the same size, a scalar and a vector, or a scalar and a matrix)

`transpose`

Yes

Yes

`hermitian`

Yes

Yes

The name of the function appears on the block. The appropriate number of input ports appears automatically.

Tip

Use the Math Function block when you want vector or matrix output.

### Newton-Raphson Reciprocal Algorithm Method

The `reciprocal` function with the Newton-Raphson algorithm method calculates the reciprocal with Newton-Raphson approximation method. The function uses recursive approximation to find better approximations to the roots of a real-valued function.

The reciprocal of a real number $a$ is defined as a zero of the function:

`$f\left(x\right)=\frac{1}{x}-a$`

Simulink® chooses an initial estimate in the range $0<{x}_{0}<\frac{2}{a}$, as this is the domain of convergence for the function.

To successively compute the roots of the function, specify the Number of iterations parameter. The process is repeated as follows:

`${x}_{i+1}={x}_{i}-\frac{f\left({x}_{i}\right)}{f\text{'}\left({x}_{i}\right)}={x}_{i}+\left({x}_{i}-a{x}_{i}{}^{2}\right)={x}_{i}.\left(2-a{x}_{i}\right)$`

$f\text{'}\left(x\right)$ is the derivative of the function $f\left(x\right)$.

### Data Type Support

This table shows the input data types that each function of the block can support.

FunctionSingleDoubleHalf*BooleanBuilt-in integerFixed point

`exp`

Yes

Yes

Yes

`log`

Yes

Yes

Yes

`10^u`

Yes

Yes

Yes

`log10`

Yes

Yes

Yes

`magnitude^2`

Yes

Yes

Yes

Yes

Yes

`square`

Yes

Yes

Yes

Yes

Yes

`pow`

Yes

Yes

Yes

`conj`

Yes

Yes

Yes

Yes

Yes

`reciprocal` with Exact method

Yes

Yes

Yes

Yes

Yes

`reciprocal` with Newton-Raphson method (for more information, see Output)

Yes

Yes

Yes

Yes

`hypot`

Yes

Yes

Yes

`rem`

Yes

Yes

Yes

Yes

`mod`

Yes

Yes

Yes

Yes

`transpose`

Yes

Yes

Yes

Yes

Yes

Yes

`hermitian`

Yes

Yes

Yes

Yes

Yes

For more information on half-precision arithmetic operations, see The Half-Precision Data Type in Simulink (Fixed-Point Designer).

## Ports

### Input

expand all

Input signal specified as a scalar, vector, or matrix. All supported modes accept both real and complex inputs, except for `reciprocal`, which does not accept complex fixed-point inputs. See Description for more information.

#### Dependencies

Data type support for this block depends on the Function you select and the size of the input(s). For more information, see Data Type Support.

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

Input signal specified as a scalar, vector, or matrix. All supported modes accept both real and complex inputs, except for `reciprocal`, which does not accept complex fixed-point inputs.

#### Dependencies

To enable this port, set Function to `hypot`, `rem`, or `mod`.

Data type support for this block depends on the Function you select, and the size of the input(s). For more information, see Data Type Support.

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

### Output

expand all

Output signal specified as a scalar, vector, or matrix. The dimensions of the block output depend on the Function you select and the size of the inputs. The block output is real or complex, depending on what you select for Output signal type. See Description for more information.

#### `reciprocal` with Newton-Raphson Method

The `reciprocal` with Newton-Raphson method output data type depends on the input data type:

Input data typeOutput data type
singlesingle
doubledouble
built-in integerbuilt-in integer
built-in fixed-pointbuilt-in fixed-point

`fi` (value, 0, word_length, fraction_length)

`fi` (value, 0, word_length, word_lengthfraction_length–1)

`fi` (value, 1, word_length, fraction_length)

`fi` (value, 1, word_length, word_lengthfraction_length–2)

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

## Parameters

expand all

### Main

Specify the mathematical function. See Description for more information about the options for this parameter.

#### Dependency

Setting Function to `pow` enables the Signed power parameter.

#### Programmatic Use

 Block Parameter: `Operator` Type: character vector Values: ```'exp' | 'log' | '10^u' | 'log10' | 'magnitude^2' | 'square' | 'pow' | 'conj' | 'reciprocal' | 'hypot' | 'rem' | 'mod' | 'transpose' | 'hermitian'``` Default: `'exp'`

Algorithm method for `reciprocal` function, specified as `Exact` or `Newton-Raphson`. To calculate reciprocal with the Newton-Raphson approximation method, select `Newton-Raphson`. Otherwise, select `Exact`.

#### Dependency

Setting Function to `reciprocal` enables this parameter.

#### Programmatic Use

 Block Parameter: `AlgorithmType` Type: character vector Values: `'Exact' | 'Newton-Raphson'` Default: `'Exact'`

Take into account sign of the input signal when calculating power, specified as on or off. This parameter applies only for even-order roots, such as u1/2, u1/4, and so forth.

• on — Calculate power of the absolute value of the input, multiplied by the sign of the input.

• off — Calculate power of the actual input value. If the first input is negative and the second input is an even-order root, return `nan`.

#### Dependency

Setting Function to `pow` enables this parameter.

#### Programmatic Use

 Block Parameter: `SignedPower` Type: character vector Values: `'on' | 'off' | ` Default: `'on'`

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

FunctionInput Signal TypeOutput Signal Type
AutoRealComplex

`exp`, `log`, `10u`, `log10`, `square`, `pow`, `reciprocal`, `conjugate`, `transpose`, `hermitian`

`real`

`complex`

`real`

`complex`

`real`

`error`

`complex`

`complex`

`magnitude squared`

`real `

`complex`

`real`

`real`

`real`

`real`

`complex`

`complex`

`hypot`, `rem`, `mod`

`real`

`complex`

`real`

`error`

`real`

`error`

`complex`

`error`

#### Programmatic Use

 Block Parameter: `OutputSignalType` Type: character vector Values: `'auto' | 'real' | 'complex'` Default: `'auto'`

Number of Newton-Raphson iterations, specified as a scalar.

#### Dependencies

To enable this parameter, set:

• Function to `reciprocal`.

• Algorithm method to `Newton-Raphson`.

#### Programmatic Use

 Block Parameter: `Iterations` Type: character vector Values: `'3' | scalar` Default: `'3'`

Specify the sample time as a value other than -1. For more information, see Specify Sample Time.

#### Dependencies

This parameter is not visible unless it is explicitly set to a value other than `-1`. To learn more, see Blocks for Which Sample Time Is Not Recommended.

#### Programmatic Use

 Block Parameter: `SampleTime` Type: character vector Values: scalar or vector Default: `'-1'`

### Signal Attributes

Lower value of the output range that Simulink checks.

Simulink uses the minimum to perform:

Note

Output minimum does not saturate or clip the actual output signal. Use the Saturation block instead.

#### Programmatic Use

 Block Parameter: `OutMin` Type: character vector Values: `'[ ]'`| scalar Default: `'[ ]'`

Upper value of the output range that Simulink checks.

Simulink uses the maximum value to perform:

Note

Output maximum does not saturate or clip the actual output signal. Use the Saturation block instead.

#### Programmatic Use

 Block Parameter: `OutMax` Type: character vector Values: `'[ ]'`| scalar Default: `'[ ]'`

Specify the output data type. You can set it 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 button to display the Data Type Assistant, which helps you set the data type attributes. For more information, see Specify Data Types Using Data Type Assistant.

#### Dependencies

• To enable this parameter, set the Function to `magnitude^2`, `square`, or `reciprocal`.

• For the `magnitude^2` and `square`, when input is a floating-point data type smaller than single precision, the ```Inherit: Inherit via internal rule``` output data type depends on the setting of the Inherit floating-point output type smaller than single precision configuration parameter. Data types are smaller than single-precision when the number of bits needed to encode the data type is less than the 32 bits needed to encode the single precision data type. For example, `half` and `int16` are smaller than single precision.

#### Programmatic Use

 Block Parameter: `OutDataTypeStr` Type: character vector Values: ```'Inherit: Inherit via internal rule``` | ```'Inherit: Same as first input'``` | ```'Inherit: Inherit via back propagation'``` | `'double'` | `'single'` | `'half'` | `'int8'` | `'uint8'` | `'int16'` | `'uint16'` | `'int32'` | `'uint32'` | `'int64'` | `'uint64'` | `'fixdt(1,16)'` | `'fixdt(1,16,0)'` | `'fixdt(1,16,2^0,0)'` | ```''``` Default: ```'Inherit: Same as first input'```

Select this parameter to prevent the fixed-point tools from overriding the Output data type you specify on the block. For more information, see Use Lock Output Data Type Setting (Fixed-Point Designer).

#### Dependencies

To enable this parameter, set the Function to `magnitude^2`, `square`, or `reciprocal`.

#### Programmatic Use

 Block Parameter: `LockScale` Type: character vector Values: `'off' | 'on'` Default: `'off'`

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

Block parameters always round to the nearest representable value. To control the rounding of a block parameter, enter an expression using a MATLAB rounding function into the mask field.

#### Dependencies

To enable this parameter, set the Function to `magnitude^2`, `square`, or `reciprocal`.

#### Programmatic Use

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

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.

#### Dependencies

To enable this parameter, set the Function to `magnitude^2`, `square`, `conj`, `reciprocal`, or `hermitian`.

#### Programmatic Use

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

## Block Characteristics

 Data Types `Boolean` | `double` | `fixed point` | `half` | `integer` | `single` Direct Feedthrough `yes` Multidimensional Signals `yes` Variable-Size Signals `yes` Zero-Crossing Detection `no`

## See Also

Introduced before R2006a

## Support

#### Model-Based Design for Embedded Control Systems

Download white paper