Sie sind auf Seite 1von 71

Familiarization with Matlab

Kandarpa Kumar Sarma


Department of Electronics and Communication
Technology, Gauhati University.
Background-

zA Picture Speaks a
Thousand Words

RC in Electronics and Instumentation,


2010 2
Background …

z Matlab is a product of The MathWorks Inc.


z It is an ambitious product, versatile and have
excelled as the most preferred test and
validation software.
z Matlab is derived from two words MATrix
LABoratory.
z MATLAB is an interactive, matrix-based
system for scientific and engineering numeric
computation and visualization.

RC in Electronics and Instumentation,


2010 3
Why Matlab?

z Perform a host of tasks- Mathematical, Statistical,


System-Design, Verification etc.
z Communicate with its predecessors like Fortran and C.
z Strong graphic, library and documentation support.
z Use simulation as a tool of system design by resorting
to the SIMULINK facility.
z Beginners can have a feel of the environment by using
built-in demos.
z Toolbox support.

RC in Electronics and Instumentation,


2010 4
Versions…

z Current is Matlab 2008 –usually run in a client-


server type set-up with internet access and
the PCs having fixed TCP/IPs.
z Cost- for five to ten user keys with about 10
toolboxes with complete documentation- Rs.15
Lakhs.
z Matlab 6 provides most of the useful facilities
and toolboxes required.
z Cost- for five to ten user keys with about 10
toolboxes with complete documentation- Rs.5
Lakhs.
RC in Electronics and Instumentation,
2010 5
Matlab Windows..

z Desktop Logo

RC in Electronics and Instumentation,


2010 6
Matlab Windows..

RC in Electronics and Instumentation,


2010 7
Matlab Windows..

z Command
Prompt

RC in Electronics and Instumentation,


2010 8
Matlab Windows..Menu Bar

RC in Electronics and Instumentation,


2010 9
Toolboxes….

RC in Electronics and Instumentation,


2010 10
Command History…

RC in Electronics and Instumentation,


2010 11
Getting Started with simple
arithmetic..
z Best way is to start from
the command prompt.
z Let a=5, b=10;
z It is an example of
variable assignment. In
C/ C++ such
assignments re possible
only after declaration of
the variables.
z c=a+b;
z c;

RC in Electronics and Instumentation,


2010 12
Data Types Supported……

z Variable-
z integer,
z floating point,

z character,

z strings.

RC in Electronics and Instumentation,


2010 13
Integers….data types…

z Integers are used as unsigned 8-bit


integer. No signs are associated by
definition. The values of a UINT8 range
from 0 to 255. Other such types are
UINT16, UINT32, INT8, INT16, INT32. A
complete list of data types supported can
be obtained by typing
z help datatype at the command promt.

RC in Electronics and Instumentation,


2010 14
Vectors…
z X=[1 2 3 4 5 6 7];
z X(1)=1, 1st element
z X(3)=3
z X(5)=5, 5th element;
z To plot the vector X,
simply type plot (x) in
the command
window. The plot will
be as in fig.

RC in Electronics and Instumentation,


2010 15
More on vectors.
z Let X=1:2:10; It
means assign vector
X values between 1
and 10 at gaps of 2.
z Y=2*X means Y will
be assigned all the
values of X multiplied
by two.

RC in Electronics and Instumentation,


2010 16
Addition of vectors.. They must be of
same length
z Let X1=1:5
z Let X2=1:6
z X3=X1+X2;
z Output will be

RC in Electronics and Instumentation,


2010 17
More on vectors…

z Let X=1:5;
z To square all the
elements in the
vector simply type
Y=X.^2

RC in Electronics and Instumentation,


2010 18
Vectors.. cont

z To change a row
vector to a column
vector, just type X'

RC in Electronics and Instumentation,


2010 19
Vector Multiplication….

z Let X=1:5;
z Let Y=2*X;
z Z=X.*Y; It assigns
the value of the
product between the
vectors X and Y.

RC in Electronics and Instumentation,


2010 20
Use of certain built-in functions.

z To calculate the cosine of a vector X, simply


type cos(X). To plot the value of cos(x), simply
type plot (cos (X)).

RC in Electronics and Instumentation,


2010 21
A few more built-in functions….
z Sin(x) to find the sin of
each of the elements of
the vector X
z Log (x) to find the log of
each of the elements of
the vector X. Also there
is the provision of finding
log base 10.
z exp(X) to find the
exponential of vector X

RC in Electronics and Instumentation,


2010 22
Built-in functions and use

z Mean (X) to find the


mean value of the
vector X
z Sum (X) to add all
the elements in the
vector X.

RC in Electronics and Instumentation,


2010 23
Built-in functions….

z Built-in functions have there own


documentation.
z Accessed by simply typing,
z say for sum, help sum at the command
prompt.

RC in Electronics and Instumentation,


2010 24
Difficulty with using Matlab from
command prompt
z Longer programs cannot be written.
z Previous steps cannot be modified.
z Solution is to use the Matlab editor to
write longer programs.

RC in Electronics and Instumentation,


2010 25
Matlab editor

z It can be started
by simply clicking
the new graphic
in the menu bar.
It starts a new/
blank editor
window.

RC in Electronics and Instumentation,


2010 26
First program in the Matlab
editor..
z clc means clear
screen
z Clear all means
clear all previously
declared variables.
z Close all means
close all previously
opened graphic
windows.

RC in Electronics and Instumentation,


2010 27
First program..
z X=1:10 is a vector
declaration, then
find its cosine
values are
assigned to a new
variable x1 and
then plot. The
figure call opens a
graphic window.

RC in Electronics and Instumentation,


2010 28
First program….
z Nest to save, click the
save icon or go to file
option and use save as..,
create a folder and save
the program by a name.
z To run the program simple
click the run icon after
saving. During run, the
Matlab compiler will itself
prompt to save the
program if not saved by a
name and if no so will also
save all modifications
done there after.

RC in Electronics and Instumentation,


2010 29
Special Values
z MATLAB includes a number of predefined special values.
z Can be used at any time without initializing them.
z Stored in ordinary variables.
z They can be overwritten or modified by a user.
z If a new value is assigned to one of these variables, then that
new value will replace the default one in all later calculations.
>> circ1 = 2 * pi * 10;
>> pi = 3;
>> circ2 = 2 * pi * 10;
Never change the values of predefined variables.

RC in Electronics and Instumentation,


2010 30
Special Values….
z pi: π value up to 15 significant digits
z i, j: sqrt(-1)
z Inf: infinity (such as division by 0)
z NaN: Not-a-Number (division of zero by zero)
z clock: current date and time in the form of a 6-element
row vector containing the year, month, day, hour,
minute, and second
z date: current date as a string such as 16-Feb-2004
z eps: epsilon is the smallest difference between two
numbers
z ans: stores the result of an expression

RC in Electronics and Instumentation,


2010 31
Arithmetic Operators

z addition a+b → a+b


z subtraction a-b → a-b
z multiplication axb → a*b
z division a/b → a/b
z power ab → a^b
z exponential e-a → exp(-a)

RC in Electronics and Instumentation,


2010 32
Relational Operators…

z greater than (with >)


z less than (with <)

z greater than or equal (with >=)

z less than or equal (with <=), and

z not equal (with ~=)

RC in Electronics and Instumentation,


2010 33
Hierarchy of operations
z x=3*2+6/2
z Processing order of operations is important
z parenthesises (starting from the innermost)
z exponentials (from left to right)
z multiplications and divisions (from left to right)
z additions and subtractions (from left to right)
z >> x = 3 * 2 + 6 / 2
z x=9

RC in Electronics and Instumentation,


2010 34
Complex Numbers
Matlab supports complex numbers…
>> i
>> sqrt(-1)
>> j
>> 4 + 6j
>> 4 + 6*j
>> C = 1 – 2i;
>> real(C)
>> imag(C)
>> abs(C)
>> angle(C)*180/pi
RC in Electronics and Instumentation,
2010 35
Matrices

Entering Matrices:
z Enter an explicit list of elements.
z Load matrices from external data files.
z Generate matrices using built-in functions.
z Create matrices with your functions (m-
files).

RC in Electronics and Instumentation,


2010 36
Built-in arithmetic functions
z result = function_name( input );
z abs, sign
z log, log10, log2
z exp
z sqrt
z sin, cos, tan
z asin, acos, atan
z max, min
z round, floor, ceil, fix
z mod, rem
z help elfun help for elementary math
functions
RC in Electronics and Instumentation,
2010 37
Graphs and plots…
Clc;
Clear all;
Close all;
x = 0:pi/100:2*pi;
y = sin(x);
plot( x, y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the
Sine Function')

RC in Electronics and Instumentation,


2010 38
Multiple Graphs….

Clc;
Clear all;
Close all;
t =
0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
plot(t,y1,t,y2)
grid on
RC in Electronics and Instumentation,
2010 39
Matrices…..
A = [ 1 6 3 2 1 3;
5 1 0 1 1 8;
9 6 7 1 0 2;
4 1 5 1 4 1]
z Separate the elements of a row with blanks or
commas.
z Use a semicolon, ; , to indicate the end of each row.
z Surround the entire list of elements with square
z brackets, [ ].

RC in Electronics and Instumentation,


2010 40
Matrices….. A=Magic(4)
A = [16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1]
A1=sum (A)
A2=sum(A')'
A3=sum(diag(A))
A4=fliplr(A)
A5 = sum (diag (fliplr(A)))
Fliplr-Flip matrix in left/right
direction.

RC in Electronics and Instumentation,


2010 41
Matrix.. Subscripts
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

z The element in row i and column j of A is


denoted by A(i,j), for example A(4,2) is 15.
z The ith row is denoted by A(i,:) and
z The jth column is denoted by A(:,j), for example
A(3,:) is [9 6 7 12].

RC in Electronics and Instumentation,


2010 42
Generating Matrices
z rand() Uniformly distributed random elements
z randn() Normally distributed random elements
R = randn(3,5)
R=
1.0668 0.2944 -0.6918 -1.4410 0.0593
-1.3362 0.8580 0.5711 -0.0956 0.7143
1.2540 -0.3999 -0.8323 1.6236 -1.5937

RC in Electronics and Instumentation,


2010 43
Matrix operations…

z Clc;
z Clear all; B=inv(A)
Warning: Matrix is close to singular or
z Close all; badly scaled. Results may be
inaccurate. RCOND = 1.306145e-017.
z A=magic(4);
B=
z B=inv(A) % to
calculate the 1.0e+014 *
inverse of matrix A. 0.9382 2.8147 -2.8147 -0.9382
2.8147 8.4442 -8.4442 -2.8147
-2.8147 -8.4442 8.4442 2.8147
-0.9382 -2.8147 2.8147 0.9382

RC in Electronics and Instumentation,


2010 44
Matrix operations…eigen values

z To find an
approximation to the
eigen values of a
matrix eig(A); C=eig(A)

z C=eig(A) is a vector C=
containing the eigen
34.0000
values of a square 8.9443
matrix A. -8.9443
0.0000

RC in Electronics and Instumentation,


2010 45
Vectors and Matrices.. Multiplication
& Addition
z Multiplicationand addition of vectors and
matrices must follow strict rules.
z You add A row vector cannot be added
to a column vector.
z For multiplication the number of columns
must be equal to the number of rows

RC in Electronics and Instumentation,


2010 46
Vectors and Matrices.. Multiplication
& Addition…..
z >> v = [1 2 3]‘
z Ans v = 1 2 3
z >> b = [2 4 6]‘
z Ans b = 2 4 6
z >> v+b
z Ans = 3 6 9
z >> v-b
z ans = -1 -2 -3

RC in Electronics and Instumentation,


2010 47
Vectors and Matrices.. Multiplication
& Addition…..
z v*b is not permitted.
z A display will come as
z ??? Error using ==> *Inner matrix
dimensions must agree.
z >> v*b‘
z ans = 2 4 6 4 8 12 6 12 18
z >> v'*b
z ans = 28

RC in Electronics and Instumentation,


2010 48
Matlab and Calculus.... Differentiation

zA built in function diff can be used. DIFF-


Difference and approximate derivative.
z DIFF(X), for a vector X, is [X(2)-X(1)
X(3)-X(2) ... X(n)-X(n-1)].
z DIFF(X), for a matrix X, is the matrix of
row differences,
[X(2:n,:) - X(1:n-1,:)].

RC in Electronics and Instumentation,


2010 49
Matlab and Calculus.... Differentiation…..example

zh = .001;
z x = 0:h:pi;
z diff(sin(x.^2))/h
z It means differentiation of sin x2 w.r.t h
for given values of h and x.

RC in Electronics and Instumentation,


2010 50
Matlab and Calculus....
Differentiation…..example

z INLINE function- INLINE Construct


INLINE object. INLINE(EXPR) constructs
an inline function object from the
MATLAB expression contained in the
string EXPR. Examples:
z g = inline('t^2') gives g (t) = t^2
z g = inline('sin(2*pi*f + theta)') gives
g (f, theta) = sin(2*pi*f + theta)
RC in Electronics and Instumentation,
2010 51
Differentiation…..example

z >> syms x, (SYMS Short-cut for


constructing symbolic objects.)
z f=inline('x^3', 'x')
z diff (f(x))
z ans = 3*x^2

RC in Electronics and Instumentation,


2010 52
Differentiation… use of Dsolve

z DSOLVE is Symbolic solution of ordinary


differential equations.
z DSOLVE('eqn1','eqn2', ...) accepts symbolic
equations representing ordinary differential
equations and initial conditions.
z By default, the independent variable is 't'.

RC in Electronics and Instumentation,


2010 53
Differentiation… use of
Dsolve…..example
z dsolve ('Dx = -a*x') returns
ans = exp(-a*t)*C1
z The letter 'D' denotes differentiation with
respect to the independent variable, i.e.
usually d/dt. A "D" followed by a digit
denotes repeated differentiation; e.g., D2
is d^2/dt^2.
z Initial conditions are specified by
equations like 'y(a)=b' or 'Dy(a) = b'
RC in Electronics and Instumentation,
2010 54
Differentiation… use of
Dsolve…..example
z For the equation xy’ + 1=y, the dsolve
symbolic ODE can be written as
dsolve('x*Dy + 1 =y', 'x') which returns
ans = 1+x*C1
z Y = dsolve('Dy = y^2*(1-y)') returns Y =
t+1/y-log(y)+log(-1+y)+C1=0

RC in Electronics and Instumentation,


2010 55
Dsolve…..example

z dsolve('Df = f + sin(t)', 'f(pi/2) = 0')


z dsolve('D2y = -a^2*y', 'y(0) = 1, Dy(pi/a) = 0')
z S = dsolve('Dx = y', 'Dy = -x', 'x(0)=0', 'y(0)=1')
z S = dsolve('Du=v, Dv=w, Dw=-u','u(0)=0,
v(0)=0, w(0)=1')
z w = dsolve('D3w = -w','w(0)=1, Dw(0)=0,
D2w(0)=0')
z y = dsolve('D2y = sin(y)'); pretty(y)

RC in Electronics and Instumentation,


2010 56
Calculus with Matlab…..Use of
Solve
z SOLVE Symbolic solution of algebraic
equations.
z solve ('p*sin(x) = r') chooses 'x' as the
unknown and returns ans = asin(r/p)
z [x,y] = solve('x^2 + x*y + y = 3','x^2 - 4*x + 3 =
0') returns
x= [ 1]
[ 3]
y = [ 1]
[ -3/2]
RC in Electronics and Instumentation,
2010 57
Integration using Matlab …. Use of
INT
z INT Integrate.
z Example:
syms x x1 alpha u t;
A = [cos(x*t),sin(x*t);-sin(x*t),cos(x*t)];
int(1/(1+x^2)) returns ans = atan(x)
z Example: int(‘x^2’, ‘x’) returns ans =
1/3*x^3

RC in Electronics and Instumentation,


2010 58
Integration using Matlab …. Use of
INT….
z For definite integration-
z syms x, int(exp(-x^4), 0, 1) returns ans =
int(exp(-x^4),x = 0 .. 1). For its numeric
integration quadl() can be used.
z QUADL- Numerically evaluate integral,
adaptive Lobatto quadrature.
z Q = QUADL(FUN,A,B) tries to approximate
the integral of function FUN from A to B to
within an error of 1.e-6 using high order
recursive adaptive quadrature.

RC in Electronics and Instumentation,


2010 59
Integration using Matlab …. Use of
INT and QUADL….example
z syms x, int(exp(-x^4), 0, 1);
z quadl(vectorize(exp(-x^4)), 0, 1) returns
ans = 0.8448
z Other forms of use of QUADL are
z Q = quadl('1./(x.^3-2*x-5)',0,2);
z F = inline('1./(x.^3-2*x-5)');
Q = quadl(F,0,2);

RC in Electronics and Instumentation,


2010 60
Multiple integration using Matlab

z For expressions of the following type

π 2π
f ( x, y ) = ∫ π∫ ( x cos
0
y + y sin x )dydx

dblquad maybe used for numerical double integration

RC in Electronics and Instumentation,


2010 61
Multiple integration using
Matlab….example
z Q = dblquad(inline('y*sin(x)+x*cos(y)'),
pi, 2*pi, 0, pi) which returns Q = -9.8696

RC in Electronics and Instumentation,


2010 62
Initializing with Keyboard Input

z The input function displays a prompt


string in the Command Window and then
waits for the user to respond.
z v1= input( ‘Enter an input value: ’ );
v2 = input( ‘Enter data: ’ );
v3 = input( ‘Enter data: ’ ,`s`);

RC in Electronics and Instumentation,


2010 63
Matlab- Simulink
z Tool for modeling, simulating, and analyzing
dynamic systems.
z Linear and Nonlinear systems,
z Configured in continuous time, sampled time,
or a mixture
z Signals can be fed to static and dynamic
blocks.
z Results can be observed in scopes, displays
or can be saved to a file.
z Data can be connected from one block to
another, can be branched, multiplexed etc.
RC in Electronics and Instumentation,
2010 64
Resources in Simulink

z Simulink facility
can be invoked
by typing
simulink in
the command
window

RC in Electronics and Instumentation,


2010 65
Continuous block set ….

z Continuous
time domain
operations

RC in Electronics and Instumentation,


2010 66
Discontinuities block-set

z Dead zones
z Quantizer
z Rate Limiter

RC in Electronics and Instumentation,


2010 67
Discrete Systems Block

z Resources for
signals and
processes in
discrete domain

RC in Electronics and Instumentation,


2010 68
How to create a simple Simulink
block?
z click the create
new model
option.
z blocks from block-
sets can be
dragged and
dropped

RC in Electronics and Instumentation,


2010 69
Example- Multiplying two sinusoids

RC in Electronics and Instumentation,


2010 70
Thank You

RC in Electronics and Instumentation,


2010 71

Das könnte Ihnen auch gefallen