Вы находитесь на странице: 1из 125

BAB 1.....

MATLAB BASICS

Vectors
Matrices
Functions
Plotting
Polynomials
Using M-files in MATLAB
Getting help in MATLAB
Solving a Differential Equation
ABOUT MATLAB
MATLAB is an interactive program for numerical computation and data visualization; it is
used extensively by control engineers for analysis and design. There are many different
toolboxes available which extend the basic functions of MATLAB into different application
areas; in these tutorials, we will make extensive use of the Control Systems Toolbox.
MATLAB is supported on Unix, Macintosh, and Windows environments; a student version of
MATLAB is available for personal computers. For more information on MATLAB, contact the
MathWorks.
The purpose behind these tutorials is that you can view them in one window while running
MATLAB in another window. You should be able to re-do all of the plots and calculations in
the tutorials by cutting and pasting text from the tutorials into MATLAB or an m-file.
In MATLAB Basics gives an introduction to using MATLAB. This section will be very helpful
too go through before using the program for the first time. Many features will be familiar,
particularly if you have used other math packages or programming languages. Most
functions and conventions are also logical and convenient, which makes it easy to use if you
have some knowledge of syntax.

VECTORS
Let's start off by creating something simple, like a vector (aka a one dimensional array).
Enter each element of the vector (separated by a space) between brackets, and set it equal
to a variable. For example, to create the vector a, enter into the MATLAB command window:
>> a = [1 2 3 4 5 6 9 8 7]
MATLAB should return:
a=
123456987
Let's say you want to create a vector with elements between 0 and 20 evenly spaced in
increments of 2. In MATLAB you can use the following shorthand to create such a vector:
>> t = 0:2:20
t=

0 2 4 6 8 10 12 14 16 18 20
It is important to remember that in MATLAB (unlike most programming languages) vectors
are indexed from 1. This means location of the first element is 1. Specific elements can be
called like this:
>> t(1)
ans =
0
Manipulating vectors is almost as easy as creating them. First, suppose you would like to
add 2 to each of the elements in vector 'a'. The synatx is the following:
>> b = a + 2
b=
3 4 5 6 7 8 11 10 9
Now suppose, you would like to add two vectors together. If the two vectors are the same
length, it is easy. Simply add the two as shown below.
>> c = a + b
c=
4 6 8 10 12 14 20 18 16
Subtraction of vectors of the same length works exactly the same way. Multiplication of
vectors must be done using "dot star" ( .* ) so the corresponding elements of each of the
vectors are multiplied together.
>> c= a.*b
c=
3 8 15 24 35 28 99 80 63
Element by element operations are necessary for other uses also. If you wanted an vector of
powers of 2, you would need to use a dot caret ( .^ ).
>>powers=1:5;
>>powers_of_2=2.^powers
powers_of_2 =
2 4 8 16 32
likewise you could create a vector of the first five squares:
>>bases=1:5;
>>squares=bases.^2

squares =
1 4 9 16 25

MATRICES
Entering matrices (or two dimensional arrays) into MATLAB is the same as entering vectors,
except each row of elements is separated by a semicolon (;) or a return. The two methods of
creating matrix B which has complex elements, are shown below.
>> B = [1i 1; 2i 2]
B=
0 + 1.0000i 1.0000
0 + 2.0000i 2.0000
>> B = [1i 1
2i 2]
B=
0 + 1.0000i 1.0000
0 + 2.0000i 2.0000
Note that both "i" and "j" represent the imaginary number (sqrt(-1)).
Matrices in MATLAB can be manipulated in many ways. For one, you can find the transpose
of a matrix using the apostrophe. However if the matrix is complex, the apostrophe will give
the complex conjugate transpose. To get the transpose for complex matrices, use dot
apostrophe " .' " (The two commands are the same if the matrix is not complex.).
>> B_complex_conj_transpose = B'
B_complex_conj_transpose =
0 - 1.0000i 0 - 2.0000i
1.0000 2.0000
>> B_transpose = B.'
B_transpose =
0 + 1.0000i 0 + 2.0000i
1.0000 2.0000
Now you can multiply the two matrices D and C together. Remember that order matters when
multiplying matrices.
>> C = [16 35 35 26; 43 56 59 18; 18 57 46 40]
C=

16 35 35 26
43 56 59 18
18 57 46 40
>> D = [63 28 83; 67 95 45; 47 78 44; 16 49 37]
D=
63 28 83
67 95 45
47 78 44
16 49 37
>> E = C * D
E=
5414 7777 5405
9522 12008 9351
7755 11467 7563
>> E = D * C
E=
3706 8504 7675 5462
5967 10230 10020 5252
4898 8521 8271 4386
3029 5413 5153 2778
Another option for matrix manipulation is that you can multiply the corresponding elements of
two matrices using the .* operator (the matrices must be the same size to do this).
>> F = [1 2;3 4]
F=
12
34
>> G = [2 3;4 5]
G=
23
45
>> H = F .* G
H=
26
12 20

If you have a square matrix, like E, you can also multiply it by itself as many times as you like
by raising it to a given power.
>> F^3
ans =
37 54
81 118
If wanted to cube each element in the matrix, just use the element-by-element cubing.
>> F.^3
ans =
18
27 64
You can also find the inverse of a matrix:
>> X = inv(F)
X=
-2.0000 1.0000
1.5000 -0.5000
or its eigenvalues:
>> eig(F)
ans =
-0.3723
5.3723
There is even a function to find the coefficients of the characteristic polynomial of a matrix.
The poly function creates a vector whose elements are the coefficients of the characteristic
polynomial.
>> p = poly(F)
p=
1.0000 -5.0000 -2.0000
Remember that the eigenvalues of a matrix are the same as the roots of its characteristic
polynomial. Therefore the result of using the roots command will be the same as the result of
using the eig command with matrix F.
>> roots(p)
ans =

5.3723
-0.3723

FUNCTIONS
To make life easier, MATLAB includes many standard functions. Each function is a block of
code that accomplishes a specific task. MATLAB contains all of the standard functions such
as sin, cos, log, exp, sqrt, as well as many others. Commonly used constants, such as pi and
e, are also incorporated into MATLAB.
Some functions return one number. For example:
>> sin(pi/4)
ans =
0.7071
Some functions return multiple numbers. For example (M is a 5 by 4 matrix):
>> size(M)
ans =
54
The returns (answers) of these functions can be stored in variables using the equals sign.
Single return
>> S = sin(pi/4)
S=
0.7071
Multiple return:
>> S = size(M)
S=
54
or
>> [x y] = size(M)
x=
5

y=
4
To determine the usage of any function, type help functionname at the MATLAB command
window. MATLAB even allows you to write your own functions with the function command;
follow the link to learn how to write your own functions and see a listing of the functions we
created for this tutorial.
A few more random notes:
You can get the value of a particular variable at any time by typing its name.
>> B
B=
123
456
789

PLOTTING
It is also easy to create plots in MATLAB. Suppose you wanted to plot a sine wave as a
function of time. First make a time vector (the semicolon after each statement tells MATLAB
we don't want to see all the values) and then compute the sin value at each time.
>> t=0:0.25:7;
>> y = sin(t);
>> plot(t,y)

The plot contains approximately one period of a sine wave. Basic plotting is very easy in
MATLAB, and the plot command has extensive add-on capabilities. I would recommend you
visit the plotting page to learn more about it.

POLYNOMIALS
In MATLAB, a polynomial is represented by a vector. To create a polynomial in MATLAB,
simply enter each coefficient of the polynomial into the vector in descending order. For
instance, let's say you have the following polynomial:

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
MATLAB can interpret a vector of length n+1 as an nth order polynomial. Thus, if your
polynomial is missing any coefficients, you must enter zeros in the appropriate place in the
vector. For example,

would be represented in MATLAB as:


>> y = [1 0 0 0 1]
You can find the value of a polynomial using the polyval function. For example, to find the
value of the above polynomial at s=2,
>> polyval([1 0 0 0 1],2)
ans =
17
This can also be useful for plotting. Here's how (I set t to range from -6 to 4 to show all the
roots.):
>> t = -6:.1:4;
>> x = [1 3 -15 -2 9];
>> plot(t, polyval(x,t))
>> grid on

As you can see from the plot the function crosses zero 4 times. To find the exact locations
use the roots command.
>> roots([1 3 -15 -2 9])
ans =
-5.5745
2.5836
-0.7951
0.7860
Let's say you want to multiply two polynomials together. The product of two polynomials is
found by taking the convolution of their coefficients. MATLAB's function conv that will do this
for you.
>> x = [1 2]; y = [1 4 8];
>> z = conv(x,y)
z=
1 6 16 16
Note: You can have more that one statement on a single line, so long as you separate them
with either a semicolon or comma.
Dividing two polynomials is just as easy. The deconv function will return the remainder as
well as the result. Let's divide z by y and see if we get x.
>> [xx, R] = deconv(z,y)
xx =
12

R=
0000
As you can see, this is just the polynomial/vector x from before. If y had not gone into z
evenly, the remainder vector would have been something other than zero.
USING M-FILES IN MATLAB
M-files allow the user write functions using MATLAB commands. This is especially useful
because if statements, for loops, etc. can be incorporated to allow it to operate like a regular
program. However M-file syntax is the same as the syntax used in the command window of
MATLAB.
To create an m-file just go to new in the file menu, hit the new document button or create a
text file and title it with the extension '.m'. To run the file hit the run button in the m-file editor
or type in the name of the file, without the .m, in the command window. Note that the file must
be in the current directory to be called from the command window. The current directory is
displayed at the top of the MATLAB window.
An m-file could be used to plot sine and cosine together. It would look like this:
% create the independent variable (vector)
t=0:0.25:7;
% define the functions of the variable
% (these are also vectors)
y=sin(t);
y2=cos(t);
% plot the the two functions, w/characteristics
% plot sine as a line with circles at points (every .25)
% plot cosine as a dotted line
% set legend
plot(t,y,'-o', t,y2,'--')
legend('o', 'sin', '--', 'cos')
The result is this figure:

In MATLAB the percent sign (%) can be used to comment your m-file code. Much like the "//"
in Java, this will comment out anything after it on that line only. This is useful, especially
when keeping track of a long list of functions or when trying to locate a problem.
For more on m-files see the Introduction to M-files page.

GETTING HELP IN MATLAB


Included with MATLAB is a help database for each function. Accessible using this:
>> help commandname
You do need to know the name of the command that you are looking for; a list of the all the
ones used in these tutorials is given in the command listing; a link to this page can be found
at the bottom of every tutorial and example page.
In addition there are a number of others ways to get help on MATLAB. MATLAB help
provides a hierarchal table of contents, an index, a search, and demos. Note that the entries
in the index are topics not titles of help articles and that the titles of the help are not
necessarily in the index. However you can search the article titles, article text, function
names or MATLAB's online knowledge base. Frequently used help articles can be saved in
and easily accessed from the "favorites" as web pages can be in a web browser.

SOLVING A DIFFERENTIAL EQUATION


In this section, we will use all the skills described above to solve a differential equation from a
2nd order dynamic response problem for a mass-spring-damper system. This will be done
using an m-file. The text of the m-file is below and can be copied directly into an m-file. The
following is the equation this section solves.

With the initial conditions:

It is good to start an m-file with a heading.


%
% diffeq.m 8/2/04
% author: Brent Selby
% Matlab Basics Tutorial
%
Put in the values for the constants that will be used.

M = 550;
B = 7000;
K = 90000;
F = 21610;
This is the total solution: the homogeneous solution plus the particular solution

The particular solution we assume is constant because the right side of the equation is
constant. The derivatives of a constant are zero. So divide the whole equation by K and solve
for zp.

>> zp = F/K
zp =
0.2401
The homogeneous solution is the solution for which the right side of the equation (where F is)
is equal to zero. Assume the homogeneous solution of a 2nd order equation is a constant
times e to the power of a constant times t.

Dividing by C*e^(lambda*t) gives us a quadratic equation. Using the roots command gives us
the values for lambda that we need.

>> lambda = roots([M B K])


lambda =
-6.3636 +11.0969i
-6.3636 -11.0969i
We can see that the lambdas are complex. This is not a problem, it does, however, tell us
that we should expect to see an oscillatory response.
To find the values of the constants of the homogeneous equation the initial conditions are
needed. The total solution must be used for this. This is how to develope the equations.

The result is a set of linear equations. This set of linear equations can be written as a vectormatrix equation. The inverse function is used to solve it and C is the 2x1 matrix containing
the constants C1 and C2. This is how it is done:

You can see that a is the coefficients in front of the constants C1 and C2 in the initial
condition equations. The matrix C contains those constants and when multiplied by a gives
the left side of the initial condition equations. The matrix b contains the right side of those
equations. Here is the MATLAB code to solve for C:
a = [1 1; lambda(1) lambda(2)];
b = [(.3-zp);-4];
>> C = inv(A1)*A2
C=
0.0299 + 0.1631i
0.0299 - 0.1631i
Now to graph the total solution.

t=0:.01:2;
z = zp + C(1)*exp(lambda(1)*t) + ...
C(2)*exp(lambda(2)*t);
% plotting and setting up window
plot(t,z)
grid on
title('Solution to Differential Equation')
xlabel('t')
ylabel('z')

The complete m-file for solving this is: diffeq.m.

BAB 2..OVERVIEW

In this tutorial we will learn how to model and analyze a physical system in MATLAB. This
tutorial provides canonical steps, theory, and syntax toward solving most problems. Steps in
this tutorial will be illustrated with a typical second order mass-spring-damper example. For
more specific examples, refer to the various example problems.
EXAMPLE
The example system we will be analyzing consists of a mass which is constrained by a
spring, a damper, and an outside applied force.

Figure 1 - Second order system setup

This system is second order because its behavior depends on position, x, and both its first
and second derivatives. The spring force is directly proportional to the position, the friction
force is dependent on velocity, which is the first derivative of position, and the inertia force is
dependent on acceleration, which is the second derivative of position.
A second order translational system is among the most common of all physical systems and
can be adapted to simulate other types of problems including rotational (mass-springdamper) and electrical (resistor-inductor-capacitor circuits) systems. A second order system
also includes all common types of forces, spring, damper, and inertial making it ideal for a
tutorial problem.

BAB 3..FREE BODY DIAGRAM


A free body diagram is a modeling representation for a single body in a system which shows
all external forces and the inertia force. To solve a free body diagram, apply D'Alembert's
Law to each mass or point that moves with unknown velocity. For each mass or point, use
arrows to indicate all external forces and the inertia force. The arrow indicates the positive
direction of the force.
Free body diagrams can be created to model virtually any physical situation, translational,
rotational, electrical, hydraulic, or thermal.
TRANSLATIONAL FREE BODY DIAGRAM
Typical forces involved in a translational system include:
Force

Mathematical Expression

Dependent Variable

Spring Forces

kx

Position

Friction Forces

bv = bx'

Velocity

Inertial Forces

ma = mv' = mx"

Acceleration

Applied Forces

f(t)

Any

Draw each force onto the diagram in the appropriate direction. Forces must be solved for
separately for each axis of movement (x, y, and z).
ROTATIONAL FREE BODY DIAGRAM
Typical forces involved in a rotational system include:
Force

Mathematical Expression

Dependent Variable

Spring Forces

Angular Position

Friction Forces

b = b'

Angular Velocity

Inertial Forces

m = m' = m"

Angular Acceleration

Applied Forces

f(t)

Any

Determine which direction each of the torques act in, and draw them onto the diagram in the
appropriate direction. Forces must be solved for separately for each axis of rotation (x, y,
and z).
ELECTRICAL FREE BODY DIAGRAM
Although no free body diagram is created for an electrical system in the literal sense, an
equivalent circuit diagram should be created.
Typical elements involved in an electrical system include:
Element
Voltage Source
Resistor

Mathematical Expression
ei(t)
eR = Ri

Back EMF
Inductor

eb = qx'
eL = Li'

Kirchoff's current law states that the sum of the current flowing into a node is equal to the
sum of the current flowing out. This implies that all elements in a series circuit share the
same current. Determine whether each element acts with or against the positive convention,
and sum these voltage relations to form the governing equation for the circuit.
EXAMPLE
Draw the free body diagram for the second order system below:

Figure 1 - System setup: Mass - Spring - Damper


In order to draw the free body diagram for the above figure we must separately consider
each of the forces acting on the body and then sum them together. In this example there are
three external forces acting on the body, the applied force acting on the block, f(t), the friction
of the block on the stationary surface, quantified as b x', and the spring force, quantified as k
x. Both the friction and spring forces pull to the left resisting the applied force. The positive
sense of the inertial force, quantified as m x", also pulls to the left resisting the applied force.
The summation of these four forces must equal zero.
Force

Direction

Mathematical Expression

Applied Force

Toward the Right

f(t) (positive)

Spring Force

Toward the Left

kx (negative)

Friction Force

Toward the Left

bx' (negative)

Inertial Force

Toward the Left

mx" (negative)

FREE BODY DIAGRAM


Drawing the four vectors in the table above yields the following free body diagram.

Figure 2 - Free body diagram: Mass - Spring - Damper


GOVERNING EQUATION
According to D'Alembert's Law, the sum of all forces acting on a body including the inertia
force is equal to zero.
Using D'Alembert's Law:
Sum of all forces = f(t) - kx - bx' - mx" = 0

BAB 4..TRANSFER FUNCTION


The modeling equation gathered from the free body diagram is in the time domain. Some
analysis are easier to perform in the frequency domain. In order to convert to the frequency
domain, apply the Laplace Transform to determine the transfer function of the system.
The Laplace Transform converts linear differential equations into algebraic expressions which
are easier to manipulate. The Laplace Transform converts functions with a real dependent
variable (such as time) into functions with a complex dependent variable (such as frequency,
often represented by s).
The transfer function is the ratio of the output Laplace Transform to the input Laplace
Transform assuming zero initial conditions. Many important characteristics of dynamic or
control systems can be determined from the transfer function.
The general procedure to find the transfer function of a linear differential equation from input
to output is to take the Laplace Transforms of both sides assuming zero conditions, and to
solve for the ratio of the output Laplace over the input Laplace.
HOW TO FIND THE TRANSFER FUNCTION
In most cases the governing equation will be linear, consisting of a variable and its
derivatives. The Laplace Transform allows a linear equation to be converted into a
polynomial. The most useful property of the Laplace Transform for finding the transfer
function is the differentiation theorem. Several properties are shown below:

Linearity

Time Domain
f(t) + g(t)

Function

x(t)

1st Derivative

x'(t)

nd

2 Derivative

x"(t)

nth Derivative

xn(t)

Frequency Domain

Note: While linearity allows Laplace Transforms to be added, the same does not hold true
for multiplication. f(t)g(t) does not equal F(s)G(s). The solution to multiplication requires
convolution, please refer to a differential equations book.
In order to convert the time dependent governing equation to the frequency domain, perform
the Laplace Transform to the input and output functions and their derivatives. These

transformed functions must then be substituted back into the governing equation assuming
zero initial conditions. Because the transfer function is defined as the output Laplace
function over the input Laplace function, rearrange the equation to fit this form.
EXAMPLE
Find the transfer function of the second order tutorial example problem:
From the free body diagram we were able to extract the following governing equation:
f(t) - kx - bx' - mx" = 0
The notation of the Laplace Transform operation is L{ }.

When finding the transfer function, zero initial conditions must be assumed, so x(0) = x'(0) =
0.
Taking the Laplace Transform of the governing equation results in:
F(s) - k[X(s)] - b[sX(s)] - m[s2X(s)] = 0
Collecting all the terms involving X(s) and factoring leads to:
[ms2 + bs + k] X(s) = F(s)
The transfer function is defined as the output Laplace Transform over the input Laplace
Transform, and so the transfer function of this second order system is:
X(s)/F(s) = 1/[ms2 + bs + k]

HOW TO INPUT THE TRANSFER FUNCTION INTO MATLAB


In order to enter a transfer function into MATLAB, the variables much be given numerical
value, because MATLAB cannot manipulate symbolic variables without the symbolic toolbox.
Enter the numerator and denominator polynomial coefficients separately as vectors of
coefficients of the individual polynomials in descending order. The syntax for defining a
transfer function in MATLAB is:
transferfunction = tf(num, den)
where num is defined as the vector of numerator coefficients, and den is defined as the
vector of denominator coefficients.
EXAMPLE
Input the transfer function X(s)/F(s) = 1/[ms2 + bs + k] into MATLAB:

For illustration purposes, this example uses m = 2, b = 5, and k = 3.


>> m = 2;
>> b = 5;
>> k = 3;
>> num = [ 1 ];
>> den = [ m b k ];
>> tutorial_tf = tf(num, den)
MATLAB will assign the transfer function under the name tutorial_tf, and output the following:
Transfer function:
1
--------------2 s^2 + 5 s + 3

STEP RESPONSE USING THE TRANSFER FUNCTION


Once the transfer function is entered into MATLAB it is easy to calculate the response to a
step input. To calculate the response to a unit step input, use:
step(transferfunction)
where transferfunction is the name of the transfer function of the system.
For steps with magnitude other than one, calculate the step response using:
step(u * transferfunction)
where u is the magnitude of the step and transferfunction is the name of the transfer function
of the system.
EXAMPLE
Find the unit step response and the step response when u = 4 of tutorial_tf using MATLAB:
To find the unit step response:
>> step(tutorial_tf)
The MATLAB output will be the following plot of the unit step response:

To find the step response when u = 4:


>> u = 4;
>> step(u * tutorial_tf)
The MATLAB output will be the following plot of the step response:

IMPULSE RESPONSE USING THE TRANSFER FUNCTION


MATLAB can also plot the impulse response of a transfer function. Because the transfer
function is in the form of output over input, the transfer function must be multiplied by the
magnitude of the impulse. The syntax for plotting the impulse response is:
impulse(u * transferfunction)
where u is the magnitude of the impulse and transferfunction is the name of the transfer
function of the system.
EXAMPLE
Find the impulse response of tutorial_tf with an input of u = 2 using MATLAB:
>> u = 2;
>> impulse(u * tutorial_tf)
The MATLAB output will be the following plot of the impulse response:

BODE PLOT USING THE TRANSFER FUNCTION


MATLABs bode command plots the frequency response of a system as a bode plot. The
syntax for the bode plot function in MATLAB is:

bode(transferfunction)
where transferfunction is the name of the transfer function system.
EXAMPLE
Find bode plot of the frequency response of the system tutorial_tf using MATLAB:
>> bode(tutorial_tf)
The MATLAB output will be the following bode plot of the frequency response:

STATE SPACE FROM TRANSFER FUNCTION


MATLAB can find the state space representation directly from the transfer function in two
ways. To find the state space representation of the system from the numerator and
denominator of the transfer function in the form
x' = Ax + Bu
y = Cx + Du
use MATLAB's tf2ss command:
[A, B, C, D] = tf2ss(num,den)
where num is the vector of the numerator polynomial coefficients, and den is the vector of the
denominator polynomial coefficients.

In order to find the entire state space system in addition to the separate matrices from the
transfer function, use the following command:
statespace = ss(transferfunction)
where transferfunction is the name of the transfer function system.
EXAMPLE
Find A, B, C, and D, the state space vectors of tutorial_tf using MATLAB:
>> [A, B, C, D] = tf2ss(num,den)
The MATLAB output will be:
A=
-2.5000 -1.5000
1.0000
0
B=
1
0
C=
0

0.5000

D=
0
Find the state space system of tutorial_tf using MATLAB:
>> tutorial_ss = ss(tutorial_tf)
MATLAB will assign the state space system under the name tutorial_ss, and output the
following:
a=
x1
x2

x1
x2
-2.5 -0.375
4
0

b=
u1
x1 0.25
x2 0
c=
y1
d=

x1 x2
0 0.5

u1
y1 0
Continuous-time model.

BAB 5..STATE SPACE MODEL


The state space model represents a physical system as n first order coupled differential
equations. This form is better suited for computer simulation than an nth order input-output
differential equation.
The general vector-matrix form of the state space model is:

where y is the output equation, and x is the state vector

PARTS OF A STATE SPACE REPRESENTATION


State Variables: a subset of system variables which if known at an initial time to along with
subsequent inputs are determined for all time t>+t0
State Equations: n linearly independent first order differential equations relating the first
derivatives of the state variables to functions of the state variables and the inputs.
Output equations: algebraic equations relating the state variables to the system outputs.
Note: The state variables are generally variables representing the state of energy storage
elements in a system.
The state space model can be found using the following steps:

Identify energy storage elements


Determine energy relations for each element
Pick off state variables from energy relations
Find trivial state equations
Use physics and algebra to find the other state equations
Define a state vector (stack state variables in a vector)
Take derivative of the state vector and relate this derivative to a vector-matrix
multiplication of the state vector and the inputs

IDENTIFY ENERGY STORAGE ELEMENTS AND SELECT STATES

In order to determine your state variables, you must identify energy storage elements. From
the free body diagram, determine which energy storage elements your problem has. In
many cases there can be multiple instances of each element.
Energy Storage Element
Spring
Inertia
Damper
Applied Force

Energy Relation
kx2
mv2
Does not store energy
Does not store energy

State Variables
x
v
None
None

EXAMPLE
Identify the energy storage elements and select the states of the tutorial problem:

From the example setup one can see that the setup consists of one instance each of the
applied force, a damper, a spring, and an inertia. Looking at the table above, our state vector
must consist of the variables x and v.

IDENTIFYING TRIVIAL STATE EQUATIONS


Using definitions from mathematics, identify trivial state equations.
For example, by definition:
v = x'
Therefore the trivial state equation for the above system is x' = v which expresses the
derivative of position in terms of states and inputs, in this case, just the velocity.
DETERMINING OTHER
INTERCONNECTIONS

STATE

EQUATIONS

USING

ELEMENT

LAWS

AND

In order to find the remaining state equations, we have to investigate other relations
between the state variables based on element laws and interconnections. The most
commonly used of all interconnections is the governing equation from the free body
diagram.
EXAMPLE

The equation gathered from the free body diagram was:


mx" + bx' + kx - f(t) = 0
Substituting the definitions of the states into the equation results in:
mv' + bv + kx - f(t) = 0
Solving for v' gives the state equation:
v' = (-b/m) v + (-k/m) x + f(t)/m
The desired output is for the position, x, so:
y=x
Now the derivatives of the state variables are in terms of the state variables, the inputs, and
constants.
x' = v
v' = (-k/m) x + (-b/m) v + f(t)/m
y=x

PUTTING INTO VECTOR-MATRIX FORM


Now to put the equations above into the form:

where y is the output equation, and x is the state vector


EXAMPLE
Our state vector consists of two variables, x and v so our vector-matrix will be in the form:

The first row of A and the first row of B are the coefficients of the first state equation for x'.
Likewise the second row of A and the second row of B are the coefficients of the second
state equation for v'. C and D are the coefficients of the output equation for y. This yields:

Therefore

HOW TO INPUT THE STATE SPACE MODEL INTO MATLAB


In order to enter a state space model into MATLAB, the variables much be given numerical
value, because MATLAB cannot manipulate symbolic variables without the symbolic
toolbox. Enter the coefficient matrices A, B, C, and D into MATLAB. The syntax for defining
a state space model in MATLAB is:
statespace = ss(A, B, C, D)
where A, B, C, and D are from the standard vector-matrix form of a state space model.
EXAMPLE
Input the tutorial state space model into MATLAB:
Theoretical values must be determined for the constants m, b, and k. For the sake of
example, we will take m = 2, b = 5, and k = 3.
>> m = 2;
>> b = 5;
>> k = 3;
>> A = [ 0 1 ; -k/m -b/m ];
>> B = [ 0 ; 1/m ];
>> C = [ 1 0 ];
>> D = 0;

>> tutorial_ss = ss(A, B, C, D)


This assigns the state space model under the name tutorial_ss and output the following:
a=
x1 x2
x1 0 1
x2 -1.5 -2.5
b=
u1
x1 0
x2 0.5
c=
x1 x2
y1 1 0
d=
u1
y1 0
Continuous-time model.

EXTRACTING A, B, C, D MATRICES FROM A STATE SPACE MODEL


In order to extract the A, B, C, and D matrices from a previously defined state space model,
use MATLAB's ssdata command.
[A, B, C, D] = ssdata(statespace)
where statespace is the name of the state space system.
EXAMPLE
Extract the A, B, C, and D matrices from tutorial_ss using MATLAB:
>> [A, B, C, D] = ssdata(tutorial_ss)
The MATLAB output will be:
A=
-2.5000 -0.3750
4.0000
0
B=
0.2500
0
C=

0.5000

D=
0

STEP RESPONSE USING THE STATE SPACE MODEL


Once the state space model is entered into MATLAB it is easy to calculate the response to a
step input. To calculate the response to a unit step input, use:
step(statespace)
where statespace is the name of the state space system.
For steps with magnitude other than one, calculate the step response using:
step(u * statespace)
where u is the magnitude of the step and statespace is the name of the state space system.
EXAMPLE
Find the unit step response and the step response when u = 4 of tutorial_ss using
MATLAB:
First to find the unit step response:
>> step(tutorial_ss)
The MATLAB output will be the following plot of the unit step response:

To find the step response when u = 4:


>> u = 4;
>> step(u * tutorial_ss)
The MATLAB output will be the following plot of the step response:

IMPULSE RESPONSE USING THE STATE SPACE MODEL


MATLAB can also plot the impulse response of a state space model. The syntax for the
impulse response function in MATLAB is:
impulse(u * statespace)
where u is the magnitude of the impulse and statespace is the name of the state space
system.
EXAMPLE
Find the impulse response of tutorial_ss with an input of u = 2 using MATLAB:
>> u = 2;
>> impulse(u * tutorial_ss)
The MATLAB output will be the following plot of the impulse response:

BODE PLOT USING THE STATE SPACE MODEL


MATLABs bode command plots the frequency response of a system as a bode plot. The
syntax for the bode plot function in MATLAB is:
bode(statespace)
where statespace is the name of the state space system.
EXAMPLE
Find bode plot of the frequency response of the system tutorial_ss using MATLAB:
>> bode(tutorial_ss)
The MATLAB output will be the following bode plot of the frequency response:

TRANSFER FUNCTION FROM STATE SPACE


MATLAB can determine the transfer function from the state space representation in two
ways. To find the numerator matrix and denominator vector of the transfer function of the
system from the matrices of the state space model use MATLAB's ss2tf command:
[num, den] = ss2tf(A, B, C, D)
where num is defined as the numerator matrix, and den is defined as the denominator
matrix, and A, B, C, and D are the coefficients of the state space model.
In order to find the entire transfer function system from the state space model, use the
following command:
transferfunction = tf(statespace)
where statespace is the name of the state space system, and transferfunction is the name of
the generated transfer function system.
EXAMPLE
Find num and den, the numerator and denominator of the transfer function using A, B, C,
and D, the state space model coefficient matrices of tutorial_ss using MATLAB:
>> [num, den] = ss2tf(A, B, C, D)
The MATLAB output will be:

num =
0

0.5000

den =
1.0000

2.5000

1.5000

Find the transfer function system of tutorial_ss using MATLAB:


>> tutorial_tf = tf(tutorial_ss)
MATLAB will assign the transfer function under the name tutorial_tf, and output the
following:
Transfer function:
0.5
----------------s^2 + 2.5 s + 1.5

BAB 6..ANALYSIS
MATLAB can use the Transfer Function or the State Space Model to find the Poles and
Zeros, Step Response, Impulse Response, and Bode Plot of a system. This section shows
the characteristics of the response of common types of systems to common test inputs. In
the MATLAB syntax that follows either a Transfer Function system or a State Space Model
system can be used.
POLES AND ZEROS
The poles of a system are the values of 's' which make the denominator of the transfer
function equal to zero or equivalently they are the eigenvalues of the 'A' matrix of the state
space model. They determine the stability of the system. The zeros of a system are the
values of 's' which make the numerator of the transfer function equal to zero. They affect the
time response but do not determine stability.
pole(system)
tzero(system)
where system is the name of the transfer function or state space system.
STEP RESPONSE
The Step Response illustrates how the system responds when a constant input is suddenly
applied.
step(u * system)
where u is defined as the step input and system is the name of the transfer function or state
space system.
DC GAIN
The DC gain is the ratio of the steady state step response to the magnitude of a step input.
dcgain(system)
where system is the name of the transfer function or state space system.
DAMPING RATIO
The damping ratio is the ratio of the actual damping to 2w n for a second order or higher
system.
damp(system)
where system is the name of the transfer function or state space system.
IMPULSE RESPONSE
The Impulse Response illustrates how the system responds when a brief input is applied.

impulse(u * system)
where u is defined as the impulse input and system is the name of the transfer function or
state space system.
BODE PLOT
Bode plots are a graphical representation of the frequency on a logarithmic scale. They are
useful for system identification, analysis, and controller design.
The Bode consists of two plots, 20log(10) of the magnitude of the frequency response vs.
log(10) of frequency, and the phase of the frequency response in degrees vs. log(10) of
frequency.
bode(system)
where system is the name of the transfer function or state space system.

CHARACTERISTICS OF VARIOUS SYSTEMS


FIRST ORDER SYSTEM

G(s) = b/(s+a) = kdc/(ts+1)


Zero/Pole - The first order system has a single pole at -a. If the pole is on the negative real
axis, then the system is stable. If the pole is on the positive real axis, then the system is not
stable. The zeros of a first order system are the values of s which makes the numerator of
the transfer function equal to zero.
DC Gain - The DC gain is the ratio of the steady state step response to the magnitude of a
step input. The DC Gain is the value of the transfer function when s = 0, which is equal to
b/a or kdc for a first order system.
Settling Time - The settling time is the time to fall within a certain percentage of the steady
state value for a step input or equivalently to decrease to a certain percentage of the initial
value for an impulse input. The equations for settling time are:
10%
2/n

5%
3/n

2%
4/n

Time Constant - The time constant is the time to reach 63% of the steady state value for a
step input or to decrease to 37% of the initial value for an impulse input. The equation for the
time constant is:
Bode Plot - The low frequency magnitude of the bode plot is 20log(10) of the DC gain. The
magnitude plot has a bend at the frequency equal to the absolute value of the pole (ie. =
a), and then decreases at 20dB for every factor of ten increase in frequency (20dB/decade). The phase plot is asymptotic to 0 degrees at low frequency, and asymptotic
to -90 degrees at high frequency. Between frequency 0.1a and 10a, the phase changes by
approximately -45 degrees for every factor of ten increase in frequency (-45
degrees/decade).
SECOND ORDER SYSTEM

G(s) = a/(s2+bs+c) = kdcn2/(s2+2ns+n2)


Settling Time - The settling time is the time to fall within a certain percentage of the steady
state value for a step input or equivalently to decrease to a certain percentage of the initial
value for an impulse input. The equations for settling time are:
10%
2 / n

5%
3 / n

2%
4 / n

Percent Overshoot - The percent overshoot is the maximum fraction by which the response
overshoots the steady state value expressed as a percentage. It equals e-sqrt
DC Gain - The DC gain is the ratio of the steady state step response to the magnitude of a
step input. The DC Gain is the value of the transfer function when s = 0, which is equal to
a/c or kdc for a second order system.
Natural Frequency - The natural frequency is the frequency at which the system would
oscillate if there were no damping (ie. if = 0). The natural frequency is n= sqrt(c).
Damping Ratio - The damping ratio is the ratio of the actual damping to 2 n. The damping
ratio is = b/(2sqrt(c)).
Zero/Pole - Zeros occur There are two poles at -n jn sqrt(1-2). The poles are real if the
two poles lie on the real axis and contains no imaginary part (n sqrt(1-2) = 0). If the poles
are real, the system is overdamped. The poles are complex if neither n or n sqrt(1-2)
equals zero giving the pole both a real and imaginary part. If the poles are complex, the
system is underdamped. If the poles consist of only an imaginary part and no real part (n =
0), the poles are imaginary. If the poles are imaginary, the system is undamped. If either
pole has positive real component, the system is unstable. The zeros of a second order
system are the values of 's' which make the numerator of the transfer function equal to zero.
Bode Plot - The magnitude of the bode plot of a second order system drops off at -40dB per
decade, while the relative phase changes from 0 to -180 degrees at -90 degrees per decade.
HIGHER ORDER SYSTEM
Frequency Response - Higher order systems have many variations and are so complex that
parameters that characterize the first and second order systems are difficult to generalize on
a higher order system. The most general way to study a higher order system is by examining
its frequency response.
Zero/Pole - The stability of a higher order system can still be determined by the poles of the
transfer function.
DC Gain - The DC gain is the ratio of the steady state step response to the magnitude of a
step input. The DC Gain is the value of the transfer function when s = 0.
EXAMPLE
Our tutorial system is a second order system governed by the transfer function in the form,
G(s) = a/(s2+bs+c) = kdcn2/(s2+2ns+n2):
X(s)/F(s) = 1/[ms2 + bs + k]
where m = 2, b = 5, and k = 3, so using MATLAB:

>> m = 2;
>> b = 5;
>> k = 3;
>> a = 1/m
>> b = b/m
>> c = k/m
a=
0.5000
b=
2.5000
c=
1.5000
For the sake of simplicity the following example will use a new system name, tutorial_sys,
which is interchangable with both the equivalent transfer function system, tutorial_tf, and the
state space system, tutorial_ss, defined previously.
>> tutorial_sys = tutorial_tf = tutorial_ss

NATURAL FREQUENCY
The natural frequency is n= sqrt(c).
>> wn = sqrt(c)
wn =
1.2247
DAMPING RATIO
The damping ratio is = b/(2sqrt(c)).
>> zeta = b/(2*sqrt(c))
zeta =
1.0206
To find the damping ratio of the system using MATLAB's damp function.
>> damp(tutorial_sys)
ans =
1.0210

POLES
A second order system has two poles at -n jn sqrt(1-z2).
In this example the two poles occur at:
>> pole1 = -zeta*wn + j*wn*sqrt(1-zeta^2)
>> pole2 = -zeta*wn - j*wn*sqrt(1-zeta^2)
pole1 =
-1.5000
pole2 =
-1.0000
To find the pole(s) of the system using MATLAB's pole function.
>> pole(tutorial_sys)
ans =
-1.5000
-1.0000
This second order system has two negative real poles, poles that have only negative real
parts and no imaginary parts, so this system is overdamped and stable.

ZEROS
The zero of a system is when the numerator of the transfer function equals zero, which
makes the value of the transfer function zero. By inspection the numerator of this example
problem equals 1, so no value of s will yield a numerator equaling zero.
To find the zero(s) of the system using MATLAB's tzero function.
>> tzero(tutorial_sys)
ans
Empty matrix: 0-by-1
This system has no zero, meaning that no value of s will make the numerator equal zero.

STEP RESPONSE

The Step Response illustrates how the system responds when a constant input is suddenly
applied. In order to find the step response of this system using MATLAB, use the step
function.
First to find the unit step response, u = 1.
>> u = 1;
>> step(u * tutorial_sys)
The MATLAB output will be the following plot of the unit step response:

This shows how the system will respond with a constant input force of 1 unit applied
suddenly at t = 0.
Next to find the step response when u = 4.
>> u = 4;
>> step(u * tutorial_sys)
The MATLAB output will be the following plot of the step response:

This shows how the system will respond with a constant input force of 4 units applied
suddenly at t = 0.

SETTLING TIME
The settling time is the time it takes to fall within a certain percentage of the steady state
value for a step input or equivalently to decrease to a certain percentage of the initial value
for an impulse input. The settling times for a system are:
10%
2 / n

5%
3 / n

2%
4 / n

With a damping ratio greater than 1, and with both real poles, this system is overdamped. An
overdamped system cannot have its settling time calculated this way.

PERCENT OVERSHOOT
The percent overshoot is the maximum fraction by which the response overshoots the steady
state value expressed as a percentage. It equals e-zp/sqrt(1-z^2).
With a damping ratio greater than 1, and with both real poles, this system is overdamped. An
overdamped system will not overshoot.

DC GAIN
The DC Gain is the ratio of the steady state step response to the magnitude of a step input.
The DC Gain is the value of the transfer function when s = 0, which is equal to a/c or kdc.
In this example setup the DC Gain equals a/c
>> k = a/c
k=
0.3333
To find the DC Gain using MATLAB's dcgain function:
>> dcgain(tutorial_sys)
ans =
0.3333

IMPULSE RESPONSE
The Impulse Response illustrates how the system responds when a brief input is applied. In
order to find the impulse response of this system with an input impulse of 2 use MATLAB's
impulse function.
>> u = 2;
>> impulse(u * tutorial_sys)
The MATLAB output will be the following plot of the impulse response:

BODE PLOT
Bode plots are a graphical representation of the frequency on a logarithmic scale. They are
useful for system identification, analysis, and controller design. In order to find the bode plot
of this tutorial system using MATLAB, use the bode function.
>> bode(tutorial_sys)
The MATLAB output will be the following bode plot of the frequency response:

The magnitude of the bode plot of a second order system drops off at -40dB per decade at
high frequencies, while the relative phase changes from 0 to -180 degrees at -90 degrees
per decade at the intermediate frequencies.

BAB 7..SYSTEM IDENTIFICATION


System parameters such as the damping ratio, the natural frequency, and the DC gain can
be found using the step response or bode plot.
ESTIMATING THE ORDER OF A SYSTEM
The order and relative degree of a system can be estimated from either the step response or
the bode plot. The relative degree of a system is the difference between the order of the
denominator over the order of the numerator of the transfer function and is the lowest order
the system can be.
STEP RESPONSE - If the response of the system to a non-zero step input has a zero slope
when t=0, the system must be second order or higher because the system has a relative
degree of two or higher.
If the step response shows oscillations, the system must be a second order or higher
underdamped system and have a relative degree of two or higher.
BODE PLOT - The phase plot can be a good indicator of order. If the phase drops below
-90 degrees, the system must be second order or higher. The relative degree of the system
has to be at least as great as the number of multiples of -90 degrees achieved asymptotically
at the lowest point on the phase plot of the system.

IDENTIFYING A SYSTEM FROM THE STEP RESPONSE


DAMPING RATIO - For an underdamped second order system, the damping ratio can be
calculated from the percent overshoot using the following formula:

= -ln(%OS/100) / sqrt(2+ln2(%OS/100))
where %OS is the percent overshoot, which can be approximated off the plot of the step
response.
DC GAIN - The DC gain is the ratio of the steady state step response to the magnitude of a
step input.
DC Gain = steady state output / step magnitude

NATURAL FREQUENCY - The natural frequency of an underdamped second order system


can be found from the damped natural frequency which can be measured off the plot of the
step response and the damping ratio which was calculated above.

n = d / sqrt(1 - 2)
where wd is the damped frequency in rad/s which is 2p/Dt where Dt is the time interval
between two consecutive peaks on the plot of the step response.

IDENTIFYING A SYSTEM FROM THE BODE PLOT


DC GAIN - The DC Gain of a system can be calculated from the magnitude of the bode plot
when s=0.
DC Gain = 10M(0)/20
where M(0) is the magnitude of the bode plot when jw=0.
NATURAL FREQUENCY - The natural frequency of a second order system occurs when the
phase of the response is -90 degrees relative to the phase of the input.

n = -90
where -90 is the frequency at which the phase plot is at -90 degrees.
DAMPING RATIO - The damping ratio of a system can be found with the DC Gain and the
magnitude of the bode plot when the phase plot is -90 degrees.

= K / (2*10(M

-90/20)

where M-90 is the magnitude of the bode plot when the phase is -90 degrees.

IDENTIFYING SYSTEM PARAMETERS


If the type of system is known, specific system parameters can be extracted from the step
response or the bode plot.
The general form of the transfer function of a first order system is

G(s) = b/(s+a) = K/(s+1).


The general form of the transfer function of a second order system is

G(s) = a/(s^2+bs+c) = Kn2/(s2+2ns+n2).

UNDERDAMPED EXAMPLE
IDENTIFICATION USING THE STEP RESPONSE
Identify the system with the following response to a step input of magnitude 3.

ESTIMATING ORDER
This system clearly is a second order or higher underdamped system because the system
oscillates. The step response also has a zero initial slope when t=0, reinforcing the
conclusion that the system is second order or higher. For this reason we will model this
system as an underdamped second order system.
DAMPING RATIO
The damping ratio of this system can be found using the percent overshoot. The percent
overshoot can be measured off the step response by finding the ratio of the maximum
achieved peak and the steady state output.

Off the plot, the percent overshoot is:


%OS = 100% ( peak value - steady state output ) / steady state output
Using MATLAB:
>> peak = 1.39;
>> ss = 1;
>> os = 100*(peak-ss)/ss
os

=
39

The damping ratio is:


= -ln(%OS/100) / sqrt(2+ln2(%OS/100))
Using MATLAB:
>> dampingratio = -log(os/100)/sqrt(pi^2+(log(os/100))^2)
dampingratio =
0.2871
The damping ratio is 0.29, which is less than one, so this system is underdamped.

DC GAIN
The DC gain is the ratio of the steady state step response to the magnitude of a step
input. The steady state step response can be determined from the plot of the step response,
and the magnitude of the step input is given as u=3.

The DC Gain is:


DC Gain = steady state output / step magnitude
Using MATLAB:
>> u = 3;
>> ss = 1;
>> dcgain = ss/u
dcgain

0.3333
The DC Gain of this system is 0.33.

NATURAL FREQUENCY
The natural frequency of an underdamped second order system can be found from the
damped natural frequency which can be measured off the plot of the step response and the
damping ratio which was calculated above.

The natural frequency is:


n = d / sqrt(1 - 2)
The damped natural frequency is:
d = 2/t
where t is the time interval between two consecutive peaks on the plot of the step response.
Using MATLAB:
>> dt = (11.3 - 3.82);
>> wd = 2*pi/dt;
>> wn = wd/sqrt(1-dampingratio^2)
wn =
0.8781
The natural frequency of this system is approximately 0.88 rad/s which means that in the
absence of all damping, this system will oscillate at 0.88 rad/s.

IDENTIFYING SYSTEM PARAMETERS


This second order system is modeled by a transfer function in the form

G(s) = a/(s^2+bs+c) = Kn2/(s2+2ns+n2)


The transfer function can be found by plugging the DC gain, natural frequency, and damping
ratio found from the step response into the canonical transfer function.
(0.33)*(0.88^2)/(s^2+2*(0.29)*(0.88)*s+(0.88^2))
= 0.26/(s^2+0.51s+0.77)
Because we know this system is a simple mass-spring-damper system, the form of the
transfer function is 1/(ms^2+bs+k) where m is the mass, b is the damping coefficient, and k is
the spring constant.
G(s) = 1/(ms^2+bs+k) = 0.26/(s^2+0.51s+0.77)
G(s) = 1/(3.8)s^2 + 2.0s + 3.0)
so m = 3.8, b = 2.0, and k = 3.0 in this mass-spring-damper system.
To input this transfer function into MATLAB, refer to the TRANSFER FUNCTION section of
this tutorial.

IDENTIFICATION USING THE BODE PLOT


Identify the following system with the Bode Plot.

ESTIMATING ORDER

The phase plot of the bode plot dips asymptotically to -180 degrees relative to the input.
-180 degrees is two multiples of -90 degrees, so the system is at least second order.
DC GAIN
The DC Gain of a system can be calculated from the magnitude of the bode plot when s=0.

The DC Gain is:


DC Gain = 10M(0)/20
where M(0) is the magnitude of the bode plot when jw=0.
Using MATLAB:
M0 = -9.44;
dcgain = 10^(M0/20)
dcgain =
0.3373
The DC Gain of the system is 0.34.

Natural Frequency

The natural frequency of a second order system is the frequency when the phase of the
response is -90 degrees.

The natural frequency is:

n = -90
= 0.865
where -90 is the frequency at which the phase plot is at -90 degrees.

DAMPING RATIO
The damping ratio of a system can be found with the DC Gain and the magnitude of the bode
plot when the phase plot is -90 degrees.

The damping ratio is:

= K / (2*10(M

-90/20)

where M-90 is the magnitude of the bode plot when the phase is -90 degrees.
Using MATLAB:
M90 = -4.75;
dampingratio = dcgain/(2*10^(M90/20))
dampingratio =
0.2914
The damping ratio of this system is 0.29 which is less than one, so this system is
underdamped.

IDENTIFYING SYSTEM PARAMETERS


This second order system is modeled by a transfer function in the form

G(s) = a/(s^2+bs+c) = Kn2/(s2+2ns+n2)


The transfer function can be found by plugging the DC gain, natural frequency, and damping
ratio found from the bode plot into the canonical transfer function.

(0.34)*(0.87^2)/(s^2+2*(0.29)*(0.87)*s+(0.87^2))
= 0.26/(s^2+0.50s+0.76)
Because we know this system is a simple mass-spring-damper system, the form of the
transfer function is 1/(ms^2+bs+k) where m is the mass, b is the damping coefficient, and k is
the spring constant.
G(s) = 1/(ms^2+bs+k) = 0.26/(s^2+0.50s+0.76)
G(s) = 1/(3.8)s^2 + 1.9s + 2.9)
so m = 3.8, b = 1.9, and k = 2.9 in this mass-spring-damper system.

To input this transfer function into MATLAB, refer to the TRANSFER FUNCTION section of
this tutorial.
Notice that the numbers gathered from the step response and the bode plot are very similar
but not always identical. This is because the process of determining the numbers from the
plot is inexact. Nonetheless the parameters from the two systems are very similar as
expected.
OVERDAMPED EXAMPLE
IDENTIFICATION USING THE STEP RESPONSE
Identify the system with the following response to a step input of magnitude 2.

An overdamped system has a very simple step response plot, which has few distinguishing
features to identify. For this reason, the step response is rarely used to identify an
overdamped system. The step reponse plot is still useful for estimating the order of the
system and finding the DC gain..
ESTIMATING ORDER
At first glance this system appears that it might be first order because it has no overshoot.
Upon closer inspection there is an inflection near t=0 and zooming in on the plot would show
that the slope is zero at t=0. The system must be at least second order. We will approximate
with a second order system. This system has no overshoot, and so it must be an
overdamped second order system. Thus the transfer function of the system must have two
real poles.
DC GAIN
The DC gain is the ratio of the steady state step response to the magnitude of a step
input. The steady state step response is the value of the plot as t approaches infinity on the
graph. The magnitude of the step input is given as u=2.
As can be seen from the above plot, the steady state step response is 0.5.
The DC Gain is:
DC Gain = steady state output / step magnitude
Using MATLAB:
>> u = 2;
>> ss = 0.5;
>> dcgain = ss/u
dcgain
0.2500
The DC Gain of this system is 0.25.

IDENTIFICATION USING THE BODE PLOT


Identify the following system using the Bode Plot.

ESTIMATING ORDER
The phase plot of the bode plot dips asymptotically to -180 degrees relative to the input.
-180 degrees is two multiples of -90 degrees, so the system is at least second order.
DC GAIN
The DC Gain of a system can be calculated from the magnitude of the bode plot as
frequency approaches zero.

The DC Gain is:


DC Gain = 10M(0)/20
where M(0) is the magnitude of the bode plot when jw=0.
Using MATLAB:
M0 = -12.3;
dcgain = 10^(M0/20)
dcgain =
0.2427
The DC Gain of the system is 0.24.

NATURAL FREQUENCY
The natural frequency of a second order system occurs when the phase of the response is
-90 degrees relative to the phase of the input.

The natural frequency is:

n = -90

= 1.42
where w-90 is the frequency at which the phase plot is at -90 degrees.

DAMPING RATIO
The damping ratio of a system can be found with the DC Gain and the magnitude of the bode
plot when the phase plot is -90 degrees.

The damping ratio is:


= K / (2*10(M-90/20))
where M-90is the magnitude of the bode plot when the phase is -90 degrees.
Using MATLAB:
M90 = -23.1;
dampingratio = dcgain/(2*10^(M90/20))
dampingratio =
1.7337
The damping ratio of this system is 1.73 which is greater than one, so this system is
overdamped.

IDENTIFYING SYSTEM PARAMETERS


This second order system is modeled by a transfer function in the form

G(s) = a/(s^2+bs+c) = Kn2/(s2+2ns+n2)


The transfer function can be found by plugging the DC gain, natural frequency, and damping
ratio found from the bode plot into the canonical transfer function.
(0.24)*(1.42^2)/(s^2+2*(1.73)*(1.42)*s+(1.42^2))
= 0.48/(s^2+4.91s+2.02)
Because we know this system is a simple mass-spring-damper system, the form of the
transfer function is 1/(ms2+bs+k) where m is the mass, b is the damping coefficient, and k is
the spring constant.
G(s) = 1/(ms^2+bs+k) = 0.48/(s^2+4.91s+2.02)
G(s) = 1/(2.1)s^2 + 10.2s + 4.2)
so m = 2.1, b = 10, and k = 4.2 in this mass-spring-damper system.
To input this transfer function into MATLAB, refer to the TRANSFER FUNCTION section of
this tutorial.

BAB 8.SIMULINK BASICS


Starting Simulink
Model Files
Basic Elements
Running Simulations
Building Systems
Simulink is a graphical extension to MATLAB for modeling and simulation of systems. In
Simulink, systems are drawn on screen as block diagrams. Many elements of block diagrams
are available, such as transfer functions, summing junctions, etc., as well as virtual input and
output devices such as function generators and oscilloscopes. Simulink is integrated with
MATLAB and data can be easily transferred between the programs. In these tutorials, we will
apply Simulink to the examples from the MATLAB tutorials to model the systems, build
controllers, and simulate the systems. Simulink is supported on Unix, Macintosh, and
Windows environments; and is included in the student version of MATLAB for personal
computers. For more information on Simulink, contact the MathWorks.
The idea behind these tutorials is that you can view them in one window while running
Simulink in another window. System model files can be downloaded from the tutorials and
opened in Simulink. You will modify and extend these system while learning to use Simulink
for system modeling, control, and simulation. Do not confuse the windows, icons, and menus
in the tutorials for your actual Simulink windows. Most images in these tutorials are not live they simply display what you should see in your own Simulink windows. All Simulink
operations should be done in your Simulink windows.
Starting Simulink
Simulink is started from the MATLAB command prompt by entering the following command:
>> simulink
Alternatively, you can hit the Simulink button at the top of the MATLAB window as shown
below:

When it starts, Simulink brings up the Simulink Library browser.

Open the modeling window with New then Model from the File menu on the Simulink Library
Browser as shown above.

This will bring up a new untitiled modeling window shown below.

Model Files
In Simulink, a model is a collection of blocks which, in general, represents a system. In
addition, to drawing a model into a blank model window, previously saved model files can be
loaded either from the File menu or from the MATLAB command prompt. As an example,
download the following model file by clicking on the following link and saving the file in the
directory you are running MATLAB from.
simple.mdl
Open this file in Simulink by entering the following command in the MATLAB command
window. (Alternatively, you can load this file using the Open option in the File menu in
Simulink, or by hitting Ctrl+O in Simulink.)
simple
The following model window should appear.

A new model can be created by selecting New from the File menu in any Simulink window
(or by hitting Ctrl+N).
Basic Elements
There are two major classes of items in Simulink: blocks and lines. Blocks are used to
generate, modify, combine, output, and display signals. Lines are used to transfer signals
from one block to another.

Blocks
There are several general classes of blocks:
Continuous
Discontinuous
Discrete
Look-Up Tables
Math Operations
Model Verification
Model-Wide Utilities
Ports & Subsystems
Signal Attributes
Signal Routing
Sinks: Used to output or display signals
Sources: Used to generate various signals

User-Defined Functions
Discrete: Linear, discrete-time system elements (transfer functions, state-space
models, etc.)
Linear: Linear, continuous-time system elements and connections (summing
junctions, gains, etc.)
Nonlinear: Nonlinear operators (arbitrary functions, saturation, delay, etc.)
Connections: Multiplex, Demultiplex, System Macros, etc.

Blocks have zero to several input terminals and zero to several output terminals. Unused
input terminals are indicated by a small open triangle. Unused output terminals are indicated
by a small triangular point. The block shown below has an unused input terminal on the left
and an unused output terminal on the right.

Lines
Lines transmit signals in the direction indicated by the arrow. Lines must always transmit
signals from the output terminal of one block to the input terminal of another block. On
exception to this is a line can tap off of another line, splitting the signal to each of two
destination blocks, as shown below (click the figure to download the model file called
split.mdl).

Lines can never inject a signal into another line; lines must be combined through the use of a
block such as a summing junction.
A signal can be either a scalar signal or a vector signal. For Single-Input, Single-Output
systems, scalar signals are generally used. For Multi-Input, Multi-Output systems, vector
signals are often used, consisting of two or more scalar signals. The lines used to transmit
scalar and vector signals are identical. The type of signal carried by a line is determined by
the blocks on either end of the line.
Simple Example

The simple model (from the model file section) consists of three blocks: Step, Transfer Fcn,
and Scope. The Step is a source block from which a step input signal originates. This signal
is transferred through the line in the direction indicated by the arrow to the Transfer Function
linear block. The Transfer Function modifies its input signal and outputs a new signal on a
line to the Scope. The Scope is a sink block used to display a signal much like an
oscilloscope.
There are many more types of blocks available in Simulink, some of which will be discussed
later. Right now, we will examine just the three we have used in the simple model.

Modifying Blocks
A block can be modified by double-clicking on it. For example, if you double-click on the
"Transfer Fcn" block in the simple model, you will see the following dialog box.

This dialog box contains fields for the numerator and the denominator of the block's transfer
function. By entering a vector containing the coefficients of the desired numerator or
denominator polynomial, the desired transfer function can be entered. For example, to
change the denominator to s^2+2s+1, enter the following into the denominator field:
[1 2 1]
and hit the close button, the model window will change to the following,

which reflects the change in the denominator of the transfer function.


The "step" block can also be double-clicked, bringing up the following dialog box.

The default parameters in this dialog box generate a step function occurring at time=1 sec,
from an initial level of zero to a level of 1. (in other words, a unit step at t=1). Each of these
parameters can be changed. Close this dialog before continuing.

The most complicated of these three blocks is the "Scope" block. Double clicking on this
brings up a blank oscilloscope screen.

When a simulation is performed, the signal which feeds into the scope will be displayed in
this window. Detailed operation of the scope will not be covered in this tutorial. The only
function we will use is the autoscale button, which appears as a pair of binoculars in the
upper portion of the window.
Running Simulations
To run a simulation, we will work with the following model file:
simple2.mdl
Download and open this file in Simulink following the previous instructions for this file. You
should see the following model window.

Before running a simulation of this system, first open the scope window by double-clicking on
the scope block. Then, to start the simulation, either select Start from the Simulation menu

(as shown below) or hit Ctrl-T in the model window.

The simulation should run very quickly and the scope window will appear as shown below. If
it doesn't, just double click on the block labeled "scope."

Note that the simulation output (shown in yellow) is at a very low level relative to the axes of
the scope. To fix this, hit the autoscale button (binoculars), which will rescale the axes as
shown
below.

Note that the step response does not begin until t=1. This can be changed by double-clicking
on the "step" block. Now, we will change the parameters of the system and simulate the
system again. Double-click on the "Transfer Fcn" block in the model window and change the
denominator to
[1 20 400]
Re-run the simulation (hit Ctrl-T) and you should see what appears as a flat line in the scope
window. Hit the autoscale button, and you should see the following in the scope window.

Notice that the autoscale button only changes the vertical axis. Since the new transfer
function has a very fast response, it compressed into a very narrow part of the scope
window. This is not really a problem with the scope, but with the simulation itself. Simulink
simulated the system for a full ten seconds even though the system had reached steady
state shortly after one second.
To correct this, you need to change the parameters of the simulation itself. In the model
window, select Parameters from the Simulation menu. You will see the following dialog box.

There are many simulation parameter options; we will only be concerned with the start and
stop times, which tell Simulink over what time period to perform the simulation. Change Start
time from 0.0 to 0.8 (since the step doesn't occur until t=1.0. Change Stop time from 10.0 to
2.0, which should be only shortly after the system settles. Close the dialog box and rerun the
simulation. After hitting the autoscale button, the scope window should provide a much better
display of the step response as shown below.

Building Systems
In this section, you will learn how to build systems in Simulink using the building blocks in
Simulink's Block Libraries. You will build the following system.

If you would like to download the completed model, here.


First you will gather all the necessary blocks from the block libraries. Then you will modify the
blocks so they correspond to the blocks in the desired model. Finally, you will connect the
blocks with lines to form the complete system. After this, you will simulate the complete
system to verify that it works.

Gathering Blocks
Follow the steps below to collect the necessary blocks:
Create a new model (New from the File menu or Ctrl-N). You will get a blank model
window.

Click on the Library Browser button


to open the Simulink Library Browser. Click
on the Sources option under the expanded Simulink title to reveal possible sources for
the model.

Click here for more information on block libraries.

Drag the Step block from the sources window into the left side of your model window.

From the Simulink Library Browser, drag the Sum and Gain from "Math Operations"
option found under the Simulink title.

Switch to the "Continuous" option and drag two instances of the Transfer Fcn (drag it
two times) into your model window arranged approximately as shown below. The
exact alignment is not important since it can be changed later. Just try to get the
correct relative positions. Notice that the second Transfer Function block has a 1 after
its name. Since no two blocks may have the same name, Simulink automatically
appends numbers following the names of blocks to differentiate between them.

Click on the "Sinks" option then drag over the "Scope" icon

Modify Blocks
Follow these steps to properly modify the blocks in your model.
Double-click your Sum block. Since you will want the second input to be subtracted,
enter +- into the list of signs field. Close the dialog box.
Double-click your Gain block. Change the gain to 2.5 and close the dialog box.
Double-click the leftmost Transfer Function block. Change the numerator to [1 2] and
the denominator to [1 0]. Close the dialog box.
Double-click the rightmost Transfer Function block. Leave the numerator [1], but
change the denominator to [1 2 4]. Close the dialog box. Your model should appear
as:

Change the name of the first Transfer Function block by clicking on the words
"Transfer Fcn". A box and an editing cursor will appear on the block's name as shown
below. Use the keyboard (the mouse is also useful) to delete the existing name and
type in the new name, "PI Controller". Click anywhere outside the name box to finish
editing.

Similarly, change the name of the second Transfer Function block from "Transfer
Fcn1" to "Plant". Now, all the blocks are entered properly. Your model should appear
as:

Connecting Blocks with Lines


Now that the blocks are properly laid out, you will now connect them together. Follow these
steps.
Drag the mouse from the output terminal of the Step block to the upper (positive)
input of the Sum block. Let go of the mouse button only when the mouse is right on
the input terminal. Do not worry about the path you follow while dragging, the line will
route itself. You should see the following.

The resulting line should have a filled arrowhead. If the arrowhead is open, as shown
below, it means it is not connected to anything.

You can continue the partial line you just drew by treating the open arrowhead as an
output terminal and drawing just as before. Alternatively, if you want to redraw the
line, or if the line connected to the wrong terminal, you should delete the line and
redraw it. To delete a line (or any other object), simply click on it to select it, and hit
the delete key.

Draw a line connecting the Sum block output to the Gain input. Also draw a line from
the Gain to the PI Controller, a line from the PI Controller to the Plant, and a line from
the Plant to the Scope. You should now have the following.

The line remaining to be drawn is the feedback signal connecting the output of the
Plant to the negative input of the Sum block. This line is different in two ways. First,
since this line loops around and does not simply follow the shortest (right-angled)
route so it needs to be drawn in several stages. Second, there is no output terminal to
start from, so the line has to tap off of an existing line.

To tap off the output line, hold the Ctrl key while dragging the mouse from the point on
the existing line where you want to tap off. In this case, start just to the right of the
Plant. Drag until you get to the lower left corner of the desired feedback signal line as
shown below.

Now, the open arrowhead of this partial line can be treated as an output terminal.
Draw a line from it to the negative terminal of the Sum block in the usual manner.

Now, you will align the blocks with each other for a neater appearance. Once
connected, the actual positions of the blocks does not matter, but it is easier to read if
they are aligned. To move each block, drag it with the mouse. The lines will stay
connected and re-route themselves. The middles and corners of lines can also be
dragged to different locations. Starting at the left, drag each block so that the lines
connecting them are purely horizontal. Also, adjust the spacing between blocks to

leave room for signal labels. You should have something like:

Finally, you will place labels in your model to identify the signals. To place a label
anywhere in your model, double click at the point you want the label to be. Start by
double clicking above the line leading from the Step block. You will get a blank text
box with an editing cursor as shown below

Ty
pe an r in this box, labeling the reference signal and click outside it to end editing.

Label the error (e) signal, the control (u) signal, and the output (y) signal in the same
manner. Your final model should appear as:

To save your model, select Save As in the File menu and type in any desired model
name. The completed model can be found here.

Simulation
Now that the model is complete, you can simulate the model. Select Start from the
Simulation menu to run the simulation. Double-click on the Scope block to view its output.
Hit the autoscale button (binoculars) and you should see the following.

Taking Variables from MATLAB


In some cases, parameters, such as gain, may be calculated in MATLAB to be used in a
Simulink model. If this is the case, it is not necessary to enter the result of the MATLAB
calculation directly into Simulink. For example, suppose we calculated the gain in MATLAB in
the variable K. Emulate this by entering the following command at the MATLAB command
prompt.
K=2.5

This variable can now be used in the Simulink Gain block. In your Simulink model, doubleclick on the Gain block and enter the following in the Gain field.
K

Close this dialog box. Notice now that the Gain block in the Simulink model shows the
variable K rather than a number.

Now, you can re-run the simulation and view the output on the Scope. The result should be
the same as before.

Now, if any calculations are done in MATLAB to change any of the variables used in the
Simulink model, the simulation will use the new values the next time it is run. To try this, in
MATLAB, change the gain, K, by entering the following at the command prompt.
K=5
Start the Simulink simulation again, bring up the Scope window, and hit the autoscale button.
You will see the following output which reflects the new, higher gain.

Besides variables, signals and even entire systems can be exchanged between MATLAB
and Simulink. For more information, click here.

BAB 9..SIMULINK MODEL


In Simulink it is very straightforward to represent a model of a physical system in block
diagram form. These blocks represent basic mathematical operations such as summation,
multiplication, and integration. Other blocks serve as inputs or for visualization. The best
way to learn Simulink is just to construct a model for a simple physical system.

EXAMPLE
Model the second order tutorial example system using Simulink.

Figure 1 - Second order system setup


The governing equation gathered from the free body equation is:
f(t) - k x - b x' - m x" = 0
CONSTRUCTING THE MODEL
For a system represented by an nth order input/output ordinary differential equation it is
necessary to integrate the highest derivative n times to obtain the output. For this reason,
the preferred form of the differential equation is to solve for the highest derivative as a
function of all of the other terms. For this reason it is useful to rewrite the equation of motion
in the following form:
x" = 1/m[bx' + kx + f(t)]
To begin the construction of the Simulink model, we must first start Simulink and open a new
model window. To start Simulink, type simulink at the MATLAB prompt, or click on the
icon.
To start a new model in Simulink, click File on the Simulink Library Browser window's tool
bar, click on New and then click on Model or equivalently type Ctrl+N.

CONSTRUCTING THE DERIVATIVE RELATIONS


Once a new model window is open, the first step in constructing this second order model is
to include two integrators which represent the integration of acceleration to velocity and
integration of velocity to position. In the Simulink Library Browser expand Simulink. Then
single-click on Continuous. Then drag two Integrator blocks into the Simulink model.

Next, connect the two integrators by dragging a line between them. Click on the output
arrow of the first integrator and drag it to the input arrow of the second integrator.

Label this line x' by double-clicking on the line and typing in the textbox that opens.

Next, add a line going into the first integrator and a line coming out of the second integrator.
To add the line to the input of the first integrator, click the input of the first integrator and drag

it to the left. And for the output of the second integrator, click on the output and drag it to the
right. Label the line attached to the input of the first integrator as x", and label the line
attached to the output of the second integrator as x.

It is useful to label the integrators themselves. To rename any block, click on the block's
label and type in the text box.

CONSTRUCTING THE DAMPING FORCE


Now let's construct the signal representing the damping force. The damping force is the x'
multiplied by the damping constant. We can pull x' from the output of the Integration from x"
to x' block. The Simulink equivalent to multiplication is the Gain block. From the Simulink
Library Browser window add a Gain block by expanding Simulink and then clicking on Math
Operations and then dragging the Gain block into the model.

The number inside the triangle indicates the gain of the Gain block. To change the gain of
this Gain block from 1 to b, right click on the Gain block and click on Gain parameters... A
new window labeled Block Parameters: Gain will pop up. Change the value in the field for
Gain from 1 to b.

Clicking OK will return you to the Simulink Model window.

The input of the Gain block is currently on the left side. To make the line connection simpler,
we would like the input of the Gain block to be on the right. To flip a Gain block, right click on
the block and select Format and select Flip block.

Also it may be useful to relabel this Gain block Damping Coefficient, do so by clicking on the
title and typing in the text box that opens.

In order to multiply x' with the Damping Coefficient to calculate the damping force, we need
to drag a line from the input of the Damping Coefficient to the line marked x'. Be sure it
connects to the line with the small rectangle as shown.

The damping force is now the output of the Damping Coefficient block. Drag a line out of the
output and label it Damping Force.

CONSTRUCTING THE SPRING FORCE


The spring force is determined by multiplying x with the spring constant, k. This can be done
using the same methods prescribed above for creating the Damping Force resulting in the
following addition to the model.

SUMMING OF FORCES
From looking at the governing equation, there are three forces that are summed together to
form the total force on the body, one external force and two internal forces, namely the
Damping Force and the Spring Force created above. Summation in Simulink is
accomplished by using the Sum block. From the Simulink Library Browser window,
expand Simulink and click on Math Operations and then scroll down in the right column to
find Sum. Drag the Sum block into the model.

The default Sum block has two positive inputs. The system we are modeling has one
positive input and two negative inputs. To change the number and polarity of inputs, right
click on the Sum block and select Sum parameters.... A new window titled Block
Parameters: Sum will open up. In the field marked List of signs change the value from |++
to |+--. Selecting OK will return you to the model.

The default size for the Sum block is too small to comfortably hold three inputs. To change
the size, click on on the Sum block drag on one of the corner blocks to enlarge it. Also it
might be useful to name the Sum block. To turn on the naming of the Sum block, right click
on the block and select Format and then select Show name. Change the name by clicking
on it and typing in the text box.

Now the name of the Sum block is in the way of one of the input terminals. To flip the name
to the top of the block, right click on the block and select Format and select Flip name.

Now to connect the Damping Force and Spring Force to the two negative input terminals of
the Sum block, drag the end of the arrow labeled Damping Force to a negative input of the
Sum block.

Now do the same for the Spring Force.

The third input of our Sum block is reserved for the external force. Different analyses of this
system use different sources for the applied force. Simply draw a line out of the positive
input of the Sum block and label it External Forces. Later on, the output of a source will
attach to this arrow.

The output of the Sum block is the Sum of all of the forces acting on the system. Draw a line
out of the input and label it Sum of All Forces.

CREATING X"
From Newton's Law, the Sum of All Forces is equal to mx". So dividing the sum of all forces
results in x". To multiply the Sum of All Forces by 1/m, add a Gain block with value 1/m.
From the Simulink Library Browser window, expand Simulink and select Math Operations
and drag the Gain block into the model from the right column.

Change the value of the Gain block from 1 to 1/m by right clicking on the Gain block,
selecting Gain parameters... and changing the value for Gain in the Block Parameters:
Gain window that opens up.

The block is too small to display the value of the gain. Enlarge the block slightly by clicking
on it and dragging the corner rectangles. Also it may be beneficial to rename this Gain block
Division by m.

Now connect the line labeled Sum of All Forces to the input of Division by m, and connect the
line marked x" to the output of Division by m.

Save the model by selecting File in the toolbar of the model and selecting Save as....
The finished Simulink model can be downloaded here: tutorial.mdl

The model is now complete and ready for running.

RUNNING THE MODEL


Many useful analyses of the model can be done using Simulink.
used in the model must be defined before running a simulation.
section will use the same values used in the Transfer Function
sections. Enter the following values into the MATLAB window
variable values directly from MATLAB commands.

Values for the variables


This Simulink Modeling
and State Space Model
prompt. Simulink reads

>> m = 2;
>> b = 5;
>> k = 3;
SIMULATING THE STEP RESPONSE
Once the model has been created in MATLAB it is easy to simulate the response to a step
input. In order to simulate the step response you need to add a source to provide the
external force, and you need a sink to view the response of the system. In the Simulink
Library Browser window, expand Simulink and click on Sources and then drag the Step
source
from the right column into the model. Connect the tail of the arrow labeled
External Forces to the Step source output.

To monitor the value of x add a scope sink. Go to the Simulink Library Browser window,
expand Simulink, click on Sinks, and then drag the Scope sink
from the right column
into the model. Connect the head of the arrow labeled x to the Scope sink.

The default parameters for the Step source are a Step time of 1, an Initial value of 0, a Final
value of 1, and a Sample time of 0. To change the parameters of the Step source, right click
on the Step source and select Step parameters.... In the Block Parameters: Step window,
change the parameters to whatever is desired. To find the response to a step input of
magnitude 4 with the step starting at time t=0, change the parameters to the following: a Step
time of 0, an Initial value of 0, a Final value of 4, and a Sample time of 0.

To run the simulation, click Simulation in the tool bar and select Start, or equivalently hit
Ctrl+T on the keyboard, or click the button on the tool bar.
To view the output of the Scope sink which is monitoring the value of x, double click on the
Scope in the model. The following plot of the step response will appear in a new window.

To autoscale the plot, click on the

button.

This plot shows the same response to a step input of magnitude 4 as was calculated in the
Transfer Function and State Space Model sections of this tutorial.

SINUSOIDAL RESPONSE
To find the sinusoidal response of a model, the source should be a signal generator, and the
sink should be a scope. Before adding any new source, first delete any existing source by
clicking on it and hitting delete on your keyboard. To add a signal generator, expand

Simulink in the Simulink Library Browser window and select Sources and then drag the
Signal Generator source
into the model and connect its output to the tail of the arrow
labeled External Forces. Add the Scope using the same method prescribed from above
when finding the Step Response.

To set the signal output of the Signal Generator source, double click on the Signal
Generator. To model the response to a sine wave of amplitude 4 and frequency 2 Hz,
change the values as shown below.

Be sure the values for the variables m, b, and k have been defined in the MATLAB Command
Window. Once the input, the output, and all of the variables have been defined, the model
can be run.

To run the simulation, click Simulation in the tool bar and select Start, or equivalently hit
Ctrl+T on the keyboard, or click the button on the tool bar.
To view the output of the Scope sink which is monitoring the value of x, double click on the
Scope in the model. The following plot of the sinusoidal response will appear in a new
window.

To autoscale the plot, click on the

button.

This plot shows the response to a sinusoidal input of amplitude of 4 and a frequency of 2
Hz.
Note that the output waveform is jagged. This means that the step size of the simulation is
too big. For good results, the step size should be at least 20 times smaller than the period of
the sine wave. In this case the period is 0.5 seconds, and so the step size should be 0.025
or smaller.

To change the step size of the simulation, click Simulation in the model tool bar and select
Simulation parameters.... Alternately you can press Ctrl+E to bring up the Simulation
Parameters window. Change the Max step size to 0.025.

Running the simulation again with the reduced step size and autoscaling yields the following
plot.

OBTAINING THE MATLAB MODEL


MATLAB can extract the matricies of a state space representation of a Simulink model by
using the linmod command.
The syntax for the linmod function is:
>> [ A, B, C, D ] = linmod('system')

where A, B, C, and D are from the standard vector-matrix form of a state space model and
system is the name of the Simulink model.
In order to use the linmod command with a Simulink system, the sources and sinks need to
be changed. To delete any existing sources and sinks, simply click on them and hit the
delete key on your keyboard.

The correct source to run linmod is the subsystem input. To insert this source, go to the
Simulink Library Browser window, expand Simulink, click on Sources, and find the source
In1
and drag it into the system.

The correct sink to run linmod is the subsystem input. To insert this sink, go to the Simulink
Library Browser window, expand Simulink, click on Sinks, and find the sink Out1
and
drag it into the system.

Be sure the values for the variables m, b, and k have been defined in the MATLAB Command
Window. Once the input, the output, and all of the variables have been defined, the MATLAB
model can be obtained.
In the case of our tutorial:
>> [ A, B, C, D ] = linmod('tutorial')
This defines A, B, C, and D as matricies that define a state space model in standard vectormatrix form and outputs the following:
A=
0 1.0000
-1.5000 -2.5000
B=
0
0.5000
C=
1.0000
D=
0

FREE BODY DIAGRAM


of a first order system

A first order system is one that is governed by a differential equation that consists of only a
variable and its first derivative.
In this example we will consider a truck pulling a trailer. The truck exerts an external force
pulling the trailer, the trailer also experiences rolling resistance and wind resistance, which
can be combined as one friction force on the trailer.

Figure 1 - System setup: Truck pulling a trailer


This example problem can be modeled as a sliding block where the friction acts as a velocity
dependant force, with a coefficient b.

Figure 2 - System setup: Sliding block with friction

FREE BODY DIAGRAM


In order to draw a free body diagram of the above figure we must separately consider each
and every force acting on the body and then draw them all onto the diagram.
Force

Mathematical Expression

Direction

Applied Force

f(t) (positive)

Toward the Right

Friction Force

b v (negative)

Toward the Left

Inertial Force

m v' (negative)

Toward the Left

Drawing the vectors in the table above yields the following free body diagram.

Figure 3 - Free body diagram: Sliding block with friction

GOVERNING EQUATION
Summing all of the forces on the body together in a mathematical equation yields the
governing equation for this system.
According to Newtons Laws, the sum of the forces acting on a body is equal to the mass
times the acceleration. In this example there are two forces acting on the body, the applied
force acting on the block, f(t), and the friction of the block on the stationary surface,
quantified as b v pulling to the left, resisting the applied force. The summation of those two
equal the mass times the acceleration.
Using Newton's law:
Sum of all Forces = m v' = f(t) - b v
so
f(t) - m v' - b v = 0

FREE BODY DIAGRAM


of a second order system
This example problem can be modeled as a rotational mass, with inertia I, where the friction
acts as a velocity dependant force, with coefficient B, and the spring force acts as a position
dependant force, with coefficient K. This system has non-zero initial conditions.
Solving a rotational second order problem is directly analogous to solving a linear second
order problem. The only differences are that instead of forces we deal with torques, instead
of mass we deal with inertia, and instead of translational motion we deal with rotations about
an axis.
FREE BODY DIAGRAM
In order to draw a free body diagram of the above figure we must separately consider each
and every torque acting on the body and then draw them all onto the diagram.
Force

Mathematical Expression

Direction

Spring Torque

k (negative)

Clockwise

Friction Torque

b ' (negative)

Clockwise

Inertial Torque

I " (negative)

Clockwise

Drawing the torques in the table above yields the following free body diagram.

Figure 1 - Free body diagram: Ferris Wheel as a rotational mass-spring-damper.

GOVERNING EQUATION
Summing all of the torques on the body together in a mathematical equation yields the
governing equation for this system.
According to D'Alembert's Law, the sum of all torques acting on a body including the inertial
torque is equal to zero.
Using D'Alembert's Law:

Sum of all torques = -k - b' - m" = 0


or equivalently
k + b' + I" = 0

FREE BODY DIAGRAM


of a high order system
In this example we will be considering the motion of a truck seat equipped with suspension
mounted inside a truck which has a suspension of its own. One simplified model of the
system is a double mass-spring-damper setup as shown in Figure 1 below. This model is
fourth order because there are four independent energy storage elements.

Figure 1 - System model: Dual mass-spring-damper setup


In order to draw a free body diagram of the above figure we must consider the two masses
separately. On each mass, all forces acting on the mass must be depicted.
Mass 1:
Force
Gravitational Force
Spring Force 1
Damper Force 1
Inertial Force
Spring Force 2
Damper Force 2

Mathematical Expression
m1g
k1(yg(t) - y1)
b1 (yg(t)' - y1')
m1 y1"
k2(y1 - y2)
b2(y1' - y2')

Direction
Down
Up
Up
Down
Down
Down

Mathematical Expression
m2g

Direction
Down

Mass 2:
Force
Gravitational Force

Spring Force 2
Damper Force 2
Inertial Force

k2(y1 - y2)
b2(y1' - y2')
m2 y2"

Up
Up
Down

FREE BODY DIAGRAM


Drawing these forces onto a diagram for each of the two tables results in the following free
body diagrams.

Figure 2 - Free body diagram of mass 1

Figure 3 - Free body diagram of mass 2

GOVERNING EQUATION
According to D'Alembert's Law, the sum of all forces acting on a body including the inertia
force for each mass is equal to zero.
Using D'Alembert's Law:
Sum of all forces on m1:
0 = -m1y1" + b1(yg(t)' - y1') + k1(yg(t) - y1) - b2(y1' - y2') - k2(y1 - y2) - m1g
Sum of all forces on m2:
0 = -m2y2" + b2(y1' - y2') + k2(y1 - y2) - m2g

ELIMINATING THE GRAVITY FORCES


The coordinates used up to now assume the springs k1 and k2 are completely relaxed when
y1 and y2 are equal to zero. For all practical purposes the springs k 1 and k2 will never be in
their relaxed state due to the weight of m 1 and m2. For this reason it is convenient to analyze
the system relative to its equilibrium position to eliminate the terms in the equations of motion
that include gravity.
The procedure is to define new variables which are the difference between the actual
position of the system's components and their equilibrium positions. In this case we will
define z1 = y1 - y1eq and z2 = y2 - y2eq. After defining these new variables, the next step is to
determine y1eq and y2eq.
To do this we assume that the system is in equilibrium. So y1 = y1eq and y2 = y2eq, and there is
no motion. No motion implies that y1' = y2' = 0, y1" = y2" = 0, and yg(t) = 0. Substituting these
values into the governing equations yields:
m1g = -k1y1eq - k2(y1eq - y2eq)
m2g = k2(y1eq - y2eq)
Adding both equations together yields:
(m1+m2)g = -k1y1eq
Solving for y1eq yields:
y1eq = -(m1+m2)g / k1
Solving for y2eq yields:
y2eq = -m2g/k2 + y1eq
To reformulate the equations of motion according to z1, z2, and their derivatives, we note the
following.
y1 = z1 + y1eq
y2 = z2 + y2eq

y1' = z1'
y2' = z2'

y1" = z1"
y2" = z2"

Substituting the above relations into the first governing equation yields:
0 = -m1z1" + b1(yg(t)' - z1') + k1(yg(t) - z1 - y1eq) - b2(z1' - z2') - k2(z1 +y1eq - z2 + y2eq) - m1g
Regrouping to collect the terms involving y1eq and y2eq together:
0 = -m1z1" + b1(yg(t)' - z1') + k1(yg(t) - z1) - b2(z1' - z2') - k2(z1 - z2) - k1y1eq - k2(y1eq - y2eq) - m1g
Substituting the values of y2eq = -m2g/k2 + y1eq and y1eq = -(m1+m2)g / k1 eliminates the
constant terms on the right hand side of the equation. The first differential equation becomes
the following:
0 = -m1z1" + b1(yg(t)' - z1') + k1(yg(t) - z1) - b2(z1' - z2') - k2(z1 - z2)

Similarly, substituting into the second differential equation yields:


0 = - m2z2" + b2(z1' - z2') + k2(z1 + y1eq - z2 - y2eq) - m2g
After regrouping:
0 = - m2z2" + b2(z1' - z2') + k2(z1 - z2) + k2(y1eq - y2eq) - m2g
Since m2g = k2(y1eq - y2eq):
0 = - m2z2" + b2(z1' - z2') + k2(z1 - z2)

FREE BODY DIAGRAM


of an op-amp system
The free body diagram of the op-amp circuit is different from the previous examples in that
there is no "body" to diagram. The circuit is based on fundamental relationships between
voltage and current. These relationships are based on the topology (i.e. the connections) of
the circuit through Kirchoff's voltage law and Kirchoff's current law, and are derived from
element relations such as Ohm's Law.

Figure 1 - System schematic: Op-amp in a band pass filter


NODAL ANALYSIS
To analyze an op-amp configured in negative feedback, certain idealizations are useful. One
of these idealizations is that the potential at the e- input and the e+ input are the same. The
other idealization of the op-amp is that there is no current flow through either the e - or e+
inputs. These idealizations imply important properties of the circuit, namely that e - = e+ = 0
and that current flowing through the feed forward portion of the circuit containing R 1 and C1 is
equal to the current flowing through the feedback portion of the circuit containing R 2 and C2.
This simplifies the circuit analysis because the current flowing from the feed forward into the
node e- equals the current flowing out of the node into the feedback.

KIRCHOFF'S LAWS
Kirchoff's current law states that the sum of the current flowing into a node is equal to the
sum of the current flowing out. This implies that all elements in a series circuit share the
same current. So, based on Kirchoff's law, IR1=IC1=I1 as shown below.

Similarly, Kirchoff's voltage law implies that all elements across two nodes will share the
same voltage drop. Therefore, based on this law, VC2=VR2, as shown below.

COMPONENTS
Each element is shown below with its voltage-current relationship.

These current-voltage relationships will be used later in the analysis.

GOVERNING EQUATIONS
With the given parameters and relations of the circuit above, the governing set of coupled
first order differential equations can be derived. There will be two differential equations. One
will involve the derivative of ea and the other will involve the derivative of eo, since they are
the derivatives which appear in the element laws.
To begin, the derivative relations for ea and eo are as follows:

These derivative relations are incomplete, because the expressions do not relate the
derivatives ea and eo and the input ei. The objective is then to solve for I1, IC2, and IR2 in terms
of ei, ea, and eo.
First solve for I1 in terms of ei - ea and set it equal to the derivative relation for I1.

and
Using Kirchoff's Law, the following relation holds:

Use the previously derived expressions for IC2, IR2, and I1 to substitute into the current balance
equation.
The derivative eo requires an expression for IC2.

Solve for IC2 by substituting in the equation for IR2 in terms of eo and the previously derived
equation for I1.

Placing all inputs on the right side of the equations and making the derivatives positive
yields:

FREE BODY DIAGRAM


of an electromechanical system
This example considers a loudspeaker driver schematically represented as shown in Figure
1 to the right.
To model the system it is necessary to
separately consider the electrical and
mechanical portions of this speaker.
The electrical and mechanical portions
of the speaker are coupled by the
Lorentz force acting on the mass and
by the back emf acting on the circuit.
The modeling of the electrical portion
uses a circuit diagram containing the
voltage source, a resistor, an inductor,
and a velocity dependant voltage
source. The resistor is the resistance
of the coil, and the inductor is the
inductance of the coil. The velocity
dependant voltage source represents
the effect of the back emf.
Modeling of the mechanical portion
employs a traditional free body
diagram in which a spring force, a
damping force, an inertial force, and
the Lorentz force acts on the mass.
The Lorentz force is a current
dependant actuating force.

Figure 1 - System model: Loudspeaker driver system

ELECTRICAL GOVERNING EQUATION


The circuit diagram of the electrical portion of the speaker is shown below:

The arrow indicates the direction of positive current flow, and the the plusses and minuses
indicate the direction of voltage drop. Applying Kirchoff's current law around this loop leads
to the following equation:
ei - eR - eB - eL = 0
The voltage relations for each of the elements are shown in the following table:
Element
Voltage Source
Resistor
Back EMF
Inductor

Mathematical Expression
ei(t)
eR = Ri
eb = qx'
eL = Li'

Substituting these voltage relations yields the governing equation for the circuit.
ei(t) - Ri - qx' - Li' = 0

MECHANICAL GOVERNING EQUATION


There are four forces acting on the mass representing the voice coil and cone. They are
listed in the table below:
Force
Spring Force
Damper Force
Inertial Force
Lorentz Force

Mathematical Expression
kx
bx'
mx"
qi

Direction
Toward the Left
Toward the Left
Toward the Left
Toward the Right

Drawing these forces onto a diagram results in the following free body diagram.

D'Alembert's Law states that the sum of all forces acting on a body including the inertial force
is equal to zero.
-mx" - bx' - kx + qi = 0

FREE BODY DIAGRAM


of a thermal system
In this example problem the thermal dynamics of a refrigerator will be considered. Three free
body diagrams represent the interactions between the pot roast, the compartments and the
environment.
Heat transfers through the system both by conduction and convection. The heat transfers
through the walls of the refrigerator by conduction. Convection between the refrigerator and
the freezer section is achieved with bi-directional air flow through a simple vent between the
compartments. The air circulating in the freezer section increases flow between the two
compartments just as a breeze increases a flow though an open window.
Before beginning with the free body diagram, there are several important variables and
equations to note:
Variables

Equations

Heat Transfer q

Heat transfer through a surface:


q = 1/R * (t1-t2)

Thermal Resistance R
Flow Rate
Density
Specific Heat
Temperature

Hear transfer by flow:


q = ***t
Capacitance:
C = * * V
Rate of temperature change:
' = (1/C) * q

Thermal Capacitance C
Rate of Temperature Change '

The notation to differentiate the many variables is as follows below. These subscripts allow a
convention to be used to identify the variables. For example, Qr2f_v indicates the variable
representing heat transfer from the refrigerator section "r" to the freezer section "f" by way of
convective heat transfer "v". Similarly, Rc2f is the thermal resistance for heat transfer from the
coil "c" to the freezer section "f". Finally, the capacitance, Cr, simply represents the
capacitance of the refrigerator section.
r = refrigerator section
f = freezer section
p = pot roast
c = coil

e = environment
2 = going to
d = by conduction
v = by convection

FREE BODY DIAGRAM


Freezer
Consider the following heat exchanges for the freezer compartment. The table below shows
the designation of each of the heat exchanges for the freezer section and the corresponding
mathematical expression depending on the type of heat transfer.
Heat Exchange

Designation

Mathematical Expression

Direction

Cooling coil

Qf2c_d

1/Rc2f * (c-f)

Out

Refrigerator to freezer air flow

Qr2f_v

***r

Out

Heat from environment

Qe2f_d

1/Re2f * (e-f)

In

Heat from refrigerator

Qr2f_d

1/Rr2f * (r-f)

In

Freezer to refrigerator air flow

Qf2r_v

***f

In

Drawing the heat exchanges from the chart above yields:

Summing all of the heat exchanges on the body together in a mathematical equation yields
the following thermal equation:

Refrigerator
Once again, the heat exchanges are described below with the corresponding mathematical
expressions.
Heat Exchange

Designation

Mathematical Expression

Direction

Heat to the freezer

Qr2f_d

1/Rr2f (r-f)

Out

Freezer to refrigerator air flow

Qf2r_v

***f

Out

Heat from environment

Qe2r_d

1/Re2r (e-r)

In

Heat from the Pot Roast

Qp2r_d

1/Rp2r (p-r)

In

Refrigerator to freezer air flow

Qr2f_v

***r

In

Drawing the heat exchanges from the chart above yields:

Again, the sum of the heat exchanges gives the following thermal equation:

Pot Roast
The pot roast heat exchange is simply described below with the corresponding expression.
Heat Exchange

Designation

Mathematical Expression

Direction

Heat to the freezer

Qp2r_d

1/Rp2r (p-r)

Out

Drawing the heat exchange:

The corresponding thermal equation is:

GOVERNING EQUATIONS
The differential equations for the heat exchanges of the freezer section, refrigerator section
and the pot roast are repeated below.

The heat transfer must be expressed in terms of the states and other parameters. The
substitution results in the following equations below.

FREE BODY DIAGRAM


of a hydraulic system
In this example we will consider the power steering system of a car. This system consists of
a mechanical connection between the rack and steering column, as well as a hydraulic
connection due to the power assist system.

Figure 1 - System setup: Power steering system

STEERING COLUMN
Figure 2 schematically depicts steering column setup. The steering column consists of the
following two portions, the steering column above the torsion valve and the steering column
below the torsion valve. Both of these rotation elements can be assumed to be rigid and
have negligible inertia. The model for the torsion valve is a torsional spring with spring
constant k.

Figure 2 - System setup: Steering column

The driver determines the rotational position of the upper steering column by moving the
steering wheel. Figure 3 shows the free body diagram of upper steering column. The driver
applies the force Fapplied. However the real input to the steering column is the rotational
position of the steering wheel which defines the rotational position of the upper steering
column, 1. The torsion valve applies a torque to the upper steering column which depends
on the difference between the rotational position of the upper steering column 1 and the
lower steering column 2.

Figure 3 - Free body diagram: Upper steering column

Figure 4 shows the free body diagram of the lower steering column. There are two torques
acting on the lower steering column. The first torque is the torque from the torsional spring of
the torsion valve. It is equal and opposite to the torque applied to the upper steering
column. The second torque is the result of the contact force of the rack acting on the pinion
at radius r.

Figure 4 - Free body diagram: Lower steering column

RACK
The rack has the following three external forces acting on it. The first force is the contact
force of the pinion acting on the rack. It is equal and opposite to the contact force of the rack
acting on the pinion. The second force is the friction force due to losses in the system. The
third force is the force from the piston. The piston force is the product of the area of the
piston and the pressure of the fluid inside the cylinder which is a function of the difference
between = 1 - 2.

Figure 5 - Free body diagram: Rack

FLUID SYSTEM
The fluid system consists of a hydraulic piston attached to the rack, and a rotary valve. The
amount of pressure and the direction of the pressure on the piston is determined by the value
of the assist curve given the position of the rotary valve.
Figure 6 shows the nonlinear response of the torsion valve. The following plot shows the
pressure on the piston for a given = 1 - 2 which is the difference between the steering
column angle and the pinion angle. For differences between -0.004 and 0.004 radians, there
is no power assist; that is, there is a dead zone in the power assist curve. For differences on
the order of 0.004 to 0.035 the response is approximately linear with increasing angle. For
differences in angle greater than 0.035 the power assist increases rapidly until the the
maximum pump output of 107 Pa.

Figure 6 - Plot: Nonlinear response of the torsion valve


The pressure exerted on the piston is equal to:
P() = sign() x min(4.41 x 107 v + 4.59 x 1018 v9, 107)
where v = max(0, || - d)
The max function implements the dead zone, while the min function implements the
saturation.

RELATIONS
From the free body diagram of the rack, Figure 5, the governing equation of the rack is
Fcontact + P()A - bv - ma = 0
where P() is the output of the response curve function defined above in the fluid system
section.
To eliminate Fcontact from the equation, use the free body diagram of the lower steering column
in Figure 4. The governing equation of the lower steering column is
k - Fcontactr = 0
Solving for Fcontact
Fcontact = k/r

Substituting the expression for Fcontact into the equation yields the equation of motion
k/r + P()A - bv - ma = 0

Вам также может понравиться