Documentation |
Add or subtract inputs
The Sum block performs addition or subtraction on its inputs. This block can add or subtract scalar, vector, or matrix inputs. It can also collapse the elements of a signal.
You specify the operations of the block with the List of signs parameter. Plus (+), minus (-), and spacer (|) characters indicate the operations to be performed on the inputs:
If there are two or more inputs, then the number of + and - characters must equal the number of inputs. For example, "+-+" requires three inputs and configures the block to subtract the second (middle) input from the first (top) input, and then add the third (bottom) input.
All nonscalar inputs must have the same dimensions. Scalar inputs will be expanded to have the same dimensions as the other inputs.
A spacer character creates extra space between ports on the block's icon.
For a round Sum block, the first input port is the port closest to the 12 o'clock position going in a counterclockwise direction around the block. Similarly, other input ports appear in counterclockwise order around the block.
If only addition of all inputs is required, then a numeric parameter value equal to the number of inputs can be supplied instead of "+" characters.
If only one input port is required, a single "+" or "-" collapses the element via the specified operation.
The Sum block first converts the input data type(s) to its accumulator data type, then performs the specified operations. The block converts the result to its output data type using the specified rounding and overflow modes.
Output calculation for the Sum block depends on the number of block inputs and the sign of input ports:
If the Sum block has... | And... | The formula for output calculation is... | Where... |
---|---|---|---|
One input port | The input port sign is + | y = e[0] + e[1] + e[2] ... + e[m] | e[i] is the i^{th} element of input u |
The input port sign is – | y = 0.0 – e[0] – e[1] – e[2] ... – e[m] | ||
Two or more input ports | All input port signs are – | y = 0.0 – u[0] – u[1] – u[2] ... – u[n] | u[i] is the input to the i^{th} input port |
The k^{th} input port is the first port where the sign is + | y = u[k] – u[0] – u[1] – u[2] – u[k–1] (+/–) u[k+1] ... (+/–) u[n] |
The Sum block accepts real or complex signals of the following data types:
Floating point
Built-in integer
Fixed point
Boolean
The inputs can be of different data types, unless you select the Require all inputs to have the same data type parameter. For more information, see Data Types Supported by Simulink in the Simulink^{®} documentation.
The Main pane of the Sum block dialog box appears as follows:
The Signal Attributes pane of the Sum block dialog box appears as follows:
Display the Data Type Assistant.
The Data Type Assistant helps you set the Output data type parameter.
For more information, see Specify Block Output Data Types.
Designate the icon shape of the block.
Default: round
Designate the icon shape of the block as rectangular.
Designate the icon shape of the block as round.
See Block-Specific Parameters for the command-line information.
Enter plus (+) and minus (-) characters.
Default: |++
Addition is the default operation, so if you only want to add the inputs, enter the number of input ports.
For a single vector input, "+" or "-" will collapse the vector using the specified operation.
Enter as many plus (+) and minus (-) characters as there are inputs.
You can manipulate the positions of the input ports on the block by inserting spacers (|) between the signs in the List of signs parameter. For example, "++|--" creates an extra space between the second and third input ports.
Entering only one element enables the Sum over parameter.
See Block-Specific Parameters for the command-line information.
Select dimension over which to perform the sum over operation.
Default: All dimensions
Sum all input elements, yielding a scalar.
Display the Dimension parameter, where you specify the dimension over which to perform the operation.
Selecting Specified dimension enables the Dimension parameter.
List of signs (when it has only one element) enables this parameter.
See Block-Specific Parameters for the command-line information.
Specify the dimension over which to perform the operation.
Default: 1
The block follows the same summation rules as the MATLAB^{®} sum function.
Suppose that you have a 2-by-3 matrix U.
Setting Dimension to 1 results in the output Y being computed as:
$$Y={\displaystyle {\sum}_{i=1}^{2}U(i,j)}$$
Setting Dimension to 2 results in the output Y being computed as:
$$Y={\displaystyle {\sum}_{j=1}^{3}U(i,j)}$$
If the specified dimension is greater than the dimension of the input, an error message appears.
Setting Sum over to Specified dimension enables this parameter.
See Block-Specific Parameters for the command-line information.
Enter the discrete interval between sample time hits or specify another appropriate sample time such as continuous or inherited.
Default: -1
By default, the block inherits its sample time based upon the context of the block within the model. To set a different sample time, enter a valid sample time based upon the table in Types of Sample Time.
See also Specify Sample Time in the online documentation for more information.
See Block-Specific Parameters for the command-line information.
Require that all inputs have the same data type.
Default: Off
Require that all inputs have the same data type.
Do not require that all inputs have the same data type.
See Block-Specific Parameters for the command-line information.
Select to lock data type settings of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor.
Default: Off
Locks all data type settings for this block.
Allows the Fixed-Point Tool and the Fixed-Point Advisor to change data type settings for this block.
See Block-Specific Parameters for the command-line information.
Specify the rounding mode for fixed-point operations.
Default: Floor
Rounds both positive and negative numbers toward positive infinity. Equivalent to the MATLAB ceil function.
Rounds number to the nearest representable value. If a tie occurs, rounds to the nearest even integer. Equivalent to the Fixed-Point Designer™ convergent function.
Rounds both positive and negative numbers toward negative infinity. Equivalent to the MATLAB floor function.
Rounds number to the nearest representable value. If a tie occurs, rounds toward positive infinity. Equivalent to the Fixed-Point Designer nearest function.
Rounds number to the nearest representable value. If a tie occurs, rounds positive numbers toward positive infinity and rounds negative numbers toward negative infinity. Equivalent to the Fixed-Point Designer round function.
Automatically chooses between round toward floor and round toward zero to generate rounding code that is as efficient as possible.
Rounds number toward zero. Equivalent to the MATLAB fix function.
Parameter: RndMeth |
Type: string |
Value: 'Ceiling' | 'Convergent' | 'Floor' | 'Nearest' | 'Round' | 'Simplest' | 'Zero' |
Default: 'Floor' |
For more information, see Rounding in the Fixed-Point Designer documentation.
Specify whether overflows saturate.
Default: Off
Overflows saturate to either the minimum or maximum value that the data type can represent.
For example, an overflow associated with a signed 8-bit integer can saturate to -128 or 127.
Overflows wrap to the appropriate value that is representable by the data type.
For example, the number 130 does not fit in a signed 8-bit integer and wraps to -126.
Consider selecting this check box when your model has possible overflow and you want explicit saturation protection in the generated code.
Consider clearing this check box when you want to optimize efficiency of your generated code.
Clearing this check box also helps you avoid overspecifying how a block handles out-of-range signals. For more information, see Checking for Signal Range Errors.
When you select this check box, saturation applies to every internal operation on the block, not just the output or result.
In general, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.
Parameter: SaturateOnIntegerOverflow |
Type: string |
Value: 'off' | 'on' |
Default: 'off' |
Specify the accumulator data type.
Default: Inherit: Inherit via internal rule
Use internal rule to determine accumulator data type.
Use data type of first input signal.
Accumulator data type is double.
Accumulator data type is single.
Accumulator data type is int8.
Accumulator data type is uint8.
Accumulator data type is int16.
Accumulator data type is uint16.
Accumulator data type is int32.
Accumulator data type is uint32.
Accumulator data type is fixed point fixdt(1,16,0).
Accumulator data type is fixed point fixdt(1,16,2^0,0).
The name of a data type object, for example Simulink.NumericType
See Block-Specific Parameters for the command-line information.
For more information, see Specify Data Types Using Data Type Assistant.
Select the category of accumulator data to specify
Default: Inherit
Specifies inheritance rules for data types. Selecting Inherit enables a list of possible values:
Inherit via internal rule (default)
Same as first input
Specifies built-in data types. Selecting Built in enables a list of possible values:
double (default)
single
int8
uint8
int16
uint16
int32
uint32
Specifies fixed-point data types.
Specifies expressions that evaluate to data types. Selecting Expression enables you to enter an expression.
Clicking the Show data type assistant button for the accumulator data type enables this parameter.
See Block-Specific Parameters for the command-line information.
See Specify Data Types Using Data Type Assistant.
Specify data type override mode for this signal.
Default: Inherit
Inherits the data type override setting from its context, that is, from the block, Simulink.Signal object or Stateflow^{®} chart in Simulink that is using the signal.
Ignores the data type override setting of its context and uses the fixed-point data type specified for the signal.
The ability to turn off data type override for an individual data type provides greater control over the data types in your model when you apply data type override. For example, you can use this option to ensure that data types meet the requirements of downstream blocks regardless of the data type override setting.
This parameter appears only when the Mode is Built in or Fixed point.
Specify whether you want the fixed-point data to be signed or unsigned.
Default: Signed
Specify the fixed-point data to be signed.
Specify the fixed-point data to be unsigned.
Selecting Mode > Fixed point for the accumulator data type enables this parameter.
See Block-Specific Parameters for the command-line information.
See Specifying a Fixed-Point Data Type for more information.
Specify the bit size of the word that will hold the quantized integer.
Default: 16
Minimum: 0
Maximum: 32
Large word sizes represent large values with greater precision than small word sizes.
Selecting Mode > Fixed point for the accumulator data type enables this parameter.
See Block-Specific Parameters for the command-line information.
See Specifying a Fixed-Point Data Type for more information.
Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.
Default: Binary point
Selecting Mode > Fixed point for the accumulator data type enables this parameter.
Selecting Binary point enables:
Fraction length
Selecting Slope and bias enables:
Slope
Bias
See Block-Specific Parameters for the command-line information.
See Specifying a Fixed-Point Data Type for more information.
Specify fraction length for fixed-point data type.
Default: 0
Binary points can be positive or negative integers.
Selecting Scaling > Binary point for the accumulator data type enables this parameter.
See Block-Specific Parameters for the command-line information.
See Specifying a Fixed-Point Data Type for more information.
Specify slope for the fixed-point data type.
Default: 2^0
Specify any positive real number.
Selecting Scaling > Slope and bias for the accumulator data type enables this parameter.
See Block-Specific Parameters for the command-line information.
See Specifying a Fixed-Point Data Type for more information.
Specify bias for the fixed-point data type.
Default: 0
Specify any real number.
Selecting Scaling > Slope and bias for the accumulator data type enables this parameter.
See Block-Specific Parameters for the command-line information.
See Specifying a Fixed-Point Data Type for more information.
Lower value of the output range that Simulink checks.
Default: [] (unspecified)
Specify this number as a finite, real, double, scalar value.
Note: If you specify a bus object as the data type for this block, do not set the minimum value for bus data on the block. Simulink ignores this setting. Instead, set the minimum values for bus elements of the bus object specified as the data type. For information on the Minimum parameter for a bus element, see Simulink.BusElement. |
Simulink uses the minimum to perform:
Parameter range checking (see Check Parameter Values) for some blocks
Simulation range checking (see Signal Ranges and Enabling Simulation Range Checking)
Automatic scaling of fixed-point data types
Note: Output minimum does not saturate or clip the actual output signal. Use the Saturation block instead. |
Parameter: OutMin |
Type: string |
Value: '[ ]' |
Default: '[ ]' |
Upper value of the output range that Simulink checks.
Default: [] (unspecified)
Specify this number as a finite, real, double, scalar value.
Note: If you specify a bus object as the data type for this block, do not set the maximum value for bus data on the block. Simulink ignores this setting. Instead, set the maximum values for bus elements of the bus object specified as the data type. For information on the Maximum parameter for a bus element, see Simulink.BusElement. |
Simulink uses the maximum value to perform:
Parameter range checking (see Check Parameter Values) for some blocks
Simulation range checking (see Signal Ranges and Enabling Simulation Range Checking)
Automatic scaling of fixed-point data types
Note: Output maximum does not saturate or clip the actual output signal. Use the Saturation block instead. |
Parameter: OutMax |
Type: string |
Value: '[ ]' |
Default: '[ ]' |
Specify the output data type.
Default: Inherit: Inherit via internal rule
Simulink chooses a data type to balance numerical accuracy, performance, and generated code size, while taking into account the properties of the embedded target hardware. If you change the embedded target settings, the data type selected by the internal rule might change. It is not always possible for the software to optimize code efficiency and numerical accuracy at the same time. If the internal rule doesn't meet your specific needs for numerical accuracy or performance, use one of the following options:
Specify the output data type explicitly.
Use the simple choice of Inherit: Same as first input.
Explicitly specify a default data type such as fixdt(1,32,16) and then use the Fixed-Point Tool to propose data types for your model. For more information, see fxptdlg.
To specify your own inheritance rule, use Inherit: Inherit via back propagation and then use a Data Type Propagation block. Examples of how to use this block are available in the Signal Attributes library Data Type Propagation Examples block.
Use data type of the driving block.
Use data type of first input signal.
Output data type is the same as accumulator data type.
Output data type is double.
Output data type is single.
Output data type is int8.
Output data type is uint8.
Output data type is int16.
Output data type is uint16.
Output data type is int32.
Output data type is uint32.
Output data type is fixed point fixdt(1,16,0).
Output data type is fixed point fixdt(1,16,2^0,0).
Use a data type object, for example, Simulink.NumericType.
See Block-Specific Parameters for the command-line information.
For more information, see Specify Block Output Data Types.
Select the category of data to specify.
Default: Inherit
Inheritance rules for data types. Selecting Inherit enables a second menu/text box to the right. Select one of the following choices:
Inherit via internal rule (default)
Inherit via back propagation
Same as first input
Same as accumulator
Built-in data types. Selecting Built in enables a second menu/text box to the right. Select one of the following choices:
double (default)
single
int8
uint8
int16
uint16
int32
uint32
Fixed-point data types.
Expressions that evaluate to data types. Selecting Expression enables a second menu/text box to the right, where you can enter the expression.
Clicking the Show data type assistant button enables this parameter.
See Block-Specific Parameters for the command-line information.
See Specify Data Types Using Data Type Assistant.
Specify whether you want the fixed-point data as signed or unsigned.
Default: Signed
Selecting Mode > Fixed point enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
Specify the bit size of the word that holds the quantized integer.
Default: 16
Minimum: 0
Maximum: 32
Selecting Mode > Fixed point enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
Specify the method for scaling your fixed-point data to avoid overflow conditions and minimize quantization errors.
Default: Binary point
Selecting Mode > Fixed point enables this parameter.
Selecting Binary point enables:
Fraction length
Calculate Best-Precision Scaling
Selecting Slope and bias enables:
Slope
Bias
Calculate Best-Precision Scaling
See Block-Specific Parameters for the command-line information.
See Specifying a Fixed-Point Data Type.
Specify fraction length for fixed-point data type.
Default: 0
Binary points can be positive or negative integers.
Selecting Scaling > Binary point enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
Specify slope for the fixed-point data type.
Default: 2^0
Specify any positive real number.
Selecting Scaling > Slope and bias enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
Specify bias for the fixed-point data type.
Default: 0
Specify any real number.
Selecting Scaling > Slope and bias enables this parameter.
For more information, see Specifying a Fixed-Point Data Type.
If you use - on the first input port, the Sum block reorders the inputs so that, if possible, the first input uses a + operation. For example, in the expression output = -a-b+c, the Sum block reorders the inputs so that output = c-a-b. To initialize the accumulator, the Sum block uses the first + input port.
The block avoids performing a unary minus operation on the first operand a because doing so can change the value of a for fixed-point data types. In that case, the output value differs from the result of accumulating the values for a, b, and c.
Tip To explicitly specify a unary minus operation for output = -a-b+c, you can use the Unary Minus block in the Math Operations library. |
Suppose that you have the following modelmodel:
The following block parameters apply:
Both Constant blocks, Input1 and Input 2, use int8 for the Output data type.
The Sum block uses int8 for both Accumulator data type and Output data type.
The Sum block has Saturate on integer overflow turned on.
The Sum block reorders the inputs so that the following operations occur and you get the ideal result of 127.
Step | Block Operation |
---|---|
1 | Reorders inputs from (–Input1 + Input2) to (Input2 – Input1). |
2 | Initializes the accumulator by using the first + input port: Accumulator = int8(-1) = -1 |
3 | Continues to accumulate values: Accumulator = Accumulator – int8(-128) = 127 |
4 | Calculates the block output: Output = int8(127) = 127 |
If the Sum block does not reorder the inputs, the following operations occur instead and you get the nonideal result of 126.
Step | Block Operation |
---|---|
1 | Initializes the accumulator by using the first input port: Accumulator = int8(-(-128)) = 127 Because saturation is on, the initial value of the accumulator saturates at 127 and does not wrap. |
2 | Continues to accumulate values: Accumulator = Accumulator + int8(-1) = 126 |
3 | Calculates the block output: Output = int8(126) = 126 |