Main Content

Math Function

Perform mathematical function

  • Math Function block

Libraries:
Simulink / Math Operations
HDL Coder / Math Operations

Description

The Math Function block performs many common mathematical functions.

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

FunctionDescriptionMathematical Expression MATLAB® Equivalent
exp

Exponential

eu

exp
log

Natural logarithm

ln u

log
2^u

Power of base 2

2u

2.^u
(see power)

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)

Tip

To perform square root calculations, use the Sqrt block.

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

Not applicable

log

Yes

Yes

Not applicable

2^u

Yes

Yes

Not applicable

10^u

Yes

Yes

Not applicable

log10

Yes

Yes

Not applicable

magnitude^2

Yes

Yes

Not applicable

square

Yes

Yes

Not applicable

pow

Yes

Yes

Not applicable

conj

Yes

Yes

Not applicable

reciprocal with Exact method

Yes

Yes

Not applicable

reciprocal with Newton-Raphson method

Yes

Yes

Not applicable

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)

Not applicable

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)

Not applicable

transpose

Yes

Yes

hermitian

Yes

Yes

The name of the function and the appropriate number of input ports appear on the block.

Tip

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

Newton-Raphson Reciprocal Algorithm Method

The reciprocal function that has the Newton-Raphson algorithm method calculates the reciprocal by using the Newton-Raphson approximation method. The function uses recursive approximation to find better approximations to the roots of a real-value function.

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

f(x)=1xa.

Simulink® chooses an initial estimate in the range 0<x0<2a, because this is the domain of convergence for the function.

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

xi+1=xif(xi)f'(xi)=xi+(xiaxi2)=xi.(2axi)

f'(x) is the derivative of the function f(x).

Data Type Support

This table lists 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

2^u

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

Examples

Ports

Input

expand all

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

Dependencies

Data type support for this block depends on the Function that you select and the size of the inputs. 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. Supported modes accept 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 that you select, and the size of the inputs. 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 that 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.

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. For more information about the options for this parameter, see Description.

Dependency

Setting Function to pow enables the Signed power parameter.

Programmatic Use

Block Parameter: Operator
Type: character vector
Values: 'exp' | 'log' | '2^u' | '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'

When calculating power, specified as on or off, take into account sign of the input signal.. 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, returns 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, 2^u, 10^u, 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 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

To set the block parameter value programmatically, use the set_param function.

Parameter: SampleTime
Values: "-1" (default) | scalar or vector in quotes

Signal Attributes

Lower value of the output range that the software checks.

The software uses the minimum to perform:

Tips

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

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: OutMin
Values: '[]' (default) | scalar in quotes

Upper value of the output range that the software checks.

The software uses the maximum value to perform:

Tips

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

Programmatic Use

To set the block parameter value programmatically, use the set_param function.

Parameter: OutMax
Values: '[]' (default) | scalar in quotes

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)

The Data Type Assistant helps you set data attributes. To use the Data Type Assistant, click the Show data type assistant button. 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)' | '<data type expression>'
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 Modes (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'

ActionRationaleOverflowsExample

Select Saturate on integer overflow 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 Saturate on integer overflow 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. 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

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

Version History

Introduced before R2006a

expand all