Professional Documents
Culture Documents
INTRODUCTION TO MATLAB
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
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.
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
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
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,
y = [1 0 0 0 1]
A = [1; 2; 3]
A=
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
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
B. ^A
10
400
27000
A^2
A. ^2
2^A
??? Error using ==> ^
2. ^A
== Equal
~= not equal
A==B
2 + 2 ==4
1
A>B
A<B
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
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 =
ZZZ = Z(:,2)
ZZZ =
5
8