# Documentation

## Models with Time Delays

How to create models that include time delays.

### Modeling Time Delays

Use the following model properties to represent time delays in linear systems.

• InputDelay, OutputDelay — Time delays at system inputs or outputs

• ioDelay, InternalDelay — Time delays that are internal to the system

In discrete-time models, these properties are constrained to integer values that represent delays expressed as integer multiples of the sample time. To approximate discrete-time models with delays that are a fractional multiple of the sample time, use thiran.

### First Order Plus Dead Time Model

This example shows how to create a first order plus dead time model using the InputDelay or OutputDelay properties of tf.

To create the following first-order transfer function with a 2.1 s time delay:

$G\left(s\right)={e}^{-2.1s}\frac{1}{s+10},$

enter:

G = tf(1,[1 10],'InputDelay',2.1)

where InputDelay specifies the delay at the input of the transfer function.

 Tip   You can use InputDelay with zpk the same way as with tf:G = zpk([],-10,1,'InputDelay',2.1)

For SISO transfer functions, a delay at the input is equivalent to a delay at the output. Therefore, the following command creates the same transfer function:

G = tf(1,[1 10],'OutputDelay',2.1)

Use dot notation to examine or change the value of a time delay. For example, change the time delay to 3.2 as follows:

 G.OutputDelay = 3.2;

To see the current value, enter:

G.OutputDelay ans = 3.2000
 Tip   An alternative way to create a model with a time delay is to specify the transfer function with the delay as an expression in s:Create a transfer function model for the variable s.s = tf('s'); Specify G(s) as an expression in s.G = exp(-2.1*s)/(s+10);

### Input and Output Delay in State-Space Model

This example shows how to create state-space models with delays at the inputs and outputs, using the InputDelay or OutputDelay properties of ss.

Create a state-space model describing the following one-input, two-output system:

$\begin{array}{l}\frac{dx\left(t\right)}{dt}=-2x\left(t\right)+3u\left(t-1.5\right)\\ y\left(t\right)=\left[\begin{array}{c}x\left(t-0.7\right)\\ -x\left(t\right)\end{array}\right].\end{array}$

This system has an input delay of 1.5. The first output has an output delay of 0.7, and the second output is not delayed.

 Note:   In contrast to SISO transfer functions, input delays are not equivalent to output delays for state-space models. Shifting a delay from input to output in a state-space model requires introducing a time shift in the model states. For example, in the model of this example, defining T = t – 1.5 and X(T) = x(T + 1.5) results in the following equivalent system:$\begin{array}{l}\frac{dX\left(T\right)}{dT}=-2X\left(T\right)+3u\left(T\right)\\ y\left(T\right)=\left[\begin{array}{c}X\left(T-2.2\right)\\ -X\left(T-1.5\right)\end{array}\right].\end{array}$All of the time delays are on the outputs, but the new state variable X is time-shifted relative to the original state variable x. Therefore, if your states have physical meaning, or if you have known state initial conditions, consider carefully before shifting time delays between inputs and outputs.

To create this system:

1. Define the state-space matrices.

A = -2; B = 3; C = [1;-1]; D = 0; 
2. Create the model.

G = ss(A,B,C,D,'InputDelay',1.5,'OutputDelay',[0.7;0])

G is a ss model.

 Tip   Use delayss to create state-space models with more general combinations of input, output, and state delays, of the form:$\begin{array}{l}\frac{dx}{dt}=Ax\left(t\right)+Bu\left(t\right)+\sum _{j=1}^{N}\left(Ajx\left(t-tj\right)+Bju\left(t-tj\right)\right)\\ y\left(t\right)=Cx\left(t\right)+Du\left(t\right)+\sum _{j=1}^{N}\left(Cjx\left(t-tj\right)+Dju\left(t-tj\right)\right)\end{array}$

### Transport Delay in MIMO Transfer Function

This example shows how to create a MIMO transfer function with different transport delays for each input-output (I/O) pair.

Create the MIMO transfer function:

$H\left(s\right)=\left[\begin{array}{cc}{e}^{-0.1}\frac{2}{s}& {e}^{-0.3}\frac{s+1}{s+10}\\ 10& {e}^{-0.2}\frac{s-1}{s+5}\end{array}\right].$

Time delays in MIMO systems can be specific to each I/O pair, as in this example. You cannot use InputDelay and OutputDelay to model I/O-specific transport delays. Instead, use ioDelay to specify the transport delay across each I/O pair.

To create this MIMO transfer function:

1. Create a transfer function model for the variable s.

s = tf('s');
2. Use the variable s to specify the transfer functions of H without the time delays.

H = [2/s (s+1)/(s+10); 10 (s-1)/(s+5)]; 
3. Specify the ioDelay property of H as an array of values corresponding to the transport delay for each I/O pair.

H.ioDelay = [0.1 0.3; 0 0.2];

H is a two-input, two-output tf model. Each I/O pair in H has the time delay specified by the corresponding entry in tau.

### Closing Feedback Loops with Time Delays

This example shows how internal delays arise when you interconnect models that have input, output, or transport time delays.

Create a model of the following control architecture:

G is the plant model, which has an input delay. C is a proportional-integral (PI) controller.

To create a model representing the closed-loop response of this system:

1. Create the plant G and the controller C.

G = tf(1,[1 10],'InputDelay',2.1); C = pid(0.5,2.3); 

C has a proportional gain of 0.5 and an integral gain of 2.3.

2. Use feedback to compute the closed-loop response from r to y.

T = feedback(C*G,1); 

The time delay in T is not an input delay as it is in G. Because the time delay is internal to the closed-loop system, the software returns T as an ss model with an internal time delay of 2.1 seconds.

 Note:   In addition to feedback, any system interconnection function (including parallel and series) can give rise to internal delays.

T is an exact representation of the closed-loop response, not an approximation. To access the internal delay value, enter:

T.InternalDelay

A step plot of T confirms the presence of the time delay:

step(T)

 Note:   Most analysis commands, such as step, bode and margin, support models with internal delays.

The internal time delay is stored in the InternalDelay property of T. Use dot notation to access InternalDelay. For example, to change the internal delay to 3.5 seconds, enter:

 T.InternalDelay = 3.5

You cannot modify the number of internal delays because they are structural properties of the model.

### Discrete-Time Transfer Function with Time Delay

This example shows how to create a discrete-time transfer function with a time delay.

Specify time delays for discrete-time models in the same way as for continuous-time models, except for discrete-time models, delay values must be integer multiples of the sample time. For example, create the following first-order transfer function, with a sample time of Ts = 0.1 s:

$H\left(z\right)={z}^{-25}\frac{2}{z-0.95}.$

H = tf(2,[1 -0.95],0.1,'InputDelay',25)

Setting InputDelay to 25 results in a delay of 25 sampling periods.

 Tip   Use thiran to approximate discrete-time models with delays that are a fractional multiple of the sample time.

### Time-Delay Approximation

Many control design algorithms cannot handle time delays directly. For example, techniques such as root locus, LQG, and pole placement do not work properly if time delays are present. A common technique is to replace delays with all-pass filters that approximate the delays.

To approximate time delays in continuous-time models, use the pade command to compute a Padé approximation. The Padé approximation is valid only at low frequencies, and provides better frequency-domain approximation than time-domain approximation. It is therefore important to compare the true and approximate responses to choose the right approximation order and check the approximation validity.

For discrete-time models, use absorbDelay to convert a time delay to factors of 1/z where the time delay is an integer multiple of the sample time. Use thiran to approximate fractional time delays.

### Time-Delay Approximation in Continuous-Time Open-Loop Model

This example shows how to approximate delays in a continuous-time open-loop system using pade.

Padé approximation is helpful when using analysis or design tools that do not support time delays.

1. Create sample open-loop system with an output delay.

s = tf('s'); P = exp(-2.6*s)/(s^2+0.9*s+1); 

P is a second-order transfer function (tf) object with a time delay.

2. Compute the first-order Padé approximation of P.

Pnd1 = pade(P,1) 
Pnd1 = -s + 0.7692 ---------------------------------- s^3 + 1.669 s^2 + 1.692 s + 0.7692 Continuous-time transfer function. 

This command replaces all time delays in P with a first-order approximation. Therefore, Pnd1 is a third-order transfer function with no delays.

3. Compare the frequency response of the original and approximate models using bodeplot.

h = bodeoptions; h.PhaseMatching = 'on'; bodeplot(P,'-b',Pnd1,'-.r',{0.1,10},h) legend('Exact delay','First-Order Pade','Location','SouthWest') 

The magnitude of P and Pnd1 match exactly. However, the phase of Pnd1 deviates from the phase of P beyond approximately 1 rad/s.

4. Increase the Padé approximation order to extend the frequency band in which the phase approximation is good.

Pnd3 = pade(P,3); 
5. Compare the frequency response of P, Pnd1 and Pnd3.

bodeplot(P,'-b',Pnd3,'-.r',Pnd1,':k',{0.1 10},h) legend('Exact delay','Third-Order Pade','First-Order Pade',... 'Location','SouthWest') 

The phase approximation error is reduced by using a third-order Padé approximation.

6. Compare the time domain responses of the original and approximated systems using stepplot.

stepplot(P,'-b',Pnd3,'-.r',Pnd1,':k') legend('Exact delay','Third-Order Pade','First-Order Pade',... 'Location','Southeast') 

Using the Padé approximation introduces a nonminimum phase artifact ("wrong way" effect) in the initial transient response. The effect is quite pronounced in the first-order approximation, which dips significantly below zero before changing direction. The effect is reduced in the higher-order approximation, which far more closely matches the exact system's response.

 Note:   Using too high an approximation order may result in numerical issues and possibly unstable poles. Therefore, avoid Padé approximations with order N>10.

### Time-Delay Approximation in Continuous-Time Closed-Loop Model

This example shows how to approximate delays in a continuous-time closed-loop system with internal delays, using pade.

Padé approximation is helpful when using analysis or design tools that do not support time delays.

1. Create sample continuous-time closed-loop system with an internal delay.

Construct a model Tcl of the closed-loop transfer function from r to y.

s = tf('s'); G = (s+1)/(s^2+.68*s+1)*exp(-4.2*s); C = pid(0.06,0.15,0.006); Tcl = feedback(G*C,1); 

Examine the internal delay of Tcl.

Tcl.InternalDelay 
ans = 4.2000 
2. Compute the first-order Padé approximation of Tcl.

Tnd1 = pade(Tcl,1); 

Tnd1 is a state-space (ss) model with no delays.

3. Compare the frequency response of the original and approximate models.

h = bodeoptions; h.PhaseMatching = 'on'; bodeplot(Tcl,'-b',Tnd1,'-.r',{.1,10},h); legend('Exact delay','First-Order Pade','Location','SouthWest'); 

The magnitude and phase approximation errors are significant beyond 1 rad/s.

4. Compare the time domain response of Tcl and Tnd1 using stepplot.

stepplot(Tcl,'-b',Tnd1,'-.r'); legend('Exact delay','First-Order Pade','Location','SouthEast'); 

Using the Padé approximation introduces a nonminimum phase artifact ("wrong way" effect) in the initial transient response.

5. Increase the Padé approximation order to see if this will extend the frequency with good phase and magnitude approximation.

Tnd3 = pade(Tcl,3); 
6. Observe the behavior of the third-order Padé approximation of Tcl. Compare the frequency response of Tcl and Tnd3.

bodeplot(Tcl,'-b',Tnd3,'-.r',Tnd1,'--k',{.1,10},h); legend('Exact delay','Third-Order Pade','First-Order Pade',... 'Location','SouthWest'); 

The magnitude and phase approximation errors are reduced when a third-order Padé approximation is used.

Increasing the Padé approximation order extends the frequency band where the approximation is good. However, too high an approximation order may result in numerical issues and possibly unstable poles. Therefore, avoid Padé approximations with order N>10.

### Approximate Different Delays with Different Approximation Orders

This example shows how to specify different Padé approximation orders to approximate internal and output delays in a continuous-time open-loop system.

Load a sample continuous-time open-loop system that contains internal and output time delays.

load(fullfile(matlabroot,'examples','control','PadeApproximation1.mat'),'sys') sys 
sys = a = x1 x2 x1 -1.5 -0.1 x2 1 0 b = u1 x1 1 x2 0 c = x1 x2 y1 0.5 0.1 d = u1 y1 0 (values computed with all internal delays set to zero) Output delays (seconds): 1.5 Internal delays (seconds): 3.4 Continuous-time state-space model. 

sys is a second-order continuous-time ss model with internal delay 3.4 s and output delay 1.5 s.

Use the pade function to compute a third-order approximation of the internal delay and a first-order approximation of the output delay.

P13 = pade(sys,inf,1,3); size(P13) 
State-space model with 1 outputs, 1 inputs, and 6 states. 

The three input arguments following sys specify the approximation orders of any input, output, and internal delays of sys, respectively. inf specifies that a delay is not to be approximated. The approximation orders for the output and internal delays are one and three respectively.

Approximating the time delays with pade absorbs delays into the dynamics, adding as many states to the model as orders in the approximation. Thus, P13 is a sixth-order model with no delays.

For comparison, approximate only the internal delay of sys, leaving the output delay intact.

P3 = pade(sys,inf,inf,3); size(P3) 
State-space model with 1 outputs, 1 inputs, and 5 states. 
P3.OutputDelay 
ans = 1.5000 
P3.InternalDelay 
ans = Empty matrix: 0-by-1 

P3 retains the output delay, but the internal delay is approximated and absorbed into the state-space matrices, resulting in a fifth-order model without internal delays.

Compare the frequency response of the exact and approximated systems sys, P13, P3.

h = bodeoptions; h.PhaseMatching = 'on'; bode(sys,'b-',P13,'r-.',P3,'k--',h,{.01,10}); legend('sys','approximated output and internal delays','approximated internal delay only',... 'location','SouthWest') 

Notice that approximating the internal delay loses the gain ripple displayed in the exact system.

### Convert Time Delay in Discrete-Time Model to Factors of 1/z

This example shows how to convert a time delay in a discrete-time model to factors of 1/_z_.

In a discrete-time model, a time delay of one sampling interval is equivalent to a factor of 1/_z_ (a pole at z = 0) in the model. Therefore, time delays stored in the InputDelay, OutputDelay, or ioDelay properties of a discrete-time model can be rewritten in the model dynamics by rewriting them as poles at z = 0. However, the additional poles increase the order of the system. Particularly for large time delays, this can yield systems of very high order, leading to long computation times or numerical inaccuracies.

To illustrate how to eliminate time delays in a discrete-time closed-loop model, and to observe the effects of doing so, create the following closed-loop system:

G is a first-order discrete-time system with an input delay, and C is a PI controller.

G = ss(0.9,0.125,0.08,0,'Ts',0.01,'InputDelay',7); C = pid(6,90,0,0,'Ts',0.01); T = feedback(C*G,1); 

Closing the feedback loop on a plant with input delays gives rise to internal delays in the closed-loop system. Examine the order and internal delay of T.

order(T) 
ans = 2 
T.InternalDelay 
ans = 7 

T is a second-order state-space model. One state is contributed by the first-order plant, and the other by the one pole of the PI controller. The delays do not increase the order of T. Instead, they are represented as an internal delay of seven time steps.

Replace the internal delay by .

Tnd = absorbDelay(T); 

This command converts the internal delay to seven poles at z = 0. To confirm this, examine the order and internal delay of Tnd.

order(Tnd) 
ans = 9 
Tnd.InternalDelay 
ans = Empty matrix: 0-by-1 

Tnd has no internal delay, but it is a ninth-order model, due to the seven extra poles introduced by absorbing the seven-unit delay into the model dynamics.

Despite this difference in representation, the responses of Tnd exactly match those of T.

stepplot(T,Tnd,'r--') legend('T','Tnd') 

bodeplot(T,Tnd,'r--') legend('T','Tnd') 

### Fractional Time-Delay Approximation in Discrete-Time Model

Use the thiran command to approximate a time delay that is a fractional multiple of the sample time as a Thiran all-pass filter.

For a time delay of tau and a sample time of Ts, the syntax thiran(tau,Ts) creates a discrete-time transfer function that is the product of two terms:

• A term representing the integer portion of the time delay as a pure line delay, (1/z)N, where N = ceil(tau/Ts).

• A term approximating the fractional portion of the time delay (tau - NTs) as a Thiran all-pass filter.

Discretizing a Padé approximation does not guarantee good phase matching between the continuous-time delay and its discrete approximation. Using thiran to generate a discrete-time approximation of a continuous-time delay can yield much better phase matching. For example, the following figure shows the phase delay of a 10.2-second time delay discretized with a sample time of 1 s, approximated in three ways:

• a first-order Padé approximation, discretized using the tustin method of c2d

• an 11th-order Padé approximation, discretized using the tustin method of c2d

• an 11th-order Thiran filter

The Thiran filter yields the closest approximation of the 10.2-second delay.

See the thiran reference page for more information about Thiran filters.

### Frequency Response Data (FRD) Model with Time Delay

This example shows that absorbing time delays into frequency response data can cause undesirable phase wrapping at high frequencies.

When you collect frequency response data for a system that includes time delays, you can absorb the time delay into the frequency response as a phase shift. Alternatively, if you are able to separate time delays from your measured frequency response, you can represent the delays using the InputDelay, OutputDelay, or ioDelay properties of the frd model object. The latter approach can give better numerical results, as this example illustrates.

The frd model fsys includes a transport delay of 2 s. Load the model into the MATLAB® workspace and inspect the time delay.

load(fullfile(matlabroot,'examples','control','frddelayexample.mat'),'fsys') fsys.ioDelay 
ans = 2 

A Bode plot of fsys shows the effect of the transport delay, causing the accumulation of phase as frequency increases.

bodeplot(fsys) 

The absorbDelay command absorbs all time delays directly into the frequency response, resulting in an frd model with ioDelay = 0.

fsys2 = absorbDelay(fsys); fsys2.ioDelay 
ans = 0 

Comparing the two ways of representing the delay shows that absorbing the delay into the frequency response causes phase-wrapping.

bode(fsys,fsys2) 

Phase wrapping can introduce numerical inaccuracy at high frequencies or where the frequency grid is sparse. For that reason, if your system takes the form , you might get better results by measuring frequency response data for G(s) and using InputDelay, OutputDelay, or ioDelay to model the time delay .

### Internal Delays

Using the InputDelay, OutputDelay, and ioDelay properties, you can model simple processes with transport delays. However, these properties cannot model more complex situations, such as feedback loops with delays. In addition to the InputDelay and OutputDelay properties, state-space (ss) models have an InternalDelay property. This property lets you model the interconnection of systems with input, output, or transport delays, including feedback loops with delays. You can use InternalDelay property to accurately model and analyze arbitrary linear systems with delays. Internal delays can arise from the following:

• Concatenating state-space models with input and output delays

• Feeding back a delayed signal

• Converting MIMO tf or zpk models with transport delays to state-space form

Using internal time delays, you can do the following:

• In continuous time, generate approximate-free time and frequency simulations, because delays do not have to be replaced by a Padé approximation. In continuous time, this allows for more accurate analysis of systems with long delays.

• In discrete time, keep delays separate from other system dynamics, because delays are not replaced with poles at z = 0, which boosts efficiency of time and frequency simulations for discrete-time systems with long delays.

• Use most Control System Toolbox™ functions.

• Test advanced control strategies for delayed systems. For example, you can implement and test an accurate model of a Smith predictor. See the example Control of Processes with Long Dead Time: The Smith Predictor Control of Processes with Long Dead Time: The Smith Predictor .

#### Why Internal Delays Are Necessary

This example illustrates why input, output, and transport delays not enough to model all types of delays that can arise in dynamic systems. Consider the simple feedback loop with a 2 s. delay:

The closed-loop transfer function is

$\frac{{e}^{-2s}}{s+2+{e}^{-2s}}$

The delay term in the numerator can be represented as an output delay. However, the delay term in the denominator cannot. In order to model the effect of the delay on the feedback loop, the InternalDelay property is needed to keep track of internal coupling between delays and ordinary dynamics.

Typically, you do not create state-space models with internal delays directly, by specifying the A, B, C, and D matrices together with a set of internal delays. Rather, such models arise when you interconnect models having delays. There is no limitation on how many delays are involved and how the models are connected. For an example of creating an internal delay by closing a feedback loop, see Closing Feedback Loops with Time Delays.

#### Behavior of Models With Internal Delays

When you work with models having internal delays, be aware of the following behavior:

• When a model interconnection gives rise to internal delays, the software returns an ss model regardless of the interconnected model types. This occurs because only ss supports internal delays.

• The software fully supports feedback loops. You can wrap a feedback loop around any system with delays.

• When displaying the A, B, C, and D matrices, the software sets all delays to zero (creating a zero-order Padé approximation). This approximation occurs for the display only, and not for calculations using the model.

For some systems, setting delays to zero creates singular algebraic loops, which result in either improper or ill-defined, zero-delay approximations. For these systems:

• Entering sys returns only sizes for the matrices of a system named sys.

• Entering sys.a produces an error.

The limited display and the error do not imply a problem with the model sys itself.

#### Inside Time Delay Models

State-space objects use generalized state-space equations to keep track of internal delays. Conceptually, such models consist of two interconnected parts:

• An ordinary state-space model H(s) with an augmented I/O set

• A bank of internal delays.

The corresponding state-space equations are:

$\begin{array}{l}\stackrel{˙}{x}=Ax\left(t\right)+{B}_{1}u\left(t\right)+{B}_{2}w\left(t\right)\\ y\left(t\right)={C}_{1}x\left(t\right)+{D}_{11}u\left(t\right)+{D}_{12}w\left(t\right)\\ z\left(t\right)={C}_{2}x\left(t\right)+{D}_{21}u\left(t\right)+{D}_{22}w\left(t\right)\\ {w}_{j}\left(t\right)=z\left(t-{\tau }_{j}\right),\text{ }j=1,...,N\end{array}$

You need not bother with this internal representation to use the tools. If, however, you want to extract H or the matrices A, B1, B2, ... , you can use getDelayModel, For the example:

P = 5*exp(-3.4*s)/(s+1); C = 0.1 * (1 + 1/(5*s)); T = feedback(ss(P*C),1); [H,tau] = getDelayModel(T,'lft'); size(H)

Note that H is a two-input, two-output model whereas T is SISO. The inverse operation (combining H and tau to construct T) is performed by setDelayModel.

See [1], [2] for details.

#### Functions That Support Internal Time Delays

The following commands support internal delays for both continuous- and discrete-time systems:

Limitations on Functions that Support Internal Time Delays.  The following commands support internal delays for both continuous- and discrete-time systems and have certain limitations:

• allmargin, margin—Uses interpolation, therefore these commands are only as precise as the fineness of the specified grid.

• pole, zero—Returns poles and zeros of the system with all delays set to zero.

• ssdata, get—If an SS model has internal delays, these commands return the A, B, C, and D matrices of the system with all internal delays set to zero. Use getDelayModel to access the internal state-space representation of models with internal delays.

#### Functions That Do Not Support Internal Time Delays

The following commands do not support internal time delays:

#### References

[1] P. Gahinet and L.F. Shampine, "Software for Modeling and Analysis of Linear Systems with Delays," Proc. American Control Conf., Boston, 2004, pp. 5600-5605

[2] L.F. Shampine and P. Gahinet, Delay-differential-algebraic Equations in Control Theory, Applied Numerical Mathematics, 56 (2006), pp. 574-588