Anda di halaman 1dari 96

1.

0 MATRICS,VECTORS AND SCALARS


In MATLAB a scalar is a variable with one row and one column. Scalars are the simple variables that we use and manipulate in simple algebric

EXAMPLE 1
>>a=[0:5] a= 0 1 2 3 4 5

Example 2
>>b=[10:20] b= 10 11 19 20 12 13 14 15 16 17 18

1.1 Creating scalar


To create a scalar simply introduce it on the left hand side of an equal sign. Example >> x=1; >> y=2; >> Z=x+y Z= 3
4

1.2

Demostration on scalar addition, subtraction, multiplication and division

Example1 >> u = 5; >> v = 3; >> w = u+v; >> x = u-v; >> y = u*v; >> z = u/v; >> w,x,y,z

w= X= 2 8

y= 15

z= 1.6667

Example 3
>>x=3; >> y=4; >> z=x*y^2 z= 48

1.3 Creating Vectors


In Matlab a vector is a matrix with either one row or one column. Example : 1. To create a row vector of length 5, filled with 5, >>x=ones(1,5) x=

1
8

2. To create a column vector of length 5,filled with zeros. >>y=zeros(5,1) y= 0 0 0 0 0


9

Matrix
Matrix is a set of number arranged in a rectangular grid of rows and columns. Example:

>>A=[3 5] A= 3 5
10

The size of matrix is specified by the numbers of rows and column. If the matrix is same than it is called as square matrix. 3. Semicolon is used to separate the rows.

>>C=[1 2 3 ; 4 5 6 ; 7 8 9 ] C= 123 456 789


11

4. If we want to extract the third column of matrix c, then we write C(:,3) ans = 3 6 9
12

Simple Graphs

Example 1 To plot a simple graph


>>x=[1;2;3;4;5]; >> y=[0;.25;3;1.5;2]; >> plot(x,y)
13

14

Example 2
>> x=0:5; >> y=sinh(x); >> plot(x,y) >> xlabel('Time') >> ylabel('Sinh') >> title('Sinehyperbolic')

15

16

Example 3
>> x=0:pi/100:2*pi; >> y=sin(x); >> plot(x,y) >> xlabel('x=0:2\pi') >> ylabel('Sin of x') >> title('Plot of the sin function')

17

18

Multiple Graphs >> t=0:pi/100:2*pi; >> y1=sin(t); >> y2=sin(t+pi/2); >> plot(t,y1,t,y2) >> grid on

19

20

Exercise

For the graph above include the labels. For X-axis, Y-axis and title.

21

Solutions
>> t=0:pi/100:2*pi; >> y1=sin(t); >> y2=sin(t+pi/2); >> plot(t,y1,t,y2) >> grid on >> xlabel('x=0:2\pi') >> ylabel=('y1=sin(t)')

>> title('Multiple graphs')


22

23

Root and convolution function


1. For the given polynomial, find the roots a) P(s)=s2+6s+8 b) P(s)=s2-8s+5 c) P(s)=s2+2s-15

24

Solution
a=
>> p=[1 6 8]; >> r=roots(p) r= -4 -2

25

b=
>>p=[1 -8 15]; >> r=roots(p) r= 5 3

26

C=
>>p=[1 2 -15]; >>r =roots(p) -5 3

27

Pole and Zero Find the zero and pole for the given transfer function T(s)= (s+2)
( s2+2s+1)

28

Solution
>>sys=tf([1 2],[1 2 1]); >> p=pole(sys); >> z=zero(sys); >> p,z

p= -1 -1

z= -2
29

Pole - zero map >>sys=tf([1 2],[1 2 1]); >> p=pole(sys); >> z=zero(sys); >> p,z >> pzmap(sys)

30

Pole-Z ero M ap 1 0.8 0.6 0.4 Im a g ina r y A xis 0.2 0 -0.2 -0.4

pole

zero

-0.6 -0.8 -1 -2

-1.8

-1.6

-1.4

-1.2

-1 R eal Ax is

-0.8

-0.6

-0.4

-0.2

0
31

Excersice
Show the pole and zero for the given transfer function in s- plane. T(s)= 6s2+1 ( s3 +3s2 + 3s +1)

32

S- plane
>>sys=tf([6 0 1],[1 3 3 1]); >> p=pole(sys); >> z=zero(sys); >> p,z p= -1.0000 -1.0000 + 0.0000i -1.0000 - 0.0000i

z= 0 + 0.4082i 0 - 0.4082i >> pzmap(sys)


33

Pole-Z ero M ap 0.5 0.4 0.3 0.2 Im agina ry Axis 0.1 0 -0.1 -0.2 -0.3 -0.4 -0.5 -1.4

-1.2

-1

-0.8

-0.6

-0.4

-0.2

R eal A x is

34

To construct transfer function


1. Given numerator =(s+5) denominator =(s2 3s + 6)

>> num=[1 5]; >> den=[1 -3 6]; >> sys=tf(num,den) Transfer function: s+5 ------------s^2 - 3 s + 6
35

Example 2
2. Given numerator =(-s+6) denominator =(s2 + 4s + 4)

>>num=[-1 6]; >> den=[1 4 4]; >> sys=tf(num,den) Transfer function: -s+6 ------------s^2 + 4 s + 4
36

Multiply the polynomials using conv functions


1. Expand the following polynomials. a) N(s) = (3s2+2s+1)(s+4) b) Find the value of N(s) when s= -5 >>p=[3 2 1]; >> q=[1 4]; >> n=conv(p,q)

n= 3 14 9 4
37

To evaluate the value of N(s) when s=-5


>>value=polyval(n,-5) >>value = -66

38

Exersice
1. For the above polynomials, find the value of N(s) when s = 2 and s = -7 2. Expand the following N(s)= (-3s3+5s+6)(5s2-7s+1)

39

Operator Addition (+)


Given G1(s) = 10 / s2+2s+5 and G2(s) = 1 / s+1 Using Matlab, perform the total of G1(s) + G2(s) .

>>num1=[10];den1=[1 2 5]; >> sys1=tf(num1,den1) Transfer function: 10 ------------s^2 + 2 s + 5

40

>>num2=[1];den2=[1 1]; >> sys2=tf(num2,den2) Transfer function: 1 ----s+1 >> sys=(sys1+sys2) Transfer function: s^2 + 12 s + 15 --------------------s^3 + 3 s^2 + 7 s + 5

41

Exersice
Add the following transfer functions by using Matlab. Given G1(s) = s2 +2s / 2s2 - 4s+6 G2(s) = 2 / s+4

42

Series Connection Let the following transfer function be G1(s) = 1 / 500s2 G2(s) = s+ 1 / s+2 When both are in cascade: Sys1 Sys2 (G2) Y(s) U(s)
( G1)

T(s) = Y(s) /U(s)


43

Solution
>> numg1=[1];deng1=[500 0 0]; >> sys1=tf(numg1,deng1); >> numg2=[1 1];deng2=[1 2]; >> sys2=tf(numg2,deng2); >> sys=series(sys1,sys2)

Transfer function: s+1 ----------------- 500 s^3 + 1000 s^2

44

Parallel Connection
The following transfer functions is connected in parallel. Show the transfer function. numg1=[2 1];deng1=[1 2]; >> sys1=tf(numg1,deng1); >> numg2=[1 3];deng2=[500 0 4]; >> sys2=tf(numg2,deng2); >> sys=parallel(sys1,sys2) Transfer function: 1000 s^3 + 501 s^2 + 13 s + 10 ----------------------------- 500 s^3 + 1000 s^2 + 4 s + 8
45

Feedback For the following block diagram, find the transfer function.
R(s)
G(s)=1/ 500s2

Y(s)

H(s)=(s+1) /(s+2)

46

Solution
>>numg=[1];deng=[500 0 0]; >> sysg=tf(numg,deng); >> numh=[1 1];denh=[1 2]; >> sysh=tf(numh,denh); >> sys=feedback(sysg,sysh) Transfer function: s+2 -------------------------500 s^3 + 1000 s^2 + s + 1

47

Exercise
1. For the given bolck diagram , find the transfer
R(s)
2/(s2 + 1) (S + 1)/ (s2 + 10)

y(s)

((s2 -3) / (2s3 + 2s +1)

48

Solution
>> numg=[1];deng=[500 0 0]; >> sysg=tf(numg,deng); >> numh=[1 1];denh=[1 2]; >> sysh=tf(numh,denh); >> sys=feedback(sysg,sysh) Transfer function: s+2 -------------------------500 s^3 + 1000 s^2 + s + 1
49

2.

For the given block diagram, find the transfer function.

R(s)
G1 =2 / (s2 + 1) G2 =(s +1)/ (s2 +10)

Y(s)

H = ( s2 3) / (2s3+2s+1)

50

Solution
>> numg1=[2];deng1=[1 0 1];sys1=tf(numg1,deng1); >> numg2=[1 1];deng2=[1 0 10];sys2=tf(numg2,deng2); >> sysg=series(sys1,sys2); >> numh1=[1 0 -3];denh1=[2 0 2 1];sysh=tf(numh1,denh1); >> sys=feedback(sysg,sysh) Transfer function: 4 s^4 + 4 s^3 + 4 s^2 + 6 s + 2 ------------------------------------------------2 s^7 + 24 s^5 + s^4 + 44 s^3 + 13 s^2 + 14 s + 4
51

Multiloop reduction
For the given multi loop feedback system, compute the closed loop transfer function. Use series, parallel and feedback functions if necessary.
H2 R(s) G1 G2 G3 G4 Y(s)

H1

H3
52

53

Five steps procedure is followed:

Step 1 = Input the system transfer function into Matlab Step 2 = Move H2 behind G4 Step 3 = Eliminate G3G4 H1loop. Step 4 = Eliminate the loop containing H2. Step 5 = Eliminate the remaining loop and calculate T(s)
54

Solution
>>ng1=[1];dg1=[1 10];sysg1=tf(ng1,dg1); >> ng2=[1];dg2=[1 1];sysg2=tf(ng2,dg2); >> ng3=[1 0 1];dg3=[1 4 4];sysg3=tf(ng3,dg3); >> ng4=[1 1];dg4=[1 6];sysg4=tf(ng4,dg4); >> nh1=[1 1];dh1=[1 2];sysh1=tf(nh1,dh1); >> nh2=[2];dh2=[1];sysh2=tf(nh2,dh2); >> nh3=[1];dh3=[1];sysh3=tf(nh3,dh3); >> sys1=sysh2/sysg4; >> sys2=series(sysg3,sysg4); >> sys3=feedback(sys2,sysh1,+1); >> sys4=series(sysg2,sysg3); >> sys5=feedback(sys4,sys1); >> sys6=series(sysg1,sys5); >> sys=feedback(sys6,[1])

55

Transfer function: s^5 +4 s^4+ 6s^3 + 6s^2 + 5s + 2 ---------------------------------------------------------------------------------- 12S^6 +20s^5 + 1066 s^4 + 2517s^3 +3128s^2 + 2196s + 712

56

Root locus
The function covered are rlocus, rlocfind and residue. rlocus and rlocfind :- used to obtain the root locus plots. residue: - used for partial fraction expansion of rotational functions.

57

Closed loop transfer function


Consider the closed loop transfer function T(s) = K(s+1)(s+3) / s(s+2)(s+3)+K(s+1) Now use rlocus function to generate root locus plots. The general form of characteristics is 1 + K G(S) = 0 Step 1: - Obtain the characeteristic equation in the form of 1 + K G(S) = 0 , where K is the parameter of interest. Step 2:- Use the rlocus function to generate the plots. [r,k]=rlocus(sys). r= complex and loot location K= gain vector
58

Generating a root locus plot


Let >> p=[1 1]; q=[1 5 6 0]; >>sys=tf(p,q); >>rlocus(sys)

59

R oot Loc us 8

2 Im a g ina r y A xis

-2

-4

-6

-8 -3.5

-3

-2.5

-2

-1.5 R eal Ax is

-1

-0.5

0.5
60

>>p=[1 1];q=[1 5 6 0]; >> sys=tf(p,q); >> rlocus(sys); >> rlocfind(sys) Select a point in the graphics window selected_point = -2.4716 + 0.0248i

ans = 0.4195 graph This value will be vary. Depends on the selected value at

61

Find the step response and also find the settling Time (Ts), Peak response(Ps) Let the value of K is 20.5775 >>k=20.5775;num=k*[1 4 3]; >>den=[1 5 6+k k]; >> sys=tf(num,den); >> step(sys)
62

Ste p R es pons e 4.5 4 3.5 3 2.5 2 1.5 1 0.5 0 Sy s tem : s y s T im e (s ec ): 0.4 23 Am plitude: 4.49 Sy s tem : s y s T im e (s ec ): 1.8 Am plitude: 3.03

Amp litu d e

0.5

1.5 T im e (s ec )

2.5

63

Exersice

Compute step response for second order system when

64

Bode Diagram Bode The bode function is used to generate a bode diagram. Logspace The logspace function generate a logarithmatically spaced vector of frequencies utilized by the bode function. The magnitude and phase characteristics are placed in the workspace through the variables mag and phase.
65

Consider the transfer function given below: Plot the bode diagram.

5  0.1s 1 0.6 1 2 s (1  0.5s ) 1  s 2 s 50 50


=

66

Solution
% Bode plot >> % >> num=5*[0.1 1]; >> f1=[1 0];f2=[0.5 1]; f3=[1/2500 0.6/50 1]; >> den=conv(f1,conv(f2,f3)); >> % >> sys=tf(num,den); >> bode(sys) >>
67

Excersice
For the following transfer function sketch the Bode plots, then verify with Matlab 1 a) G(s) = ( s  1)( s  10)

68

( s  10) b) G(s) = 2 ( s  2s  50)

69

70

Gain Margin and Phase margin can be determined from both Nyquist and Bode diagram. The gain margin is a measure of how much the system gain would have to be increased for the GH(jw) locus to pass through the (-1,0) point, thus resurlting in an unstable system. Example >> num=[0.5];den=[1 2 1 0.5]; >> sys=tf(num,den); >> margin (sys)
71

B o d e D ia g r a m G m = 9 .5 5 d B ( a t 1 r a d /s e c ) , P m = 4 9 d e g ( a t 0 .6 4 4 r a d /s e c ) 50

Magnitude (dB)

-50

-100

-150 0 -45 Phase (deg) -90 -135 -180 -225 -270 -2 10


-1 0 1 2

10

10

10

10

F r e q u e n c y ( r a d /s e c )

72

NYQUIST PLOT
This section covered Nyquist, Nichols, margin ,pade and ngrid functions. It is generally more difficult to manually generate the Nyquist plot than Bode diagram. When Nyquist function generated, automatically Nyquist plot is generated.

73

Example
1. For the closed loop control system, shown below, plot Nyquist plot.

0.5 3 2 s  2s  0.5
74

Solution: >>num=[0.5];den=[1 2 1 0.5]; >> sys=tf(num,den); >> nyquist (sys)

75

N y q u is t D ia g ra m 1 .5

0 .5 Imagina ry Axis

- 0 .5

-1

- 1 .5 -1

- 0 .8

- 0 .6

- 0 .4

- 0 .2

0 R e a l Ax is

0 .2

0 .4

0 .6

0 .8

76

Example
The nyquist plot for the below system with gain and phase margins.

0.5 3 2 s  2 s  s  0.5
77

% The Nyquist plot % With a gain and phase margin calculation. >> num=[0.5];den=[1 2 1 0.5]; >> sys=tf(num,den); >> [mag,phase,w]=bode(sys); >> [Gm,Pm,Wcg,Wcp]=margin(mag,phase,w); >> nyquist(sys); >> title(['Gm=',num2str(Gm),'Pm=',num2str(Pm)])

78

G m =3 P m =4 9 .5 7 5 3 1 .5

0 .5 Imaginary Axis

- 0 .5

-1

- 1 .5 -1

- 0 .8

- 0 .6

- 0 .4

- 0 .2

0 R e a l A x is

0 .2

0 .4

0 .6

0 .8

79

Using the Nyquist function, obtain the polar plot for the following transfer function.

20 G (s) ! 2 s  8s  14
10 G ( s) ! 3 2 s  3s  3s  1
80

Nichols Chart
Plot the nichols chart for the following transfer function

81

Solution
>> num=[1];den=[0.2 1.2 10]; >> sys=tf(num,den); >> w=logspace(-1,1,400); >> nichols(sys,w); >> ngrid

82

N ic ho ls C h ar t 40 0 dB 30 0 .2 5 d B 0 .5 d B 20 Open-Loop Gain (dB) 1 dB 3 dB 6 dB 0 - 3 dB - 6 dB - 1 dB

10

-1 0

- 12 d B

-2 0

- 20 d B

-3 0 - 40 d B -3 1 5 -2 7 0 -2 2 5 -1 8 0 -1 3 5 -9 0 -4 5 0 O p en -L o op P h as e (d e g)

-4 0 -3 6 0

83

Exercise
Draw the Nichols chart for the given transfer function.

84

>>num=[1];den=[0.6 2.3 1]; >> sys=tf(num,den); >> nichols(sys); >> ngrid

85

N ic ho ls C h ar t 40 0 dB 20 0.2 5 dB 0.5 d B 1 dB 3 dB 6 dB 0 Open-Loop Gain (dB)

-1 dB -3 dB -6 dB -1 2 d B

- 20

-2 0 d B

- 40

-4 0 d B

- 60

-6 0 d B

- 80

-8 0 d B

- 10 0 - 36 0

- 31 5

- 27 0

- 22 5

- 18 0

- 13 5

- 90

-1 00 dB - 45 0

O p en - Lo op P ha s e (d eg )

86

Building a Simple Model

87

Steps
1.Enter simulink in MATLAB command Window
Library Simulink

2.Create a new model window by click New


Untitled

88

3. To create the above model click the follwing library. - Sources Library - Sinks Library - Continuous Library - Signal Routing library 4. Drag the respective blocks from the browser and drop it in the model window.
89

5. To view simulation output for 10 second, - Open the scope block - Click simulation parameters from the simulation menu. Notice stop time for 10 second. Then click OK. Close the dialog box.

90

- Choose start from simulation and watch the traces of the scope blocks input.

91

6. To save choose save from the file menu and enter the filename and location.

92

Simulink
Running a Demo Model 1. Click the start button on the bottom left corner of the MATLAB command window. 2. Select Demos from the menu. 3. Click the simulink entry in the Demos panel.

93

4. Select any one of features. Simulink Features General Automotive Aerospace.


94

5. Click on demo link to start the demo. 6. Simulink start.

95

END

96