Using Simulink    

The Simulation Parameters Dialog Box

This section discusses the simulation parameters, which you specify either on the Simulation Parameters dialog box or using sim and simset commands. Parameters are described as they appear on the dialog box panes.

The Solver Pane

The Solver pane appears when you first choose Parameters from the Simulation menu or when you select the Solver tab.

The Solver pane allows you to

Simulation Time

You can change the start time and stop time for the simulation by entering new values in the Start time and Stop time fields. The default start time is 0.0 seconds and the default stop time is 10.0 seconds.

Simulation time and actual clock time are not the same. For example, running a simulation for 10 seconds usually does not take 10 seconds. The amount of time it takes to run a simulation depends on many factors, including the model's complexity, the solver's step sizes, and the computer's speed.

Solvers

Simulation of a Simulink model entails computing its inputs, outputs, and states at intervals from the simulation start time to the simulation end time. Simulink uses a solver to perform this task. No one method for solving a model is suitable for all models. Simulink therefore provides an assortment of solvers, each geared to solving a specific type of model. The Solver pane allows you to select the solver most suitable for your model (see Improving Simulation Performance and Accuracy for information on choosing a solver). Your choices include

Fixed-step continuous solvers.   These solvers compute a model's continuous states at equally spaced time steps from the simulation start time to the simulation stop time. The solvers use numerical integration to compute the continuous states of a system from the state derivatives specified by the model. Each solver uses a different integration method, allowing you to choose the method most suitable for your model. To specify a fixed-step continuous solver for your model, select fixed-step from the solver type list on the Solver pane. Then choose one of the following options from the adjacent integration method list.

Fixed-step discrete solver.   Simulink provides a fixed-step solver that performs no integration. It is suitable for use in solving models that have no continuous states, including stateless models or models having only discrete states. To specify this solver, select fixed-step from the solver type list on the Solver pane. Then choose discrete from the adjacent integration method list.

Variable-step continuous solvers.   These solvers decrease the simulation step size to increase accuracy when a system's continuous states are changing rapidly and increase the step size to save simulation time when a system's states are changing slowly. To specify a variable-step continuous solver for your model, select variable-step from the solver type list on the Solver pane. Then choose one of the following options from the adjacent integration method list.

Variable-Step Discrete Solver.   Simulink provides a variable-step discrete solver that does no integration but does do zero-crossing detection (see Zero-Crossing Detection). Use this solver for models that have no continuous states and that have continuous signals requiring zero-crossing detection and/or have discrete blocks that operate at different sample times. Simulink uses this solver by default if you did not specify the fixed-step discrete solver and your model has no continuous states.

Simulink's fixed-step discrete solver advances the simulation by fixed-size time steps. As a result, it can take a step even when nothing is happening in the model. By contrast, Simulink's variable-step solver does not have to take a time step when nothing is happening in the model. Instead, it can adjust the step size to advance the simulation to the next point where something significant happens. Depending on the model, this can greatly reduce the number of steps and hence the time required to simulate a model.

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

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

By contrast, the variable-step solver need take a step only when the model actually generates an output.

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

Solver Options

The default solver parameters provide accurate and efficient results for most problems. In some cases, however, tuning the parameters can improve performance. (For more information about tuning these parameters, see Improving Simulation Performance and Accuracy.) You can tune the selected solver by changing parameter values on the Solver pane.

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.

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.

Generally, the default maximum step size is sufficient. If you are concerned about the solver's missing significant behavior, change the parameter to prevent the solver from taking too large a step. If the time span of the simulation is very long, the default step size might be too large for the solver to find the solution. Also, if your model contains periodic or nearly periodic behavior and you know the period, set the maximum step size to some fraction (such as 1/4) of that period.

In general, for more output points, change the refine factor, not the maximum step size. For more information, see Refine output.

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 solver tries this step size but reduces it if error criteria are not satisfied.

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 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. The default values for this parameter are a minimum step size on the order of machine precision and an unlimited number of warnings.

Error Tolerances

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 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:

The error for the ith state, ei, is required to satisfy

The following figure shows a plot of a state and the regions in which the acceptable error is determined by the relative tolerance and the absolute tolerance.

If you specify auto (the default), Simulink sets the absolute tolerance for each state initially to 1e-6. As the simulation progresses, Simulink resets the absolute tolerance for each state to the maximum value that the state has assumed thus far times the relative tolerance for that state. Thus, if a state goes from 0 to 1 and reltol is 1e-3, then by the end of the simulation the abstol is set to 1e-3 also. If a state goes from 0 to 1000, then the abstol is set to 1.

If the computed setting is not suitable, you can determine an appropriate setting yourself. You might have to run a simulation more than once to determine an appropriate value for the absolute tolerance.

The Integrator, Transfer Fcn, State-Space, and Zero-Pole blocks allow you to specify absolute tolerance values for solving the model states that they compute or that determine their output. The absolute tolerance values that you specify for these blocks override the global settings in the Simulation Parameters dialog box. You might want to override the global setting in this way, if the global setting does not provide sufficient error control for all of your model's states, for example, because they vary widely in magnitude.

The Maximum Order for ode15s

The ode15s solver is based on NDF formulas of orders one through five. Although the higher order formulas are more accurate, they are less stable. If your model is stiff and requires more stability, reduce the maximum order to 2 (the highest order for which the NDF formula is A-stable). When you choose the ode15s solver, the dialog box displays this parameter.

As an alternative, you can try using the ode23s solver, which is a fixed-step, lower order (and A-stable) solver.

Multitasking Options

If you select a fixed-step solver, the Solver pane of the Simulation Parameters dialog box displays a Mode options list. The list allows you to select one of the following simulation modes.

MultiTasking.   This mode issues an error if it detects an illegal sample rate transition between blocks, that is, a direct connection between blocks operating at different sample rates. In real-time multitasking systems, illegal sample rate transitions between tasks can result in a task's output not being available when needed by another task. By checking for such transitions, multitasking mode helps you to create valid models of real-world multitasking systems, where sections of your model represent concurrent tasks.

Use the Rate Transition block to eliminate illegal rate transitions from your model. For more information, see "Models with Multiple Sample Rates in the online documentation for the Real-Time Workshop for more information.

SingleTasking.   This mode does not check for sample rate transitions among blocks. This mode is useful when you are modeling a single-tasking system. In such systems, task synchronization is not an issue.

Auto.   This option causes Simulink to use single-tasking mode if all blocks operate at the same rate and multitasking mode if the model contains blocks operating at different rates.

Output Options

The Output options area of the dialog box enables you to control how much output the simulation generates. You can choose from three options:

Refine output.   The Refine output choice provides additional output points when the simulation output is too coarse. This parameter provides an integer number of output points between time steps; for example, a refine factor of 2 provides output midway between the time steps, as well as at the steps. The default refine factor is 1.

To get smoother output, it is much faster to change the refine factor instead of reducing the step size. When the refine factor is changed, the solvers generate additional points by evaluating a continuous extension formula at those points. Changing the refine factor does not change the steps used by the solver.

The refine factor applies to variable-step solvers and is most useful when you are using ode45. The ode45 solver is capable of taking large steps; when graphing simulation output, you might find that output from this solver is not sufficiently smooth. If this is the case, run the simulation again with a larger refine factor. A value of 4 should provide much smoother results.

Produce additional output.   The Produce additional output choice enables you to specify directly those additional times at which the solver generates output. When you select this option, Simulink displays an Output Times field on the Solver pane. Enter a MATLAB expression in this field that evaluates to an additional time or a vector of additional times. The additional output is produced using a continuous extension formula at the additional times. Unlike the refine factor, this option changes the simulation step size so that time steps coincide with the times that you have specified for additional output.

Produce specified output only.   The Produce specified output only choice provides simulation output only at the specified output times. This option changes the simulation step size so that time steps coincide with the times that you have specified for producing output. This choice is useful when you are comparing different simulations to ensure that the simulations produce output at the same times.

Comparing Output options.   A sample simulation generates output at these times.

Choosing Refine output and specifying a refine factor of 2 generates output at these times.

Choosing the Produce additional output option and specifying [0:10] generates output at these times

and perhaps at additional times, depending on the step size chosen by the variable-step solver.

Choosing the Produce Specified Output Only option and specifying [0:10] generates output at these times.

In general, you should specify output points as integers times a fundamental step size. For example,

is more accurate than


  Controlling Execution of a Simulation The Workspace I/O Pane