Perform mathematical function
expand all in page
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 listin Math Function block.
Function | Description | Mathematical Expression | MATLAB® Equivalent |
---|---|---|---|
exp | Exponential |
| exp |
log | Natural logarithm |
| log |
2^u | Power of base 2 |
|
|
10^u | Power of base 10 |
|
|
log10 | Common (base 10) logarithm |
| log10 |
magnitude^2 | Complex modulus |
|
|
square | Power 2 |
|
|
pow | Power |
| power |
conj | Complex conjugate |
| conj |
reciprocal with Exact method | Reciprocal |
|
|
reciprocal with Newton-Raphson method | Reciprocal | See Newton-Raphson Reciprocal Algorithm Method | None |
hypot | Square root of sum squares |
| hypot |
rem | Remainder after division | — | rem |
mod | Modulus after division | — | mod |
transpose | Transpose |
|
|
hermitian | Complex conjugate transpose |
|
|
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. Thefunctions support these types of operations.
Function | Scalar Operations | Element-Wise Vector and Matrix Operations | Vector 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 avector, 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 avector, 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 avector, 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 methodcalculates the reciprocal by using the Newton-Raphson approximation method. Thefunction uses recursive approximation to find better approximations to the roots ofa real-value function.
The reciprocal of a real number is defined as a zero of the function:
Simulink® chooses an initial estimate in the range , because this is the domain of convergence for thefunction.
To successively calculate the roots of the function, specify the Number ofiterations parameter. The process is repeated as follows:
is the derivative of the function .
Data Type Support
This table lists the input data types that each function of the block can support.
Function | Single | Double | Half* | Boolean | Built-In Integer | Fixed Point |
---|---|---|---|---|---|---|
| Yes | Yes | Yes | — | — | — |
| Yes | Yes | Yes | — | — | — |
| Yes | Yes | Yes | — | — | — |
| Yes | Yes | Yes | — | — | — |
| Yes | Yes | Yes | — | — | — |
| Yes | Yes | Yes | — | Yes | Yes |
| Yes | Yes | Yes | — | Yes | Yes |
| Yes | Yes | Yes | — | — | — |
| Yes | Yes | Yes | — | Yes | Yes |
| Yes | Yes | Yes | — | Yes | Yes |
| Yes | Yes | — | — | Yes | Yes |
| Yes | Yes | Yes | — | — | — |
| Yes | Yes | Yes | — | Yes | — |
| Yes | Yes | Yes | — | Yes | — |
| Yes | Yes | Yes | Yes | Yes | Yes |
| 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
Open Model
Ports
Input
expand all
Port_1 — Input signal
scalar | vector | matrix
Input signal specified as a scalar, vector, or matrix. Supported modes accept real andcomplex inputs, except for reciprocal
, which does notaccept complex fixed-point inputs. See Description.
Dependencies
Data type support for this block depends on the Function that youselect 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
Port_2 — Input signal
scalar | vector | matrix
Input signal specified as a scalar, vector, or matrix. Supported modes accept real andcomplex inputs, except for reciprocal
, which does notaccept complex fixed-point inputs.
Dependencies
To enable this port, set Function tohypot
,rem
, ormod
.
Data type support for this block depends on the Function that youselect, 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
Port_1 — Result of the operation of the function on the input orinputs
scalar | vector | matrix
Output signal specified as a scalar, vector, or matrix. The dimensions of the blockoutput depend on the Function that you select andthe size of the inputs. The block output is real or complex, dependingon what you select for Output signal type. SeeDescription.
reciprocal
with Newton-Raphson Method
The reciprocal
with Newton-Raphson methodoutput data type depends on the input data type:
Input Data Type | Output Data Type |
---|---|
single | single |
double | double |
built-in integer | built-in integer |
built-in fixed-point | built-in fixed-point |
|
|
|
|
Data Types: half
| single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| Boolean
| fixed point
Parameters
expand all
Main
Function — Math function
exp
(default) | log
| 2^u
| 10^u
| log10
| magnitude^2
| square
| pow
| conj
| reciprocal
| hypot
| rem
| mod
| transpose
| hermitian
Specify the mathematical function. For more information about the options for thisparameter, see Description.
Dependency
Setting Function topow
enables the Signedpower parameter.
Programmatic Use
Block Parameter:Operator |
Type: charactervector |
Values:'exp' | 'log' | '2^u' | '10^u' | 'log10' |'magnitude^2' | 'square' | 'pow' | 'conj' | 'reciprocal' |'hypot' | 'rem' | 'mod' | 'transpose' |'hermitian' |
Default:'exp' |
Algorithm method — Algorithm method for reciprocal
function
Exact
(default) | Newton-Raphson
Algorithm method for reciprocal
function, specifiedas Exact
orNewton-Raphson
. To calculate reciprocalwith the Newton-Raphson approximation method, selectNewton-Raphson
. Otherwise, selectExact
.
Dependency
Setting Function toreciprocal
enables this parameter.
Programmatic Use
Block Parameter:AlgorithmType |
Type: charactervector |
Values:'Exact' | 'Newton-Raphson' |
Default:'Exact' |
Signed power — Power signedness
on (default) | off
When calculating power, specified as on or off, take into account sign of the inputsignal.. This parameter applies only for even-order roots, such asu1/2,u1/4, and soforth.
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 firstinput is negative and the second input is an even-order root,returns
nan
.
Dependency
Setting Function topow
enables this parameter.
Programmatic Use
Block Parameter:SignedPower |
Type: charactervector |
Values:'on' | 'off' | |
Default:'on' |
Output signal type — Complexity of output signal
auto
(default) | real
| complex
Specify the output signal type of the Math Function block asauto
, real
, orcomplex
.
Function | Input SignalType | Output Signal Type | ||
---|---|---|---|---|
Auto | Real | Complex | ||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Programmatic Use
Block Parameter:OutputSignalType |
Type: charactervector |
Values:'auto' | 'real' | 'complex' |
Default:'auto' |
Number of iterations — Number of Newton-Raphson iterations
3 (default) | scalar
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: charactervector |
Values:'3' | scalar |
Default:'3' |
Sample time (-1 for inherited) — Interval between samples
-1
(default) | scalar | vector
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" |
Signal Attributes
Output minimum — Minimum output value for range checking
[]
(default) | scalar
Lower value of the output range that Simulink checks.
Simulink uses the minimum to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters) for some blocks.
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 the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
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: '[ ]' |
Output maximum — Maximum output value for range checking
[]
(default) | scalar
Upper value of the output range that Simulink checks.
Simulink uses the maximum value to perform:
Parameter range checking (see Specify Minimum and Maximum Values for Block Parameters) for some blocks.
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 the results of some simulation modes such as SIL or external mode. For more information, see Optimize using the specified minimum and maximum values (Embedded Coder).
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: '[ ]' |
Output data type — Specify the output data type
Inherit: Same as firstinput
(default) | Inherit: Inherit via internal rule
| 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>
Specify the output data type. You can set it to:
A rule that inherits a data type, for example,
Inherit: Inherit via backpropagation
The name of a built-in data type, for example,
single
The name of a data type object, for example, a
Simulink.NumericType
objectAn expression that evaluates to a data type, for example,
fixdt(1,16,0)
The Data Type Assistant helps you set dataattributes. To use the Data Type Assistant, click . For more information, see Specify Data Types Using Data Type Assistant.
Dependencies
To enable this parameter, set theFunction to
magnitude^2
,square
, orreciprocal
.For the
magnitude^2
andsquare
, when input is afloating-point data type smaller than single precision, theInherit: Inherit via internalrule
output data type depends on thesetting of the Inherit floating-point output type smaller than single precision configuration parameter. Data types are smaller thansingle-precision when the number of bits needed to encodethe data type is less than the 32 bits needed to encode thesingle precision data type. For example,half
andint16
aresmaller than single precision.
Programmatic Use
Block Parameter:OutDataTypeStr |
Type: charactervector |
Values: 'Inherit: Inherit via internalrule | 'Inherit: Same as firstinput' | 'Inherit: Inherit via backpropagation' | 'double' |'single' | 'half' |'int8' | 'uint8' |'int16' | 'uint16' |'int32' | 'uint32' |'int64' | 'uint64' |'fixdt(1,16)' |'fixdt(1,16,0)' |'fixdt(1,16,2^0,0)' | '<datatype expression>' |
Default: 'Inherit:Same as first input' |
Lock output data type setting against changes by the fixed-point tools — Prevent fixed-point tools from overriding data types
off
(default) | on
Select this parameter to prevent the fixed-point tools from overridingthe Output data type you specify on the block. Formore information, see Use Lock Output Data Type Setting (Fixed-Point Designer).
Dependencies
To enable this parameter, set the Function tomagnitude^2
,square
, orreciprocal
.
Programmatic Use
Block Parameter:LockScale |
Type: charactervector |
Values:'off' | 'on' |
Default:'off' |
Integer rounding mode — Rounding mode for fixed-point operations
Floor
(default) | Ceiling
| Convergent
| Nearest
| Round
| Simplest
| Zero
Rounding mode for fixed-point operations. For more information, seeRounding (Fixed-Point Designer).
Block parameters always round to the nearest representable value. Tocontrol the rounding of a block parameter, enter an expression using aMATLAB rounding function into the mask field.
Dependencies
To enable this parameter, set the Function tomagnitude^2
,square
, orreciprocal
.
Programmatic Use
Block Parameter:RndMeth |
Type: charactervector |
Values:'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' |'Round' | 'Simplest' | 'Zero' |
Default:'Floor' |
Saturate on integer overflow — Choose the behavior when integer overflow occurs
on
(default) | boolean
Action | Rationale | Overflows | Example |
---|---|---|---|
Select Saturate on integer overflow checkbox. | Your model has possible overflow and you want explicit saturation protectionin the generated code. | Overflows saturate to either the minimum ormaximum value that the data type canrepresent. | The maximum value that the |
Do not select Saturate on integer overflowcheck box. | You want to optimize efficiency of yourgenerated code. You want to avoidoverspecifying how a block handles out-of-rangesignals. For more information, see Troubleshoot Signal Range Errors. | Overflows wrap to the appropriate valuethat is representable by the datatype. | The maximum value that the |
When you select this check box, saturation applies to every internal operation on theblock, not just the output or result. The code generation process candetect when overflow is not possible. In this case, the code generatordoes not produce saturation code.
Dependencies
To enable this parameter, set the Function tomagnitude^2
,square
,conj
,reciprocal
, orhermitian
.
Programmatic Use
Block Parameter:SaturateOnIntegerOverflow |
Type: charactervector |
Value:'off' | 'on' |
Default:'on' |
Block Characteristics
Data Types |
|
Direct Feedthrough |
|
Multidimensional Signals |
|
Variable-Size Signals |
|
Zero-Crossing Detection |
|
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using Simulink® Coder™.
HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.
HDL Coder™ provides additional configuration options that affect HDLimplementation and synthesized logic.
HDL Architecture
Function | Architecture | Description |
---|---|---|
conj | ComplexConjugate | Calculate complex conjugate. |
hermitian | Hermitian | Calculate hermitian. |
transpose | Transpose | Calculate array transpose. See Math Functionin the Simulink documentation. |
| ShiftAdd | Perform modulus or remainder operation on a built-ininteger types input by using a non-restoring divisionalgorithm that performs multiple shift and add operations tocompute the
|
Reciprocal Architecture
You can generate HDL code for single-precision floating-point data types in nativefloating-point mode by using Math
architecture forreciprocal
function. Thereciprocal
function in the Math Functionblock also supports the architectures listed in the table.
When you set the Algorithm method toExact
, you can select this options in HDL Blockproperties of this block.
Architecture | Parameters | Additional Cycles of Latency | Description |
---|---|---|---|
ShiftAdd | None | Signed input: (Input word length + 4) Unsigned input: (Input wordlength + 4) | Perform reciprocal operation on a fixed-point input by using a non-restoringdivision algorithm that performs multiple shift and addoperations to compute the reciprocal. This architectureprovides improved accuracy compared to the Newton-Raphsonapproximation method. When you use fixed-pointdata types, following criteria must be satisfied forgenerating the HDL code:
|
You can apply sharing optimization for Math Function Reciprocal block inShiftAdd
architecture. For more information, seeResource Sharing (HDL Coder).
This block has multi-cycle implementations that introduce additionallatency in the generated code. To see the added latency, view thegenerated model or validation model. See Generated Model and Validation Model (HDL Coder).
HDL code generation also supports the Newton-Raphson algorithm method forreciprocal
function. Select the Algorithmmethod as Newton-Raphson
to use thesearchitectures for reciprocal
function in the MathFunction block.
Architecture | Additional Cycles of Latency | Description |
---|---|---|
ReciprocalNewton (default) | Iterations + 1 | Use the multirate implementation of the iterativeNewton method. Select this option to optimize area foryour design. The default value for The recommended value for |
ReciprocalNewtonSingleRate | (Iterations * 2) + 1 | Use the single rate pipelined Newton method. Selectthis option to optimize speed for your design, or if youwant a single rate implementation. Thedefault value for The recommended value for |
ReciprocalRsqrtBasedNewtonSingleRate | Signed input: ( Unsigned input:( | Use the single rate pipelined Newton method. Selectthis option to optimize speed, or if you want a singlerate implementation. The default value for The recommended value for |
HDL Block Properties
General | |
---|---|
ConstrainedOutputPipeline | Number of registers to place at the outputs by moving existing delays within your design. Distributed pipelining does not redistribute these registers. The default is |
InputPipeline | Number of input pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is |
OutputPipeline | Number of output pipeline stages to insert in the generated code. Distributed pipelining and constrained output pipelining can move these registers. The default is |
LatencyStrategy | To enable this property, set HDL architecture to |
CustomLatency | To enable this property, set HDL architecture to |
Native Floating Point | |
---|---|
CheckResetToZero | Use this property for the mod and rem functionsof the Math Function block. If you have numbersa and b such that thequotient a/b is close to an integer, thissetting treats a as an integral multiple ofb , and rem(a,b) = 0.This result is numerically accurate and matches the simulationresults. Calculating this result uses additional resources andincreases the area footprint on the target FPGA device. For moreinformation, see CheckResetToZero (HDL Coder). |
HandleDenormals | Specify whether you want HDL Coder to insert additional logic to handle denormal numbers in your design. Denormal numbers are numbers that have magnitudes less than the smallest floating-point number that can be represented without leading zeros in the mantissa. The default is |
LatencyStrategy | Specify whether to map the blocks in your design to |
NFPCustomLatency | To specify a value, set LatencyStrategy to |
MaxIterations | Use this property for the mod and rem functionsof the Math Function block. If you have numbersa and b that aresignificantly large integers, you can increaseMaxIterations to match the simulationresults. Calculating this result uses additional resources andincreases the area footprint on the target FPGA device. For moreinformation, see MaxIterations (HDL Coder). |
Supported Datatypes and Functions in Native Floating-Point
In the Math Function block, these functions are the supported functions with NativeFloating-Point.
Math Functions | Supported Floating-Point Datatypes | Complex Data Support | ||
---|---|---|---|---|
Half | Single | Double | ||
exp | No | Yes | No | No |
log | No | Yes | Yes | No |
2^u | No | Yes | No | No |
10^u | No | Yes | No | No |
log10 | No | Yes | No | No |
magnitude^2 | No | Yes | Yes | Yes |
square | No | Yes | No | Yes |
pow | No | Yes | No | No |
conj | No | Yes | No | Yes |
reciprocal | Yes | Yes | Yes | No |
hypot | No | Yes | No | No |
rem | No | Yes | No | No |
mod | No | Yes | No | No |
transpose | Yes | Yes | Yes | Yes |
hermitian | No | Yes | No | Yes |
Restrictions
When you use a reciprocal
implementation:
Input must be scalar and must have integer or fixed-point (signed orunsigned) data type.
The output must be scalar and have integer or fixed-point (signed orunsigned) data type.
Only the
Zero
rounding mode issupported.The Saturate on integer overflow option on theblock must be selected.
PLC Code Generation
Generate Structured Text code using Simulink® PLC Coder™.
Fixed-Point Conversion
Design and simulate fixed-point systems using Fixed-Point Designer™.
The Math Function block only supports fixed-point conversion in certainconfigurations. For more information, see the Block Support Table.
Version History
Introduced before R2006a
expand all
R2023a: Math Function
Block Supports 2^u
The Math Function block now supports the2^u
function. The MATLAB equivalent is 2.^u
(see power).
See Also
Sqrt | Trigonometric Function
Topics
- hisl_0004: Usage of natural logarithm and base 10 logarithm operations
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.
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
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)
Contact your local office