4.2 Simulating System Behavior
Once input signals are supplied to StateResponse or OutputResponse in the form of a function in the specified variable, as described in Section 4.1, a symbolic solution based on Eq. (4.2) or Eq. (4.5) is attempted. There are, however, situations when the symbolic solution is either impossible or just too time consuming. In such cases, StateResponse and OutputResponse can be used to carry out a simulation of the state and output responses. For continuoustime systems, the simulation is based on the approximate numerical solution of the underlying differential equations using the builtin function NDSolve and is invoked when the range for the timedomain variable in the form of {t, 0, tmax}, or simply {t, tmax}, is given instead of the variable t. The result then appears in terms of InterpolatingFunction objects. This regime is referred to as the analog simulation.
For discretetime systems, the same input syntax causes the input functions to be sampled on a uniform time grid. The grid starts at time (or ). The discrete simulation then proceeds in a straightforward manner according to Eq. (4.4) starting from the initial conditions for and then iterating for . Instead of supplying the input functions and the range for the time variable, you can give the input sequences explicitly. Note that if you supply the input signals in the form of input sequences for the continuoustime system, the system is first converted to discrete time, and then the simulation is performed. To facilitate the conversion, the response functions accept the options pertinent to ToDiscreteTime, notably Sampled may be used to set the sampling period, and Method to choose the conversion method.
When input signals are discretized, the input sequences u must represent a matrix each row of which corresponds to a signal at one input, and the number of rows must be equal to the number of inputs. For singleinput systems, the matrix may be reduced to a vector.
State and output responses in the simulation mode.
Let us simulate the output of the Tbridge network in Figure 4.2 to a squarewave signal.
Figure 4.2. A Tbridge.
This is the transfer function for the Tbridge network.
In[18]:=
Out[18]=
Here is the transfer function for a given set of parameters.
In[19]:=
Out[19]=
This utility function creates a square wave with period .
In[20]:=
Supplying the input signal together with both the time variable and the duration of time period causes OutputResponse to perform in the simulation mode. The output is a list of simulated responses. In this case, there is only one element in the list since we are dealing with a singleoutput system.
In[21]:=
Out[21]=
This plots the input (the solid line) and the output (the dashed line) signals.
In[22]:=
This produces the output of the circuit using the discrete simulation with the sampling period . Note that the sampling period is supplied to OutputResponse to specify the distance between points in the input vector (produced by the Table function); this is necessary to convert the system to discrete time properly. The output is a list of simulated responses. In this case, there is only one vector in the list.
In[23]:=
In[24]:=
Out[24]=
The result can be drawn with the builtin Mathematica function ListPlot (MultipleListPlot for multiple outputs). Here, however, we use a utility function, SimulationPlot, described later in this section.
In[25]:=
This compares the results of the analog and discrete simulations. Clearly, the chosen sampling period is too large to accurately approximate the behavior of the circuit.
In[26]:=
Output response simulation plots.
SimulationPlot provides a convenient way to compute and plot the output response of a system with a single function call. The syntax for SimulationPlot closely resembles the one for OutputResponse. As with OutputResponse, SimulationPlot produces the analog or discrete simulation depending on the type of input system, unless the input signal is specifically given as input sequences, in which case the discrete simulation takes place. You can also force the discrete simulation of a continuous system by giving the option Sampled to SimulationPlot. The option is ignored if the input signal is already sampled. For the purpose of simulation, the sampling period cannot take symbolic values.
As an exception, simulation of the response of continuoustime systems to the Dirac delta function is always performed in the discretesimulation mode. To carry out analog simulation, you can mimic the Dirac delta function with a continuous impulse of small width. See the impulse response examples in Section 4.3. Note that, for a continuoustime system, the impulse response is simulated as a decay of some initial state conditions in the absence of the input signal. The initial conditions are determined by the components of the matrix .
To facilitate computation of the output response and conversion to discretetime, if necessary, SimulationPlot takes the options for OutputResponse and ToDiscreteTime. Additionally, SimulationPlot accepts the options for the builtin plotting functions Plot, ListPlot, or MultipleListPlot and routes the options to appropriate functions.
This is an analog simulation of the output response of the bridge circuit to a squarewave input signal for another set of parameters of the bridge.
In[27]:=
This is a discrete simulation for the same parameters. The input system is discretized using the specified sampling period.
In[28]:=
This is a square pulse function.
In[29]:=
Out[29]=
This simulates the behavior of the bridge for yet another set of parameters. To ensure the correct result of NDSolve, which is called internally to compute the output response, we limit the value of MaxRelativeStepSize.
In[30]:=
This converts a particular bridge circuit to the discretetime domain.
In[31]:=
Out[31]=
Here is the response of this circuit to random noise. Note that SimulationPlot picks up the sampling period from the input system.
In[32]:=
Although SimulationPlot generates only output response curves, it is a simple matter to get it to plot the state response as well. This can be done by adding an appropriate matrix to the StateSpace object composed of only matrices and . In the rest of the section we give an example.
Let us consider the linearized statespace system for the depth control problem of a submarine (see Figure 4.3) for small angles and constant velocity ft/s as described in Dorf (1992). We will assume that the state variables are , and , where is the angle of attack and the input variable is , the deflection of the stern plane. We will compute the state response of the system to a step command for the stern plane of from zero initial conditions using the discretetime approximation with sampling period seconds.
Figure 4.3. Depth control of a submarine.
Here is the twomatrix statespace model.
In[33]:=
Out[33]=
To compute the state response, we insert a diagonal matrix containing some weighting coefficients.
In[34]:=
Out[34]=
This finds the discretetime approximation of the system.
In[35]:=
Out[35]=
This plots the state response. We numericalize the input function to avoid supplying Degree (°) in symbolic form. Note that SimulationPlot automatically picks up as many points as needed for the discrete simulation to cover the specified time frame.
In[36]:=
