You are on page 1of 10

DEPARTMENT OF ELECTRICAL (TELECOMMUNICATION) ENGINEERING SUKKUR INSTITUTE OF BUSINESS ADMINISTRATION Feedback Control Systems (Lab) Name: __________________________________________ Date:

_________________________ Score: _____________________Signature of Instructor_________________________________ Lab Practice # 01 AN INTRODUCTION TO MATLAB

Objectives: The objectives of these notes are to give a brief general introduction to MATLAB and to familiarize you with the most commonly used functions. The notes contain exercises that you should try out as you go along and are intended to be reasonably self-contained. However the MATLAB manuals contain tutorial introductions and there are built-in demonstrations and tutorial material in the MATLAB software itself. To become familiar with MATLAB Declaring matrices in MATLAB command window. Performing various matrix operations, to manipulate them. Basic plotting techniques

Theory: What is MATLAB? The name MATLAB is short for MATrix LABoratory. It is a commercial software package whose main function is to perform calculations on matrices, row vectors and column vectors. It is widely used in both industry and academic institutions. It possesses many of the features of a high level, numerically oriented programming language, and it has a large collection of built-in functions for performing matrix and vector operations in a way that is very simple for the user. For example, to find the determinant of a matrix A one needs only enter: det(A). MATLAB commands can be entered one line at a time, or the user can write programs of MATLAB code and define his/her own functions. In this session, we shall only use the one-line-at-a-time interpretive mode for simplicity, but the regular user will find that the power of the package is considerably extended by writing his/her own programs and functions.

Use Of Matlab In Control Engineering Its a very powerful tool used in control system designing. User can develop its own applications, can find the desired Transfer Function, simulate and analyze the result, check out various possibilities for multiple inputs, can work in frequency and time domain, develop your own block diagrams etc. Simulink library gives a huge range of defined blocks for creating applications without writing codes.

Entering and quitting MATLAB To enter MATLAB, simply double click on the MATLAB icon. To leave MATLAB and return to the PCs operating system simply type: quit

Creating and manipulating matrices and vectors MATLAB works on matrices and vectors that may be real or complex. Results can be displayed in a variety of different formats. Let us enter a row vector into the MATLAB workspace. Type in: v = [2 4 7 5]

This creates a variable v whose current value is a row vector with four elements as shown. After pressing return the value of v will be echoed back to you. To suppress the echo, one uses a semicolon following the command line. Thus w = [1 3 8 9]; Creates another row vector w, but does not echo. To check that w has appeared in the MATLAB workspace, type who. Which will display all the variables in the MATLAB workspace, and to check the value of w simply type w. Operations on row vectors can be best illustrated by some simple exercises. Column vectors can be typed in directly in one of two ways. For example, to enter the command vector
1 1 z 0 0

you can type z = [1; 1; 0; 0];

or

z = [1 1 0 0];

One way of creating matrices is by multiplying row and column vectors. There are various ways of entering matrices. For example, to enter the matrix
1 2 M 3 4

the most obvious ways are to type M = [1 2; 3 4]; M = [1 2 or

3 4]; but there are more complex ways such as M = [[1 3][2 4]];

Operators Used In MATLAB Point operator: point operator is used for element-by-element calculations e.g. C = A.*A D = A./B It will multiply corresponding elements of A with A. It will divide corresponding elements of A by B.

(note: order of matrices must be same)

Transpose: It converts column vector into row vector and vice versa e.g. A= B= B= [1 2 3 4] A 1 2 3 4 is a row vector B equals to the transpose of A. B is a column vector.

Semicolon: o If used inside an array, semicolon terminates the corresponding line operation and shifts the cursor to the next line without executing it i.e. it breaks up a row and shifts the next element to the next line or row e.g.
3

C = [0;2;4;6] C= 0 2 4 6 C is a column vector.

o If used after an array, semicolon suppresses the echo of vectors and matrices when ENTER is pressed. Parenthesis: ( ) are used to execute any function you specify e.g. eye(2) Arithmetic Operators: Arithmetic operators perform numeric computations, for example, adding two numbers or raising the elements of an array to a given power. The following table provides a summary. Operator + .* ./ .\ .^ * / \ ^ Description Addition Subtraction Multiplication Right division Left division Power Matrix multiplication Matrix right division Matrix left division Matrix power

Relational Operators: Relational operators compare operands quantitatively, using operators like "less than" and "not equal to." The following table provides a summary. Operator < <= > >= Description Less than Less than or equal to Greater than Greater than or equal to
4

Operator == ~=

Description Equal to Not equal to

Logical Operators: The symbols &, |, and ~ are the logical array operators AND, OR, and NOT. These operators are commonly used in conditional statements, such as if and while, to determine whether or not to execute a particular block of code. Logical operations return a logical array with elements set to 1 (true) or 0 (false), as appropriate. These are shown in the table below. Logical Operation A&B A|B ~A Equivalent Function and(A, B) or(A, B) not(A)

Colon: o Creates a list of numbers e.g. A=0:5 A=0 1 2 3 4 5 o Works with all the entries in specified dimensions e.g. Q = [1 2 ; 3 4]; R = Q(: , 2) R=3 4

Built-in Functions and MATLAB help MATLAB has a very large number of built-in functions. To see the full list of those variables on your machine type help To get information on a specific function (inv for example) type help inv The help command is very useful. Most of the MATLAB built-in functions have very descriptive names and are easily remembered or guessed. Some of them are: clear clc trigonometric functions zeros and ones
5

inv( ) linspace( ) logspace( ) eye( ) det( ) diag( )

Basic Plotting Commands and Codes: Continuous Time Plots: Plot command: PLOT command is used to plot continuous tome signal defined in time domain as shown in figure. Plot(t,y) where y is desired function and t is time. Subplot: It is used to display 2 or more graphical function on the same graph with two different plots as shown in the figure. Example: subplot(2,1,1) 2 shows number of rows a figure window is broken up into. 1 shows number of columns a figure window is broken up into. Last 1 shows, out of 2 functions 1st function is plotted first. (Note: Use plot command first) subplot(2,1,2)

Multiple functions on same plot: to plot two or more function simultaneously on one graph use the following code Plot (t,function1, t,function2) Plot(t,y1,t,y2) Plot(t,y1,o,t,y2,*)

Plotting with symbols: To plot certain distinct values on the continuous graph use the following code. Plot(t,y,*) Graph along function and distinct points are plotted

Bar charts: Bar(x)

Discrete Plots: Stem Command: STEM command to plot discrete time signals. Define n vector rather t. Modifying Your Graph: AXIS Command: Define x and y axis and give range for x and y axis axis ([x1 x2 y1 y2]) X-LABEL Command: It gives x-axis appropriate label xlabel(time in seconds) Y-LABEL Command: It gives x-axis appropriate label ylabel(particular function) TITLE: It gives suitable title to your graph. title(Electronics) TEXT: It adds text on the desired position (row and column) on the graph. text (a, b,Electronics Engineering) text (1.3,2.1,Electronics Engineering) Where a and b are row and column positions
7

Legend: legend(string1, string2 , string3 , ...) puts a legend on the current plot using the specified strings as labels. HOLD ON: hold on holds the current plot and all axis properties so that subsequent graphing commands add to the existing graph. hold on sets the Next Plot property of the current figure and axes to "add".

Program to plot a graph The results of signal processing computations in MATLAB are huge matrices containing, for example, frequency responses, which one would like to display in graphical form. We will go through a simple example of generating data, plotting the graph, putting labels on the axes etc. We will plot the graph of y = sin(t); for t going from 0 to 20 seconds. First we need to generate a vector t containing the values of time that we want to use. Type: t=linspace(0,20,100); which makes t into a row vector of 100 values from 0 to 20 inclusive. Then type ys = sin(t); To plot the set of points type: Plot(t,ys) The basic graph can be prettied up using the following self explanatory sequence of commands: xlabel(Time in seconds) ylabel(sin(t)) title(your Roll No.) grid Explore: whos, who, pwd, hold, sqrt, exp function and M-file. (Attach their answers in the handouts).

Exercise: You are required to perform them on MATLAB and copy those to word and attach printouts.

1 1 2 3 1 If v = [2 4 7 5], and w = [1 3 8 9]; z and M 2 3 1 0 4 6 5 0


1. Investigate the effect of the following command and explain operation/function: a. v(2) b. sum = v + w c. diff = v w d. vw = [v w] e. vw(2: 6) f. v g. z h. z*v i. [v; w] j. v*z k. [z; v] l. z + v m. M(1,1) n. M(1:2,1:2) o. M(:,1) p. M(2,:) q. M(:,end) r. M(:,end-1) s. M(:,[1 3]) t. M(:,:) u. M(:,2)=[] v. fliplr(M) w. flipud(M) x. M(:) convert matrix column vector y. M=M(:) convert matrix to row vector 2. Apply DIAG function to vector v and matrix M mentioned in exercise#1. Explain the results 3. Generate a row vector a=1:5; Investigate the following commands and explain: i. a(2)=6; ii. a([ 1 3])=0 iii. a(1,2)=100 iv. a(3)=[ ] v. a([1 4])=[]

4. Write a matlab code to plot following functions( attach graphs). Also provide axes and labels to graphs Use t=1:0.01:10 (i) cos (pi*t) (ii) sin(t) (iii) (iii) cos(t) (iv) (iv) cosec(2*pi*t) 5. Repeat problem 4 using subplot and legend commands for plotting the given functions. 6. Repeat problem 4 using the commands plot(t,y1,t,y2..) and legend. (NOTE: Write your roll number , xlabel, ylabel on each graph. 7. Differentiate Linspace(1,10) and logspace(1,10) . 8. How can you save a program in MATLAB. What is the advantage of writing a program in M-file and if any error occurs in M-file how can you check that error. What role command window plays here? Observation: Attach the prints of the plots you obtain in the exercises above.

10