You are on page 1of 9

CONTROL ENGINEERING LAB

16/05/2014

INTRODUCTION TO MATLAB
1). What is Matlab??
MATLAB is a high-level language and interactive environment for numerical
computation, visualization, and programming. Using MATLAB, you can analyze data, develop
algorithms, and create models and applications. The language, tools, and built-in math functions
enable you to explore multiple approaches and reach a solution faster than with spreadsheets or
traditional programming languages, such as C/C++ or Java.
You can use MATLAB for a range of
applications, including signal processing and communications, image and video processing,
control systems, test and measurement, computational finance, and computational biology.

2). Matlab Overview:


On starting matlab window should roughly look as in Figure below. If it
does not, click on "View" in the menu bar and select "Desktop layout", "Default". Four main
divisions appear in the window:
1.
2.
3.
4.

Current folder displays the content of the current work-directory.


Workspace lists the variables assigned by the user,
Command history shows the last commands used,
Command window where the commands are typed and the outputs
displayed.

Mechanical Engineering Department, U.E.T, Taxila

CONTROL ENGINEERING LAB

16/05/2014

3) Commonds Manipulation:
To do work in MATLAB, you type commands at the >> prompt.
Often these commands will look like standard arithmetic, or function calls similar to many other
computer languages. By doing this, you can assign sequences to variables and then manipulate
them many ways. You can even write your own functions and programs using MATLAB's
control structures. The following sections will describe the most commonly used commands on
MATLAB and give simple examples using them.
3.1) Clear Screen:
This command is basically used to clear the screen or delete all the data in
script file.
Clc=It clears work space.
Clear all=It removes all variables.
3.2) Matrix Manupolation:
MATLAB has a variety of built-in functions to make it easier for you
to construct vectors or matrices without having to enumerate all the elements. Different types of
matrix operations are given below.
ROW MATRIX:
In matlab a row matrix is manuplated by writing the following commonds
in the commond window.
clc
clear all
a=[1 2 3]
RESULT:
a =
1

COLUMN MATRIX:
In matlab a colimn matrix is manuplated by writing the following
commonds in the commond window.
clc
clear all
a=[1;2;3]
RESULT:
a =
1
2
3

Mechanical Engineering Department, U.E.T, Taxila

CONTROL ENGINEERING LAB

16/05/2014

ZEROS MATRIX:
The zeros function is similar to the ones function. Typing zeros(m,n)
will create an m-by-n matrix of zeros, and zeros(x) will create a two-by-four matrix of zeros, if x
is defined the same way as above .e.g.
clc
clear all
zeros(4)
RESULT: ans =
0
0
0
0

0
0
0
0

0
0
0
0

0
0
0
0

&
clc
clear all
zeros(4,3)
RESULT: ans =
0
0
0
0

0
0
0
0

0
0
0
0

ONES MATRIX:
The ones function will create a matrix whose elements are all ones.
Typing ones(m,n) will create an m row by n column matrix of ones.
clc
clear all
ones(4)
RESULT: ans =
1
1
1
1

1
1
1
1

1
1
1
1

1
1
1
1

clc
clear all
ones(4,3)
RESULT: ans =
1
1
1
1

1
1
1
1

1
1
1
1

SIZE OF A MATRIX:
clc
clear all

Mechanical Engineering Department, U.E.T, Taxila

CONTROL ENGINEERING LAB

16/05/2014

a=[1 2 3;2 4 6]
size(a)
RESULT: ans =
2

LENGTH OF A MATRIX:
clc
clear all
a=[1 2 3;2 4 6]
length(a)
RESULT: ans =
3

CALL ANY NO.OR ROW


clc
clear all
a=[1 2;3 4]
a(2,2)
RESULT: a =
1
3

2
4

ans =
4

3.3)- NO.OF ELEMENTS BETWEEN TWO ENTRIES:


Clc
clear all
b=[0:2:10]
RESULT: b =
0
2
clc
clear all
linspace(0,10,20)

10

ans =
Columns 1 through 12
0
0.5263
1.0526
1.5789
3.1579
3.6842
4.2105
Columns 13 through 20
6.3158
6.8421
7.3684
9.4737
10.0000

2.1053
4.7368

7.8947

2.6316
5.2632

8.4211

8.9474

3.4)- SIMPLE VECTOR OPERATION:


a) Sum:
Mechanical Engineering Department, U.E.T, Taxila

5.7895

CONTROL ENGINEERING LAB

16/05/2014

We will often want to perform an operation on each element of a vector while


doing a computation. For example, we may want to add two vectors by adding all of the
corresponding elements. The addition (+) and subtraction (- operators are defined to work on
matrices as well as scalars.e.g.
clc
clear all
x=[1 2 3]
y=[2 4 6]
x+y
RESULT:
x =
1

y =

ans =
3

b) Multiplication:
Multiplying two matrices element by element is a little different. The *
symbol is defined as matrix multiplication when used on two matrices. To specify element-wise
multiplication, we use, *.e.g
clc
clear all
x=[1 2 3]
y=[2 4 6]
x.*y
RESULT:
x =
1

y =

ans =
2

18

3.4)-SOLVING LINEAR EQUATION:


clc
clear all

Mechanical Engineering Department, U.E.T, Taxila

CONTROL ENGINEERING LAB

16/05/2014

a=[1 2;6 8]
b=[2 4;9 6]
x=inv(a)*b
RESULT: a =
1
6

2
8

2
9

4
6

b =

x =
0.5000
0.7500

-5.0000
4.5000

3.5)- POLYNOMIALS:
FIND ROOTS OF ANY POLYNOMIAL!
clc
clear all
p=[1 0 -2 -5]
roots(p)
RESULT:

p =
1

-2

-5

ans =
2.0946
-1.0473 + 1.1359i
-1.0473 - 1.1359i
MULTIPLICATION OF TWO POLYNOMIALS
clc
clear all
p=[1 0 -2 -5]
q=[2 0 4 5]
conv(p,q)
RESULT: p =
1

-2

-5

q =
2

Mechanical Engineering Department, U.E.T, Taxila

CONTROL ENGINEERING LAB

16/05/2014

ans =
2

-5

-8

-30

-25

DERIVATIVE OF A POLYNOMIAL
clc
clear all
p=[1 0 -2 -5]
polyder(p)
RESULT: p =
1

-2

-5

ans =
3

-2

INTEGRATION OF A POLYNOMIAL
clc
clear all
p=[1 0 -2 -5]
polyint(p)
RESULT: p =
1

-2

-5

ans =
0.2500

-1.0000

-5.0000

PARTIAL FRACTION
clc
clear all
b=[-4 -8]
a=[1 6 8]
[r,p,k]=residue(b,a)
RESULT: b =
-4

-8

a =
8

r =
-4
0

Mechanical Engineering Department, U.E.T, Taxila

CONTROL ENGINEERING LAB

16/05/2014

p =
-4
-2
K= []

3.6)-PLOTTING:
MATLAB has several commands to allow you to display results of your
computations graphically. The plot command is the simplest way of doing this. If x is a
vector, plot(x) will plot the elements of x against their indices. The adjacent values of x will
be connected by lines. For example:
clc
clear all
x=[1 2 3 4 5]
plot(x)
RESULT:

clc
clear all
x=[1 2 3 4 5]
plot(x,'.')
RESULT:

Mechanical Engineering Department, U.E.T, Taxila

CONTROL ENGINEERING LAB

clc
clear all
x=[1 2 3 4 5]
y=[2 7 5 3 9]
plot(x,y)
xlabel('speed')
ylabel('time')
title('graph')

Mechanical Engineering Department, U.E.T, Taxila

16/05/2014

You might also like