Chapter 3. Simulating a Model

In this chapter, we'll learn how to simulate a model. We'll start with a little theory about the solver and simulation time in order to understand the main concepts behind the Simulink engine. Then we will assemble the test system by putting together the cruise controller and the car model, and run various simulations. We'll be able to calibrate our cruise controller and we'll learn what really sets Simulink apart—many different source blocks and one mighty sink block, Scope.

The mandatory theory

Before starting the simulation, we'll have a closer look at how Simulink computes the simulation results, in order to be able to choose the appropriate simulation time and solver.

Let's build a simple system that finds the solution to this problem:

The mandatory theory

The exact mathematical solution is y(t) = e -t.

The Simulink model (which we'll save as example.slx) that implements the problem is:

The mandatory theory

Notice that we've set the Initial condition source parameter of the Integrator block to be external.

To configure the simulation time and solver of this model, click on the Model Configuration Parameters option from the Simulation menu (keyboard shortcut: Ctrl + E).

A new window will open; select the Solver node from the left-side panel, which will give you access to configure the Solver options and Simulation time parameters.

Under the Solver options section, select the Type list value as Fixed-step (don't worry, we'll explain the meaning later) as shown in the following screenshot:

The mandatory theory

Then let's edit the Scope block parameters. We want to see where Simulink does the calculations, so we'll click on the Style tab and set the Marker option as round to both lines:

The mandatory theory

Now that we have set up our example, let's begin by examining the simulation times.

The simulation times – when the math is done

There are three main times to be set before starting with the simulation: the simulation start time, the simulation stop time, and the simulation step size.

The Start time and Stop time parameters under Simulation time are self-explanatory; they tell Simulink the time period (in seconds) required to perform the simulation. Usually Start time is set to 0.0, and Stop time to either inf (meaning that the simulation will never end until it's stopped by the user) or a time when the transient effects in the model become negligible. The default value of Stop time is 10.0.

The step size defines the time period that Simulink uses to sample the inputs and calculate the outputs. The default value is set to auto; that is, Simulink will try to determine the appropriate step size.

Let's run the simulation and see the result by double-clicking on the Scope block. We should see this graph:

The simulation times – when the math is done

By zooming in on the graph a little, we'll find that the samples have been computed every 0.2 seconds (0.2, 0.4, 0.6, and so on). This is confirmed by the warning that Simulink issued in MATLAB's Command Window, which is seen as follows:

Warning: Unable to determine a fixed step size based on the sample times in the model 'example', because the model does not have any discrete sample times. Picking a fixed step size of (0.2) based on simulation start and stop times.

Let's take a quick note of the result at 1 second: it is y(1) = 0.3677.

The exact solution is e-1 = -0.3679, so the simulation result is fairly accurate.

Now, in the Model Configuration Parameters window, let's set Fixed-step size (fundamental sample time) as 1, and run the simulation again.

You'll be presented with this graph:

The simulation times – when the math is done

What happened? We told Simulink to acquire new samples every second; thus, the resulting curve is less accurate. If we look at the result for 1 second, we can see that it has changed to y(1) = -0.3333. With such a large step size, we shouldn't be surprised that the solution Simulink found is showing a greater difference from the exact one.

Most of the time, your models will give unpredictable results because you chose an inadequately long step size. Vice versa, you may have defined a very small time step size, just to be on the safe side, and your simulation could take ages. If you've got a fast computer, try setting a step size of 0.00001 and running the simulation again (you'll have to uncheck the Limit data points to last option in the Scope parameter window under the History tab).

Tip

The rule of thumb is to have a time step smaller than half the smallest period of your analog signals if the system is not stiff like this one—more about that in the later sections.

The solvers – these great unknown

The solvers are the answers to the question: how does Simulink run the simulation?

Every solver performs the simulation by advancing one time step and computing the new outputs starting from the previous state(s) of the system until the end of the simulation time. The initial system state must be fully defined.

The appropriate solver for a model can be chosen by defining these characteristics:

  • Time step size, which can be fixed or variable
  • Presence of continuous states in the system
  • Stiffness of the system

Variable-step versus fixed-step solvers

A variable-step solver will choose to use a shorter time step when the signal derivative is changing faster in order to obtain a better approximation, while it will use a longer step when the result changes at a slower pace. The most used variable-step solver is the ode45 solver (this is the default Simulink solver as well).

A fixed-step solver will, on the other hand, always use the same step size. The simplest is the ode1 solver (the famous Euler method).

We've already run our example with a fixed-step solver. Let's change this to a variable-step solver by opening the Model Configuration Parameters window and setting the ode45 solver.

We now have the option to configure more Solver parameters; we'll allow it to use a Max step size of 1 second and a Min step size of 0.2 seconds, setting other parameters to the default value. Running the simulation now will give us the following result:

Variable-step versus fixed-step solvers

We can see that the step size is indeed variable: the second value has been computed after only 0.2 seconds, and then the step size grew bigger and stayed at 1 second. We got a good approximation in the problematic region while using longer steps in the almost constant region, thus achieving a faster simulation time than a fixed-step solver with a step size of 0.2 seconds.

But how does the solver determine when it's necessary to reduce the step size? The answer is by looking at the tolerances defined in the Model Configuration Parameters window.

Relative tolerance defines the maximum error as a percentage of the actual value. The default value (1e-3) means that an error of 0.1 percent is accepted.

Absolute tolerance defines the maximum error as a scalar value. When it is set to auto, Simulink will assume 1e-6 initially (that is, 0.000001), then change it to the maximum error calculated with the relative tolerance.

The Shape preservation option, off (Disable all) by default, helps to increase the accuracy when the solution's derivative varies rapidly at the cost of a more computing-intensive simulation (thus increasing the overall time to compute the result).

Finally, there's the option Number of consecutive min steps, which defines how many times the solver can use a time step smaller than the minimum step size violations defined before issuing a warning or an error.

Let's set the Relative tolerance parameter to 1e-5 and run the simulation. We'll obtain this result:

Variable-step versus fixed-step solvers

This time, the solver chose to use more shorter steps while the signal was changing to improve the accuracy, thanks to the relative tolerance we've set; then the step size grew gradually back to 1 second.

Variable-step solvers give us a good trade-off between result approximation and overall simulation times in models, whereas fixed-step solvers are too computationally intensive to be used efficiently.

Note

Fixed-step solvers are required for code generation and real-time control purposes because Simulink can't go back in time to give a more accurate result.

Continuous versus discrete

A system can be continuous or discrete based on the presence of integrators or derivatives in the model (these are called continuous states). In other words, if the system is represented by the means of one or more differential equations, the system is continuous. Our cruise controller is continuous because it has an integral component.

The continuous solver can choose to perform several iteration cycles in a single time step to reach the best possible approximation of the final result. Such solvers fall into the ODE (Ordinary Differential Equation solvers) category.

If a system is discrete, there are no differential equations but only memories and mathematical operands. A discrete solver is unable to perform the numerical integration required by continuous states.

The continuous system requires continuous solvers, while Simulink will switch to a discrete solver for discrete systems even if a continuous solver has been specified.

Try to set a discrete solver in our example system and run a simulation. An error window will appear, telling us that a discrete solver can't be used because the model contains continuous states.

Stiff versus nonstiff

In a stiff continuous system, certain solvers are unable to compute the result unless the step size is small enough; thus, a stiff solver is needed. These systems can be identified by an overall slowly changing solution that can vary rapidly in a very small time period.

Our example is mildly stiff: the ode45 solver had to use a small step size to compute the result close to t = 0, but it managed to get the job done by changing the time step. A more appropriate choice would have been the ode23t solver (moderately stiff), which would have given the following result:

Stiff versus nonstiff

Let's choose another nonstiff solver, the fixed-step ode1, implementing the Euler method. Set the Fixed step size parameter to 1.5 and run the simulation:

Stiff versus nonstiff

That's interesting: Using the Euler method with a long time step shows the result to be oscillating around the mathematical result, but the solution is still convergent.

Setting the step size to 2 seconds will show that it can't converge anymore, and any step size higher than 2 seconds will make the simulation diverge from the correct result and go towards infinite oscillation. The following figure shows the result with a step size of 2.1 and a simulation time of 20 seconds:

Stiff versus nonstiff

Tip

The following article presents stiff solvers in a way that is easy to understand: http://blogs.mathworks.com/seth/2012/07/03/why-do-we-need-stiff-ode-solvers/.

The documentation center, accessible by pressing F1, has detailed information about the solver types and parameters. This information can be found by navigating to Simulink | Simulation | Configure simulation.

Now that we've learned how solvers work and how to choose the right solver, we can start with the practice.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.136.226