0% found this document useful (0 votes)
4 views

lab1_control_systems

The document outlines a MATLAB exercise for EE 371 at IIT Patna, focusing on control systems and instrumentation. It includes objectives, MATLAB commands for solving equations, plotting time responses, and performing frequency response analysis. Additionally, it covers symbolic calculations, state-space modeling, and provides various problems for practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

lab1_control_systems

The document outlines a MATLAB exercise for EE 371 at IIT Patna, focusing on control systems and instrumentation. It includes objectives, MATLAB commands for solving equations, plotting time responses, and performing frequency response analysis. Additionally, it covers symbolic calculations, state-space modeling, and provides various problems for practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EE 371 Experiment 1 IIT Patna

Matlab Exercise
EE371 Instrumentation & Control Laboratory Duration: 3 hours

Objective
Familiarisation with MATLAB software from control system engineer point of view.

Computer Aided Learning 1


   
1 1 8s
3s + 6 + I1 (s) + − 4 − I2 (s) = 2 (1)
5s 5s s + 92
   
1 1
−4− I1 (s) + 6s + 11 + I2 (s) = 0 (2)
5s 5s

[8s/(s2 + 92 )][4 + (1/5s)]


I2 (s) = (3)
[3s + 6 + (1/5s)][6s + 11 + (1/5s)] − [4 + (1/5s)]2
1. Write MATLAB commands to evaluate and simplify Equations (3), which gives I2 (s).

syms s
t1 =8* s /( s ^2+81)
t2 =4+1/5/ s
t3 =3* s +6+1/5/ s
t4 =6* s +11+1/5/ s
t5 = t2
t112 = t1 * t2 /( t3 * t4 - t5 ^2)
simplify ( t112 )
2. Write MATLAB commands to solve Equations (1) and (2) to obtain the I (s) of equation (3)
syms s
m =[3* s +6+1/5/ s -4 -1/5/ s ; -4 -1/5/ s 6* s +11+1/5/ s ]
e =[8* s /( s ^2+81) ; 0]
I = inv ( m ) * e
   
8 1 1 12
4s + + V1 (s) + − 4s − V2 (s) = (4)
15 2s 5 s
   
1 11 −1
4s + V1 (s) + 4s + V2 (s) = 2 (5)
5 30 s +1
3. Write MATLAB commands to obtain V1 (s) and V2 (s) from Equations (4) and (5).
m =[4* s +8/15+1/2/ s -4*s -1/5 ; -4*s -1/5 4* s +11/30]
i =[12/ s ; -1/( s ^2+1) ]
V = inv ( m ) * i

Computer Aided Learning 2


Two methods for plotting the time response are presented here. The MATLAB commands are

step (Laplace transfer function)


ezplot (symbolic time function, [min time max time]

We will also use the command the

conv([coeff1]), [coeff2]).

1
For the step command the transfer function coefficients are input in either vector form, followed
by the “tf” command, or the “zpk” form may be used to define the transfer function. For example,
suppose we want the step response for a system with transfer function
10(s + 1) 10(s + 1)
T (s) = 2
=
s + 5s + 6 (s + 2)(s + 3)
The step response using the vector form may be created by
num=[10 10]
den=[1 5 6]
t=tf(num, den)
step(t)
Another approach is to use the “zpk” command
t=zpk(-1, [-2 -3], 10)
step(t)
In either case, the computer, in effect, multiplies T (s) by 1/s to get the step response. That response
is then calculated and plotted.
Responses to other than a step response may be taken by employing a simple trick with the step
command. If you want the time response of some Y (s) = T (s)R(s) when R(s) is not 1/s, obtain
the step response of sY (s). The computer can be thought of as multiplying by 1/s, then cancelling
the s, resulting in the time response of Y (s).
Suppose we want the time response for the T (s) when
r(t) = 6 cos 4t
6s
R(s) = 2
s + 16
Then
10(s + 1) 6s
sY (s) = sT (s)R(s) = s 2
(s + 2)(s + 3) (s + 16)
To employ the step command in vector form, the “conv” (meaning convolve) command may be used
to multiply polynomial vectors
snum=60*conv([1 0 0], [1 1])
den1=conv([1 2], [1 3])
den=conv(den1, [1 0 16])
sy=tf(snum, den)
step(sy)
Prob 1. Use the step command to get the step response of the following:
5
(a) T (s) =
(s + 4)2
5(s + 6)
(b) T (s) =
(s + 1 + j4)(s + 1 − j4)
Prob 2. Use the step command to get the time response of the following:
32
(a) Y (s) =
(s + 2)2 (s2 + 16)
(b) Y (s) = T (s)R(s),
where
48
T (s) =
(s + 3)(s2 + s + 16)
r(t) = 3e−3t

2
If we want to solve

Y (s) = T (s)R(s)
10(s + 1)
Y (s) =
s2 + 5s + 6
r(t) = 6 cos 4t

we can use symbolic commands

sysm s t
T=10*(s+1)/(s^
2+5*s+6)
R=laplace(6*cos(4t))
Y=RT
y=ilaplace (Y)
ezplot (y, [0 10])

Suppose we want the ramp response and the ramp error response for
10
T (s) =
s2 + 3s + 10
Let Y (s) represent the Laplace transform of the ramp response while E(s) represents the Laplace
transform of the ramp error response obtained by using the step command

r1=zpk([], 0, 1)
numt=10
dent=[1 3 10]
t=tf(numt, dent)
sy=t*r1
step(sy)
te=1-t
se=te*r1
step(se)

Here step(sy) provides the ramp response of Y (s) while step(re) provides the ramp error response
of E(s).
Prob 3. Use MATLAB to get the step and ramp responses of
20
(a) T (s) =
s2
+ 5s + 25
9s + 20
(b) T (s) = 3
s + 6s2 + 20
Prob 4. Find the closed-loop step and ramp responses for the following open-loop transfer functions,
assuming unity feedback
20
G(s) =
s(s + 1)(s + 2)
Ans.

r1=zpk([], 0, 1)
ga=zpk([], [0, -1, -2], 20)
ta=ga/(1+ga)
r1=zpk([], 0, 1)
sya=ta*r1
step(sya)

3
Since the MATLAB software assumes that the gain K is positive, to obtain a negative gain
root locus, a negative sign must be introduced into the open-loop transfer function. For example, a
negative-gain root locus for a system can be obtained as follows:

gh=zpk([-3], [-1+j*2 -1-j*2 3 0], -3)

Or, by defining

n=[-3 -9]
d=[1 -1 -1 -15 0]
gh=tf(n,d)

and then using


rltool(gh)

Computer Aided Learning 3


0.1 Frequency Response analysis
This section shows how MATLAB can be used for frequency response analysis. The main frequency
response commands are as follows:
bode, nyquist, nichols, margin
We must first define the system transfer function as an object using the “tf” command. For
instance, consider the system
10
G(s) =
s + 10
g = tf (10, [1, 10])

0.1.1 Bode plot


To obtain a quick sketch of the Bode plot we use the “bode” command
bode(g)
Other forms of the bode command are:
bode(g, {wmin, wmax}) which draws the Bode plot for frequencies between wmin ans wmax in
radians per second, and
bode(g,w)
uses the user-supplied vector w of frequencies, in radians per second, at which the Bode response is
to be evaluated. We can use the ”logspace” to generate logarithmically spaced frequency vectors.
bode(g1, g2, g3, · · · , w)
plots the Bode response of several systems g1, g2, etc. on a single plot. The frequency vector w is
optional. We can also specify a color, line style, and marker for each system, as in
bode(g1, ‘opt’, g2, ‘opt’, g3, ‘opt’);
where ‘opt’ stands for plot options (type “hello plot” to find out about plotting options.) For
instance, to plot s/(s + 1) in red with double-dashed lines and 1/(s + 1) in magenta using the marker
x we enter
bode(tf([1, 0], [1, 1]), ‘r-’, tf([1], [1 1]), ‘mx’)
when invoked with left-hand arguments, such as the following,
[Mag, phase, W]=bode(g, · · · )
the magnitude and phase are returned as three-dimensional arrays. We can then convert them to
column vectors and plot them directly. The “subplot(m, n, p)” command splits the page in m rows
and n columns and put the current graph in the cell p. The “semilogx” command is used to create

4
semilog plots with a logarithmic x-axis.

g=tf(1, [1, 2, 2]);


w=logspace(-1, 2, 100); % creates a vector of size 100 from
10−1 to 102
[magn, phase] = bode(g, w)
mag = magn(:,:)’; ph = phase (:, :)’; % extract mag and
phase as column vector
subplot(2,1,1), semilogx (w, 20*log10(mag)), grid
subplot(2, 1, 2), semilogx (w, ph), grid

0.1.2 Nyquist plot


The Nyquist plot is obtained similarly using the “nyquist” command
g=tf(10, [1, 10]); nyquist(g), grid

0.1.3 Nicholas plot


The Nicholas plot, obtained using the nicholas command, has similar syntax to ‘bode’ and ‘nyquist’,
g=tf(10, [1, 10]); nicholas(g), grid
Moreover, we can add the M circles and N circles using the command
ngrid.
To obtain relative stability measures such as gain and phase margin, we use the “margin” com-
mand

g=zpk([], [0, -1, -1, -2], 1)


1
zero/pole/gain :
s(s + 1)(s + 2)
[Gm, Pm, Wcg, Wcp]=margin(g).

Without left-hand arguments, the Bode plot is drawn with the margins indicate on the plot
margin(g)

Computer Aided Learning 4


In this section, the following MATLAB commands are introduced:
den =[ coefficients ]
roots ( den )
tf ( num , den )
zpk [ num roots ] , [ den roots ] , gain k )
Suppose we want to use MATLAB to enter the following transfer function into the computer.

5(s + 6)
T (s) = (6)
s2 + 6s + 25

num =[5 30]


den =[1 6 25]
t = tf ( num , den )
The roots of a polynomial follow from
Roots ( den )
The other approach is to use the zero-pole-gain or zpk form. In that case, numerator and
denominator roots are placed in respective vectors and an empty vector [] denote lack of a root
when a constant is present, usually for the numerator. For the system above, we can create T (s) by
inputting the zero, poles, and gain k.

5
t = zpk ( -6 ,[ -3+ j *4 -3 - j *4] ,5)
4. Write MATLAB commands using the ”tf” command to create
8s
Ta (s) = (7)
s2 + 92
(2/9)(s + 1/20)
Tb (s) = (8)
s3 + (23/6)s2 + (259/90)s + 1/10

Computer Aided Learning 5


You should thoroughly understand the basis of laplace and inverse laplace transforming. To enhance
that understanding, and not to replace basic knowledge, MATLAB proveides the ability to carry
out tedious symbolic calculations. In this section, the following commands are covered:
syms
laplace ( time function )
ilaplace ( Laplace function )
If we want the inverse laplace transform of

(s + 8)
Y (s) = (9)
s2 + 6s + 36
then we can get y(t) by the commands
syms s t
Y =( s +8) /( s ^2+6* s +36)
y = ilaplace ( Y )
5. Use MATLAB to get the inverse laplace transforms of

(s + 13)
Y (s) = (10)
s(s2 + 4s + 13)
9
Y (s) = (11)
s(s + 3)2
6.Write commands to get the following laplace transforms.

y(t) = 1 + 4e−2t cos(3t) (12)

y(t) = 4 − 4e−6t (13)

Computer Aided Learning 6


To create a state-space model we use “ss” command with the following syntax:

SY S = SS(A, B, C, D)

For example, to define the following system:


   
1 2 −1
ẋ = x+ u
3 4 6
 
y = 0 −14 x + 8u

we enter the following commands

>> a = [1, 2; 3, 4]; b = [−1; 6]; c = [0, −14]; d = 8;


>> g = ss(a, b, c, d)

6
We convert from state-space to transfer function form using the “tf” command.

>> g s = tf (g)

We can then transfer back to state space form using the “ss” command

>> g ss = ss(g s)
Moreover the “tf” and “ss” commands create system objects that MATLAB commands can
interpret. If we want direct access to the object features such as numerator, denominator, and the
A, B, C, D matrices, we need to extract them. To extract the A, B, C, D matrices, we use the
“ssdata” command.
>> [a, b, c, d] = ssdata(g ss)
We can now use these matrices to determine stability, controllability and other system properties.
the system can be transformed to other forms using the “canon” and “ss2ss” commands. The
“canon” command has the following syntax:

>> sc = canon(sys,0 type0 )

where type is either ’companion’ or ’modal.’ The latter diagonalizes the system (also known as the
modal realization), an the former converts it to companion form (a variation of the phase variable
or dual-phase variable forms)

>>sd = canon(g ss,0 modal0 )


>>[sd] = canon(g ss,0 com0 )

The “ss2ss” command has the syntax


sys2 = ss2ss(sys1, P ) or [a2, b2, c2, d2] = ss2ss(a, b, c, d.P )
where P is the non-singular state transformation matrix. Note that our definition of P is different
from that of MATLAB.
Here is an example:
   
−2 1 1 1
ẋ = −3 0 0 x + 1 u
0 0 0 1
 
2 −2 1
y= x
0 −1 1

Use the transformation  


1 0 1
P = 0 1 3
0 0 4
>>p = [1, 0, 1; 0, 1, 3; 0, 0, 4];
>>a = [−2, 1, 1; −3, 0, 0; 0, 0, 0];
>>b = [1; 1; 1];
>>c = [2, −2, 1; 0, −1, 1]; d = [0; 0];
>>g = ss(a, b, c, d);
>>tf (g) % the transf er f unction of 2 − output system
To transform using the given P matrix we use “inv(p)” to get P −1 . We then extract the new
A, B, C, D matrices to verify our answers: [a2, b2, c2, d2] = ss2ss(a, b, c, d, inv(p))
Compare the foregoing answer with the ones from the formulas: Ā = P −1 AP, B̄ = P −1 B, C̄ =
CP

>>p\a ∗ p
>>p\b
>>c ∗ p

7
Computer Aided Learning 7
System stability can be obtained by looking at the poles or eigenvalues of the system. MATLAB
has two commands to find poles and zeros. They are “pole” and “tzero.” The “pole” command
returns the poles and “tzero” returns the so-called transmission zeros of the system. If the A matrix
is available, we can determine asymptotic stability by finding the eigenvalues of A using the “eig”
command. For the system defined by
   
−2 1 1 1
ẋ = −3 0 0 x + 1 u
0 0 0 1
 
y = 2 −2 1 x

we get

>>pole(g)
>>eig(a)
>>tzero(g)

The system controllability and observability can be determined by examining the corresponding
matrices. MATLAB has “ctrb” abd “obsv” to obtain them. For the above-defined system, we get,

>>CO = ctrb(a, b)
>>OB = obsv(a, c)

We can then use “det” and “rank” to find the determinant and rank of these matrices.
Q: Determine the stability, controllability and observability of
a = [−1, −2, 0; 1, 2, 0; −2, −1, −3]; b = [1; 0; 0]; c = [1, 0, 1], d = 0

Solution of state equation can be obtained using the “lsim” and ‘initial” commands.

LSIM (SY S, U, T, X0)

plots the response of a state-space system with input U (T ) and initial state X0 . The time sequence
T must be defined as a vector, and the input must be of the same size as T . Here is an example:

g = tf (1, [1, 1.4, 1])


gs = ss(g)
t = linspace(0, pi, 100); u = sin(2 ∗ t); x0 = [−2, 3];
lsim(gs, u, t, x0)

We invoke with left hand arguments,

[Y, T, X] = LSIM (SY S, U, T, X0)

returns the output Y , the state vector X and time vector T used fo simulation, and no plot is drawn
on the screen.
[y, t, x] = lsim(gs, u, t, x0);
subplot(211), plot(t, x(:, 1)), subplot(212), plot(t, x(:, 2))

The “initial” command is used to solve for the zero input response (ZIR) of the state-space
system:ẋ = Ax subject to initial conditions:
IN IT IAL(SY S, X0, T, T F ) simulates and plots the time response from t = 0 to the final time
t − TF.
IN IT IAL(SY S, X0, T ) specifies a time vector T to be used for simulation
[Y, T, X] = IN IT IAL(SY S, X0, . . . ) returns the left hand side vectors but does not plot.

initial(gs, [−1, 2])

You might also like