Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
52 views

The Matlab Language

MATLAB is a high-level language and interactive environment. You can use MATLAB in a wide range of applications, including signal and image processing, communications, control design, test and measurement, and computational biology. MATLAB eliminates the need for 'for' loops, as a result, one line of MATLAB code can often replace several lines of C or C++ code.

Uploaded by

Shubham Garg
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

The Matlab Language

MATLAB is a high-level language and interactive environment. You can use MATLAB in a wide range of applications, including signal and image processing, communications, control design, test and measurement, and computational biology. MATLAB eliminates the need for 'for' loops, as a result, one line of MATLAB code can often replace several lines of C or C++ code.

Uploaded by

Shubham Garg
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 30

MATLAB:

MATLAB® is a high-level language and interactive


environment that enables you to perform computationally
intensive tasks faster than with traditional programming
languages such as C, C++, and Fortran.

You can use MATLAB in a wide range of applications,


including signal and image processing, communications,
control design, test and measurement, financial modeling
and analysis, and computational biology. Add-on toolboxes
(collections of special-purpose MATLAB functions, available
separately) extend the MATLAB environment to solve
particular classes of problems in these application areas.

MATLAB provides a number of features for documenting and


sharing your work. You can integrate your MATLAB code with
other languages and applications, and distribute your
MATLAB algorithms and applications.

Key Features
• High-level language for technical computing
• Development environment for managing code, files,
and data
• Interactive tools for iterative exploration, design, and
problem solving
• Mathematical functions for linear algebra, statistics,
Fourier analysis, filtering, optimization, and numerical
integration
• 2-D and 3-D graphics functions for visualizing data
• Tools for building custom graphical user interfaces
• Functions for integrating MATLAB based algorithms
with external applications and languages, such as C,
C++, Fortran, Java, COM, and Microsoft Excel

The MATLAB® Language

1
The MATLAB® language supports the vector and matrix
operations that are fundamental to engineering and
scientific problems. It enables fast development and
execution.

With the MATLAB language, you can program and develop


algorithms faster than with traditional languages because
you do not need to perform low-level administrative tasks,
such as declaring variables, specifying data types, and
allocating memory. In many cases, MATLAB eliminates the
need for ‘for’ loops. As a result, one line of MATLAB code can
often replace several lines of C or C++ code.

At the same time, MATLAB provides all the features of a


traditional programming language, including arithmetic
operators, flow control, data structures, data types, object-
oriented programming (OOP), and debugging features.

2
EIGEN VALUES:

Eigenvalues and eigenvectors.


E = EIG(X) is a vector containing the eigenvalues of a square
matrix X.

[V,D] = EIG(X) produces a diagonal matrix D of eigenvalues


and a
full matrix V whose columns are the corresponding
eigenvectors so
that X*V = V*D.

[V,D] = EIG(X,'nobalance') performs the computation with


balancing
disabled, which sometimes gives more accurate results for
certain
problems with unusual scaling. If X is symmetric,
EIG(X,'nobalance')
is ignored since X is already balanced.

E = EIG(A,B) is a vector containing the generalized


eigenvalues
of square matrices A and B.

[V,D] = EIG(A,B) produces a diagonal matrix D of


generalized
eigenvalues and a full matrix V whose columns are the
corresponding eigenvectors so that A*V = B*V*D.

EIG(A,B,'chol') is the same as EIG(A,B) for symmetric A and


symmetric

3
positive definite B. It computes the generalized eigenvalues
of A and B
using the Cholesky factorization of B.
EIG(A,B,'qz') ignores the symmetry of A and B and uses the
QZ algorithm.
In general, the two algorithms return the same result,
however using the
QZ algorithm may be more stable for certain problems.
The flag is ignored when A and B are not symmetric.

% calculating the eigen values


a=[1 2 1;2 4 6;-5 4 8]
'the eigen values of the given matrix are'
eig(a)

OUTPUT:

a=

1 2 1
2 4 6
-5 4 8

ans =

the eigen values of the given matrix are

4
ans =

-1.3742
3.8853
10.4889

NYQUIST:

NYQUIST Nyquist frequency response of LTI models.

NYQUIST(SYS) draws the Nyquist plot of the LTI model SYS


(created with either TF, ZPK, SS, or FRD). The frequency
range
and number of points are chosen automatically. See
BODE for
details on the notion of frequency in discrete-time.

NYQUIST(SYS,{WMIN,WMAX}) draws the Nyquist plot for


frequencies
between WMIN and WMAX (in radians/second).

NYQUIST(SYS,W) uses the user-supplied vector W of


frequencies
(in radian/second) at which the Nyquist response is to be
evaluated.
See LOGSPACE to generate logarithmically spaced
frequency vectors.

5
NYQUIST(SYS1,SYS2,...,W) plots the Nyquist response of
multiple
LTI models SYS1,SYS2,... on a single plot. The frequency
vector
W is optional. You can also specify a color, line style, and
marker
for each system, as in
nyquist(sys1,'r',sys2,'y--',sys3,'gx').

[RE,IM] = NYQUIST(SYS,W) and [RE,IM,W] = NYQUIST(SYS)


return the
real parts RE and imaginary parts IM of the frequency
response
(along with the frequency vector W if unspecified). No
plot is
drawn on the screen. If SYS has NY outputs and NU
inputs,
RE and IM are arrays of size [NY NU LENGTH(W)] and the
response
at the frequency W(k) is given by RE(:,:,k)+j*IM(:,:,k).

% to construct a nyquist plot


clc
clear
sys=tf([2],[conv([0.2 1 0],[0.5 1])])
nyquist(sys)

6
OUTPUT:

Transfer function:
2
---------------------
0.1 s^3 + 0.7 s^2 + s

7
ROOT LOCUS:

RLOCUS(SYS) computes and plots the root locus of the


single-input,
single-output LTI model SYS. The root locus plot is used
to
analyze the negative feedback loop

8
+-----+
---->O----->| SYS |----+---->
-| +-----+ |
| |
| +---+ |
+-------| K |<----+
+---+

and shows the trajectories of the closed-loop poles when


the feedback
gain K varies from 0 to Inf. RLOCUS automatically
generates a set of
positive gain values that produce a smooth plot.

RLOCUS(SYS,K) uses a user-specified vector K of gain


values.

RLOCUS(SYS1,SYS2,...) draws the root loci of multiple LTI


models
SYS1, SYS2,... on a single plot. You can specify a color,
line style,
and marker for each model, as in
rlocus(sys1,'r',sys2,'y:',sys3,'gx').

[R,K] = RLOCUS(SYS) or R = RLOCUS(SYS,K) returns the


matrix R of
complex root locations for the gains K. R has LENGTH(K)
columns
and its j-th column lists the closed-loop roots for the gain
K(j).

9
% plotting root locus and checking the stability
clc
clear
s1=tf([1 1],[conv([1 3 0],[1 2 5])]);
t1=feedback(s1,1)
figure(1);
rlocus(t1)
for k=1:.1:100;
sys=k*s1;
sys1=feedback(sys,1);
p=pole(sys1);
r=real(p);
if max(r)>=0
'the system becomes unstable for values of gain k greater than
and equal to'
k=k
break
end
end

s2=tf([3 2],[1 6 0 0]);


t2=feedback(s2,1)
figure(2);
rlocus(t2)
'system is stable for all values of gain k'

s3=tf([1],[1 2 0 0]);
t3=feedback(s3,1)
figure(3);
rlocus(t3)
'system is unstable for all values of gain k'

OUTPUT:

10
Transfer function:
s+1
-------------------------------
s^4 + 5 s^3 + 11 s^2 + 16 s + 1

ans =

the system becomes unstable for values of gain k greater than and
equal to

k=

24.5000

Transfer function:
3s+2
---------------------
s^3 + 6 s^2 + 3 s + 2

ans =

system is stable for all values of gain k

Transfer function:
1
---------------
s^3 + 2 s^2 + 1

ans =

system is unstable for all values of gain k

11
FIGURE 1

12
FIGURE 2
Root Locus
6

2
Imaginary Axis

-2

-4

-6
-6 -5 -4 -3 -2 -1 0
Real Axis

13
FIGURE 3

Root Locus
5

1
Imaginary Axis

-1

-2

-3

-4

-5
-6 -5 -4 -3 -2 -1 0 1 2
Real Axis

STATE SPACE MODEL:

14
SS Creates state-space model or converts model to state
space.

SYS = SS(A,B,C,D) creates a SS object SYS representing


the
continuous-time state-space model
dx/dt = Ax(t) + Bu(t)
y(t) = Cx(t) + Du(t)
You can set D=0 to mean the zero matrix of appropriate
dimensions.
If one or more of the matrices A,B,C,D have uncertainty,
SS returns
an uncertain state-space (USS) model (Robust Control
Toolbox only).

SYS = SS(A,B,C,D,Ts) creates a discrete-time state-space


model with
sample time Ts (set Ts=-1 if the sample time is
undetermined).

SYS = SS creates an empty SS object.


SYS = SS(D) specifies a static gain matrix D.

CONVERSION:

SS2TF State-space to transfer function conversion.


[NUM,DEN] = SS2TF(A,B,C,D,iu) calculates the transfer
function:

NUM(s) -1
H(s) = -------- = C(sI-A) B + D
DEN(s)
of the system:
.
x = Ax + Bu
y = Cx + Du

from the iu'th input. Vector DEN contains the coefficients


of the

15
denominator in descending powers of s. The numerator
coefficients
are returned in matrix NUM with as many rows as there
are
outputs y.

CONTROLLABILITY:

CTRB Compute the controllability matrix.

CO = CTRB(A,B) returns the controllability matrix [B AB


A^2B ...].

CO = CTRB(SYS) returns the controllability matrix of the


state-space model SYS with realization (A,B,C,D). This is
equivalent to CTRB(sys.a,sys.b).

OBSERVABILITY:

OBSV Compute the observability matrix.

OB = OBSV(A,C) returns the observability matrix [C; CA;


CA^2 ...]

CO = OBSV(SYS) returns the observability matrix of the


state-space model SYS with realization (A,B,C,D). This is
equivalent to OBSV(sys.a,sys.c).

16
%obtaining response curves using matlab with
initial state X(0)=0

a=[-1 1;-1 0]
b=[0;2]
c=[1 0]
d=[0]
sys=ss(a,b,c,d)
t=0:.1:10;

%for unit step input


u(1:length(t))=1;
y=lsim(sys,u,t);
figure(1);
plot(y)

%for exponentially decaying input


u1=exp(-t);
z=lsim(sys,u1,t);
figure(2);
plot(z)

17
OUTPUT:
a=

-1 1
-1 0

b=

0
2

c=

1 0

d=

a=
x1 x2
x1 -1 1
x2 -1 0

b=
u1
x1 0
x2 2

c=
x1 x2

18
y1 1 0

d=
u1
y1 0

FIGURE 1
2.5

1.5

0.5

0
0 20 40 60 80 100 120

FIGURE 2

19
0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

-0.1
0 20 40 60 80 100 120

% determination of controllability and


observability

a=[-1 -3 -3;0 -2 1;2 0 -1];


b=[3;0;1];
c=[1 2 0];
d=[0];
sys=ss(a,b,c,d);
co=ctrb(sys);
ob=obsv(sys);
x=rank(a);
y=rank(co);
z=rank(ob);
if x==y
'system is controllable'
else
'system is uncontrollable'
end
if x==z
'system is observable'
else
'system is not observable'
End

20
OUTPUT
a=

-1 -3 -3
0 -2 1
2 0 -1

b=

3
0
1

c=

1 2 0

d=

21
a=
x1 x2 x3
x1 -1 -3 -3
x2 0 -2 1
x3 2 0 -1

b=
u1
x1 3
x2 0
x3 1

c=
x1 x2 x3
y1 1 2 0

d=
u1
y1 0

Continuous-time model.

ans =

system is controllable

ans =

system is observable

22
BODE PLOT:

BODE Bode frequency response of LTI models.

BODE(SYS) draws the Bode plot of the LTI model SYS


(created with
either TF, ZPK, SS, or FRD). The frequency range and
number of
points are chosen automatically.

BODE(SYS,{WMIN,WMAX}) draws the Bode plot for


frequencies
between WMIN and WMAX (in radians/second).

[MAG,PHASE] = BODE(SYS,W) and [MAG,PHASE,W] =


BODE(SYS) return the
response magnitudes and phases in degrees (along with
the frequency
vector W if unspecified). No plot is drawn on the screen.
If SYS has NY outputs and NU inputs, MAG and PHASE are
arrays of

23
size [NY NU LENGTH(W)] where MAG(:,:,k) and
PHASE(:,:,k) determine
the response at the frequency W(k). To get the
magnitudes in dB,
type MAGDB = 20*log10(MAG).

MARGIN:

[Gm,Pm,Wcg,Wcp] = MARGIN(SYS) computes the gain


margin Gm, the
phase margin Pm, and the associated frequencies Wcg
and Wcp,
for the SISO open-loop model SYS (continuous or
discrete).
The gain margin Gm is defined as 1/G where G is the gain
at
the -180 phase crossing. The phase margin Pm is in
degrees.

% to draw bode plot and determine the maximum phase


lag
clc
clear
sys=tf([0.5 1],[0.2 1])
bode(sys)
[gm,pm,wcg,wcp] = margin(sys)
'the phase lag provided by the given system is'
lag=pm

OUTPUT:
Transfer function:

24
0.5 s + 1
---------
0.2 s + 1

gm =

Inf

pm =

-180

wcg =

NaN

wcp =

ans =

the phase lag provided by the given system is

lag =

-180

25
B o d e D ia g r a m
8

6
M a g n itu d e (d B )

0
3 0

2 0
P h a s e (d e g )

1 0

0
- 1 0 1 2
1 0 1 0 1 0 1 0
F re q u e n c y ( r a d /s e c )

26
Simulink
Simulink is a software package for modeling, simulating, and
analyzing dynamic systems. It supports linear and nonlinear
systems, modeled in continuous time, sampled time, or a
hybrid of the two. Systems can also be multirate, i.e., have
different parts that are sampled or updated at different
rates.

For modeling, Simulink provides a graphical user interface


(GUI) for building models as block diagrams, using click-and-
drag mouse operations. With this interface, you can draw
the models just as you would with pencil and paper (or as
most textbooks depict them). This is a far cry from previous
simulation packages that require you to formulate
differential equations and difference equations in a language
or program. Simulink includes a comprehensive block library
of sinks, sources, linear and nonlinear components, and
connectors. You can also customize and create your own
blocks.

27
Effects of non-linearities:

1
s2 + 3 s+ 1 0
T ra n sfe r F c n 2

1
1 0
s2 + 3 s + 1 0
S te p G a in S a t u r a t iT o r na n s f e r F c n S c o p e

1
s2 + 3 s + 1 0
S a t u r a B t i ao cn k 1l a T s r h a n s f e r F c n 1

28
Step Response:

4
s2 + 4
d a m p in g ra tio = 0

4
s2 + 4 s + 4
d a m p in g ra tio = 1

4
S c o p e
S te p s2 + 2 s + 4
d a m p in g ra tio = 0 .5

4
s2 + 6 s + 4
d a m p in g ra tio = 1 .5

29
INDEX

S.N TITLE PAGE SIGNATUR


o. No. E

1. Introduction of MATLAB 1-2

2. Determination of “Eigen Values” 3-4

3. Constructing “Nyquist Plot” 5-7

4. Constructing “Root Locus” 8-13

5. Study of “State Space Model” 14-21

6. Constructing “Bode Plot” 22-24

7. Study of “Simulink” 25-29

30

You might also like