Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Dr. N. YADAIAH
Associate professor
Dept. of Electrical & Electronics Engineering
J N T University, College of Engineering, HYDERABAD
Yadaiahn@hotmail.com
Development Environment: This is the set of tools and facilities that help us to
use MATLAB functions and files. Many of these tools are graphical user
interfaces. It includes the MATLAB desktop and Command Window, a command
history, an editor and debugger, and browsers for viewing help, the workspace,
files, and the search path.
Graphics: MATLAB has extensive facilities for displaying vectors and matrices
as graphs, as well as annotating and printing these graphs. It includes high-level
functions for two-dimensional and three-dimensional data visualization, image
processing, animation, and presentation graphics. It also includes low-level
functions that allow you to fully customize the appearance of graphics as well as
to build complete graphical user interfaces on your MATLAB applications.
General information
computer tells you the computer type you are using
clock gives you wall clock time and date as a vector.
date tells you the date as a string.
more controls the paged output according to the screen size.
ver gives the license and the version information about MATLAB
installed on your computer.
bench benchmarks your computer on running MATLAB.
compared to other computers.
cputime Elapsed CPU time.
etime Elapsed time function
tic Start stopwatch timer
toc Read stopwatch timer
Punctuation Marks and Other Symbols
, Comma: A comma is used to:
• separate variables in the input and output list of a function,
Example: [t ,x] = ode23 ( 'pend' ,to ,tf ,x0),
• separate the row and column indices in a matrix,
Example: A(m,n), A(l: 10,3) etc.,
• separate different commands on the same line.
Example: plot(x, y) , grid, xlabel('x') etc.
; Semicolon: A semicolon is used to:
• suppress the MATLAB' output of a command,
Example: x=1:10 ; y= A*x; etc.,
MATLAB WINDOWS:
Once the program starts, the window that opens, shown below, contains three
smaller windows, which are:
the Command Window,
the Workspace Window, and
the Command History Window.
But default, all directories that are installed by the MATLAB installer are included in this
path.
Step 2:
• So far we have talked about how to access the MATLAB and its features.
• As we know that, in any programming language is consists of:
(i) Data types
(ii) Algebraic and logical operators.
(iii) Mathematical relations.
(iv) Control statements
(v) Input and output accessing
(vi) Functions and subprograms.
The MATLAB is very big package and but, it is almost equal to a language. We now
discuss the major features of programming language with MATLAB.
Method:
1. Create a new file:
2. Type the code
3. Write & save with suitable name with .m extension.
4. Now go back to MATLAB command prompt and type the file without .m
to run.
Method:
1 Create a new file:
2 Type the code
3 Write & save with suitable name with .m extension.
4 Now go back to MATLAB command prompt and type the file
without .m to run.
Numeric data types include signed and unsigned integers, single- and double-
precision floating point, and sparse matrices of double-precision. The following
hold true for numeric data types in MATLAB:
A cell array provides a storage mechanism for dissimilar kinds of data. For
example, you can store a 1-by-50 char array, a 7-by-13 double array, and a 1-by-
1 uint32 in cells of the same cell array.
The MATLAB structure data type is similar to the cell array in that it also stores
dissimilar kinds of data. But, in this case, it stores the data in named fields rather
than in cells.
MATLAB data types are implemented as classes. We can also create MATLAB
classes of our own.
/ Slash or matrix right division. B/A is roughly the same as B*inv(A). More
precisely, B/A =(A'\B')'.
./ Array right division. A./B is the matrix with elements A(i,j)/B(i,j). A and B
must have the same size, unless one of them is a scalar.
.^ Array power. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A
and B must have the same size, unless one of them is a scalar.
' Matrix transpose. A' is the linear algebraic transpose of A.
.' Array transpose. A.' is the array transpose of A. For complex matrices, this
does not involve conjugation.
Operator Description
& AND
| OR
~ NOT
Operator Precedence
It can build expressions that use any combination of arithmetic, relational, and
logical operators. Precedence levels determine the order in which MATLAB
evaluates an expression. The precedence rules for MATLAB operators from
highest precedence level to lowest precedence level:
1. Parentheses ()
2. Transpose(.'), power(.^), complex conjugate transpose('), matrix
power(^)
3. Unary plus (+), unary minus (-), logical negation (~)
4. Multiplication (.*), right division (./), left division(.\), matrix multiplication
(*),
matrix right division (/), matrix left division (\)
5. Addition (+), subtraction (-)
6. Colon operator (:)
7. Less than (<), less than or equal to (<=), greater than (>), greater than or
equal
to (>=), equal to (==), not equal to (~=)
8. Logical AND (&)
9. Logical OR (|)
if statements
• The if statement evaluates a logical expression and executes a group of
statements when the expression is true.
if rem(n,2) ~= 0
M = odd_magic(n)
elseif rem(n,4) ~= 0
M = single_even_magic(n)
else
M = double_even_magic(n)
end
The optional elseif and else keywords provide for the execution of alternate
groups of statements.
An end keyword, which matches the if, terminates the last group of statements.
The groups of statements are delineated by the four keywords - no braces or
brackets are involved.
Only the first matching case is executed. There must always be an end to match
the switch.
The logic of the magic squares algorithm can also be described by
for n = 3:32
r(n) = rank(magic(n));
end
r
The semicolon terminating the inner statement suppresses repeated printing, and
the r after the loop displays the final result.
It is a good idea to indent the loops for readability, especially when they are
nested.
for i = 1:m
for j = 1:n
H(i,j) = 1/(i+j);
end
end
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if sign(fx) == sign(fa)
a = x; fa = fx;
else
b = x; fb = fx;
end
end
x
The result is a root of the polynomial x3 - 2x - 5, namely
x = 2.09455148154233
continue
• The continue statement passes control to the next iteration of the for or while
loop in which it appears, skipping any remaining statements in the body of the
loop. In nested loops, continue passes control to the next iteration of the for or
while loop enclosing it.
break
The break statement lets us exit early from a for or while loop. In nested loops,
break exits from the innermost loop only.
a = 0; fa = -Inf;
b = 3; fb = Inf;
while b-a > eps*b
x = (a+b)/2;
fx = x^3-2*x-5;
if fx == 0
break
elseif sign(fx) == sign(fa)
a = x; fa = fx;
else
b = x; fb = fx;
end
end
x
• Once a matrix exists, its elements are accessed by specifying their row and
column indices. Thus A(i,j) in MATLAB refers to the element ai,j of matrix A, i.e,
the element in the ith row and jth column.
>> A(2,3)
ans =
6
>> A(3,3)=8
A=
1 2 3
4 5 6
7 8 8
>> B=A(2:3,1:3)
B=
4 5 6
7 8 8
• Matrix dimensions are determined automatically by MATLAB, i.e., no explicit
dimension declarations are required. The dimensions of an existing matrix A
may be obtained with the command size(A) or more explicitly with [m,n] =
size(A), which assigns the number of rows and columns of A to the variables
m and n, respectively.
>> [m,n]=size(A)
m=
3
n=
3
>>
Reshaping matrices
Matrices can be reshaped into a vector or any other appropriately sized matrix:
• As a vector : All the elements of a matrix A can be strung into a single column
vector b by the command b = A(:)
>> A
A=
1 2 3
4 5 6
7 8 8
>> A = [1 2;3 4]
A=
1 2
3 4
>> A'
ans =
1 3
2 4
>> B=[1+2i 2+2i; 3i 2-2i]
B=
1.0000 + 2.0000i 2.0000 + 2.0000i
0 + 3.0000i 2.0000 - 2.0000i
>> B'
ans =
1.0000 - 2.0000i 0 - 3.0000i
2.0000 - 2.0000i 2.0000 + 2.0000i
>>
>> A=[A v]
??? Error using ==> horzcat
All matrices on a row in the bracketed expression must have the
same number of rows.
>>
>> A = [ 1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> sqrt_A = sqrt(A) sqrt is an array operation. It gives the square root of each element
of A .
sqrt_A =
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495
2.6458 2.8284 3.0000
>> asqrt = sqrtm(A) sqrtm, on the other hand, is a true matrix function i.e., it
computes A .
>> eig(a)
ans =
16.1168
-1.1168
-0.0000
>>
X is the unique square root for which every eigenvalue has nonnegative
real part. If A has any eigenvalues with negative real parts then a
complex result is produced. If A is singular then A may not have a
square root. A warning is printed if exact singularity is detected.
Example:
>> a =[1 0;0 2]
a=
1 0
0 2
>> sqrtm(a)
ans =
1.0000 0
0 1.4142
>>
>> exp_a = exp(A)
exp_a =
1.0e+003 *
0.0027 0.0074 0.0201
0.0546 0.1484 0.4034
1.0966 2.9810 8.1031
Function files
A function file begins with a function definition line, which has a well
defined list of inputs and outputs. Without this line, the file becomes a script file.
The syntax of the function definition line is as follows:
Syntax
user_entry = input('prompt')
user_entry = input('prompt','s')
Description
The response to the input prompt can be any MATLAB expression, which is
evaluated using the variables in the current workspace.
File I/O
File Opening, Closing, and Positioning
fopen, fclose, fseek, ftell, frewind, ferror.
File Reading and Writing
fread, fwrite, fprintf, fscanf, fgetl, fgets.
Syntax
fid = fopen(filename)
fid = fopen(filename, permission)
[fid, message] = fopen(filename, permission, machineformat)
fids = fopen('all')
[filename, permission, machineormat] = fopen(fid)
Description
fid = fopen(filename) :opens the file filename for read access. (On PCs, fopen
opens files for binary read access.)
fid is a scalar MATLAB integer, called a file identifier. The fid will be used as the first
argument to other file input/output routines. If fopen cannot open the file, it returns -1.
Two file identifiers are automatically available and need not be opened. They are fid=1
(standard output) and fid=2 (standard error).
fid = fopen(filename, permission) opens the file filename in the mode specified by
permission. permission can be:
'r' : Open file for reading (default).
'w' : Open file, or create new file, for writing; discard existing contents, if any.
'a' : Open file, or create new file, for writing; append data to the end of the file.
'r+' : Open file for reading and writing.
'w+' : Open file, or create a new file, for reading and writing; discard existing
contents, if any.
'a+' : Open file, or create new file, for reading and writing; append data to the end of
the file.
'A' : Append without automatic flushing; used with tape drives
'W' : Write without automatic flushing; used with tape drives
The 'W' and 'A' permissions are designed for use with tape drives and do not
automatically perform a flush of the current output buffer after output operations. For
example, open a 1/4" cartridge tape on a SPARCstation for writing with no auto-flush:
fid = fopen('/dev/rst0','W')
Example
The example uses fopen to open a file and then passes the fid, returned by fopen, to other
file I/O functions to read data from the file and then close the file.
fid=fopen('fgetl.m');
while 1
tline = fgetl(fid);
if ~ischar(tline), break, end
disp(tline)
end
fclose(fid);
Syntax
A = fscanf(fid,format)
[A,count] = fscanf(fid,format,size)
Description
A = fscanf(fid, format) reads all the data from the file specified by fid, converts it
according to the specified format string, and returns it in matrix A. Argument fid is an
integer file identifier obtained from fopen. format is a string specifying the format of the
data to be read.
[A,count] = fscanf(fid, format,size) reads the amount of data specified by size, converts it
according to the specified format string, and returns it along with a count of elements
successfully read. size is an argument that determines how much data is read.
Valid options are:
fscanf differs from its C language namesakes scanf() and fscanf() in an important respect
-- it is vectorized in order to return a matrix argument. The format string is cycled
through the file until an end-of-file is reached or the amount of data specified by size is
read in.
If %s is used, an element read may use several MATLAB matrix elements, each
holding one character. Use %c to read space characters or %s to skip all white
space.
Mixing character and numeric conversion specifications cause the resulting matrix to be
numeric and any characters read to appear as their ASCII values, one character per
MATLAB matrix element.
Examples
The example in fprintf generates an ASCII text file called exp.txt that looks like:
0.00 1.00000000
0.10 1.10517092
...
1.00 2.71828183
Read this ASCII file back into a two-column MATLAB matrix:
fid = fopen('exp.txt');
a = fscanf(fid,'%g %g',[2 inf]) % It has two rows now.
a = a';
fclose(fid)
Syntax
[A,count] = fread(fid,size,precision)
[A,count] = fread(fid,size,precision,skip)
Description
[A,count] = fread(fid,size,precision) reads binary data from the specified file and
writes it into matrix A. Optional output argument count returns the number of
elements successfully read. fid is an integer file identifier obtained from fopen.
precision is a string that specifies the format of the data to be read. It commonly
contains a datatype specifier such as int or float, followed by an integer giving the
size in bits. Any of the strings in the following table, either the MATLAB version or
their C or Fortran equivalent, may be used. If precision is not specified, the
default is 'uchar'.
The following formats map to an input stream of bits rather than bytes.
By default, numeric values are returned in class 'double' arrays. To return numeric values
stored in classes other thatn double, create your precision argument by first specifying
your source format, then following it with the characters "=>", and finally specifying
your destination format.
Syntax
A = fread(obj,size)
A = fread(obj,size,'precision')
[A,count] = fread(...)
[A,count,msg] = fread(...)
Arguments
obj A serial port object.
Size The number of values to read.
'precision' The number of bits read for each value, and the interpretation of
the bits as character, integer, or floating-point values.
Description
A = fread(obj, size) reads binary data from the device connected to obj, and
returns the data to A.
strread
Read formatted data from a string
Syntax
A = strread('str')
A = strread('str','',N)
A = strread('str','',param,value,...)
A = strread('str','',N,param,value,...)
[A,B,C,...] = strread('str','format')
[A,B,C,...] = strread('str','format',N)
[A,B,C,...] = strread('str','format',param,value,...)
[A,B,C,...] = strread('str','format',N,param,value,...)
Description
The first four syntaxes are used on strings containing only numeric data. If the
input
string, str, contains any text data, an error is generated.
A = strread('str') reads numeric data from the string, str, into the single variable A.
The next four syntaxes can be used on numeric or nonnumeric data. In this case,
strread reads data from the string, str, into the variables A, B, C, and so on, using
the specified format.
The functions associated with reading data are given below.
Function Description
Name
fgetl Read one line of text from the device and discard the terminator
fgets Read one line of text from the device and include the terminator
fread Read binary data from the device
fscanf Read data from the device, and format as text
feadasync Read data asynchronously from the device
ftopasync Stop asynchronous read and write operations
print sends the contents of the current figure, including bitmap representations of
any user interface controls, to the printer using the device and system printing
command defined by printopt.
print(...) is the function form of print. It enables you to pass variables for any input
arguments. This form is useful passing filenames and handles. See Batch
Processing for an example.
Syntax
count = fprintf(fid, format, A,...)
Description
count = fprintf(fid,format,A,...) formats the data in the real part of matrix A (and in
any additional matrix arguments) under control of the specified format string, and
writes it to the file associated with file identifier fid. fprintf returns a count of the
number of bytes written.
Argument fid is an integer file identifier obtained from fopen. (It may also be 1 for
standard output (the screen) or 2 for standard error. Omitting fid causes output to
appear on the screen.
Step 3:
>> A=[5 -3 2;-3 8 4;2 4 -9]
A=
5 -3 2
-3 8 4
2 4 -9
>> b=[10;20;9]
b=
10
20
9
>> X=A\b
X=
3.4442
3.1982
1.1868
>>
>> C=[A b]
C=
5 -3 2 10
-3 8 4 20
2 4 -9 9
>> Cr = rref(C)
Cr =
1.0000 0 0 3.4442
0 1.0000 0 3.1982
0 0 1.0000 1.1868
>>
The last column of the matrix Cr is the solution.
Av = λv (1)
where λ is a scalar. The problem is to find λ and v for a given A so that equation
(1) is satisfied.
In the output list, V is an n×n matrix whose columns are eigen vectors and D is
an n×n diagonal matrix which has the eigenvalues of A on its diagonal.
The function eig can also be used with one output argument, ex: lamdas=eig(A),
in which case the function gives only the eigenvalues in the vector lamdas.
V=
-0.1709 0.8729 0.4570
-0.2365 0.4139 -0.8791
0.9565 0.2583 -0.1357
D=
-10.3463 0 0
0 4.1693 0
0 0 10.1770
>> lamdas=eig(A)
lamdas =
-10.3463
4.1693
10.1770
>>
4. Matrix factorizations
(i) LU factorization: The name of built-in function is lu.
[L, U] = lu(A)
MATLAB returns a lower triangular matrix L amd an upper triangular matrix
U such that L*U = A
>> A=[5 -3 2;-3 8 4; 2 4 -9]
A=
5 -3 2
-3 8 4
2 4 -9
>> [L, U] = lu(A)
L=
1.0000 0 0
-0.6000 1.0000 0
0.4000 0.8387 1.0000
U=
5.0000 -3.0000 2.0000
0 6.2000 5.2000
0 0 -14.1613
>> a = L*U
a=
5.0000 -3.0000 2.0000
-3.0000 8.0000 4.0000
2.0000 4.0000 -9.0000
[Q,R]=qr(A)
MATLAB returns an orthogonal matrix Q and an upper triangular matrix R such that
Q*R = A.
R=
-6.1644 5.0289 3.2444
0 -7.9819 3.2969
0 0 -8.9221
>> b=Q*R
b=
5.0000 -3.0000 2.0000
-3.0000 8.0000 4.0000
2.0000 4.0000 -9.0000
>>
(iii) Singular Value Decomposition(svd): The name of the built-in function is svd.
The following syntax is used to obtained svd for A.
[U, D, V] = svd(A)
MATLAB returns two orthogonal matrices U and V, and a diagonal matrix D, with
the singular values of A as the diagonal entries, such that U*D*V = A.
>> A
A=
5 -3 2
-3 8 4
2 4 -9
>> [U,D, V] = svd(A)
U=
-0.1709 0.4570 0.8729
-0.2365 -0.8791 0.4139
0.9565 -0.1357 0.2583
D=
10.3463 0 0
0 10.1770 0
0 0 4.1693
V=
0.1709 0.4570 0.8729
0.2365 -0.8791 0.4139
-0.9565 -0.1357 0.2583
>> c = U*D*V
c=
-2.6833 -5.3901 1.3217
-4.1844 6.5120 -5.3930
0.3345 5.5902 8.3449
fzero requires an initial guess and returns the value of x closest to the guess at
which f(x) is zero.
function f= transf(x);
%TRANSF: computes f(x) = sin(x)-exp(x)+5
%call syntax: f=transf(x)
f = sin(x) - exp(x) + 5;
>> x = fzero('transf',1)
x=
1.7878
>>
To use quadl, just replace quad with quadl in the above syntax.
The following steps are involved in numerical integration using these built-in functions:
Step 1: Write a function that returns the value of integrand f(x) given the value of x. It
should be able accept the input x as a vector and corresponding produce the value of the
integrand as a vector.
Step 2: Decide which function to use – quad or quadl (quad is faster but less accurate
than quadl).
∫e
− x2
dx
1/ 2
Solution:
Step 1:
function y = expf(x);
% expf function to evaluate exp(-x^2)
y = exp(-x.^2); % the .^ operator is used for vector x
Step 2:
>> y=quad('expf',1/2,3/2)
y=
0.39490739269182
>> y=quadl('expf',1/2,3/2)
y=
0.39490738751836
>>
7. Double Integration
To evaluate integrals of the form
y max xmax
∫ ∫ f ( x, y)dx dy
y min xmin
MATLAB provides a function dblquad. The calling syntax for dblquad is:
∫ ∫ (1 − 6 x
2
I= y )dx dy
− 0
>> f=inline('1-6*x.^2*y');
>> I = dblquad(f,0,2,-1,1)
I=
4.00000000000000
>>
8. Ordinary Differential Equations
In MATLAB the functions, ode23 and ode45 are implementations of 2nd/3rd – order and
4th /5th – order Runge-Kutta methods, respectively. Solving most ordinary differential
equations (ODEs) using these functions involves the following steps:
For ODEs of order ≥2, this step involves introducing new variables and recasting the
original equation(s) in terms of 1st-order ODEs in the new variables and their vector form
as:
x!1 f1 ( x1 , x 2 . . . x n , t )
x! f ( x , x . . . x , t )
2 = 2 1 2 n
. .
x! n f n ( x1 , x 2 . . . x n , t )
Step 2: Write a function to compute the state derivative.
Step 3: Use the built-in ODE solvers ode23 or ode45 to solve the equations
The syntax of use of ode23 is shown below. To use ode45 just replace ‘ode23’ with
‘ode45’.
[time, solution] =ode23(‘function’,tspan, x0)
where , time → time vector; solution → solution matrix; function → xdot = function(t,x);
tspan → time span [t0 tfinal]; x0 → initial condition.
Step 4: Extract the desired variables from the output and interpret the variables.
Example: Solve the equation of motion of a nonlinear pendulum
θ!! + ω 2 sin θ = 0 ⇒ θ!! = − ω 2 sin θ
with the initial conditions θ(0) = 1 , θ! (0) = 0
z
•
1 z1
2
• =
z 2 − ω sin( z1 )
z! = f (z )
Step 2 : Write a function to compute the new state derivative:
• PLANT
• SYSTEM
• STATE
I PLANT O
Environment
Ydesired
+ -
U(t) O
Controller PLANT
+
P→ Plant
I→ Input
O→ Output
1. Analysis
Given : I, P
Find : O
2. Modeling ( Identification )
Given : I, O
Find: P
3 Control:
Given : P, O
Find: I
• Coast
• Computational complexity
• Manufacturability
• Reliability
• Maintainability
• Adaptability
• Understandability
• Politics
Features
• The Control System Toolbox implements the most prevalent
“classical” and “modern” linear control techniques for the design
and analysis of automatic control systems.
• The Control System Toolbox lets the user to
- model ,
- simulate , and
- analyze LTI systems
(Both continuous & discrete time).
•
X (t ) = f(X(t), u(t)) (1a)
y(t) = g(X(t) u(t)) (1b)
where f(.) and g(.) are nonlinear functions.
If you wish to supply the time vector t at which the response will be computed,
the following command is used.
Step(sys,t)
You can specify either a final time t = Tfinal or a vector of evenly spaced time samples
of the form
t = 0: dt : Tfinal
When invoked with left-hand arguments such as
[y, t]= step(sys)
[y, t, X] = step(sys)
y = step(sys,t)
no plot is generated on the screen. Hence it is necessary to use a plot
command to see the response curves.
The vector y and matrix X contain the output and state response of the
system respectively, evaluated at the computation points returned in the time
vector t.
Depending on the type of model use, the data for the model may consist of a
simple numerator/denominator pair for transfer functions or four matrices for
state-space models. MATLAB provides LTI objects TF and SS for transfer
function and state-space models respectively.
sys=tf(num, den)
num and den vectors specify n(s) and d(s) respectively, of the transfer function
G(s) = n(s)/d(s).
sys_ss = ss(sys_tf)
Construction of the TF object for this discrete-time model requires num and
den polynomials in z, and the sampling interval T.
sys1 = tf ( num,den,’T’ )
sys2 = zpk ( z,p,k, ‘T’ )
sys3 = ss ( a,b,c,d, ‘T’ )
Continuous/Discrete Conversions:
The Control System toolbox provides the user an additional features (tools) for
conversion of different LTI models : c2d, d2c, d2d.
Frequency Response :
>> bode(sys); % Bode plot.
>> nyquist(sys); % Nyquist plot.
>> nichols(sys); % Nichols plot.
>> sigma(sys); % singular value plot.
>> freqresp(sys,w); % complex frequency response.
s+2
Transfer function: ----------------
s^2 + s + 10
Model conversion:
The functions tf, zpk, ss can also perform model conversion.
Example: » h=tf(1,[1 2 1])
Transfer function : 1
-------------------
(s^2 + 2s + 1)
» zpk(h) - zpk representation of the TF ‘h’ is derived.
Zero/pole/gain : 1
------------
(s +1)^2
Transfer function:
s^2 + 2 s + 2
------------------
s^2 + 2 s
Example: Consider wn = 5 rad/sec and damping ratio is 0.4. Generate the
standard second order system.
>> wn=5;
>> damping_ratio=0.4;
>> [num0,den]=ord2(wn,damping_ratio);
>> num = 5^2*num0;
>> printsys(num,den,'s')
25
--------------
s^2 + 4 s + 25
>>
***
>> prog1
enter "1" for I/O model and "2" for state space model
1
choice = 1
the numerator of the transfer function =1
num = 1
the denominator of the transfer function=[1 1 1]
den = 1 1 1
rise_time = 1.6400
peak_time = 3.6300
max_overshoot = 0.1630
settling_time = 5
>>
/**** prog2_1.m *****/
% This program is to find gm & pm from bode
num=input('enter numerator of the transfer function=')
den=input('enter denominator of the transfer function=')
sys=tf(num,den)
bode(sys)
[gm,pm,wcg,wcp]=margin(sys)
Transfer function:
s+2
-----------------------------
s^4 + 2 s^3 + 5 s^2 + 2 s + 3
gm = 1.2361
pm = 4.9632
wcg = 1.2720
wcp = 1.1940
>>
%polar plot
num=input('numerator=')
den=input('den=')
w=0.5:0.01:1.15;
[re,im,w]=nyquist(num,den,w);
%convert polar coordinates into rectangular coordinates by defining
%z,r,theta
z=re+i*im;
r=abs(z);
theta=angle(z);
polar(theta,r)
/************************************/
/**** prog3_1.m***/
num=input('numerator of transfer function=')
den=input('denominator of transfer function=')
[r,k]=rlocus(num,den);
rlocus(num,den)
m=size(r)
n=length(k)
p=m(1,2)
for i=1:n
for j=1:p
if real(r(i,j))>0
break;
k=1;
end
end
end
if(k==1)
>> prog3_1
numerator of transfer function=[0 0 0 1]
num = 0 0 0 1
den = 1 4 5 0
/ * ** prog3_2.m ***/
num=input('numerator of the transfer function=')
den=input('denominator of the transfer function=')
sys=tf(num,den)
bode(sys)
[gm,pm,wcg,wcp]=margin(sys)
if (gm<0)|(pm<0)
disp('given system is unstable')
else
disp('given system is stable')
end
>> prog3_2
numerator of the transfer function=[0 9 1.8 9]
Transfer function:
9 s^2 + 1.8 s + 9
-------------------
s^3 + 1.2 s^2 + 9 s
gm = Inf
pm = 96.5548
wcg = NaN
wcp = 9.7492
> prog4
enter "1" for conversion of i/o model to state variable model and "2" for vice
versa1
choice = 1
numerator of transfer function=[1 1]
num = 1 1
denominator of transfer function=[1 2 5 9]
den = 1 2 5 9
a=
-2 -5 -9
1 0 0
0 1 0
b=
1
0
0
c= 0 1 1
d= 0
>>
/ *** prog3_3.m Nyquist plot ***/
num=input('numerator of transfer function=')
den=input('denominator of transfer function=')
sys=tf(num,den);
nyquist(sys)
[gm,pm,wcg,wcp]=margin(sys)
if (gm<0)|(pm<0)
>> prog3_3
numerator of transfer function=[0 0 1]
num = 0 0 1
gm = Inf
pm = 68.8999
wcg = Inf
wcp = 1.1662
• To start Simulink, first start MATLAB, then start Simulink in two ways:
Block Diagrams
A Simulink block diagram is a pictorial model of a dynamic system. It consists of
a set of symbols, called blocks, interconnected by lines. Each block represents
an elementary dynamic system that produces an output either continuously (a
continuous block) or at specific points in time (a discrete block). The lines
represent connections of block inputs to block outputs. Every block in a block
diagram is an instance of a specific type of block. The type of the block
determines the relationship between a block's outputs and its inputs, states, and
time.
Blocks
Blocks represent elementary dynamic systems that Simulink knows how to
simulate.
• Simulation of the system occurs in two phases: model initialization and model
execution.
• Determines the sample times of all blocks in the model whose sample
times we did not explicitly specify. Allocates and initializes memory used
to store the current values of each block's states and outputs.