basic matlab guide

81
A REPORT OF LAB-I (EE-6241) MASTER OF ENGINEERING WITH SPECIALIZATION IN DIGITAL TECHNIQUES AND INSTRUMENTATION Submitted to: Submitted By: Dr. SANDEEP BHONGADE NIKITA GUPTA SESSION -

Upload: nikita-gupta

Post on 11-Feb-2016

37 views

Category:

Documents


1 download

DESCRIPTION

BASIC MATLAB PROGRAMS AND SIMULINK MODELS

TRANSCRIPT

Page 1: Basic Matlab Guide

A REPORT OF LAB-I (EE-6241)

MASTER OF ENGINEERING

WITH SPECIALIZATION IN

DIGITAL TECHNIQUES AND INSTRUMENTATION

Submitted to: Submitted By:

Dr. SANDEEP BHONGADE NIKITA GUPTA

DEPARTMENT OF ELECTRICAL ENGINEERING

SESSION - 2014-15

Page 2: Basic Matlab Guide

SHRI G. S. INSTITUTE of TECHNOLOGY & SCIENCE, INDORE (M.P.)

ASSIGNMENT-1

MATLAB INTRODUCTION

1. INTRODUCTIONThe name MATLAB stands for MATrixLABoratory. It is a language for technical computing developed by the TheMathworks, Inc. A numerical analyst called Cleve Moler wrote the first version of MATLAB in the 1970s. It has since evolved into a successful commercial software package. It provides a single platform for computation, visualization, programming and software development. All problems and solutions in MATLAB are expressed in notation used in linear algebra and essentially involve operations using matrices and vectors.As part of the undergraduate Electrical Engineering program, you will be using MATLAB to solve problems in

Circuits Communication systems Digital signal processing Control systems Probability and statistics

In addition, you can use MATLAB to build Graphical User Interfaces (GUIs) so that you can develop user friendly custom software. The MATLAB software environment has a core module (called MATLAB) and associated with that are a set of "Toolboxes" that perform specialized computations. MATLAB is a high-performance language for technical computing.MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation ofalgorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, andFortran. Although MATLAB is intended primarily for numerical computing, an optional toolbox uses the MuPAD symbolic engine, allowing access to symbolic computing capabilities. An additional package, Simulink, adds graphical multi-domain simulation and Model-Based Design for dynamic and embedded systems. In 2004, MATLAB had around one million users across industry and academia.[3] MATLAB users come from various backgrounds of engineering, science, and economics. MATLAB is widely used in academic and research institutions as well as industrial enterprises.

2. STARTING MATLABAfter logging into your account, you can enter MATLAB by double-clicking on the MATLAB shortcut icon on your Windows desktop. When you start MATLAB, a special window called the MATLAB desktop appears. The desktop is a window that contains other windows. The major tools within or accessible from the desktop are:

The Command Window The Command History The Workspace The Current Directory The Help Browser

1 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 3: Basic Matlab Guide

The Start buttonWhen MATLAB is started for the first time, the screen looks like the one that shown in the Figure 1

>> for full versionEDU> for educational version

Online help can be accessed for all MATLAB commands by issuing the help command.>> help <type name of command here>

To get started, you can simply type>> help

Figure 1: The graphical interface to the MATLAB workspace

2 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 4: Basic Matlab Guide

3. GENERAL PURPOSE COMMANDS

OPERATORS AND SPECIAL CHARACTERS

+ Plus; addition operator

- Minus; subtraction operator.

* Scalar and matrix multiplication operator

^ Scalar and matrix exponentiation operator.

\ Left-division operator.

/ Right-division operator

: Colon; generates regularly spaced elements and represents an entire row or column

() Parentheses; encloses function arguments and array indices; overrides precedence.

[ ] Brackets; enclosures array elements.

. Decimal point.

, Comma; separates statements and elements in a row.

… Ellipsis; line-continuation operator.

; Semicolon; separates columns and suppresses display.

% Percent sign; designates a comment and specifies formatting.

= Assignment (replacement) operator.

COMMANDS FOR MANAGING A SESSION

clc Clears Command window.clear Removes variables from memory.exist Checks for existence of file or variable.global Declares variables to be global.help Searches for a help topic.lookfor Searches help entries for a keyword.

quit Stops MATLABwho Lists current variables.whos list the variables and describes their matrix size.

3 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 5: Basic Matlab Guide

SYSTEM AND FILE COMMANDS

cd Changes current directory.

date Displays current date.

delete Deletes a file.

dir Lists all files in current directory.

load Loads workspace variables from a file.

path Displays search path.

pwd Displays current directory.

save Saves workspace variables in a file.

what Lists all MATLAB files in the current directory

type Displays contents of a file.

COMMANDS USEFUL IN PLOTTING.

plot(x,y) creates an Cartesian plot of the vectors x & y.

plot(y) creates a plot of y vs. the numerical values of the elements in the y-vector.

grid Displays gridlines.

title('text') places a title at top of graphics plot.

xlabel('text') writes 'text' beneath the x-axis of a plot.

ylabel('text') writes 'text' beside the y-axis of a plot.

Hold on maintains the current plot in the graphics window while executing subsequent plotting commands

Hold off turns OFF the 'hold on' option.

4 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 6: Basic Matlab Guide

ASSIGNMENT-2

MATRIX OPERATIONS AND PLOTS

1. MATRIX OPERATIONS

The most basic MATLAB data structure is the matrix: a two-dimensional, rectangular shaped data structure capable of storing multiple elements of data in an easily accessible format. These data elements can be numbers, characters, and logical states of true or false, or even other MATLAB structure types. MATLAB uses these two-dimensional matrices to store single numbers and linear series of numbers as well.

1.1MATRIX CREATION

To create a matrix it is simply introduced on the left hand side of an equal sign. This means that the expression on the right side of the equal sign must evaluate to a matrix.FOR EXAMPLE:-

>> A=[1 2;8 9]

A = 1 2 Matrix of order 2x2

8 9

>> B=[4 5 8;6 9 7;3 3 4]

B = 4 5 8Matrix of order 3x3

6 9 7

3 3 4

>> C=[1 5 9 7;4 3 9 7;5 2 8 1]

C =1 5 9 7Matrix of order 3x4

4 3 9 7

5 2 8 1

1.2 MATRIX TRANSPOSE

In linear algebra, the transpose of a matrix A is another matrix AT (also written A′, Atr, tA or At) created by any one of the following equivalent actions:

FOR EXAMPLE:-

5 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 7: Basic Matlab Guide

>> A=[1 2;8 9]

A = 1 2

8 9

>> A' gives the transpose of matrix ‘A’

ans = 1 8

2 9

>> B=[4 5 8;6 9 7;3 3 4]

B = 4 5 8

6 9 7

3 3 4

>> B' gives the transpose of matrix ‘B’

ans = 4 6 3

5 9 3

8 7 4

>> C=[1 5 9 7;4 3 9 7;5 2 8 1]

C =1 5 9 7

4 3 9 7

5 2 8 1

>> C' gives the transpose of matrix ‘C’

ans = 1 4 5

5 3 2

9 9 8

7 7 1

1.3 DETERMINANT OF A MATRIX

In linear algebra, the determinant is a value associated with a square matrix. It can be computed from the entries of the matrix by a specific arithmetic expression, while other ways to determine its value exist as well. The determinant provides important information about a

6 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 8: Basic Matlab Guide

matrix of coefficients of a system of linear equations, or about a matrix that corresponds to a linear transformation of a vector space.

d = det(X) returns the determinant of the square matrix X

FOR EXAMPLE:-

>> A=[1 2;8 9]

A = 1 2

8 9

>> D=det(A) gives the value of determinant of matrix‘A’

D = -7

>> B=[4 5 8;6 9 7;3 3 4]

B = 4 5 8

6 9 7

3 3 4

>> D=det(B) gives the value of determinant of matrix‘B’

D = -27.0000

1.4 RANK OF A MATRIX

The rank of a matrix A is the size of the largest collection of linearly independent columns of A (the column rank) or the size of the largest collection of linearly independent rows of A (the row rank). For every matrix, the column rank is equal to the row rank.FOR EXAMPLE:-

>> A=[1 2;8 9]

A = 1 2

8 9

>> R=rank(A) gives the value of rank of matrix ‘A’

R = 2

>> B=[4 5 8;6 9 7;3 3 4]

B = 4 5 8

6 9 7

7 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 9: Basic Matlab Guide

3 3 4

>> R=rank(B) gives the value of rank of matrix ‘B’

R =3

>> C=[1 5 9 7;4 3 9 7;5 2 8 1]

C =1 5 9 7

4 3 9 7

5 2 8 1

>> R=rank(C) gives the value of rank of matrix ‘C’

R = 3

1.5 EIGEN VALUES AND EIGEN VECTORS OF A MATRIX

To determine the eigenvalues and eigenvectors in MATLAB is to use the eigfunction. For an nxnmatrix A, eig (A) returns anx1 column vector whose elements are the eigen values of A. The command in the form

[V D] = eig(A)FOR EXAMPLE:-

>> A=[4 5 8;6 7 9;3 6 4]

A = 4 5 8

6 7 9

3 6 4

>> d=eig(A) ‘d’ is the eigen values for matrix ‘A’

d = 17.1981

-1.0990 + 0.7711i

-1.0990 - 0.7711i

>> [V D]=eig(A) ‘V’ is the eigen vector for matrix ‘A’

V = 0.5408 0.7833 0.7833

0.7126 0.0020 - 0.2740i 0.0020 + 0.2740i

0.4469 -0.5005 + 0.2468i -0.5005 - 0.2468i

D = 17.1981 0 0

8 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 10: Basic Matlab Guide

0 -1.0990 + 0.7711i 0

0 0 -1.0990 - 0.7711i

1.6 MATHEMATICAL OPERATIONS ON MATRIX

Matlab can perform the standard arithmetic operations on matrices, vectors, and scalars (that is, on 2-, 1-, and 0-dimensional arrays): addition, subtraction, and multiplication.

1.6.1 ADDITION

FOR EXAMPLE:-

>> A=[4 8 7;9 6 3;1 5 9]

A = 4 8 7

9 6 3

1 5 9

>> B=[5 9 6;8 7 6;1 5 9]

B = 5 9 6

8 7 6

1 5 9

>> C=A+B

C = 9 17 13

17 13 9

2 10 18

>> C= plus (A,B)

C = 9 17 13

17 13 9

2 10 18

1.6.2 SUBTRACTION

FOR EXAMPLE:-

>> A=[4 8 7;9 6 3;1 5 9]

9 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 11: Basic Matlab Guide

A = 4 8 7

9 6 3

1 5 9

>> B=[5 9 6;8 7 6;1 5 9]

B = 5 9 6

8 7 6

1 5 9

>> C=A-B

C = -1 -1 1

1 -1 -3

0 0 0

>> C=minus(A,B)

1.6.3 MULTIPLICATION

FOR EXAMPLE:-

>> A=[4 8 7;9 6 3;1 5 9]

A = 4 8 7

9 6 3

1 5 9

>> B=[5 9 6;8 7 6;1 5 9]

B = 5 9 6

8 7 6

1 5 9

>> C=A*B

C = 91 127 135

96 138 117

54 89 117

>> C=mtimes(A,B)

10 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 12: Basic Matlab Guide

C = 91 127 135

96 138 117

54 89 117

1.6.4 ARRAY WISE MULTIPLICATION

FOR EXAMPLE:-

>> A=[4 8 7;9 6 3;1 5 9]

A = 4 8 7

9 6 3

1 5 9

>> B=[5 9 6;8 7 6;1 5 9]

B = 5 9 6

8 7 6

1 5 9

>> C=A.*B

C = 20 72 42

72 42 18

1 25 81

1.6.5 MATRIX RIGHT DIVISION

FOR EXAMPLE:-

>> A=[4 8 7;9 6 3;1 5 9]

A = 4 8 7

9 6 3

1 5 9

>> B=[5 9 6;8 7 6;1 5 9]

B = 5 9 6

8 7 6

1 5 9

11 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 13: Basic Matlab Guide

>> C=A/B

C = 0.7143 0.0173 0.2900

-0.0000 1.1818 -0.4545

0 0 1.0000

1.6.6 ARRAY WISE RIGHT DIVISION

FOR EXAMPLE:-

>> A=[4 8 7;9 6 3;1 5 9]

A = 4 8 7

9 6 3

1 5 9

>> B=[5 9 6;8 7 6;1 5 9]

B = 5 9 6

8 7 6

1 5 9

>> C=A./B

C = 0.8000 0.8889 1.1667

1.1250 0.8571 0.5000

1.0000 1.0000 1.0000

1.6.7 LEFT DIVISION

FOR EXAMPLE:-

>> A=[4 8 7;9 6 3;1 5 9]

A = 4 8 7

9 6 3

1 5 9

>> B=[5 9 6;8 7 6;1 5 9]

B = 5 9 6

12 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 14: Basic Matlab Guide

8 7 6

1 5 9

>> C=A\B

C = 0.6103 -0.0103 0.7692

0.5487 1.2513 -0.8462

-0.2615 -0.1385 1.3846

1.6.8 ARRAY WISE LEFT DIVISION

FOR EXAMPLE:-

>> A=[4 8 7;9 6 3;1 5 9]

A = 4 8 7

9 6 3

1 5 9

>> B=[5 9 6;8 7 6;1 5 9]

B = 5 9 6

8 7 6

1 5 9

>> C=A.\B

C = 1.2500 1.1250 0.8571

0.8889 1.1667 2.0000

1.0000 1.0000 1.0000

2.SPECIAL MATRIX FUNCTION

2.1 ONES FUNCTIONCreate an array of all 1’s. Matlab provides a number of useful built–in matrices of any desired size.ones (m, n) gives an m × n matrix of 1’s,>>ones(3)

ans = 1 1 1

1 1 1

13 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 15: Basic Matlab Guide

1 1 1

2.2 ZEROS FUNCTIONZeros(m,n) gives an m × n matrix of 0’s,

>>zeros(5)

ans =0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

2.3 RANDOM FUNCTIONUniformly distributed random numbers and arrays

>>rand(2,6)

ans = 0.8147 0.1270 0.6324 0.2785 0.9575 0.1576

0.9058 0.9134 0.0975 0.5469 0.9649 0.9706

2.4 MAGIC FUNCTION

>>magic(4)

ans =16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

2.5 IDENTITY MATRIX FUNCTIONThe n×n identity matrix is a matrix of zeros except for having ones along its leading diagonal (top left to bottom right). This is called eye (n) in Matlab (since mathematically it is usually denoted by I).>>eye(8)ans = 1 0 0 0 0 0 0 00 1 0 0 0 0 0 00 0 1 0 0 0 0 00 0 0 1 0 0 0 00 0 0 0 1 0 0 0

14 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 16: Basic Matlab Guide

0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 00 0 0 0 0 0 0 1

3.INTRODUCTION TO PLOTTING

Plotting a given data set or the results of computation is possible with very few commands. You are highly encouraged to plot mathematical functions and results of analysis as often as possible. Trying to understand mathematical equations with graphics is an enjoyable and very efficient way of learning mathematics. Being able to plot mathematical functions and data freely is the most important step, and this section is written to assist you to do just that.

3.1 SINE PLOTsymsx;x=[0:pi/200:2*pi];y=sin(x);plot(x,y);gridonxlabel('time');ylabel('amplitude');title('sine plot');

3.2COSINE PLOT

symsx;x=[-pi:pi/120:pi];y=cos(x);plot(x,y);

15 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 17: Basic Matlab Guide

gridonxlabel('time');ylabel('amplitude');title('cosine plot');

3.3TANGENT PLOT

symsx;x=[-pi/2+0.1:pi/120:pi/2-0.1];y=tan(x);plot(x,y);gridonxlabel('time');ylabel('amplitude');title('tangent plot');

16 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 18: Basic Matlab Guide

3.4COTANGENT PLOT

symsx;x=[0.1:pi/200:pi-0.1];y=cot(x);plot(x,y);gridonxlabel('time');ylabel('amplitude');title('cotangent plot');

17 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 19: Basic Matlab Guide

3.5EXPONENTIALLY RISING PLOT

symsx;x=[0:0.1:4];y=exp(x);plot(x,y);gridonxlabel('time');ylabel('amplitude');title('exponential plot');

18 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 20: Basic Matlab Guide

3.6EXPONENTIALLY FALLING PLOT

symsx;x=[0:0.1:4];y=exp(-x);plot(x,y);gridonxlabel('time');ylabel('amplitude');title('exponential plot');

19 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 21: Basic Matlab Guide

3.7LAPLACE AND INVERSE LAPLACE

>>syms t s

F= -1.25+3.5*t*exp(-2*t)+1.25*exp(-2*t);

laplace(F)

ans = 5/(4*(s + 2)) + 7/(2*(s + 2)^2) - 5/(4*s)

>>syms t s

F=(s-5)/(s*(s+2)^2);

ilaplace(F)

ans =5/(4*exp(2*t)) + (7*t)/(2*exp(2*t)) - 5/4

20 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 22: Basic Matlab Guide

ASSIGNMENT-3

INTRODUCTION TO SIMULATION

1. INTRODUCTION

Simulink software models, simulates, and analyzes dynamic systems. It enables us to pose a question about a system, model the system, and see what happens.

With Simulink, we can easily build models from scratch, or modify existing models to meet our needs. Simulink supports linear and nonlinear systems, modeled in continuous time, sampled time, or a hybrid of the two. Systems can also be multirate—having different parts that are sampled or updated at different rates.

Thousands of scientists and engineers around the world use Simulink to model and solve real problems in a variety of industries, including:

Aerospace and Defense Automotive Communications Electronics and Signal Processing Medical Instrumentation

2. SIMULINK PRODUCT DESCRIPTION

Simulink is a ”block diagram environment” for multidomain simulation and model based design.

It supports system level design, simulation, automatic code generation and continuous test and verification of embedded system.

Simulink provides a graphical editor, custom table block libraries and solvers for modeling and simulating dynamic system. It is interacted with MATLAB, enabling you to incorporate MATLAB algorithms into models and export further results to MATLAB for further analysis.

3. MODEL BASED DESIGN

Model based design is a process that enables faster, more cost effective development of dynamic system, including control system, signal processing, and communication system.tn the model based design, a system model is at the centre of the development process, from requirement development through design implementation and testing.

21 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 23: Basic Matlab Guide

3.1 TOOLS FOR MODEL-BASED DESIGN

3.1.1COMMONLY USED BLOCKS

3.1.2SOURCES

3.1.3SINKS

22 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 24: Basic Matlab Guide

3.1.4 FROM AND GOTO BLOCK

3.1.5TRANSFER FUNCTION BLOCK

3.1.6 CURRENT AND VOLTAGE MEASUREMENT

23 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 25: Basic Matlab Guide

3.1.7 AC VOLTAGE SOURCE

3.1.8 POWERGUI BLOCK

3. HALF WAVE DIODE RECTIFIER

To design a simulink model of a single phase half wave diode rectifier.

TOOLS USED:Simpower systems, commonly used blocks.

SIMULINK MODEL OF SINGLE PHASE HALF WAVE RECTIFIER:

24 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 26: Basic Matlab Guide

SIMULATION RESULTS:

4. FULL WAVE DIODE RECTIFIER

To design a Simulink model of a single phase full wave dioderectifier.

TOOLS USED:Simpower systems, commonly used blocks.

THEORY: In full wave rectifier both the both half-cycles of the input are utilized with the

help of two or four diode working alternately.

In a Full Wave Rectifier circuit four individual rectifying diodes connected in a closed loop

"bridge" configuration to produce the desired output. The main advantage of this bridge

circuit is that it does not require a special centre tapped transformer, thereby reducing its size

and cost.

25 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 27: Basic Matlab Guide

SIMULINK MODEL OF SINGLE PHASE FULL WAVE RECTIFIER:

SIMULATION RESULTS:

26 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 28: Basic Matlab Guide

5. PROPORTIONAL-INTEGRAL-DERIVATIVE CONTROLLER

A proportional-integral-derivative controller (PID controller) is a control loop feedback

mechanism (controller) widely used in industrial control systems. A PID controller calculates

an error value as the difference between a measured process variableand a desired set point.

The controller attempts to minimize the error by adjusting the process through use of a

manipulated variable.

where

: Proportional gain, a tuning parameter: Integral gain, a tuning parameter: Derivative gain, a tuning parameter

: Error : Time or instantaneous time (the present): Variable of integration; takes on values from time 0 to the present  .

SIMULINK MODEL OF PID CONTROLLER:

SUBSYSTEM MODEL:

27 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 29: Basic Matlab Guide

OUTPUT WAVEFORM:

28 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 30: Basic Matlab Guide

6. DAMPED SYSTEM

Damping is an influence within or upon an oscillatory system that has the effect of reducing, restricting or preventing its oscillations.

The damping of a system can be described as being one of the following:

Overdamped: The system returns (exponentially decays) to equilibrium without oscillating.

Critically damped: The system returns to equilibrium as quickly as possible without oscillating.

Underdamped: The system oscillates (at reduced frequency compared to the undamped case) with the amplitude gradually decreasing to zero.

Undamped: The system oscillates at its natural resonant frequency (ωo).

SIMULINK MODEL FOR DAMPING SYSTEM:

SIMPLER MODEL:

29 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 31: Basic Matlab Guide

OUTPUT WAVEFORM:

30 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 32: Basic Matlab Guide

ASSIGNMENT-4

CONTROL SYSTEM TOOLBOX

Control System Toolbox provides industry-standard algorithms and apps for systematically analyzing, designing, and tuning linear control systems. You can specify your system as a transfer function, state-space, zero-pole-gain or frequency-response model. Apps and functions, such as step response plot and Bode plot, let you visualize system behavior in time domain and frequency domain. You can tune compensator parameters using automatic PID controller tuning, Bode loop shaping, root locus method, LQR/LQG design, and other interactive and automated techniques. You can validate your design by verifying rise time, overshoot, settling time, gain and phase margins, and other requirements.

1.CREATION OF TRANSFER FUNCTION

1. FIRST ORDER:

>> A=20

A = 20

>> B= [2 5]

B = 2 5

>>tf(A,B)

Transfer function: 20 ------- 2 s + 5

2. SECOND ORDER

>> A=60

A = 60

>> B=[6 8 7]

B = 6 8 7

>>tf(A,B)

Transfer function: 60 --------------- 6 s^2 + 8 s + 7

31 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 33: Basic Matlab Guide

3. THIRD ORDER SYSTEM

>> A=50

A = 50

>> B=[2 3 5 6]

B = 2 3 5 6

>>tf(A,B)

Transfer function: 50-----------------------2 s^3 + 3 s^2 + 5 s + 6

2. CONVERSION OF TRANSFER FUNCTION INTO STATE SPACE

>> a=20

>> b=[5 7 3]

b = 5 7 3

>>tf(a,b)

Transfer function: 20---------------5 s^2 + 7 s + 3

>> [A,B,C,D]= tf2ss(a,b)

A = -1.4000 -0.6000

1.0000 0

B = 1

0

C = 0 4

D = 0

3. CONVERSION FROM STATE SPACE EQUATION TO TRANSFER FUNCTION

>> A=[2 3;4 1]

A = 2 3

4 1

32 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 34: Basic Matlab Guide

>> B=[1;0]

B = 1

0

>> C=[0 4]

C = 0 4

>> D=0

D = 0

>> [b,a]=ss2tf(A,B,C,D)

b = 0 0 16.0000

a = 1 -3 -10

>>tf(b,a)

Transfer function: 16--------------b s^2 - 3 s – 10

4. ANALYSIS OF FIRST ORDER SYSTEM

EXAMPLE: 1

>>num=20;

>>den=[5 6];

>> f=tf(num,den)

Transfer function:

20-------5 s + 6

1. BODE PLOT

>>bode(f)

33 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 35: Basic Matlab Guide

2. NYQUIST PLOT

>>nyquist(f)

34 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 36: Basic Matlab Guide

3. ROOT LOCUS PLOT

>>rlocus(f)

4. NICHOLS PLOT

>>nicholsplot(f)

35 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 37: Basic Matlab Guide

5. UNIT STEP RESPONSE

>>step(f)

6. IMPULSE RESPONSE

>>impulse(f)

36 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 38: Basic Matlab Guide

7. GAIN MARGIN AND PHASE MARGIN

>> [Gm Pm WgWc] = margin(f)

Gm= Inf

Pm = 107.4576

Wg = NaN

Wc= 3.8158

2. EFFECT OF ADDING OF POLES AND ZEROS ON IMPULSE RESPONSE OF A SYSTEM

>>num=20;

den=[5 6];

f=tf(num,den)

Transfer function:

20------- Standard transfer function5 s + 6

>>impulse(f) Command for obtaining impulse response

>>hold on

>>num=[20 10];

den=[5 6];

f=tf(num,den)

Transfer function:

20 s + 10--------- Transfer function on addition of zeros 5 s + 6

>>impulse(f)

>>num=20;

den=[5 6 3];

f=tf(num,den)

Transfer function:

37 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 39: Basic Matlab Guide

20--------------- Transfer function on addition of poles5 s^2 + 6 s + 3

>>impulse(f)

3. EFFECT OF ADDING OF POLES AND ZEROS ON STEP RESPONSE OF A SYSTEM

>>num=20;

den=[5 6];

f=tf(num,den)

Transfer function:

20------- Standard transfer function5 s + 6>>step(f) Command for obtaining step response

>> hold on

38 | P a g e L A B - I ( E E - 6 2 4 1 )

Effect of adding poles

Effect of adding zeros

Page 40: Basic Matlab Guide

>>num=[20 10];

den=[5 6];

f=tf(num,den)

Transfer function:

20 s + 10--------- Transfer function on addition of zeros 5 s + 6

>>step(f)

>>num=20;

den=[5 6 3];

f=tf(num,den)

Transfer function:

20--------------- Transfer function on addition of poles5 s^2 + 6 s + 3

>>step(f)

39 | P a g e L A B - I ( E E - 6 2 4 1 )

Effect of adding poles

Effect of adding zeros

Page 41: Basic Matlab Guide

4. EFFECT OF ADDING OF POLES AND ZEROS ON BODE PLOT

>>num=20;

den=[5 6];

f=tf(num,den)

Transfer function:

20------- Standard transfer function5 s + 6

>>bode(f) Command for obtaining bode plot

>> hold on

>>num=[20 10];

den=[5 6];

f=tf(num,den)

Transfer function:

20 s + 10--------- Transfer function on addition of zeros 5 s + 6

>>bode(f)

>>num=20;

den=[5 6 3];

f=tf(num,den)

Transfer function:

20--------------- Transfer function on addition of poles5 s^2 + 6 s + 3

>>bode(f)

40 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 42: Basic Matlab Guide

5. EFFECT OF ADDING OF POLES AND ZEROS ON NYQUIST PLOT

>>num=20;

den=[5 6];

f=tf(num,den)

Transfer function:

20------- Standard transfer function5 s + 6

>>nyquist(f) Command for obtaining bode plot

>> hold on

>>num=[20 10];

den=[5 6];

41 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 43: Basic Matlab Guide

f=tf(num,den)

Transfer function:

20 s + 10--------- Transfer function on addition of zeros 5 s + 6

>>nyquist(f)

>>num=20;

den=[5 6 3];

f=tf(num,den)

Transfer function:

20--------------- Transfer function on addition of poles5 s^2 + 6 s + 3

>>nyquist(f)

42 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 44: Basic Matlab Guide

5. EFFECT OF ADDING OF POLES AND ZEROS ON ROOT LOCUS PLOT

>>num=20;

den=[5 6];

f=tf(num,den)

Transfer function:

20------- Standard transfer function 5 s + 6

>>rlocus(f)Command for obtaining root locus plot

>> hold on

>>num=[20 10];

den=[5 6];

f=tf(num,den)

Transfer function:

20 s + 10--------- Transfer function on addition of zeros 5 s + 6

>>rlocus(f)

>>num=20;

den=[5 6 3];

f=tf(num,den)

Transfer function:

20--------------- Transfer function on addition of poles5 s^2 + 6 s + 3

>>rlocus

43 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 45: Basic Matlab Guide

6. EFFECT OF ADDING OF POLES AND ZEROS ON NICHOLS PLOT

>>num=20;

den=[5 6];

f=tf(num,den)

Transfer function:

20------- Standard transfer function5 s + 6

>>nicholsplot(f)Command for obtaining nichols plot

>> hold on

>>num=[20 10];

den=[5 6];

44 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 46: Basic Matlab Guide

f=tf(num,den)

Transfer function:

20 s + 10--------- Transfer function on addition of zeros 5 s + 6

>>nicholsplot(f)

>>num=20;

den=[5 6 3];

f=tf(num,den)

Transfer function:

20--------------- Transfer function on addition of poles5 s^2 + 6 s + 3

>>nicholsplot(f)

45 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 47: Basic Matlab Guide

7.CALCULATE THE PHASE TRAJECTORY OF A SYSTEM USING SIMULINK MODEL

EXAMPLE:-

x1.=2 x 1+ x2

x2.=−15 x1−6 x 2+3 u

¿ y=x 1

Simulinkmodel for the given state space equation is given by-

XY Graph-

46 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 48: Basic Matlab Guide

Graph for x 1.

from scope1:-

Graph for x2.

from scope2:-

47 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 49: Basic Matlab Guide

Output-y from scope:-

8. RESPONSE OF A SYSTEM USING STATE SPACE EQUATIONS

[ x1.

¿]¿¿

¿¿

¿¿

1. STEP RESPONSE

>> a=[-1 -1;6.5 0];

>> b=[1 1;1 0];

>> c=[1 0;0 1];

>> d=[0 0;0 0];

>>step(a,b,c,d)

48 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 50: Basic Matlab Guide

2. IMPULSE RESPONSE

>>impulse(a,b,c,d)

49 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 51: Basic Matlab Guide

3. BODE PLOT

>>bode(a,b,c,d)

4. NYQUIST PLOT

>>nyquist(a,b,c,d)

50 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 52: Basic Matlab Guide

5. ROOT LOCUS PLOT

>>rlocus(a,b(:,1),c(2,:),d(1,2))

9. TRANSIENT STATE ANALTSIS

>>Wn=5; %natural frequency of oscillation in rad/sec

>> d=0.4; %damping ratio of the second order system

>> num0=1;

>> [num0,den]=ord2(Wn,d);

>>num=5^2*num0;

>>printsys(num,den,'s')

num/den =

25 ------------------s^2 + 4 s + 25

51 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 53: Basic Matlab Guide

10. OBTAINING CASCADE,PARALLEL AND FEEDBACK TRANSFER FUNCTION WITH MATLAB

1.CASCADE OR SERIES SYSTEM

>> num1=10;

>> den1=[2 6];

>>tf(num1,den1)

Transfer function:

10-------2 s + 6

>> num2=5;

>> den2=[8 3];

>>tf(num2,den2)

Transfer function:

5-------8 s + 3

>> [num,den]=series(num1,den1,num2,den2);

>>printsys(num,den)

num/den =

50 ------------------ 16 s^2 + 54 s + 18

2. PARALLEL SYSTEM

>> num1=10;

den1=[2 6];

tf(num1,den1)

Transfer function:

10-------

52 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 54: Basic Matlab Guide

2 s + 6

>> num2=5;

>> den2=[8 3];

>>tf(num2,den2)

Transfer function:

5-------8 s + 3

>> [num,den]=parallel(num1,den1,num2,den2);

>>printsys(num,den)

num/den =

90 s + 60 ----------------------- 16 s^2 + 54 s + 18

3. FEEDBACK SYSTEM

>> num1=10;

den1=[2 6];

tf(num1,den1)

Transfer function:

10-------2 s + 6

>> num2=5;

>> den2=[8 3];

>>tf(num2,den2)

Transfer function:

5-------8 s + 3

53 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 55: Basic Matlab Guide

>> [num,den]=feedback(num1,den1,num2,den2);

>>printsys(num,den)

num/den =

80 s + 30 ----------------------- 16 s^2 + 54 s + 68

11. TO OBTAIN THE STATE MODEL FROM DIFFERENTIAL EQUATION

EXAMPLE:

dydt

+5 y=10u

For the given differential equation the simulink model is given by:-

54 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 56: Basic Matlab Guide

ASSIGNMENT-5

LOOPS AND COMPENSATORS

1. COMPENSATORS

A compensator is a component in a control system that improves an undesirable frequency response in a feedback and control system. It is a fundamental building block in classical control theory. Compensators influence disciplines as varied as robotics, satellite control, automobile diagnostics, and laser frequency stabilization. They are an important building block in analog control systems, and can also be used in digital control.

PHASE COMPENSATION

Occasionally, it is necessary to alter the phase characteristics of a given system, without altering the magnitude characteristics. To do this, we need to alter the frequency response in such a way that the phase response is altered, but the magnitude response is not altered. To do this, we implement a special variety of controllers known as phase compensators. They are called compensators because they help to improve the phase response of the system.

There are two general types of compensators: Lead Compensators, and Lag Compensators. If we combine the two types, we can get a special Lead-Lag Compensator system.When designing and implementing a phase compensator, it is important to analyze the effects on the gain and phase margins of the system, to ensure that compensation doesn't cause the system to become unstable. phase lead compensation:- 1 it is same as addition of zero to open loop TF since from pole zero point of view zero is nearer to origin than pole hence effect of zero dominant.

1.1 LAG COMPENSATOR

The transfer function for a lag compensator is the same as the lead-compensator, and is as follows:

>>wm = 0.84; % gain-crossover frequency

alpha = 10; % phase-lag compensator parameter

T = 10/wm; % phase-lag compensator time constant

K = 10; % DC compensator gain

% Phase-lag compensator C(s)

cnum = K*[T 1];

cden = [T*alpha 1];

55 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 57: Basic Matlab Guide

% Open-loop sys G(s)

gnum = [1];

gden = [1 1 0];

% Unity-Gain Feedback Loop H(s)

hnum = [1];

hden = [1];

% Open-loop sys C(s)*G(s)

numo = conv(cnum,gnum);

deno = conv(cden,gden);

% Closed-loop sys

[gnumc,gdenc] = feedback(K*gnum,gden,hnum,hden,-1);

[numc,denc] = feedback(numo,deno,hnum,hden,-1);

bode(cnum,cden);

56 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 58: Basic Matlab Guide

1.2 LEAD COMPENSATORS

wm = 4.5; % gain-crossover frequency

alpha = 0.3; % phase-lead compensator parameter

T = 1/wm/sqrt(alpha); % phase-lead compensator time constant

K = 10; % DC compensator gain

% Phase-lead compensator C(s)

cnum = K*[T 1];

cden = [T*alpha 1];

% Open-loop sys G(s)

gnum = [1];

gden = [1 1 0];

% Unity-Gain Feedback Loop H(s)

hnum = [1];

hden = [1];

% Open-loop sys C(s)*G(s)

numo = conv(cnum,gnum);

deno = conv(cden,gden);

% Closed-loop sys

[gnumc,gdenc] = feedback(K*gnum,gden,hnum,hden,-1);

[numc,denc] = feedback(numo,deno,hnum,hden,-1);

bode(cnum,cden);

57 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 59: Basic Matlab Guide

2. FOR LOOP

Execute block of code specified number of times

This component functions like the MATLAB for loop, except that instead of executing a statement, it executes its child components. It must have at least one child component to execute.

Loop Type

The loop type can have incremented indices or a vector of indices. For more information on for loops and indices, see for in the MATLAB documentation.

Incremented indices: Executes a for loop of the form:

forvarname=x:y:z

Statement 1;

Statement 2;

……….

58 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 60: Basic Matlab Guide

end

Start: Corresponds to x in the previous expression.

Increment: Corresponds to y in the previous expression.

End: Corresponds to z in the previous expression.

Vector of Indices: Executes a for loop of the form:

For varname=[a b c ...]

Specify appropriate values in the Vector field in the form a b c ....

PROGRAM:To find square of first 5 numbers

syms x

for x = 1:5

y=x.*x

end

Result:

y = 1

y = 4

y = 9

y =16

y =25

PROGRAM : To find factorial of a number

syms i;

fact=1;

for i=2:5

fact=fact*i;

end

Result:

fact =120

59 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 61: Basic Matlab Guide

2.1 NESTED FOR LOOP:

Syntax of ‘Nested for loop’ is as given below:

For var1 = x:y:z

Statement1

Statement2

…..

For var2 = a:b:c

Statement3

Statement4

….

End

End

Start: Corresponds to x,a in the previous expression.

Increment: Corresponds to y,b in the previous expression.

End: Corresponds to z,c in the previous expression.

PROGRAM: Incrementing each value from 1-3 by unit value four times

syms m;

syms n;

syms sum;

for m = 1:3

for n= 1:4

sum = m+n

end

end

Result:

sum = 2 for m=1

sum = 3

60 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 62: Basic Matlab Guide

sum = 4

sum = 5

sum = 3 for m=2

sum = 4

3. WHILE LOOP

Repeatedly execute statements while condition is true

Syntax

while (expression)

statements

end

while expression, statements, end repeatedly executes one or more MATLAB statements in a loop, continuing until expression no longer holds true or until MATLAB encounters a break, or return instruction,thus forcing an immediately exit of the loop. If MATLAB encounters a continue statement in the loop code, it immediately exits the current pass at the location of the continue statement, skipping any remaining code in that pass, and begins another pass at the start of the loop statements.

expression is a MATLAB expression that evaluates to a result of logical 1 (true) or logical 0 (false). expression can be scalar or an array. It must contain all real elements, and the statement all(A(:)) must be equal to logical 1 for the expression to be true.

expression usually consists of variables or smaller expressions joined by relational operators (e.g., count < limit) or logical functions (e.g., isreal(A)). Simple expressions can be combined by logical operators (&&, ||, ~) into compound expressions such as the following. MATLAB evaluates compound expressions from left to right, adhering to Operator Precedence rules.

(count< limit) && ((height - offset) >= 0)

statement is one or more MATLAB statements to be executed only while the expression is true or nonzero.

The scope of a while statement is always terminated with a matching end.

PROGRAM:To display 2n until less than 10,000

syms x;

x = 1;

n=0;

61 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 63: Basic Matlab Guide

while(x<10000)

v = [x, n]

x = 2^(n+1);

n = n+1;

end

Result:

v =

1 0

v =

2 1

v =

4 2

v =

8 3

v =

16 4

v =

32 5

v =

64 6

v =

128 7

v =

256 8

v =

512 9

v =

62 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 64: Basic Matlab Guide

1024 10

v =

2048 11

v =

4096 12

v =

8192 13

4. IF-ELSE

Execute statements if condition is true

Syntax

if (expression)

statements

end

Description

if expression, statements, end evaluates expression and, if the evaluation yields logical 1 (true) or a nonzero result, executes one or more MATLAB commands denoted here as statements.

expression is a MATLAB expression, usually consisting of variables or smaller expressions joined by relational operators (e.g., count < limit), or logical functions (e.g., isreal(A)). Simple expressions can be combined by logical operators (&&, ||, ~) into compound expressions such as the following. MATLAB evaluates compound expressions from left to right, adhering to operator precedence rules.

(count< limit) && ((height - offset) >= 0)

4.1 NESTED IF-ELSE

Nested if statements must each be paired with a matching end.

The if function can be used alone or with the else and elseif functions. When using elseif and/or else within an if statement, the general form of the statement is

if expression1

statements1

63 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 65: Basic Matlab Guide

else if expression2

statements2

else

statements3

end

end

PROGRAM:If a (number< 5) then number = number +1, else number = number -1

syms i;

i=[10];

if i<5

i=i+1;

else i=i-1;

end

Result:

i =9

PROGRAM:To use nested if else statements in a program

i = 10;

j = 20;

if i>5

k = 5*i+j;

else if (i==20) & (j==50)

k= i+j;

else

k=j*i;

end

end

Result:

64 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 66: Basic Matlab Guide

k =70

5. SWITCH CASE

Switch among several cases, based on expression

Syntax

switch (switch_expr)

case ‘case_expr’

statement, ..., statement

case {case_expr1, case_expr2, case_expr3, ...}

statement, ..., statement

otherwise

statement, ..., statement

end

The switch statement syntax is a means of conditionally executing code. In particular, switch executes one set of statements selected from an arbitrary number of alternatives. Each alternative is called a case, and consists of

The case statement

One or more case expressions

One or more statements

In its basic syntax, switch executes the statements associated with the first case where switch_expr == case_expr. When the case expression is a cell array (as in the second case above), the case_expr matches if any of the elements of the cell array matches the switch expression. If no case expression matches the switch expression, then control passes to the otherwise case (if it exists). After the case is executed, program execution resumes with the statement after the end.

The switch_expr can be a scalar or a string. A scalar switch_expr matches a case_expr if switch_expr==case_expr. A string switch_expr matches a case_expr if strcmp(switch_expr,case_expr) returns logical 1 (true).

PROGRAM :To use Switch statement in a program

65 | P a g e L A B - I ( E E - 6 2 4 1 )

Page 67: Basic Matlab Guide

method = 'day1';

switch (method)

case 'day1'

disp('monday')

case 'day2'

otherwise

disp('FRIDAY')

disp('Unknown method.')

end

Result:

Monday

6. BREAK

B reak terminates the execution of a for or while loop. Statements in the loop that appear after the break statement are not executed.

In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop

PROGRAM:To use break statement in a program

syms i;

for i=1:54

if i==52

break

end

m=1/i;

end

Result:

m = 0.0196

66 | P a g e L A B - I ( E E - 6 2 4 1 )