Anda di halaman 1dari 10

The Islamia University of Bahawalpur

University College of Engineering and Technology

Department of Electrical Engineering


Control Systems Lab Manual
LAB SESSION: 01

Student Name: ------------------------------------------------------------------------

Roll No:

Lab Instructor Signature: --------------------------------------------------------------

Date:

AN INTRODUCTION TO MATLAB
1. 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, but in addition 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 need 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 or 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 or her own programs and functions.

2. Objectives
The objectives of this lab to give a brief general introduction to MATLAB and to familiarize you with the
most commonly used functions of MATLAB.
The notes contain exercises that you should try out as you go along and are intended to be reasonably selfcontained. However, the MATLAB manuals contain tutorial introductions and there are built-in
demonstrations and tutorial material in the MATLAB software itself.

3. 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

4. Creating and manipulating matrices and vectors

MATLAB works on matrices and vectors that may be real or complex. Variables do not have to be
declared. 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 have been echoed back to you. To suppress the echo, one uses a semi-colon
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.

Exercise 1: Investigate the effect of the following commands:


(a) v(2) (b) sum = v + w (c) diff = v w (d) vw = [v w] (e) vw(2:6) (f) v
One way to generate a column vector is to take the transpose of a row vector, as you will have found in
exercise 1(f). Column vectors can be typed in directly in one of two ways. For example, to enter the
command vector

Let
v=[2 3];
w=[5 3];
>> v(2)
ans = 3

1
1

0

0

Or

>>diff=v-w
diff= -3

>> v'
Ans=
2
3

>> vw=[v w]
vw =

>>sum=v+w
sum = 7 6

z = [1; 1; 0; 0]

Exercise 2: Investigate the effect of the following commands


(i) z (b) z*v (c) [v; w] (d) v*z (e) [z; v] (f) z + v
Let
>> z=[5 3 ;2 4];
>> v=[4 3;5 3];
>> z*v
Ans=
35 24
28 18

v=[4 3; 5 3];
w=[4 3;3 2];
>> [v;w]
ans =
4
5
4
3

>> z+v
>> ans=
9 8
5 7

3
3
3
2

Exercise 3
Investigate the effect of the following commands:
(a) N = inv(M) (b) M*N (c) I = eye(2) (d) M + I (e) M*z(1:2) (f) v(3:4)*M
(g) M(1,1) (h) M(1:2,1:2) (i) M(:,1) (j) M(2,:)
>> N=[5 4;3 5];
>> M=[5 6;8 5];
>> N=inv(M)
N=
-0.2174 0.2609
0.3478 -0.2174

% inv (command for taking inverse of a matrix)

>> r=M*N
r=
1 0
0 1

% multiplication of two vectors

>> I=eye(2)
I=
1 0
0 1

% eye (for making an identity matrix )

>> s=M+I
S=
6 6
8 6

% (addition of two vetors)

>> M(1,1)
ans =
5

% ( digit of 1 st row and 1st column)

>> M(1:2,1:2)
Ans=
5 6
8 5

% ( making of matrix M)

>> M(:,1)
ans =
5
8

% (command for taking 1 st column)

>> M(2,:)
ans =
8 5

% (command for taking 2 nd row)

5. Built-in Functions and MATLAB help


You have already encountered some built-in functions. In addition to standard arithmetic operators, such as
+ and *, you have met inv(A) that generates the inverse of A, and eye(n) that generates the n-by-n
identity matrix. 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.
Exercise 4:
Use the help command to find out about the following built-in functions and make up your own
simple examples:
1. ones 2. zeros 3. det
ones(2,2)
ans =
1 1
1 1
zeros(2,2)
ans=
0 0
0 0

M=[2 3;4 5];


ans=
2 3
4 5
det(M)=
-2

6. Polynomials in MATLAB
In Matlab, a polynomial is represented by a vector. To create a polynomial in Matlab, simply enter each
coefficient of the polynomial into the vector in descending order. For instance, let's say you have the
following polynomial:

To enter this into Matlab, just enter it as a vector in the following manner

x = [1 3 -15 -2 9]
ans =
1 3 -15 -2 9
Matlab can interpret a vector of length n+1 as an nth order polynomial. Thus, if your polynomial is
missing any coefficients, you must enter zeros in the appropriate place in the vector. For example,

would be represented in Matlab as:


y = [1 0 0 0 1]
You can find the value of a polynomial using the polyval function. For example, to find the value of the
above polynomial at s=2,
z = polyval ([1 0 0 0 1],2)
z=
17
You can also extract the roots of a polynomial. This is useful when you have a degree polynomial such as

Finding the roots would be as easy as entering the following command;

roots([1 3 -15 -2 9])


-5.5745
2.5836
-0.7951
0.7860
Let's say you want to multiply two polynomials together. The product of two polynomials is found by
taking the convolution of their coefficients. Matlab's function conv that will do this for you.

x = [1 2];
y = [1 4 8];
z = conv(x,y)
z=
1 6 16 16
Dividing two polynomials is just as easy. The deconv function will return the remainder as well as the
result. Let's divide z by y and see if we get x.

[xx, R] = deconv(z,y)
xx =
1 2
R=
0 0 0 0
As you can see, this is just the polynomial/vector x from before. If y had not gone into z evenly, the
remainder vector would have been something other than zero.
If you want to add two polynomials together which have the same order, a simple z=x+y will work (the
vectors x and y must have the same length). In the general case, the user-defined function, polyadd can be
used. To use polyadd, copy the function into an m-file, and then use it just as you would any other
function in the Matlab toolbox. Assuming you had the polyadd function stored as a m-file, and you
wanted to add the two uneven polynomials, x and y, you could accomplish this by entering the command:

z = polyadd(x,y)
x=

y=
1 2

z=
1 4 8

1 5 10

7. Matrices in MATLAB
Entering matrices into Matlab is the same as entering a vector, except each row of elements is separated
by a semicolon (;) or a return:

B = [1 2 3 4;5 6 7 8;9 10 11 12]


B=

B=

[1 2 3 4
5 6 7 8
9 10 11 12]

1 2 3 4
5 6 7 8
9 10 11 12

B=
1 2 3 4
5 6 7 8
9 10 11 12

Matrices in Matlab can be manipulated in many ways. For one, you can find the transpose of a matrix
using the apostrophe key:
C = B'

C=
1
2
3
4

5 9
6 10
7 11
8 12

It should be noted that if C had been complex, the apostrophe would have actually given the complex
conjugate transpose. To get the transpose, use .' (the two commands are the same if the matix is not
complex). Now you can multiply the two matrices B and C together. Remember that order matters when
multiplying matrice

D=B*C
D=
30 70 110
70 174 278
110 278 446
Another option for matrix manipulation is that you can multiply the corresponding elements of two
matrices using the .* operator (the matrices must be the same size to do this).

E = [1 2;3 4]
F = [2 3;4 5]

E=

G=E*F

F=

G=
1 2
3 4

2 6
12 20

2 3
4 5
If you have a square matrix, like E, you can also multiply it by itself as many times as you like by raising
it to a given power.

E^3
ans =
37 54
81 118
If wanted to cube each element in the matrix, just use the element-by-element cubing.

E.^3
ans =
1
27

8
64

You can also find the inverse of a matrix:

X = inv(E)
X=
-2.0000 1.0000
1.5000 -0.5000
There is even a function to find the coefficients of the characteristic polynomial of a matrix. The "poly"
function creates a vector that includes the coefficients of the characteristic polynomial.

p = poly(E)
p=
1.0000 -5.0000 -2.0000

Remember that the eigenvalues of a matrix are the same as the roots of its characteristic polynomial:

roots(p)
ans =
5.3723
-0.3723
8. Graphs

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.
t=0:0.1:8;
y=sin(t);
plot(t,y)

Exercise 5:
1. Plotting sine and cosine wave using subplot.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.

clc;
x=0:0.04:10;
y=sin(x*pi);
subplot(221)
plot(x,y,'g*')
xlabel('time')
ylabel('amplitude')
title('sine wave')
x=0:0.05:12;
y=cos(x*pi);
subplot(222)
plot(x,y,'m*')
xlabel('time')
ylabel('amplitude')
title('cosine wave')
x=0:0.05:15;
y=cos(x*pi);
subplot(223)
plot(x,y,'r*')
xlabel('time')
ylabel('amplitude')
title('cosine wave')
x=0:0.06:18;
y=sin(x*pi);
subplot(224)
plot(x,y,'c*')
xlabel('time')
ylabel('amplitude')
title('sine wave')
print -dtiff lab1.tiff

Anda mungkin juga menyukai