• No results found

Using M ATLAB to discretise systems

In document Advanced Control using MATLAB (Page 85-89)

From differential to difference equations

2.8 Solving the vector differential equation

2.8.2 Using M ATLAB to discretise systems

but for ǫ6= 0, then the Jordan form drastically changes to the diagonal matrix

 1 +√ǫ 0 0 1−√ǫ



In summary, for serious numerical calculation we should use the matrix exponential function expm. Remember not to confuse finding the exponent of a matrix,expm, with the MATLAB func-tionexpwhich simply finds the exponent of all individual elements in the matrix.

2.8.2 Using M

ATLAB

to discretise systems

All of the complications of discretising continuous systems to their discrete equivalent can be circumvented by using the MATLAB command c2dwhich is short for continuous to discrete.

Here we need only to pass the continuous system of interest, and the sampling time. As an example we can verify the conversion of the double integrator system shown on page2.8.

G = tf(1,[1 0 0]) %Continuous system G(s) = 1/s2in transfer function form Gc = ss(G) %Convert to continuous state-space

3 Gd = c2d(Gc,2) %Convert to discrete state-space with a sample time of T = 2 a =

x1 x2

x1 1 0

8 x2 2 1

b = u1 x1 2

13 x2 2

c =

x1 x2

y1 0 1

18

d = u1 y1 0

23 Sampling time (seconds): 2 Discrete-time state-space model.

Unlike in the analytical case presented previously, here we must specify a numerical value for the sample time, T .

Example: Discretising an underdamped second order system with a sample time of T = 3 following Eqn.2.80using MATLABto compute the matrix exponential of the augmented system.

1 [A,B,C,D] = ord2(3, 0.5); % generate a second order system

Ts = 3.0; % sample time

[na,nb] = size(B);

X = expm([A,B; zeros(nb,na+nb)]*Ts); %matrix exponential

6 Phi = X(1:na,1:na); %Pull off blocks to obtain Φ & ∆.

Del = X(1:na,na+1:end);

We can use MATLABto numerically verify the expression found for the discretised double inte-grator on page60at a specific sample time, say T = 4.

The double integrator in input/output form is

G(s) = 1 s2

which in packed state space notation is

 A B

We can verify this usingc2din MATLABas demonstrated opposite.

>>Gc = tf(1,[1,0,0])%G(s) = 1/s2

>>Gc_ss = ss(Gc);%state space

3 >>dt = 4; %sample time

Once the two system matrices Φ and ∆ are known, solving the difference equation for further values of k just requires the repeated application of Eqn.2.38. Starting from known initial condi-tions x0, the state vector x at each sample instant is calculated thus

x1= Φx0+ ∆u0

The MATLABfunctionltitrwhich stands for linear time invariant time response or the more generaldlsimwill solve the general linear discrete time model as in2.81. In special cases such as step or impulse tests, you can usedstep, ordimpulse.

Problem 2.4 1. EvaluateAn where

A=

 1 1 1 0



for different values of n. What is so special about the elements of An? (Hint: find the eigenvalues ofA.)

2. What is the determinant ofAn?

3. Write a couple ofMATLABm-functions to convert between theA,B,CandDform and the packed matrix form.

4. Complete the state-space to transfer function conversion analytically started in §2.7.3, Eqn.2.53. Compare your answer with usingss2tf.

A submarine example A third-order model of a submarine from [62, p416 Ex9.17] is

˙x =

where the state vector x is defined as

x=

θ dt α 

The state θ is the inclination of the submarine and α is the angle of attack above the horizontal.

The scalar manipulated variable u is the deflection of the stern plane. We will assume that of the three states, we can only actually measure two of them, θ and α, thus

C=

 1 0 0

0 0 1



In this example, we consider just three states x, one manipulated variable u, and two outputs, y.

The stability of the open-loop system is given by the eigenvalues of A. In MATLAB, the command eig(A)returns

showing that all eigenvalues have negative real parts, indicating that the submarine is oscillatory, though stable. In addition, the complex conjugate pair indicates that there will be some oscillation to the response of a step change in stern plane.

We can simulate the response of our submarine system (Eqn2.82) to a step change in stern plane movement, u, using the stepcommand. The smooth plot in Fig.2.22 shows the result of this continuous simulation, while the ‘staired’ plot shows the result of the discrete simulation using a sample time of T = 5. We see two curves corresponding to the two outputs.

Listing 2.4: Submarine simulation

A = [0,1,0;-0.0071,-0.111,0.12;0,0.07,-0.3]; B = [0,-0.095,0.072]';

C = [1 0 0;0 0 1];

3

Gc = ss(A,B,C,0) %Continuous plant, Gc(s) dt = 5; %sample time T = 5 (rather coarse) Gd = c2d(Gc,dt) %Create discrete model

8

step(Gc,Gd); %Do step response and see Fig.2.22.

Figure 2.22: Comparing the continuous and discrete step responses of the subma-rine model.

−15

−10

−5 0

x 1

0 20 40 60 80

0 0.1 0.2 0.3 0.4

time

x 2

Gc Gd

Fig.2.22affirms that the openloop process response is stable, supporting the eigenvalue analysis.

Notice how thestepcommand automatically selected appropriate time scales for the simulation.

How did it do this?

The steady state of the system given that u = 1 is, using Eqn2.61,

1 >> u = 1;

>>xss = -A\B*u; %Steady-states given by xss=−ABu.

>>yss = C*xss

yss = %See steady-state outputs in Fig.2.22.

-9.3239

6 0.2400

which corresponds to the final values given in Fig.2.22.

We can verify the results from thec2droutine using Eqns2.71and2.72, or in this case since A is invertible, Eqn.2.73.

>> Phi = expm(A*5.0); %Don’t forget the ‘m’ in expmatrix()

>> Delta = (Phi-eye(size(A)))*(A\B) %∆=

eAT− I A−1B

This script should give the same Φ and ∆ matrices as thec2dfunction since A is non-singular.

Given a discrete or continuous model, we can compute the response to an arbitrary input se-quence usinglsim:

t = [0:dt:100]'; % time vector U = sin(t/20); %Arbitrary input U (t)

3

lsim(Gc,U,t) % continuous system simulation lsim(Gd,U) % discrete system simulation

We could explicitly demand a first-order-hold as opposed to the default zeroth order by setting the options forc2d.

optn = c2dOptions('Method','foh') %Ask for a first-order hold

Gd2 = c2d(Gc,5,optn) %Compare the step response of this with a zeroth-order hold

Problem 2.5 By using a suitable state transformation, convert the following openloop system

˙x1= x2

˙x2= 10− 2x2+ u where the inputuis the following function of the referencer,

u = 10 + 9r−

9 4  x

into a closed loop form suitable for simulation in MATLAB using the lsim function. (ie: ˙x = Ax+ Br) Write down the relevantMATLABcode segment. (Note: In practice, it is advisable to uselsimwhenever possible for speed reasons.)

Problem 2.6 1. Write an m-file that returns the state space system in packed matrix form that connects the two dynamic systemsG1andG2together,

Gall= G1· G2

Assume thatG1andG2are already given in packed matrix form.

2. (Problem from [182, p126]) Show that the packed matrix form of theinverse of Eqn.2.41, u= G−1y, is

G−1=

 A− BD−1C −BD−1

D−1C D−1



In document Advanced Control using MATLAB (Page 85-89)