Module 7: Advanced Modeling and Structural Techniques
7.1. Integrating and Differentiating Signals
Beyond basic arithmetic, Simulink provides powerful blocks for calculus operations, which are fundamental to modeling the physics of dynamic systems. The ability to integrate and differentiate signals over time is essential in fields like mechanics, electronics, and control theory.
Let’s construct a model to visualize a sine wave alongside its derivative and its integral.
- Assemble the Blocks: In a new model, place the following blocks:
- One ‘Sine Wave’ block (from Sources).
- One ‘Scope’ block (from Sinks).
- One ‘Derivative’ block (from Continuous).
- One ‘Integrator’ block (from Continuous).
- Configure the Scope: The ‘Scope’ needs to display three signals simultaneously. Open its parameters and set the Number of input ports to 3.
- Wire the Model: The wiring for this model demonstrates signal splitting.
- The output of the ‘Sine Wave’ block will branch out to three paths.
- Path 1: Connect the ‘Sine Wave’ output directly to the first port on the ‘Scope’. This is our original signal.
- Path 2: Connect the ‘Sine Wave’ output to the input of the ‘Derivative’ block. Then, connect the output of the ‘Derivative’ block to the second port on the ‘Scope’.
- Path 3: Connect the ‘Sine Wave’ output to the input of the ‘Integrator’ block. Then, connect the output of the ‘Integrator’ block to the third port on the ‘Scope’.
- Run and Analyze: Run the simulation. When you open the Scope, you will see three distinct waves. As expected from calculus, the original sin(t) wave will be accompanied by its derivative, a cos(t) wave, and its integral, a -cos(t) wave.
7.2. Creating Subsystems for Model Clarity
As models become more complex, the main canvas can become cluttered and difficult to read. A crucial technique for managing this complexity is the use of subsystems. A subsystem allows you to group a set of blocks and collapse them into a single, new block, creating a hierarchical and more organized model.
Let’s demonstrate this with a simple calculation model.
- Build the Initial Model: Create a model that calculates -(10 + 20). This requires:
- Two ‘Constant’ blocks (with values set to 10 and 20).
- An ‘Add’ block to sum them.
- A ‘Unary Minus’ block to negate the result.
- A ‘Display’ block to show the final output.
- Connect them in sequence. Running this flat model will correctly display -30.
- Select Blocks for Subsystem: To group the calculation logic, select the ‘Add’ block and the ‘Unary Minus’ block simultaneously by holding the Shift key or dragging a selection box around them.
- Create the Subsystem: With the blocks selected, click the “Create Subsystem” button that appears in the toolbar.
- Observe the Result: The two selected blocks will be replaced by a single block labeled ‘Subsystem’. The connections to and from the original group of blocks are automatically preserved. If you run the simulation again, the result is still -30, proving the functionality is unchanged.
- Explore the Subsystem: To see the underlying logic, simply double-click the new ‘Subsystem’ block. This will open a new canvas showing the original ‘Add’ and ‘Unary Minus’ blocks inside.
7.3. Implementing Iteration with For-Iterator Subsystems
While Simulink is primarily a continuous-time simulation tool, it also provides methods for implementing iterative algorithms. The ‘For Iterator Subsystem’ behaves much like a standard for loop in a text-based programming language, executing its internal block diagram a specified number of times.
We will use this block to calculate the sum of integers from 1 to N (where N=5).
- Outer Model Setup:
- On the main model canvas, place a ‘Constant’ block, a ‘For Iterator Subsystem’ block, and a ‘Display’ block.
- Set the ‘Constant’ block’s value to 5. This will be our iteration limit, N.
- Iterator Configuration:
- Open the parameters for the ‘For Iterator Subsystem’.
- Change the “Iteration limit source” from ‘internal’ to ‘external’. This creates an input port on the block.
- Connect the ‘Constant’ block to this new input port to supply the value of N. Connect the iterator’s output to the ‘Display’.
- Inner Subsystem Logic:
- Double-click the ‘For Iterator Subsystem’ to enter it.
- Inside, place a ‘Sum’ block and a ‘Delay’ block.
- The Feedback Loop: The logic inside the subsystem is critical.
- Configure the ‘Delay’ block’s ‘Delay length’ to 1. This makes it store its input value for one iteration, acting as memory.
- The iterator block itself provides the current iteration number. Connect this signal to one input of the ‘Sum’ block.
- Connect the output of the ‘Delay’ block (the sum from the previous iteration) to the other input of the ‘Sum’ block.
- Connect the output of the ‘Sum’ block (the new total) to the input of the ‘Delay’ block. This creates the feedback loop where the current sum is stored for the next iteration.
- Also, connect the output of the ‘Sum’ block to the subsystem’s output port.
- Simulation Settings:
- Crucially, for this type of iterative model, go to the model’s main settings and change the simulation ‘Stop time’ from 10 to 1. This ensures the entire loop runs once as a single, discrete event.
- Result: Run the simulation. The iterator will execute 5 times, calculating 1+2+3+4+5. The ‘Display’ block will correctly show the final result: 15.
7.4. Integrating Custom Code with MATLAB Function Blocks
For tasks where a standard Simulink block is not available or when complex custom logic is required, the ‘MATLAB Function’ block is an exceptionally powerful tool. Found in the User Defined Functions library, it allows you to embed and execute your own MATLAB code directly within a Simulink model.
Let’s create a simple function to check if an input number is even or odd.
- Build the Model: Create a model with a ‘Constant’ block, a ‘MATLAB Function’ block, and a ‘Display’ block, connected in that order.
- Write the Function: Double-click the ‘MATLAB Function’ block. This opens a dedicated code editor. Enter the following function, which checks if the input u is even (returns 0) or odd (returns 1).
- Test the Model: We can now test the function with different inputs.
- Case 1 (Odd): Set the ‘Constant’ block’s value to 5. Run the simulation. The ‘Display’ will show 1, correctly identifying the number as odd.
- Case 2 (Even): Change the ‘Constant’ block’s value to 10. Run the simulation. The ‘Display’ will now show 0, indicating an even number.
This module has covered advanced techniques that allow for the creation of highly complex, structured, and customized models using subsystems, iterators, and MATLAB functions. The final step in the modeling process is often to interact with the simulation results or even build the model itself from the MATLAB environment.