Matlab2 Simulink

Download as pdf or txt
Download as pdf or txt
You are on page 1of 73

Modelling and Simulation in

References:
[1] Simulink® User's Guide, R2021a, MathWorks.
[2] Simulink® Getting Started Guide, R2021a, MathWorks.

[What is a Simulink]
Simulink is a block diagram environment for multidomain simulation and Model-Based
Design. It supports system-level design, simulation, automatic code generation, and
continuous test and verification of embedded systems. Simulink provides a graphical
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 1/73
editor, customizable block libraries, and solvers for modeling and simulating dynamic
systems. It is integrated with MATLAB®, enabling you to incorporate MATLAB
algorithms into models and export simulation results to MATLAB for further analysis.
Key Features
• Graphical editor for building and managing hierarchical block diagrams
• Libraries of predefined blocks for modeling continuous-time and discrete-time
systems
• Simulation engine with fixed-step and variable-step ODE solvers
• Scopes and data displays for viewing simulation results

[Model-Based Design with Simulink]


Modeling is a way to create a virtual representation of a real-world system. You can
simulate this virtual representation under a wide range of conditions to see how it
behaves.
Modeling and simulation are valuable for testing conditions that are difficult to
reproduce with hardware prototypes alone. This is especially true in the early phase of
the design process when hardware is not yet available.
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 2/73
Iterating between modeling and simulation can improve the quality of the system
design early, by reducing the number of errors found later in the design process.
You can automatically generate code from a model and create test benches for system
verification. Code generation saves time and prevents the introduction of manually
coded errors.
In Model-Based Design, a system model is at the center of the workflow. Model-Based
Design enables fast and cost-effective development of dynamic systems.
Model-Based Design allows you to:
• Use a common design environment across project teams
• Link designs directly to requirements
• Identify and correct errors continuously by integrating testing with design
• Refine algorithms through multidomain simulation
• Automatically generate embedded software code and documentation

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 3/73


Model-Based Design Workflow in Simulink
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 4/73
Simulink tasks related to Model-Based Design
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 5/73
[Simulink Block Diagrams]
Simulink is a graphical modeling and simulation environment for dynamic systems. You
can create block diagrams, where blocks represent parts of a system. A block can
represent a physical component, a small system, or a function. An input/output
relationship fully characterizes a block. A Simulink block is a model element that
defines a mathematical relationship between its input and output.
Consider these examples:
• You use a megaphone to make your voice heard — The sound produced at one
end of the megaphone is amplified at the other end. The megaphone is the block,
the input is the sound wave at its source, and the output is the sound wave as you
hear it.
• You push a cart, and it moves — The cart is the block, the force you apply is the
input, and the cart's position is the output.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 6/73


The definition of a block is only complete with its inputs and outputs defined; this task
relates to the goal of the model. For example, the cart velocity may be a natural choice
as an output if the modeling goal does not involve its location.
Simulink provides block libraries that are collections of blocks grouped by functionality.
For example, to model a megaphone that multiplies its input by a constant, you use a
Gain block from the Math Operations library.
The > signs denote the inputs and outputs of a block, which can be connected to other
blocks. You can connect blocks to other blocks to form systems and represent more
complex functionality.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 7/73


At each time step, each block computes its outputs from its inputs. Once all of the
signals in a diagram are computed at a given time step, Simulink determines the next
time step and advances the simulation clock. Then each block computes their output
for this new time step.
In simulation, time progresses differently from a real clock. Each time step takes as
much time as it takes to finish the computations for that time step.
Often, the effect of a component's input on its output is not instantaneous, e.g., as in
the cart example. This action provides input to a differential equation (ODE). When
simulation requires solving a differential or difference equation, Simulink employs
numerical solvers to compute the state values for the time step.
Simulink handles data in three categories:
• Signals — Block inputs and outputs, computed during simulation
• States — Internal values, representing the dynamics of the block, computed during
simulation
• Parameters — Values that affect the behavior of a block, controlled by the user

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 8/73


[How to Build a Model in Simulink]
Open New Model (blank model)

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 9/73


2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 10/73
Open Simulink Library Browser

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 11/73


Add Blocks to a Model

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 12/73


Connect Blocks

Add Signal Viewer

Run Simulation

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 13/73


View Data in the Simulink Scope

View Data in the Simulation Data Inspector


2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 14/73
Inspect and compare data and simulation results to validate and iterate model designs.
Run a simulation in a model configured to log data to the Simulation Data Inspector.
You can view and verify model input data or inspect logged simulation data while
iteratively modifying your model diagram, parameter values, or model configuration.
Inspect Simulation Data:
Plot signals on multiple subplots, zoom in and out on specified plot axes, and use data
cursors to understand and evaluate the data.
Compare Simulation Data:
Compare individual signals or simulation runs and analyze your comparison results.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 15/73


2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 16/73
[Subsystems in Simulink]
As a model increases in size and complexity, you can simplify it by grouping blocks into
subsystems. A subsystem is a set of blocks that you group into a single Subsystem block.
Using subsystems:
• Establishes a hierarchical block diagram where a Subsystem block is on one layer and
the blocks that make up the subsystem are on another.
• Keeps functionally related blocks together.
• Helps reduce the number of blocks displayed in your model window.
• Establishes an interface with inputs and outputs.
When you make a copy of a subsystem, that copy is independent of the source
subsystem. To reuse the contents of a subsystem across a model or across models,
consider referenced subsystems, referenced models, or subsystems linked to a block
in a custom library.
Types of Subsystems

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 17/73


Nonvirtual and Virtual Blocks
When creating models, you need to be aware that Simulink blocks fall into two basic
categories: nonvirtual blocks and virtual blocks. A subsystem can be virtual or
nonvirtual. A virtual subsystem provides graphical hierarchy in a model and does not
impact the execution of a model. A nonvirtual subsystem executes as a single unit
within a model. If you add or remove a nonvirtual block, you change the model's
behavior. A subsystem is virtual unless the block is conditionally executed or the Treat
as atomic unit check box is selected. Simulink classifies nonvirtual subsystems into
several types, e.g.:

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 18/73


2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 19/73
[Simulation Basics]
You can simulate a model in the Simulink Editor using the Run button on the toolstrip.
The Run button also appears in tools within the Simulink Editor. You can simulate from
any tool that includes the button, such as the Scope viewer.
Before you start a simulation, you can specify options like simulation start time, stop
time, and the solver for solving the model. You specify these options in the
Configuration Parameters dialog box, which you can open by clicking Model Settings
on the Modeling tab. These settings are saved with the model in a configuration set.
You can create multiple configuration sets for each model and switch between them
to see the effects of different settings. See “Model Configuration Sets”.
By default, simulations start at 0.0 s and end at 10.0 s. Note In the Simulink software,
time and all related parameters (such as sample times) are implicitly in seconds. If you
choose to use a different time unit, scale parameters accordingly. The Solver
configuration pane allows you to specify other start and stop times for the currently
selected simulation configuration. Simulation time and actual clock time are not the
same. For example, if running a simulation for 10 s usually does not take 10 s as
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 20/73
measured on a clock. The amount of time it actually takes to run a simulation depends
on many factors including the complexity of the model, the step sizes, and the
computer speed.
After you set your model configuration parameters, you can start the simulation. You
can pause, resume, and stop simulation using toolstrip controls. You can also simulate
more than one model at a time, so you can start another simulation while one is
running. During simulation, you cannot make changes to the structure of the model,
such as adding or deleting lines or blocks. However, you can make some changes while
a simulation is running if they do not cause an interruption in simulation algorithm.
Run, Pause, and Stop a Simulation
To start simulating your model, click the Run button . You can pause, resume, or stop
a simulation using the corresponding controls on the tool strip. The model starts
simulating at the specified start time and runs until the specified end time. While the
simulation is running, information at the bottom of the editor shows the percentage
of simulation completed and the current simulation time.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 21/73


If an error occurs, simulation stops, and a message appears. If a warning condition
occurs, simulation completes. In both cases, click the diagnostics link at the bottom of
the editor to see the message, which helps you to locate errors.
Pausing takes effect after the current time step finishes executing. Resuming a paused
simulation occurs at the next time step.
You can use the Stop Simulation block to stop a simulation when the input to the block
is nonzero.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 22/73


[What Is Sample Time?]
Sample time is the time interval that specifies the rate (1 / sample time) for executing
block methods to produce outputs and update the internal states of blocks. Time is an
inherent component of a block diagram in that the simulation results of a block
diagram change with time. Sample time is specified as:
Continuous – Blocks run at variable times based on solver settings.
Discrete – Blocks run at specific times that are explicitly specified.
Every Simulink block has a sample time which defines when the block will execute.
Most blocks allow you to specify the sample time via a SampleTime parameter.
Common choices include discrete, continuous, and inherited sample times.
Simulink allows you to specify a block sample time directly as a numerical value or
symbolically by defining a sample time vector. In the case of a discrete sample time,
the vector is [Ts, To] where Ts is the sampling period and To is the initial time offset.
For example, consider a discrete model that produces its outputs every two seconds.
If your base time unit is seconds, you can directly set the discrete sample time by

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 23/73


specifying the numerical value of 2 as the SampleTime parameter. Because the offset
value is zero, you do not need to specify it; however, you can enter [2,0] in the Sample
time field. For non-discrete blocks, the components of the vector are symbolic values
that represent one of the types of sample time. The table also defines the explicit
nature of each sample time type and designates the associated color and annotation.
Because an inherited sample time is explicit, you can specify it as [-1, 0] or as -1.

Common Sample Time Types Sample Time Examples


Discrete [Ts, To] Unit Delay, Digital Filter

Continuous [0, 0] Integrator, Derivative


Inherited [–1, 0] Gain, Sum
Discrete Sample Time
Given a block with a discrete sample time, Simulink executes the block output or
update method at times

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 24/73


tn = nTs + To
where the sample time period Ts is always greater than zero and less than the
simulation time, Tsim. The number of periods (n) is an integer that must satisfy:
0 ≤ n ≤Tsim/Ts
As simulation progresses, Simulink computes block outputs only once at each of these
fixed time intervals of tn. These simulation times, at which Simulink executes the
output method of a block for a given sample time, are referred to as sample time hits.
Discrete sample times are the only type for which sample time hits are known a priori.
If you need to delay the initial sample hit time, you can define an offset, To. The Unit
Delay block is an example of a block with a discrete sample time.
Continuous Sample Time
Unlike the discrete sample time, continuous sample hit times are divided into major
time steps and minor time steps, where the minor steps represent subdivisions of the
major steps. The solver produces a result at each major time step. It uses results at the
minor time steps to improve the accuracy of the result at the major time step. The ODE
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 25/73
solver you choose integrates all continuous states from the simulation start time to a
given major or minor time step. The solver determines the times of the minor steps
and uses the results at the minor time steps to improve the accuracy of the results at
the major time steps. However, you see the block output only at the major time steps.
To specify that a block, such as the Derivative block, is continuous, enter [0, 0] or 0 in
the Sample time field of the block dialog.
Inherited Sample Time
If a block sample time is set to [–1, 0] or –1, the sample time is inherited, and Simulink
determines the best sample time for the block based on the block context within the
model. Simulink performs this task during the compilation stage. There are some blocks
in which the sample time is inherited (-1) by default. For these blocks, the parameter
is not visible on the block dialog box unless it is set to a non-inherited value. Examples
of these blocks include the Gain and Rounding Function blocks. As a good modeling
practice, do not change the Sample time parameter for these blocks. All inherited
blocks are subject to the process of sample time propagation

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 26/73


During a model update, for example at the beginning of a simulation, Simulink uses a
process called sample time propagation to determine the sample times of blocks that
inherit their sample times. The figure below illustrates a Discrete Filter block with a
sample time period Ts driving a Gain block. Because the output of the Gain block is the
input multiplied by a constant, its output changes at the same rate as the filter. In other
words, the Gain block has an effective sample rate equal to the sample rate of the filter.

Purely Discrete Systems


A purely discrete system is composed solely of discrete blocks and can be modeled
using either a fixed-step or a variable-step solver. Simulating a discrete system requires
that the simulator take a simulation step at every sample time hit. For a multirate
discrete system—a system whose blocks Simulink samples at different rates—the steps

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 27/73


must occur at integer multiples of each of the system sample times. Otherwise, the
simulator might miss key transitions in the states of the system. The step size that the
Simulink software chooses depends on the type of solver you use to simulate the
multirate system and on the fundamental sample time.
The fundamental sample time of a multirate discrete system is the largest double that
is an integer divisor of the actual sample times of the system. For example, suppose
that a system has sample times of 0.25 and 0.50 seconds. The fundamental sample
time in this case is 0.25 seconds. Suppose, instead, the sample times are 0.50 and 0.75
seconds. The fundamental sample time is again 0.25 seconds.
Consider the following example of a simple multirate system. For this example, the
DTF1 Discrete Transfer Fcn block Sample time is set to [1 0.1] [], which gives it an offset
of 0.1. The Sample time of the DTF2 Discrete Transfer Fcn block is set to 0.7 , with no
offset. The solver is set to a variable-step discrete solver. Running the simulation and
plotting the outputs.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 28/73


2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 29/73
Hybrid Systems
Hybrid systems contain both discrete and continuous blocks and thus have both
discrete and continuous states. However, Simulink solvers treat any system that has
both continuous and discrete sample times as a hybrid system.
You can simulate hybrid systems using any of the integration methods, but certain
methods are more effective than others. For most hybrid systems, ode23 and ode45
are superior to the other solvers in terms of efficiency.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 30/73


[Modelling Best Practices]
Model a Continuous System
To model the differential equation
x´= –2x(t)+u(t),
where u(t) is a square wave with an amplitude of 1 and a frequency of 1 rad/sec, use
an integrator block and a gain block. The Integrator block integrates its input x´ to
produce x. Other blocks needed in this model include a Gain block and a Sum block. To
generate a square wave, use a Signal Generator block and select the Square Wave form
but change the default units to radians/sec. Again, view the output using a Scope block.
Gather the blocks and define the gain.
In this model, to reverse the direction of the Gain block, select the block, then on the
Format tab, click Flip left-right . To create the branch line from the output of the
Integrator block to the Gain block, hold down the Ctrl key while drawing the line.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 31/73


An important concept in this model is the loop that includes the Sum block, the
Integrator block, and the Gain block. In this equation, x is the output of the Integrator
block. It is also the input to the blocks that compute x´, on which it is based. This
relationship is implemented using a loop. The Scope displays x at each time step. For a
simulation lasting 10 seconds, the output looks like this:

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 32/73


The equation you modeled in this example can also be expressed as a transfer function.
The model uses the Transfer Fcn block, which accepts u as input and outputs x. So, the
block implements x/u. If you substitute sx for x´ in the above equation, you get
sx = –2x + u.
Solving for x gives
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 33/73
x = u/(s + 2)
or,
x/u = 1/(s + 2).
The Transfer Fcn block uses parameters to specify the numerator and denominator
coefficients. In this case, the numerator is 1 and the denominator is s+2. Specify both
terms as vectors of coefficients of successively decreasing powers of s. In this case the
numerator is [1] (or just 1) and the denominator is [1 2].
The results of this simulation are identical to those of the previous model.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 34/73


Best-Form Mathematical Models
Series RLC Example

You can often formulate the mathematical system you are modeling in several ways.
Choosing the best-form mathematical model allows the simulation to execute faster
and more accurately. For example, consider a simple series RLC circuit. According to
Kirchoff's voltage law, the voltage drop across this circuit is equal to the sum of the
voltage drop across each element of the circuit.
VAC = VR + VL + VC

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 35/73


Using Ohm's law to solve for the voltage across each element of the circuit, the
equation for this circuit can be written as

You can model this system in Simulink by solving for either the resistor voltage or
inductor voltage. Which you choose to solve for affects the structure of the model and
its performance.
Solving Series RLC Using Resistor Voltage
Solving the RLC circuit for the resistor voltage yields

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 36/73


Circuit Model
The following diagram shows this equation modeled in Simulink where R is 70, C is
0.00003, and L is 0.04. The resistor voltage is the sum of the voltage source, the
capacitor voltage, and the inductor voltage. To calculate the current, multiply the
resistor voltage by a gain of 1/R. Calculate the capacitor voltage by integrating the
current and multiplying by a gain of 1/C. Calculate the inductor voltage by taking the
derivative of the current and multiplying by a gain of L.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 37/73


This formulation contains a Derivative block associated with the inductor. Whenever
possible, you should avoid mathematical formulations that require Derivative blocks
as they introduce discontinuities into your system. Numerical integration is used to
solve the model dynamics through time. These integration solvers take small steps
through time to satisfy an accuracy constraint on the solution. If the discontinuity
introduced by the Derivative block is too large, it is not possible for the solver to step
across it. In addition, in this model the Derivative, Sum, and two Gain blocks create an
algebraic loop. Algebraic loops slow down the model's execution and can produce less
accurate simulation results.
Solving Series RLC Using Inductor Voltage
To avoid using a Derivative block, formulate the equation for the inductor voltage.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 38/73


Circuit Model
The inductor voltage is the sum of the voltage source, the resistor voltage, and the
capacitor voltage. You need the current in the circuit to calculate the resistor and
capacitor voltages. To calculate the current, integrate the inductor voltage and divide
by L. Calculate the capacitor voltage by integrating the current and dividing by C.
Calculate the resistor voltage by multiplying the current by a gain of R. This model
contains only integrator blocks and no algebraic loops. As a result, the model simulates
faster and more accurately.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 39/73


Grouping Models
When incomplete system requirements and a developing system design prevent you
from defining file management and model architecture upfront, you can still model
fundamental system algorithms. By organizing the model into inputs, outputs, and
systems, you create a general framework for model components as the model grows.
These blocks, which represent the system, are grouped in an area. The two other areas
contain system inputs and outputs.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 40/73


Manage Signal Lines
As a model grows, model components help functionally and visually organize blocks in
the model. To similarly organize signal lines in the model, you can apply a variety of
strategies, such as grouping signals into buses.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 41/73


To reduce the number of signal lines, you can connect a viewer directly to signal lines
or enable data logging for signal lines. By choosing a way to visualize simulation data
without using a sink block, you can avoid extra signal lines.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 42/73


To further reduce the number of signal lines, you can group signal lines into a bus by
using a Bus Creator or Out Bus Element block. All signal lines retain their identities
when grouped in a bus and can be separated downstream from the bus.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 43/73


2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 44/73
[How Simulink Works]
Model Compilation
The first phase of simulation occurs when the system’s model is open, and you simulate
the model. In the Simulink Editor, click Run. Running the simulation causes the Simulink
engine to invoke the model compiler. The model compiler converts the model to an
executable form, a process called compilation. In particular, the compiler:
• Evaluates the model's block parameter expressions to determine their values.
• Determines signal attributes, e.g., name, data type, numeric type, and
dimensionality, not explicitly specified by the model and checks that each block
can accept the signals connected to its inputs.
• Propagates the attributes of a source signal to the inputs of the blocks that it drives
in order to compute previously unspecified attributes in the blocks.
• Performs block reduction optimizations.
• Flattens the model hierarchy by replacing virtual subsystems with the blocks that
they contain.
• Determines the block execution order by task-based sorting.
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 45/73
• Determines the sample times of all blocks in the model whose sample times you
did not explicitly specify.
Link Phase
In this phase, the Simulink engine allocates memory needed for working areas (signals,
states, and run-time parameters) for execution of the block diagram. It also allocates
and initializes memory for data structures that store run-time information for each
block.
Method Execution Lists
In the Link phase, the Simulink engine also creates method execution lists. These lists
list the most efficient order in which to invoke a model's block methods to compute its
outputs. The block execution order lists generated during the model compilation phase
are used to construct the method execution lists.
Block Priorities

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 46/73


You can assign update priorities to blocks. The output methods of higher priority blocks
are executed before those of lower priority blocks. These priorities are honored only if
they are consistent with its block execution order.
Simulation Loop Phase
Once the Link Phase completes, the simulation enters the simulation loop phase. In
this phase, the Simulink engine successively computes the states and outputs of the
system at intervals from the simulation start time to the finish time, using information
provided by the model. The successive time points at which the states and outputs are
computed are called time steps. The length of time between steps is called the step
size. The step size depends on the type of solver used to compute the system's
continuous states, the system's fundamental sample time, and whether the system's
continuous states have discontinuities (Zero-Crossing Detection).
The Simulation Loop phase has two subphases: the Loop Initialization phase and the
Loop Iteration phase. The initialization phase occurs once, at the start of the loop. The
iteration phase is repeated once per time step from the simulation start time to the
simulation stop time.
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 47/73
At the start of the simulation, the model specifies the initial states and outputs of the
system to be simulated. At each step, new values for the system's inputs, states, and
outputs are computed, and the model is updated to reflect the computed values. At
the end of the simulation, the model reflects the final values of the system's inputs,
states, and outputs. The Simulink software provides data display and logging blocks.
You can display and/or log intermediate results by including these blocks in your
model.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 48/73


2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 49/73
[Loop Iteration]
At each time step, the Simulink engine:
1. Computes the model outputs.
The Simulink engine initiates this step by invoking the Simulink model Outputs method.
The model Outputs method in turn invokes the model system Outputs method, which
invokes the Outputs methods of the blocks that the model contains in the order
specified by the Outputs method execution lists generated in the Link phase of the
simulation.
2. Computes the model's states.
The Simulink engine computes a model's states by invoking a solver. Which solver it
invokes depends on whether the model has no states, only discrete states, only
continuous states, or both continuous and discrete states.
If the model has only discrete states, the Simulink engine invokes the discrete solver
selected by the user. The solver computes the size of the time step needed to hit the
model's sample times. It then invokes the Update method of the model. The model
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 50/73
Update method invokes the Update method of its system, which invokes the Update
methods of each of the blocks that the system contains in the order specified by the
Update method lists generated in the Link phase.
If the model has only continuous states, the Simulink engine invokes the continuous
solver specified by the model. Depending on the solver, the solver either in turn calls
the Derivatives method of the model once or enters a subcycle of minor time steps
where the solver repeatedly calls the model's Outputs methods and Derivatives
methods to compute the model's outputs and derivatives at successive intervals within
the major time step. This is done to increase the accuracy of the state computation.
The model Outputs method and Derivatives methods in turn invoke their
corresponding system methods, which invoke the block Outputs and Derivatives in the
order specified by the Outputs and Derivatives methods execution lists generated in
the Link phase.
3. Optionally checks for discontinuities in the continuous states of blocks.
Zero-crossing detection is used to detect discontinuities in continuous states
accurately without resorting to excessively small time-steps during a simulation.
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 51/73
4. Computes the time for the next time step.
Steps 1 through 4 are repeated until the simulation stop time is reached.
[Compare Solvers]
A dynamic system is simulated by computing its states at successive time steps over a
specified time span, using information provided by the model. The process of
computing the successive states of a system from its model is known as solving the
model. No single method of solving a model suffices for all systems. Accordingly,
Simulink provides a set of programs, known as solvers, each of which embodies a
particular approach to solving a model. The Configuration Parameters dialog box
allows you to choose the solver best suited to your model.
Fixed-Step Versus Variable-Step Solvers
You can choose the solvers provided by Simulink based on the way they calculate step
size: fixed-step and variable-step.
Fixed-step solvers solve the model at regular time intervals from the beginning to the
end of the simulation. The size of the interval is known as the step size. You can specify
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 52/73
the step size or let the solver choose the step size. Generally, decreasing the step size
increases the accuracy of the results while increasing the time required to simulate the
system.
Variable-step solvers vary the step size during the simulation. They reduce the step size
to increase accuracy when a model's states are changing rapidly and increase the step
size to avoid taking unnecessary steps when the model's states are changing slowly.
Computing the step size adds to the computational overhead at each step but can
reduce the total number of steps, and hence the simulation time required to maintain
a specified level of accuracy for models with rapidly changing or piecewise continuous
states.
Fixed-step and variable-step solvers compute the next simulation time as the sum of
the current simulation time and the step size. The Type control on the Solver
configuration pane allows you to select the type of solver. With a fixed-step solver, the
step size remains constant throughout the simulation. With a variable-step solver, the
step size can vary from step to step, depending on the model dynamics. In particular,
a variable-step solver increases or reduces the step size to meet the error tolerances

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 53/73


that you specify. The choice between these types depends on how you plan to deploy
your model and the model dynamics. If you plan to generate code from your model and
run the code on a real-time computer system, choose a fixed-step solver to simulate the
model. You cannot map the variable-step size to the real-time clock.
If you do not plan to deploy your model as generated code, the choice between a
variable-step and a fixed-step solver depends on the dynamics of your model. A
variable-step solver might shorten the simulation time of your model significantly. A
variable-step solver allows this saving because, for a given level of accuracy, the solver
can dynamically adjust the step size as necessary. This approach reduces the number
of steps required. The fixed-step solver must use a single step size throughout the
simulation, based on the accuracy requirements. To satisfy these requirements
throughout the simulation, the fixed-step solver typically requires a small step.
Continuous Versus Discrete Solvers
Simulink provides both continuous and discrete solvers. When you select a solver type,
you can also select a specific solver. Both sets of solvers include discrete and
continuous solvers. Discrete and continuous solvers rely on the model blocks to
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 54/73
compute the values of any discrete states. Blocks that define discrete states are
responsible for computing the values of those states at each time step. However, unlike
discrete solvers, continuous solvers use numerical integration to compute the
continuous states that the blocks define. When choosing a solver, determine first
whether to use a discrete solver or a continuous solver.
If your model has no continuous states, then Simulink switches to either the fixed-step
discrete solver or the variable-step discrete solver. If your model has only continuous
states or a mix of continuous and discrete states, choose a continuous solver from the
remaining solver choices based on the dynamics of your model. Otherwise, an error
occurs.
Continuous solvers use numerical integration to compute a model's continuous states
at the current time step based on the states at previous time steps and the state
derivatives. Continuous solvers rely on the individual blocks to compute the values of
the model's discrete states at each time step.
Discrete solvers exist primarily to solve purely discrete models. They compute the next
simulation time step for a model and nothing else. In performing these computations,
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 55/73
they rely on each block in the model to update its individual discrete states. They do
not compute continuous states.
The solver library contains two discrete solvers: a fixed-step discrete solver and a
variable-step discrete solver. The fixed-step solver by default chooses the step size and
simulation rate fast enough to track state changes in the fastest block in your model.
The variable-step solver adjusts the simulation step size to keep pace with the actual
rate of discrete state changes in your model. This adjustment can avoid unnecessary
steps and shorten simulation time for multirate models. Note You must use a
continuous solver to solve a model that contains both continuous and discrete states
because discrete solvers cannot handle continuous states. If, on the other hand, you
select a continuous solver for a model with no states or discrete states only, Simulink
software uses a discrete solver.
Explicit Versus Implicit Continuous Solvers
You represent an explicit system by an equation
x˙ = f (x)

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 56/73


For any given value of x, you can compute x˙ by substituting x in f(x) and evaluating the
equation.
Equations of the form
F(x˙, x) = 0
are considered to be implicit. For any given value of x, you must solve this equation to
calculate x˙.
While you can apply an implicit or explicit continuous solver to solve all these systems,
implicit solvers are designed specifically for solving stiff problems. Explicit solvers solve
non-stiff problems. An ordinary differential equation problem is said to be stiff if the
desired solution varies slowly, but there are closer solutions that vary rapidly. The
numerical method must then take small time steps to solve the system. Stiffness is an
efficiency issue. The stiffer a system is, the longer it takes for the explicit solver to
perform a computation. A stiff system has both slowly and quickly varying continuous
dynamics. When compared to explicit solvers, implicit solvers provide greater stability

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 57/73


for oscillatory behavior. However, implicit solvers are also computationally more
expensive.
One-Step Versus Multistep Continuous Solvers
The Simulink solver library provides both one-step and multistep solvers. The one-step
solvers estimate using the solution at the immediately preceding time point, and the
values of the derivative at multiple points between tn and tn-1. These points are minor
steps. Multistep solvers use the results at several preceding time steps to compute the
current solution. Simulink provides one explicit multistep solver, ode113, and one
implicit multistep solver, ode15s. Both are variable-step solvers.
Zero-Crossing Detection
A variable-step solver dynamically adjusts the time step size, causing it to increase
when a variable is changing slowly and to decrease when the variable changes rapidly.
This behavior causes the solver to take many small steps in the vicinity of a
discontinuity because the variable is rapidly changing in this region. This improves
accuracy but can lead to excessive simulation times. Simulink uses a technique known

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 58/73


as zero-crossing detection to accurately locate a discontinuity without resorting to
excessively small time steps. Usually, this technique improves simulation run time, but
it can cause some simulations to halt before the intended completion time.
[Algebraic Loop Concepts]
Direct Feedthrough
Direct feedthrough is where the output port signal of a block is computed from the
values of its input port signals. The output signal value is a function of the input signal
values.

Some examples of blocks with direct feedthrough inputs are:


• Math Function
• Gain
2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 59/73
• Product
• State-Space when the D matrix coefficient is nonzero
• Sum
• Transfer Fcn when the numerator and denominator are of the same order
• Zero-Pole, when the block has as many zeros as poles
Two examples of non-direct feedthrough blocks are Integrator and Unit Delay.
Algebraic Loop

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 60/73


A signal loop between blocks with direct feedthrough is called an algebraic loop. An
algebraic loop generally occurs when an input port of a block with direct feedthrough
is driven directly by the output port of the same block or indirectly through other blocks
with direct feedthrough. Direct feedthrough means that Simulink needs the value of
the block’s input signal to compute its output at the current time step. Such a signal
loop creates a circular dependency of block outputs and inputs in the same time-step.
This results in an algebraic equation that needs solving at each time-step, adding
computational cost to the simulation.
The figure shows an example of an algebraic loop. The Sum block is an algebraic
variable xa that is constrained to equal the first input u minus xa (for example, xa = u –
xa). The solution of this simple loop is xa = u/2.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 61/73


Mathematical Interpretation
Simulink contains a suite of numerical solvers for simulating ordinary differential
equations (ODEs), which are systems of equations that you can write as
x˙ = f (x, t),
where x is the state vector and t is the independent time variable. Some systems of
equations contain additional constraints that involve the independent variable and the
state vector, but not the derivative of the state vector. Such systems are called
differential algebraic equations (DAEs),
The term algebraic refers to equations that do not involve any derivatives. You can
express DAEs that arise in engineering in the semi-explicit form
x˙ = f(x, xa, t)
0 = g(x, xa, t),
where:
• f and g can be vector functions.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 62/73


• The first equation is the differential equation.
• The second equation is the algebraic equation.
• The vector of differential variables is x.
• The vector of algebraic variables is xa.
In Simulink models, algebraic loops are algebraic constraints. Models with algebraic
loops define a system of differential algebraic equations. Simulink solves the algebraic
equations (the algebraic loop) numerically for xa at each step of the ODE solver.
The model in the figure is equivalent to this system of equations in semi-explicit form:
x˙ = f (x, xa, t) = xa
0 = g(x, xa, t) = − x + u − 2xa .

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 63/73


At each step of the ODE solver, the algebraic loop solver must solve the algebraic
constraint for xa before calculating the derivative x˙.
Artificial Algebraic Loops
An artificial algebraic loop occurs when an atomic subsystem or Model block causes
Simulink to detect an algebraic loop, even though the contents of the subsystem do
not contain a direct feedthrough from the input to the output. When you create an
atomic subsystem, all Inport blocks are direct feedthrough, resulting in an algebraic
loop. The following model represents a simple proportional control of the plant
described by

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 64/73


1
G( s) =
s 2 + 2s + 1

The system has neither algebraic variables nor direct feedthrough and does not contain
an algebraic loop.
Modify the model as described in the following steps:
• Enclose the Controller and Plant blocks in a subsystem.
• In the subsystem dialog box, select Treat as atomic unit to make the subsystem
atomic.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 65/73


• In the Diagnostics pane of the Model Configuration Parameters, set the Algebraic
Loop parameter to error.
When simulating this model, an algebraic loop occurs because the subsystem is direct
feedthrough, even though the path within the atomic subsystem is not direct
feedthrough. Simulation stops with an algebraic loop error.

[How the Algebraic Loop Solver Works]


When a model contains an algebraic loop, Simulink uses a nonlinear solver at each time
step to solve the algebraic loop. The solver performs iterations to determine the
solution to the algebraic constraint, if there is one. As a result, models with algebraic
loops can run more slowly than models without algebraic loops.
For example, suppose your model has a Sum block with two inputs—one additive, the
other subtractive. If you feed the output of the Sum block to one of the inputs, you
create an algebraic loop where all of the blocks include direct feedthrough. The Sum
block cannot compute the output without knowing the input. Simulink detects the

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 66/73


algebraic loop, and the algebraic loop solver solves the loop using an iterative loop. In
the Sum block example, the software computes the correct result this way:
xa(t) = u(t) / 2.
The algebraic loop solver uses a gradient-based search method, which requires
continuous first derivatives of the algebraic constraint that correspond to the algebraic
loop. As a result, if the algebraic loop contains discontinuities, the algebraic loop solver
can fail.
Limitations of the Algebraic Loop Solver
Algebraic loop solving is an iterative process. The Simulink algebraic loop solver is
successful only if the algebraic loop converges to a definite answer. When the loop fails
to converge, or converges too slowly, the simulation exits with an error.
Implications of Algebraic Loops in a Model
If your model contains an algebraic loop:
• You cannot generate code for the model.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 67/73


• The Simulink algebraic loop solver might not be able to solve the algebraic loop.
• While Simulink is trying to solve the algebraic loop, the simulation can execute slowly.
For most models, the algebraic loop solver is computationally expensive for the first
time step. Simulink solves subsequent time steps rapidly because a good starting point
for xa is available from the previous time step.
You can use Simscape to model systems that span mechanical, electrical, hydraulic, and
other physical domains as physical networks. Simscape constructs the DAEs that
characterize the behavior of a model. The software integrates these equations with the
rest of the model and then solves the DAEs directly.
[How to Remove Algebraic Loops]
• Introduce a Delay to Remove Algebraic Loops

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 68/73


This is one approach to remove algebraic loops in larger models where such loops can
occur due to feedback between subsystems. Consider the model in the example. There
are two algebraic loops caused by the atomic subsystems in the model
• Blackbox A -> Blackbox B -> Blackbox C -> Blackbox A
• Blackbox B -> Blackbox C -> Blackbox B
If you do not know the contents of these subsystems, you may break the loops by
adding a Unit Delay block outside the subsystems. There are three ways to do this.
- Add a Unit Delay between Blackbox A and Blackbox C.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 69/73


- Add a Unit Delay between Blackbox B and Blackbox C.

- Add Unit Delay blocks to both algebraic loops.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 70/73


• Solve Algebraic Loops Manually
If Simulink cannot solve the algebraic loop, the software reports an error. Use one of
these techniques to solve the loop manually:
• Restructure the underlying DAEs using techniques such as differentiation or change
of coordinates. These techniques put the DAEs in a form that is easier for the algebraic
loop solver to solve.
• Convert the DAEs to ODEs, which eliminates any algebraic loops.
• How to Eliminates Artificial Algebraic Loops

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 71/73


When you enable Minimize algebraic loop occurrences, Simulink tries to eliminate
artificial algebraic loops. In this example, the model contains an atomic subsystem that
causes an artificial algebraic loop. The contents of the atomic subsystem are not direct
feedthrough, but Simulink identifies the atomic subsystem as direct feedthrough.
- If the Algebraic loop diagnostic is set to error, simulating the model results in an
error because the model contains an artificial algebraic loop involving its atomic
subsystem.
To eliminate this algebraic loop, in the Diagnostics pane of Model Configuration
Parameters, set the Algebraic loop parameter to warning or none. Also, in the Data
Import/Export pane, make sure the Signal logging parameter is disabled. If signal
logging is enabled, Simulink cannot eliminate artificial algebraic loops.
- Eliminate Artificial Algebraic Loops Caused by Atomic Subsystems
If an atomic subsystem causes an artificial algebraic loop, convert the atomic
subsystem to a virtual subsystem. This change has no effect on the behavior of the
model. The algebraic loop is automatically solved because there is no algebraic

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 72/73


constant. After you make the subsystem virtual, Simulink does not invoke the algebraic
loop solver during simulation.
- Bundled Signals That Create Artificial Algebraic Loops
Some models bundle signals together. This bundling can cause Simulink to detect an
algebraic loop, even when an algebraic constraint does not exist. If you redirect one or
more signals, you may be able to remove the artificial algebraic loop.

2102653 SPECIAL TOPICS IN POWER ELECTRONICS : Simulink 73/73

You might also like