You are on page 1of 19

Exptno:1

INTRODUCTION TO MATLAB

High Performance Numeric Computation and Visualization Software

MATLAB stands for MATRIX LABORATORY. MATLAB is a technical


computing environment for high performance numeric computation and visualization.
MATLAB integrates numerical analysis, matrix computation, signal processing, and
graphics in an easy to use environment, where problems and solutions are expressed just
as they are written mathematically without traditional programming.

MATLAB also features of application a specific solution that is toolboxes. Areas


in which toolboxes are available include signal processing, control systems design,
dynamic systems simulation, systems identification, neural networks and others.

MATLAB integrates mathematical computing, visualization, and a powerful


language to provide a flexible environment for technical computing. MATLAB includes
tools for: Data acquisition Data analysis and exploration Visualization and image
processing Algorithm prototyping and development Modeling and simulation
Programming and application development

A BRIEF LIST OF MATLAB COMMANDS


Some Basic Commands (Note command syntax is case-sensitive!)
Matlab loads the program matlab into your workspace.
quit quits matlab, returning you to the operating system.
exit same as quit.
who lists all of the variables in your matlab workspace.
Whos list the variables and describes their matrix size.
Clear deletes all matrices from active workspace.
Save saves all the matrices defined in the current session into
the file, matlab.mat.
Load loads contents of matlab.mat into current workspace.
Save filename saves the contents of workspace into filename.
Mat

MATLAB COMMANDS
1. PLOT Linear plot
Plot (x, y) plots vector y versus vector x.
Various line types, plot symbols and colours may be obtained with
PLOT(x, y, ‘S’) where S is a character string made from one element from any
or all the following 3 tables

Line Style Specifier Marker Type Specifier


Plus sign +
solid (default) - Circle o
Asterisk *
Dashed -- Point .
Dotted : Square S
Dash-dot -. Diamond d
Pentagon p
Hexagon h
Line Specifier
Colour
Red r
Green g
Blue b
Cyan c
Magenta m
Yellow y
Black k
White w

2. STEM discrete sequence or stem plot


Stem (x, y) plots the data sequence y at the values specified in x.
3. STAIRS Stair step plot
Stairs (x, y) draws a stairs step graph of the elements in vector y at the locations
specified in x.
4. BAR- Bar graph
Bar (x,y) draws the columns of the M by N matrix.
5. PIE chart
Pie (x) draws a pie plot of the data in the vector x.
6. TITLE Graph title
Title (‘text’) adds text at the top of the current axis.
7. XLABEL x-axis label
xlabel (‘text’) adds text besides the X-axis on the current axis.

8. YLABEL y-axis label


ylabel (‘text’) adds text besides the Y-axis on the current axis.
9. AXIS
Control axis scaling and appearance
Axis ([xmin xmax ymin ymax]) set scaling for the x and y axes.
10. ZOOM
Zoom in and out on a 2-D plot
Zoom (factor) zooms the current axis by factor.
11. GRID Grid lines
Grid sets the x grid, y grid, and z grid properties of the current axis
12. DISP display array
Disp (x) if x is a string, the text is displayed

13. Subplot(x,y,z)
It will split the figure window into several rows & columns
X - no of rows
y – no of columns
z – position on the window.
14. Linespace (min, max)
It will changes the x-axis scaling from min to max
15. Log space (min, max)
It will change the scaling of x-axis,when the x-axis is logarithmic.xmin=10min
To xmax=10max.It is used in frequency response.
If A=[1 2 3;4 5 6;7 8 0]
Then,

Sorting columns and rows follow the syntax: B=sort(A,dim), where dim is the
dimension of the matrix with the value 1 for column; 2 for row. Matrix A is the variable specified
by the user.
Example:

Sorting columns:

Note that without dim being specified, the default value is 1. The default setting is ascending
order. The variable name of the sorted matrix can be omitted if no needed.

Sorting column in descending order:

Sorting row in descending order

The inverse of matrix A can be obtained with the command:


SPECIAL MATRICES
Zeros (n) creates an nxn matrix whose elements are zero.
Zeros (m,n) creates a m-row, n-column matrix of zeros.
Ones (n) creates a n x n square matrix whose elements are 1's
Ones (m, n) creates a m x n matrix whose elements are 1's.
Eye (n) creates the nxn identity matrix with 1's on the diagonal.
MISCELLANEOUS COMMANDS
length(x) returns the number elements in a vector.
size(x) returns the size m(rows) and n(columns) of matrix x.
rand returns a random number between 0 and 1.
rand(A) returns a matrix of size A of random numbers.
COMMANDS FOR DISPLAY FORMAT
format short
format long
format short e
format long e
format short g
format long g
format bank
LIST OF PREDEFINED VARIABLES
ans , pi, eps, inf, i , j

ELEMENTARY MATH FUNCTIONS


sqrt(x) , exp(x) , abs(x) , log(x) , factorial(x) , sin(x) , cos(x)

ROUNDING FUNCTIONS
Round (x) , fix(x) , ceil(x) , floor(x) , rem(x,y) , sign(x)
ALGEBRAIC OPERATIONS IN MATLAB
Scalar Calculations.
+ addition
- subtraction
* multiplication
/ right division (a/b means a ÷ b)
\ left division (a\b means b ÷ a)
Arithmetic operators.
+ Plus.
X + Y adds matrices X and Y. X and Y must have the same
dimensions unless one is a scalar (a 1-by-1 matrix).
A scalar can be added to anything.
- Minus.
X - Y subtracts matrix X from Y. X and Y must have the same
dimensions unless one is a scalar. A scalar can be subtracted
from anything.
* Matrix multiplication.
X*Y is the matrix product of X and Y. Any scalar (a 1-by-1 matrix)
may multiply anything. Otherwise, the number of columns of X
must equal the number of rows of Y.
.* Array multiplication
X.*Y denotes element-by-element multiplication. X and Y
must have the same dimensions unless one is a scalar.
A scalar can be multiplied into anything.
^ Matrix power.
Z = X^y is X to the y power if y is a scalar and X is square. If y is
an integer greater than one, the power is computed by repeated
multiplication. For other values of y the calculation
involves eigen values and eigenvectors.
Z = x^Y is x to the Y power, if Y is a square matrix and x is a
scalar, computed using eigen values and eigenvectors.
Z = X^Y, where both X and Y are matrices, is an error.
.^ Array power.
Z = X.^Y denotes element-by-element powers. X and Y
must have the same dimensions unless one is a scalar.
A scalar can operate into anything.
Relational operators.
< > Relational operators.
The six relational operators are <, <=, >, >=, ==, and ~=.
A < B does element by element comparisons between A and B
and returns a matrix of the same size with elements set to one
where the relation is true and elements set to zero where it is
not. A and B must have the same dimensions (or one can be a
scalar).
& Logical AND.
A & B is a matrix whose elements are 1's where both A and B
have non-zero elements, and 0's where either has a zero element.
A and B must have the same dimensions (or one can be a scalar).
| Logical OR.
A | B is a matrix whose elements are 1's where either A or B
has a non-zero element, and 0's where both have zero elements.
A and B must have the same dimensions (or one can be a scalar).
~ Logical complement (NOT).
~A is a matrix whose elements are 1's where A has zero
elements, and 0's where A has non-zero elements.
xor Exclusive OR.
xor(A,B) is 1 where either A or B, but not both, is non-zero.
See XOR.
ARRAY PRODUCTS
Sometimes it is desired to simply multiply or divide each
element of a matrix by the corresponding element of another
matrix. These are called 'array operations" in 'matlab'. Array or
element-by-element operations are executed when the operator is
preceded by a '.' (Period).

Thus
a.* b multiplies each element of a by the respective element of
b
a ./ b divides each element of a by the respective element of b
a .\ b divides each element of b by the respective element of a
a .^ b raise each element of a by the respective b element

Matlab Basics Tutorial

a = [1 2 3 4 5 6 9 8 7]
Matlab should return:

a=
1 2 3 4 5 6 9 8 7

t = 0:2:20

t=

0 2 4 6 8 10 12 14 16 18 20

b=a+2
b=
3 4 5 6 7 8 11 10 9

Polynomials

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]

x=
1 3 -15 -2 9

For example,

would be represented in Matlab as:

y = [1 0 0 0 1]

A = [1; 2; 3]

A=

B= [10; 20; 30]


B=

10

20

30

A'

123

B'

10 20 30

A+B

11

22

33

A-B

-9

-18

-27

A+10

11

12
13

B-5

15

25

A*B

??? Error using ==> * , Inner matrix dimensions must agree.

A.*B

10

40

90

A'*B

140

A*B'

10 20 30

20 40 60
30 60 90

A*2

A.*2

A\B

10

A.\B

10

10

10

2\A

0.5000

1.0000

1.5000

2.\A
0.5000

1.0000

1.5000

A/B

0 0 0.0333

0 0 0.0667

0 0 0.1000

A. /B

0.1000

0.1000

0.1000

A/2

0.5000

1.0000

1.5000

A. /2

0.5000
1.0000

1.5000

B^A

??? Error using ==> ^

Matrix dimensions must agree.

B. ^A

10

400

27000

A^2

??? Error using ==> ^

Matrix must be square.

A. ^2

2^A
??? Error using ==> ^

Matrix must be square.

2. ^A

Relational Operators Relational operators are used to compare matrices of equal


dimensions

< Less than

<= less than or equal

> Greater than

>= greater than or equal

== Equal

~= not equal

A==B

2 + 2 ==4
1

A>B

A<B

Vector and Matrix Manipulation

Commas or spaces are used to separate elements in a row while semicolons, ; , are used
to separate rows.

For example:

Z = [1,2,3; 4 5 6;7,8,9]

Z=

123

456

789

Some subscripting tips can be used to generate matrices, extract parts of matrices, and
manipulate matrices. The colon, : , is an important character in matrix manipulation.
First, the colon can be used to generate a vector. For example,
y=1:5

y=

12345

The colon, : , can also be used to increment in creating a vector.

x = 0:5:50

x=

0 5 10 15 20 25 30 35 40 45 50

The colon can also be used to construct larger matrices or extract portions of a matrix.
Using Z as defined above.

The following command extracts the first two rows and third column of Z.

ZZ = Z(1:2,3)

ZZ =

The colon, : , by itself can be used to specify an entire row or column.

The following command gives the second column of Z.

ZZZ = Z(:,2)

ZZZ =

5
8

You might also like