+ All Categories
Home > Documents > 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates...

003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates...

Date post: 21-Mar-2020
Category:
Upload: others
View: 10 times
Download: 0 times
Share this document with a friend
54
Università degli Studi di Modena e Reggio Emilia A utomation R obotics and S ystem CONTROL Modeling and Simulation Cesare Fantuzzi University of Modena and Reggio Emilia 21/03/2011 1
Transcript
Page 1: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Università degli Studidi Modena e Reggio Emilia

AutomationRobotics andSystemCONTROL

Modeling and SimulationModeling and Simulation

Cesare FantuzziUniversity of Modena and Reggio Emilia

21/03/2011

1

Page 2: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Course syllabus

� Part 1: Theory of Modeling and Simulation.� Part 2: Numerical simulation.� Part 3: How develop simulation projects using Matlab

and Simulink.� Part 4: Case studies.� Part 4: Case studies.

21/03/2011 2

Page 3: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Part 3: Matlab and Simulink

� Matlab and Simulink as simulation environment.� Numerical solvers and simulation parameters.� How to define a simulation project in Matlab.

21/03/2011 3

Page 4: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Scope of Simulink

� Simulink is for modeling, simulating, and analyzing systems whose outputs change over time.– Dynamic systems.

� Simulating a dynamic system is a two-step process1. First, a user creates a block diagram, using the

Simulink model editor, that graphically depicts time-dependent mathematical relationships among the system's inputs, states, and outputs.

2. The user then commands the Simulink software to simulate the system represented by the model from a specified start time to a specified stop time.

21/03/2011 4

Page 5: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Block Diagram Semantics

� Simulink block diagrams define time-based relationships between signals and state variables. The solution of a block diagram is obtained by evaluating these relationships over time (between a "start time" and a "stop time“). Each evaluation of these relationships is referred to as a time step.

� Signals represent quantities that change over time and are Signals represent quantities that change over time and are defined for all points in time between the block diagram's start and stop time.

� The relationships between signals and state variables are defined by a set of equations represented by blocks. Each block consists of a set of equations (block methods). These equations define a relationship between the input signals, output signals and the state variables.

21/03/2011 5

Page 6: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

An example: Model a algebraic equation

� Model as example the equation that converts Celsius temperature to Fahrenheit:TF = 9/5*(TC) + 32

� Simulate a variation of Temperature from 0 to 100 TC

21/03/2011 6

Page 7: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Solution

21/03/2011 7

Page 8: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Second example: Modeling a Simple Continuous System

� model the differential equation:

� where u(t) is a square wave with an amplitude of 1 and a where u(t) is a square wave with an amplitude of 1 and a frequency of 1 rad/sec.

� Design two models:– Using only the Integrator block:

– Using the Transfer Function Block:

21/03/2011 8

Page 9: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Solutions

21/03/2011 9

Page 10: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Continuous, discrete and hybrid Systems

� Typically the current values of some system, and hence model, outputs are functions of the previous values of temporal variables, called states.

� Computing a model's outputs from a block diagram hence entails saving the value of states at the current time step for use in computing the outputs at a subsequent time step. use in computing the outputs at a subsequent time step.

� This task is performed during simulation for models that define states.

� Two types of states can occur in a Simulink model: – A continuous state which changes continuously. – A discrete state, which is an approximation of a continuous

state where the state is updated (recomputed) using finite (periodic or aperiodic).

21/03/2011 10

Page 11: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Blocks implicitly define a model's states

� Blocks implicitly define a model's states.� In particular, a block that needs some or all of its

previous outputs to compute its current outputs implicitly defines a set of states that need to be saved between time steps.

� Such a block is said to have states. � Such a block is said to have states.

21/03/2011 11

Page 12: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

A question:

� Which is the state in the two models of the previous example?

� It is continuous or discrete?

21/03/2011 12

Page 13: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Blocks containing continuous states

21/03/2011 13

Page 14: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Blocks containing discrete states

21/03/2011 14

Page 15: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Working with States

� The model command displays information about the states defined by a model, including the total number of states defined by the model, the block that defines each state, and the initial value of each state.

� The Simulink debugger displays the value of a state at each time step during a simulation, and the Simulinkeach time step during a simulation, and the Simulinkdebugger's states command displays information about the model's current states.

� The Data Import/Export pane of a model's Configuration Parameters dialog box allows you to specify initial values for a model's states, and to record the values of the states at each time step during simulation as an array or structure variable in the MATLAB workspace.

21/03/2011 15

Page 16: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Example

21/03/2011 16

Page 17: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Example (Cont.)

>> [sys,x0,str,ts]= PrimoEsempio([ ],[ ],[ ],'sizes')� Sys (Vector of model size data)

– sys(1) = number of continuous states– sys(2) = number of discrete states– sys(3) = number of outputs– sys(4) = number of inputs– sys(5) = reserved– sys(6) = direct-feedthrough flag (1 = yes, 0 = no)– sys(7) = number of sample times (= number of rows in ts)

� X0 : Vector containing the initial conditions of the system's states� str : Vector of names of the blocks associated with the model's

states. The state names and initial conditions appear in the same order in str and x0, respectively.

� ts : An m-by-2 matrix containing the sample time (period, offset) information

21/03/2011 17

Page 18: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Running a Simulink model is generally a two-step process

� First, you specify various simulation parameters (the solver, the start and stop time for the simulation, the maximum step size, etc.).

� Second, then you start the simulation. Simulink runs the simulation from the specified start time to the specified stop time. While the simulation is running, you can interact with the time. While the simulation is running, you can interact with the simulation in various ways, stop or pause the simulation, and launch simulations of other models. If an error occurs during a simulation, Simulink halts the simulation and pops up a diagnostic viewer that helps you to determine the cause of the error.

21/03/2011 18

Page 19: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Model Compilation

� The first phase of simulation occurs when you choose Start from the Model Editor's Simulation menu, with the system's model open.

� This causes the Simulink engine to invoke the model compiler. � The model compiler converts the model to an executable form, a

process called compilation:– Evaluates the model's block parameter expressions to determine their

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

– Performs block reduction optimizations.– Flattens the model hierarchy by replacing virtual subsystems with the

blocks that they contain.– Determines the block sorted order.– Determines the sample times of all blocks in the model whose sample

times you did not explicitly specify.

21/03/2011 19

Page 20: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Model compilation (cont.)

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

� Method Execution Lists: T he 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 in which to invoke a model's block methods to compute its outputs. The block sorted order lists generated during the model compilation phase is used to construct the method execution lists.

� Block Priorities: You can assign update priorities to blocks. The output methods of higher priority blocks are executed before those of lower priority blocks. The priorities are honored only if they are consistent with its block sorting rules.

21/03/2011 20

Page 21: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

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 � 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:– Type of solver– System's fundamental sample time – whether the system's continuous states have discontinuities (Zero-

Crossing Detection).

21/03/2011 21

Page 22: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Loop Iteration at each time step

1. Computes the model's 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 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.

21/03/2011 22

Page 23: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

3. Optionally checks for discontinuities in the continuous states of blocks using the zero-crossing detection.

4. Computes the time for the next time step.

� Steps 1 through 4 are repeated until the simulation stop � Steps 1 through 4 are repeated until the simulation stop time is reached.

21/03/2011 23

Page 24: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Simulation parameters

21/03/2011 24

Page 25: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Choosing a Solver Type

� The Simulink library of solvers is divided into two major types in the Solver Pane: fixed-step and variable-step.

� You can further divide the solvers within each of these categories as: – discrete or continuous, – explicit or implicit, – one-step or multistep, – single-order or variable-order.

21/03/2011 25

Page 26: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Fixed-Step versus Variable-Step Solvers

� Both fixed-step and variable-step solvers compute the next simulation time as the sum of the current simulation time and a quantity known as the step size.

� With a fixed-step solver, the step size remains constantthroughout the simulation.

� In contrast, with a variable-step solver, the step size can vary from step to step, depending on the model dynamics. 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 that you specify.

� If you plan to generate code from your model and run the code on a real-time computer system, choose a fixed-stepsolver to simulate the model because you cannot map the variable-step size to the real-time clock.

21/03/2011 26

Page 27: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Fixed-Step versus Variable-Step Solvers (Cont.)

� 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 savings because, for a given � A variable-step solver allows this savings because, for a given level of accuracy, the solver can dynamically adjust the step size as necessary and thus reduce the number of steps.

� Whereas the fixed-step solver must use a single step size throughout the simulation based upon the accuracy requirements. To satisfy these requirements throughout the simulation, the fixed-step solver might require a very small step.

21/03/2011 27

Page 28: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Example

� The follow multirate model illustrates how the variable-step solver can shorten simulation time.

21/03/2011 28

Page 29: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Example (cont.)

� This model generates outputs at two different rates, every 0.5 second and every 0.75 second.

� To capture both outputs, the fixed-step solver must take a time step every 0.25 second (the fundamental sample time for the model).

[0.0 0.25 0.5 0.75 1.0 1.25 ...] [0.0 0.25 0.5 0.75 1.0 1.25 ...] � By contrast, the variable-step solver need take a step

only when the model actually generates an output.[0.0 0.5 0.75 1.0 1.5 2.0 2.25 ...]

� This significantly reduces the number of time steps required to simulate the model.

21/03/2011 29

Page 30: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Discrete versus Continuous Solvers

� Fixed-step and variable-step comprise two types: discrete and continuous.

� Discrete and continuous solvers rely on the model blocks to 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.continuous states that the blocks define.

� Therefore, when choosing a solver, you must first determine whether you need 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 continuous states, you must choose a continuous solver from the remaining solver choices based on the dynamics of your model. Otherwise, an error occurs.

21/03/2011 30

Page 31: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Summary of types of solvers in the Simulink library

21/03/2011 31

Page 32: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Explicit Fixed-Step Continuous Solvers

� Explicit solvers compute the value of a state at the next time step as an explicit function of the current values of both the state and the state derivative.

21/03/2011 32

Page 33: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Explicit Fixed-Step Continuous Solvers (cont.)

� None of these solvers has an error control mechanism. Therefore, the accuracy and the duration of a simulation depends directly on the size of the steps taken by the solver.

� If you specify a fixed-step solver type for a model, then by default, Simulink selects the ode3 solver, which can handle both continuous and discrete states with moderate computational effort.

� As with the discrete solver, if the model has discrete rates (sample times), then Simulink sets the step size to the fundamental sample time of the model by default. time of the model by default.

� If the model has no discrete rates, Simulink automatically uses the result of dividing the simulation total duration by 50. Consequently, the solver takes a step at each simulation time at which Simulink must update the discrete states of the model at its specified sample rates.

� However, it does not guarantee that the default solver accurately computes the continuous states of a model. Therefore, you might need to choose another solver, a different fixed step size, or both to achieve acceptable accuracy and an acceptable simulation time.

21/03/2011 33

Page 34: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Implicit Fixed-Step Continuous Solvers.

� An implicit fixed-step solver computes the state at the next time step as an implicit function of the state at the current time step and the state derivative at the next time step.

� Simulink provides one implicit fixed-step solver : ode14x.� This solver uses a combination of Newton's method and

extrapolation from the current value to compute the value of a extrapolation from the current value to compute the value of a state at the next time step. You can specify:– the number of Newton's method iterations – the extrapolation order that the solver uses to compute the next value of

a model state.� The more iterations and the higher the extrapolation order that you

select, the greater the accuracy you obtain. � However, you simultaneously create a greater computational

burden per step size.

21/03/2011 34

Page 35: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Process for Choosing a Fixed-Step Continuous Solver

� Any of the fixed-step continuous solvers in the Simulinkproduct can simulate a model to any desired level of accuracy, given a small enough step size.

� Unfortunately, it generally is not possible, or at least not practical, to decide a priori which combination of solver and step size will yield acceptable results for the continuous step size will yield acceptable results for the continuous states in the shortest time.

� Determining the best solver for a particular model generally requires experimentation.

� Following is the most efficient way to choose the best fixed-step solver for your model experimentally.

21/03/2011 35

Page 36: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Process for Choosing a Fixed-Step Continuous Solver (cont.)

� Choose error tolerances (see later).� Use one of the variable-step solvers to simulate your model to

the level of accuracy that you desire. � Start with ode45. If your model runs slowly, your problem

might be stiff and need an implicit solver.� The results of this step give a good approximation of the

correct simulation results and the appropriate fixed step size. � Use ode1 to simulate your model at the default step size for � Use ode1 to simulate your model at the default step size for

your model. � Compare the simulation results for ode1 with the simulation for

the variable-step solver. If the results are the same for the specified level of accuracy, you have found the best fixed-step solver for your model, namely ode1.

� You can draw this conclusion because ode1 is the simplest of the fixed-step solvers and hence yields the shortest simulation time for the current step size.

21/03/2011 36

Page 37: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Process for Choosing a Fixed-Step Continuous Solver (cont.)

� If ode1 does not give satisfactory results, repeat the preceding steps with each of the other fixed-step solvers until you find the one that gives accurate results with the least computational effort.

� The most efficient way to perform this task is to use a binary search technique:– Try ode3.– Try ode3.– If ode3 gives accurate results, try ode2. If ode2 gives accurate results,

it is the best solver for your model; otherwise, ode3 is the best.– If ode3 does not give accurate results, try ode5. If ode5 gives

accurate results, try ode4. If ode4 gives accurate results, select it as the solver for your model; otherwise, select ode5.

– If ode5 does not give accurate results, reduce the simulation step size and repeat the preceding process.

– Continue in this way until you find a solver that solves your model accurately with the least computational effort.

21/03/2011 37

Page 38: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Choosing a Variable-Step Solver

� As with fixed-step solvers, the set of variable-step solvers comprises a discrete solver and a subset of continuous solvers. However, unlike the fixed-step solvers, the step size varies dynamically based on the local error.

� If your model defines no states or defines only discrete states, select the discrete solver. select the discrete solver.

� In fact, if a model has no states or only discrete states, Simulinkuses the discrete solver to simulate the model even if you specify a continuous solver.

� If the model has continuous states, the continuous solvers use numerical integration to compute the values of the continuous states at the next time step.

21/03/2011 38

Page 39: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

About Variable-Step Continuous Solvers

� The variable-step solvers in the Simulink product dynamically vary the step size during the simulation.

� Each of these solvers increases or reduces the step size using its local error control to achieve the tolerances that you specify.

� Computing the step size at each time step adds to the � Computing the step size at each time step adds to the computational overhead but can reduce the total number of steps, and the simulation time required to maintain a specified level of accuracy.

� You can further categorize the variable-step continuous solvers as: one-step or multistep, single-order or variable-order, and explicit or implicit.

21/03/2011 39

Page 40: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Explicit Continuous Variable-Step Solvers

21/03/2011 40

Page 41: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Implicit Continuous Variable-Step Solvers (for Stiff models)

21/03/2011 41

Page 42: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Step Sizes

� For variable-step solvers, you can set the maximum and suggested initial step size parameters. By default, these parameters are automatically determined, indicated by the value auto.

� For fixed-step solvers, you can set the fixed step size. The default is also auto.The default is also auto.

� Maximum step size. The Max step size parameter controls the largest time step the solver can take. The default is determined from the start and stop times.

21/03/2011 42

Page 43: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Step Sizes (cont.)

� Initial step size. By default, the solvers select an initial step size by examining the derivatives of the states at the start time.

� If the first step size is too large, the solver might step over important behavior.

� The initial step size parameter is a suggested first step size. � The initial step size parameter is a suggested first step size. � The solver tries this step size but reduces it if error criteria

are not satisfied.

21/03/2011 43

Page 44: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Step Sizes (cont.)

� Minimum step size. Specifies the smallest time step the solver can take.

� If the solver needs to take a smaller step to meet error tolerances, it issues a warning indicating the current effective relative tolerance.

� This parameter can be either a real number greater than zero or a two-element vector where the first element is the minimum step size and the second element is the maximum number of size and the second element is the maximum number of minimum step size warnings to be issued before issuing an error.

� Setting the second element to zero results in an error the first time the solver must take a step smaller than the specified minimum. This is equivalent to changing the minimum step size violation diagnostic to error on the Diagnostics panel.

� Setting the second element to -1 results in an unlimited number of warnings. This is also the default if the input is a scalar.

21/03/2011 44

Page 45: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Specifying Error Tolerances for Variable-Step Solvers

� The solvers use standard local error control techniques to monitor the error at each time step.

� During each time step, the solvers compute the state values at the end of the step and also determine the local error, the estimated error of these state values.

� They then compare the local error to the acceptable � They then compare the local error to the acceptable error, which is a function of the relative tolerance (rtol) and absolute tolerance (atol). If the error is greater than the acceptable error for any state, the solver reduces the step size and tries again:

21/03/2011 45

Page 46: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Error tolerance

� Relative tolerance measures the error relative to the size of each state. The relative tolerance represents a percentage of the state's value. The default, 1e-3, means that the computed state is accurate to within 0.1%.

� Absolute tolerance is a threshold error value. This tolerance represents the acceptable error as the value of tolerance represents the acceptable error as the value of the measured state approaches zero.

The error for the i-th state is required to satisfy

21/03/2011 46

Page 47: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Relative tolerance and the Absolute tolerance.

21/03/2011 47

Page 48: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Absolute Tolerances

� You can specify auto or a real scalar.� If you specify auto (the default), Simulink initially sets the

absolute tolerance for each state to 1e-6. � As the simulation progresses, the absolute tolerance for

each state resets to the maximum value that the state has assumed so far, times the relative tolerance for that state. assumed so far, times the relative tolerance for that state.

� Thus, if a state changes from 0 to 1 and reltol is 1e-3, then by the end of the simulation, abstol becomes 1e-3 also. If a state goes from 0 to 1000, then abstol changes to 1.

21/03/2011 48

Page 49: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Speeding Up the Simulation

� When a model includes an Interpreted MATLAB Functionblock, the MATLAB interpreter is called at each time step, drastically slowing down the simulation. Use the built-in Fcnblock or the Math Function block whenever possible.

� MATLAB file S-functions also cause the MATLAB interpreter to be called at each time step. Consider either converting to be called at each time step. Consider either converting the S-function to a subsystem or to a C-MEX file S-function.

� Using a Memory block causes the variable-order solvers (ode15s and ode113) to be reset back to order 1 at each time step.

21/03/2011 49

Page 50: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Speeding Up the Simulation (cont.)

� If you changed the maximum step size, try running the simulation again with the default value (auto).

� Did you ask for too much accuracy? The default relative tolerance (0.1% accuracy) is usually sufficient. For models with states that go to zero, if the absolute tolerance parameter is too small, the simulation can take too many parameter is too small, the simulation can take too many steps around the near-zero state values. See the discussion of error in Maximum order.

� The time scale might be too long. Reduce the time interval.� The problem might be stiff, but you are using a nonstiff

solver. Try using ode15s.

21/03/2011 50

Page 51: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Speeding Up the Simulation (cont.)

� The model uses sample times that are not multiples of each other. Mixing sample times that are not multiples of each other causes the solver to take small enough steps to ensure sample time hits for all sample times.

� The model contains an algebraic loop. The solutions to algebraic loops are iteratively computed at every time step. Therefore, they severely degrade performance. Therefore, they severely degrade performance.

� Your model feeds a Random Number block into an Integrator block. For continuous systems, use the Band-Limited White Noise block in the Sources library.

� Your model contains a scope viewer that displays a large number of data points. Try adjusting the viewer parameter settings that can affect performance

21/03/2011 51

Page 52: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Tips for Building Models

� Memory issues – In general, the more memory, the better Simulink performs.

� Using hierarchy – More complex models often benefit from adding the hierarchy of

subsystems to the model. Grouping blocks simplifies the top level of the model and can make it easier to read and understand the model. The Model Browser provides useful information about model. The Model Browser provides useful information about complex models

� Cleaning up models – Well organized and documented models are easier to read and

understand. Signal labels and model annotations can help describe what is happening in a model.

21/03/2011 52

Page 53: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

Modeling strategies

� If several of your models tend to use the same blocks, you might find it easier to save these blocks in a model. Then, when you build new models, just open this model and copy the commonly used blocks from it.

� You can create a block library by placing a collection of blocks into a system and saving the system. You can then access the system by typing its name in the MATLAB access the system by typing its name in the MATLAB command window.

� Generally, when building a model, design it first on paper, then build it using the computer. Then, when you start putting the blocks together into a model, add the blocks to the model window before adding the lines that connect them. This way, you can reduce how often you need to open block libraries.

21/03/2011 53

Page 54: 003 Modeling and simulation - UNIMORE · Link Phase: In this phase, the Simulink Engine allocates memory needed for working areas (signals, states, and run-time parameters) for execution

END OF PART 321/03/2011

54


Recommended