Perform mathematical function - Simulink (2024)

Table of Contents
Description Newton-Raphson Reciprocal Algorithm Method Data Type Support Examples Ports Input Port_1 — Input signalscalar | vector | matrix Port_2 — Input signal scalar | vector | matrix Output Port_1 — Result of the operation of the function on the input orinputsscalar | vector | matrix Parameters Main Function — Math functionexp (default) | log | 2^u | 10^u | log10 | magnitude^2 | square | pow | conj | reciprocal | hypot | rem | mod | transpose | hermitian Algorithm method — Algorithm method for reciprocal function Exact (default) | Newton-Raphson Signed power — Power signedness on (default) | off Output signal type — Complexity of output signal auto (default) | real | complex Number of iterations — Number of Newton-Raphson iterations 3 (default) | scalar Sample time (-1 for inherited) — Interval between samples -1 (default) | scalar | vector Signal Attributes Output minimum — Minimum output value for range checking[] (default) | scalar Output maximum — Maximum output value for range checking [] (default) | scalar 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> Lock output data type setting against changes by the fixed-point tools — Prevent fixed-point tools from overriding data typesoff (default) | on Integer rounding mode — Rounding mode for fixed-point operations Floor (default) | Ceiling | Convergent | Nearest | Round | Simplest | Zero Saturate on integer overflow — Choose the behavior when integer overflow occurs on (default) | boolean 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 R2023a: Math Function Block Supports 2^u See Also Topics MATLAB Command Americas Europe Asia Pacific

Perform mathematical function

expand all in page

  • Perform mathematical function - Simulink (1)

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.

FunctionDescriptionMathematical ExpressionMATLAB® 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, andpower)

square

Power 2

u2

u.^2
(see power)

pow

Power

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

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. Thefunctions 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 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 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 thefunction.

To successively calculate the roots of the function, specify the Number ofiterations 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

Bounded Variable-Size Signal Basic OperationsGenerate bounded variable-size signals and illustrates some of the operations using those signals. In this example, you generate variable-size signals using the Selector block and the Switch block. The signals are used in math operations, bus creation, bus selection matrix concatenation, and to implement a discrete filter equation.

Open Model

Ports

Input

expand all

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

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

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 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 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 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'

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'

Specify the output signal type of the Math Function block asauto, real, orcomplex.

FunctionInput SignalTypeOutput 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: charactervector
Values:'auto' | 'real' | 'complex'
Default:'auto'

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

Dependencies

To enable this parameter, set:

  • Function toreciprocal.

  • Algorithm method toNewton-Raphson.

Programmatic Use

Block Parameter:Iterations
Type: charactervector
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

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

Signal Attributes

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: '[ ]'

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: '[ ]'

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, aSimulink.NumericType object

  • An 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 Perform mathematical function - Simulink (3). For more information, see Specify Data Types Using Data Type Assistant.

Dependencies

  • To enable this parameter, set theFunction tomagnitude^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 and int16 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'

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'

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'
ActionRationaleOverflowsExample

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 theint8 (signed, 8-bit integer)data type can represent is 127. Any blockoperation result greater than this maximum valuecauses overflow of the 8-bit integer. With thecheck box selected, the block output saturates at127. Similarly, the block output saturates at aminimum output value of -128.

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 theint8 (signed, 8-bit integer)data type can represent is 127. Any blockoperation result greater than this maximum valuecauses overflow of the 8-bit integer. With thecheck box cleared, the software interprets theoverflow-causing value as int8,which can produce an unintended result. Forexample, 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 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

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

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

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.

Perform mathematical function - Simulink (4)

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

Perform mathematical function - Simulink (2024)
Top Articles
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 5550

Rating: 4.3 / 5 (74 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.