Documente Academic
Documente Profesional
Documente Cultură
By
SAMAR JYOTI SAIKIA
DEPT. OF ECE, DBCET
17/6/2013
STCSCT2013
Plan
of
Talk
Introduction
17/6/2013
STCSCT2013
Introduction
MATLAB A product by Mathworks is high performance
STCSCT2013
MATrix LABoratory
Matrices are the basic building blocks in MATLAB
Thus the fundamental numeric data object in
MATLAB is the array.
Vectors, Scalars, real matrices and complex matrices
are all automatically handled as special cases of the
basic data-object.
17/6/2013
STCSCT2013
17/6/2013
STCSCT2013
MATLAB DESKTOP
The MATLAB desktop consists of:
Command Window: Simple basic operations can be done.
Command History: It provides all the details of the
STCSCT2013
17/6/2013
STCSCT2013
and variable.
Among variables the supported types are integer,
floating point, character and strings.
The complete list of data types supported by MATLAB
can be generated by typing help datatypes in the
command window.
The simplicity with MATLAB is that, whatever the data
type, users never need to worry about its declaration
i.e. there is no need to declare variables as real or
complex. MATLAB automatically sets the data types of
variables.
17/6/2013
STCSCT2013
17/6/2013
STCSCT2013
17/6/2013
STCSCT2013
10
17/6/2013
STCSCT2013
11
STCSCT2013
12
Operators Supported
MATLAB supported an extensive number of operators.
These are:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Set Operators
17/6/2013
STCSCT2013
13
Arithmetic Operators
MATLAB supports two different types of arithmetic
operations-
17/6/2013
STCSCT2013
14
Matrix multiplication.
X*Y is the matrix product of X and Y. Any scalar (a 1-by1 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.
17/6/2013
STCSCT2013
15
Relational Operators
between variables.
Relational operators perform element-by-element
comparisons between two arrays.
They return a logical array of the same size, with
elements set to logical 1 (true) where the relation is
true, and elements set to logical 0 (false) where it is
not.
The operators <, >, <=, and >= use only the real part of
their operands for the comparison.
The operators == and ~= test real and imaginary parts.
For help type help relop.
17/6/2013
STCSCT2013
16
Logical Operators
Logical
operators perform boolean/logical
operation on variables.
The symbols &, |, and ~ are the logical array
operators AND, OR and NOT.
They work element by element on arrays.
It returns logical 0 representing false and logical 1
or any nonzero element representing true.
For help type help relop.
17/6/2013
STCSCT2013
17
Bitwise Operators
Bitwise operators are used to perform bitwise
logical AND, OR, NOT, XOR and shifting
operations.
For help type help ops.
17/6/2013
STCSCT2013
18
Set Operators
MATLAB provides a set of operators for performing set
operations
vectors.
17/6/2013
STCSCT2013
19
>> [1 2 3; 4 5 6]
ans =
1
2
3
4
5
6
For example,
>> 0:10:100
ans =
0 10 20 30 40 50 60 70 80 90 100
dimensions.
To check the dimension of an existing matrix user
can use the command
[m,n]=size(A)
it will assign the numbers of rows and columns of
A to the variables m and n respectively.
Command length(A) returns the length of vector
A.
length(A) is equivalent to max(size(A)) for nonempty arrays and 0 for empty ones.
1] All the elements of a matrix A can be put into a singlecolumn vector by the command b=A(:).
2] reshape(x, m, n)-Returns the m-by-n matrix
whose elements are taken column wise from x.
An error results if x does not have mn elements.
A.
>> A=[1 2 3; 4 5 6]
A =
1
2
3
4
5
6
>> B=A'
B =
1
4
2
5
3
6
HANDLING MATRICES
Many built in functions are there in MATLAB to create
STCSCT2013
30
ones only.
D= zeros(N): generates a matrix of size N x N
containing zeros only.
E= eye(N): generates a identity matrix of size N x N
with ones in the diagonal and zeros every where.
17/6/2013
STCSCT2013
31
ELEMENTATARY MATHEMATICAL
FUNCTIONS
TRIGONOMETRIC FUNCTIONS
EXPONENTIAL FUNCTIONS
COMPLEX
ROUNDING and REMAINDER
17/6/2013
STCSCT2013
32
17/6/2013
sin
sind
sinh
asin
asind
asinh
cos
cosd
cosh
acos
acosd
acosh
tan
tand
- Sine.
- Sine of argument in degrees.
- Hyperbolic sine.
- Inverse sine.
- Inverse sine, result in degrees.
- Inverse hyperbolic sine.
- Cosine.
- Cosine of argument in degrees.
- Hyperbolic cosine.
- Inverse cosine.
- Inverse cosine, result in degrees.
- Inverse hyperbolic cosine.
- Tangent.
- Tangent of argument in degrees.
STCSCT2013
33
Applications of exponential
functions
exp
- Exponential.
expm1
- Compute exp(x)-1 accurately.
log
- Natural logarithm.
log1p
- Compute log(1+x) accurately.
log10
- Common (base 10) logarithm.
log2
- Base 2 logarithm and dissect floating
point number.
pow2
- Base 2 power and scale floating point
number.
17/6/2013
STCSCT2013
34
abs
- Absolute value.
angle
- Phase angle.
complex - Construct complex data from real and
imaginary parts.
conj
- Complex conjugate.
imag
- Complex imaginary part.
real
- Complex real part.
unwrap - Unwrap phase angle.
isreal - True for real array.
cplxpair - Sort numbers into complex conjugate pairs.
17/6/2013
STCSCT2013
35
fix
floor
ceil
round
mod
division).
rem
sign
17/6/2013
STCSCT2013
36
window.
airy
- Airy functions.
besselj - Bessel function of the first kind.
bessely - Bessel function of the second kind.
besselh - Bessel functions of the third kind (Hankel function).
besseli - Modified Bessel function of the first kind.
besselk - Modified Bessel function of the second kind.
beta
- Beta function.
betainc - Incomplete beta function.
betaln - Logarithm of beta function.
ellipj - Jacobi elliptic functions.
ellipke - Complete elliptic integral.
erf
- Error function.
17/6/2013
STCSCT2013
37
17/6/2013
erfc
- Complementary error function.
erfcx
- Scaled complementary error function.
erfinv - Inverse error function.
expint - Exponential integral function.
gamma
- Gamma function.
gammainc - Incomplete gamma function.
gammaln - Logarithm of gamma function.
psi
- Psi (polygamma) function.
legendre - Associated Legendre function.
cross
- Vector cross product.
dot
- Vector dot product.
STCSCT2013
38
17/6/2013
STCSCT2013
39
window".
The main tool of MATLAB graphics is the plot command bearing
syntax,
plot(xvalues, yvalues, `style-options)
STCSCT2013
40
Style Options
There are many style options for changing the
appearance of a plot.
For example:
1. To join the points using a red dash-dotted line.
plot (x,y,r-.)
2. To plot the points using blue crosses without joining
them with lines, and
plot(x,y,bx)
3. To plot the points using blue crosses and joins them
with a blue dotted line.
plot(x,y,b:x)
4. Colors, symbols and lines can be combined.
Line-style Option
red (1 0 0)
g
B
y
green (0 1 0)
blue (0 0 1)
yellow (1 1 0)
-:
-.
m
C
W
magenta (1 0
1)
cyan (0 1 1)
white (1 1 1)
black (0 0 0)
solid line
(default)
dashed line
dotted line
dash-dot
line
Marker-style Option
+
plus sign
O
*
.
circle
asterisk
point
cross
s
d
^
<
square
diamond
upward pointing
triangle
downward pointing
triangle
right pointing
triangle
left pointing triangle
pentagram
hexagram
v
>
User can put labels, titles and text on a plot by using the
commands:
xlabel(text ) - labels the x-axis with text
ylabel(text ) - labels the y-axis with text
title(text)
- titles the plot with text
text(x,y,text) -places text at position x, y
gtext(text)
- use mouse to place text .
>> n=1:10;
x1=log(n);
% fun_1
x2=log10(n);
% fun_2
x3=5*exp(-n);
% fun_3
x4=rand(1,length(n)); % fun_4
plot(n,x1,'-ro',n,x2,'-.b',n,x3,'-k',n,x4,'om'),
title('plot with legend'),
legend('fun_1','fun_2','fun_3','fun_4');
Axis:
In MATLAB once plot is generated axis function can be used to
change axis limits.
Syntax:
axis([xmin xmax ymin ymax])
xmin, ymin- Minimum values for x-axis and y-axis
xmax, ymax- Maximum values for x-axis and y-axis
There are also some useful predefined string arguments for the axis
command:
STCSCT2013
53
Overlay plots:
MATLAB provides three different commands of generating overlay plots.
1. hold command.
2. line command.
3. plot command itself.
Syntax:
hold- Hold current graph
hold on- Holds the current plot and all axis properties so that
subsequent graphing commands add to the existing graph.
hold off- returns to the default mode whereby PLOT commands erase
the previous plots and reset all axis properties before drawing new plots.
line(x,y)- adds the line in vectors X and Y to the current axes.
Three-Dimensional
Plots
The plot3 command is used for visualizing three-dimensional
graphics.
Syntax:
plot3(x,y,z)
where x, y and z are three vectors of the same length, plots a line in
3-space through the points whose coordinates are the elements of x,
y and z.
Plots in 3-D can be annotated with functions already mentioned for 2D plots like xlabel, ylabel, title, text etc.
MATLAB provides a very important command view to specify the
viewing angle for an observer.
Syntax:
view(az,el)
where, argument az is the azimuth angle and el is the elevation angle.
17/6/2013
STCSCT2013
55
>> t=0:pi/70:15*pi;
f1=sin(t);
f2=cos(t);
figure, subplot(2,2,1);plot3(f1,f2,t); grid on;
axis square;
xlabel('sin(t)');ylabel('cos(t)');zlabel('t');
title('A circular helix');
subplot(2,2,2);plot3(f1,f2,t);view(0,90);
xlabel('sin(t)');ylabel('cos(t)');
title('projection in the X-Y plane');
subplot(2,2,3);plot3(f1,f2,t);view(0,0);
xlabel('sin(t)');ylabel('cos(t)');
title('projection in the X-Z plane');
subplot(2,2,4);plot3(f1,f2,t);view(90,0);
xlabel('sin(t)');ylabel('cos(t)');
title('projection in the Y-Z plane');
17/6/2013
STCSCT2013
57
>> t=linspace(-2*pi,2*pi,20);
>> x=linspace(0,0.5,8);
x=tan(cos(t1));
y=x*0.75;
figure, stem(t1,x1);
z=2*sin(x)+3*cos(y);
title('use of stem');
stem3(x,y,z);
xlabel('t');ylabel('tan(cos(t))');
graphics.
bar command
hist command
stair command
MATLAB command bar is used to present data in vertical
columns.
Syntax
bar(x,y)
It draws the columns of the M-by-N matrix y as M groups of
N vertical bars. The vector X must not have duplicate values.
>> n=1000
>> x=[1 4 6 8]
y= [5 2;5 4;3 4; 3.5 3]
figure, bar(x, y),
title('Bar plot')
beta=1.5
y=-beta*log(rand(1,n))
x=0.2:0.4:10
figure, hist(y,x),
ylabel('count'),
>> a
=linspace(0,2*pi,30);
b=linspace(2*pi,4*pi,30);
x=[a b];
figure, stairs(x,sin(x))
title('Stair plot');
title(' Histogram')
Generation of m-file
Generation of m-FILE
It is already known that using command prompt we
editor.
In matlab editor we can write the codes and the
codes can be modified in the time of writing or
later.
Store the executed files.
Generate the user defined function which
reduces the code complexity and code
redundancy in the main program.
Matlab Editor
The editor is a platform in the Matlab package by
Matlab Editor
Function
clc
clear all
close all
functions.
Space is not allowed in the m- file name.
Generation of m-file
Conclusion
Now a days MATLAB is used in every field of research
or in labs or in industry.
The basic component which MATLAB deals with is the
matrix.
Whatever the field of application, MATLAB converts
all the data into matrix or matrix arrays.
We have discussed briefly the introduction of matlab,
data types used, different types of operators it
supports.
We have also discussed how to create an m-file.
17/6/2013
STCSCT2013
71
References
Sarma, K.K. (2010), MATLAB Demystified, Basic
17/6/2013
STCSCT2013
72
Thank You
17/6/2013
STCSCT2013
73