MATLAB中iterate程序,Iterate Subsystem Execution

Iterate Subsystem Execution

A logically executed subsystem block runs one or more times at the

current time step when enabled by a control block. A control block

implements control logic similar to that expressed by a programming language statement (e.g.,

if-then, switch, while, for).

Iterator subsystems are one type of logically executed subsystem that execute one or more

times during a time step in response to a control block internal to the subsystem block.

Note

The While Iterator Subsystem and For Iterator Subsystem

blocks must not contain blocks with continuous states (for example, blocks from the

Continuous block library). The sample times of all blocks within the subsystem must be

either inherited (-1) or constant (inf).

Models with While Structures

The While Iterator Subsystem block is a Subsystem block

preconfigured as a starting point for creating a subsystem that repeats execution during a

simulation time step while a logical (Boolean) expression is true.

Consider the following model. To open model, see

264117b00bee2395272e3eee7073dfb2.png

An input of 1 (true) to the While

Iterator block activates the subsystem. At each time step, the current iterative

number is added to a running total until a maximum sum is reached.

The while structure in the model can be represented with the

following pseudo code.

maximum_sum = 10;

sum = 0;

iteration_number = 0

condition = (maximum_sum > 0)

WHILE condition NOT EQUAL 0

iteration_number = iteration_number + 1

sum = sum + iteration_number

IF (sum > maximum_sum OR iteration_number > maximum_iterations) THEN

condition = 0

END WHILECreate Model with While Structure

To create the example model, use the following procedure.

Place a While Iterator Subsystem block in the Simulink® Editor. Double-click the subsystem block to display its contents.

dd417c4697b2716149cb42554a172a13.png

Double-click the While Iterator block to open its block parameters

dialog box. Set the Maximum number of iterations to

20 and States when starting to

reset. Select the Show iteration

number port check box.

Add Memory, Relational Operator, and

Sum blocks. Connect blocks as shown. For the Memory

block, select the Inherit sample time check box.

fab7924713eebb1b462a32c295ebefba.png

The iteration number from the output of the While Iterator block is

added to its previous value until the sum is greater or equal to the maximum sum from

Inport block 1.

Navigate to the top level of the model.

Connect a Constant block to input port 2. This block provides the

initial logical condition value for the While

Iterator block. Set the Constant value to any non-zero

number.

The While Iterator block requires an initial logical condition

(input port labeled IC) for its first iteration. This signal

enables the While Iterator Subsystem block and must originate from

outside the subsystem. If this value is nonzero, the first iteration takes

place.

Connect a second Constant block to input port 1. This block

provides a maximum value for the iterative algorithm. The algorithm adds successive

integers until a maximum value is reached.

Connect a Display block to output port 1. This block shows the

number of iterations from the While Integrator block output port.

Run a simulation.

During a single time step, the first four iteration numbers are added for a total

sum (10). With the fifth iteration, the sum (15)

is greater than the maximum sum (10), the iterations stop, and the

block waits for the next time step.

Note

Simulation time does not advance during iterative executions of a While

Iteration Subsystem block. Nevertheless, blocks in the subsystem treat each

iteration as a time step. As a result, in a While Iterator Subsystem

block, the output of a block with states (that is, a block whose output depends on its

previous input), reflects the value of its input at the previous iteration of the

while loop. The output does not reflect the

block input at the previous simulation time step. For example, a Unit Delay block in a

While subsystem outputs the value of its input at the previous iteration of the

while loop, not the value at the previous simulation time

step.Model with For Structures

The For Iterator Subsystem block is a Subsystem block preconfigured as a

starting point for creating a subsystem that repeats execution during a simulation time step

for a specified number of iterations.

Consider the following model. To open model, see

7548529e3df093a8d9d77e998c3ab8a9.png

The input to the For Iterator block specifies the number of iterations. At each time

step, the current iterative number is added to a running total for 5

iterations.

The for structure in the model can be represented with the

following pseudo code.

number_of_iterations = 5

sum = 0;

iteration_number = 0

FOR iteration_number = 0 TO number_of_iterations

iteration_number = iteration_number + 1

sum = sum + iteration_number

END FORCreate Model With For Structure

To create the example model, use the following procedure.

Place a For Iterator Subsystem block in the Simulink Editor. Double-click the subsystem block to display its contents.

0c5fad61a4183a8f4ba2762fb079b8f8.png

Double-click the For Iterator block to open its block parameters

dialog box. Set States when starting to

reset and Iteration limit source to

external.

Add Memory, Sum, and Outport blocks.

Connect blocks as shown. For the Memory block, select the

Inherit sample time check box.

6886c1cdf079809da2168cc1ee345e08.png

The iteration number from the output of the For Iterator block is

added to its previous value for the specified number of iterations from

Inport block 1.

Navigate to the top level of the model.

Connect a Constant block to input port 1. This block provides the

number of iterations for the For Iterator block. Set the

Constant value to 5.

Connect Display blocks to output ports 1 and 2. These blocks shows

the number of iterations from the For Integrator block output port and

the sum from the Memory block.

Run a simulation.

During each time step, the first five iteration numbers are added for a total sum

(15).Using Assignment Blocks

The For Iterator block works well with the Assignment block to reassign values in a

vector or matrix. The following example shows the use of a For Iterator block. Note the

matrix dimensions in the data being passed.

8aaf4fc879fe65af129d4a811157d853.png

814909bfee6685a722f8810bd0470529.png

The example outputs the sine value of an input 2-by-5 matrix (2 rows, 5 columns) using

a For subsystem containing an Assignment block. The process is as follows.

A 2-by-5 matrix is input to the Selector block and the Assignment block.

The Selector block strips off a 2-by-1 matrix from the input matrix at the

column value indicated by the current iteration value of the For Iterator

block.

The sine of the 2-by-1 matrix is taken.

The sine value 2-by-1 matrix is passed to an Assignment block.

The Assignment block, which takes the original 2-by-5 matrix as one of its

inputs, assigns the 2-by-1 matrix back into the original matrix at the column

location indicated by the iteration value.

The rows specified for reassignment in the parameter dialog box for the

Assignment block in the example are [1,2]. Because there are only

two rows in the original matrix, you could also have specified -1 for the rows (that

is, all rows).

Note

The Trigonometric Function block is already capable of taking the sine of a

matrix. The example uses the Trigonometric Function block only as an example for

changing each element of a matrix with the collaboration of an Assignment block

and a For Iterator block.

See Also

Blocks

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值