Professional Documents
Culture Documents
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,
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.
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.
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')
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.
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.
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:
Direction
Mathematical Expression
Applied Force
f(t) (positive)
Spring Force
kx (negative)
Friction Force
bx' (negative)
Inertial Force
mx" (negative)
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]
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:
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.
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.
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 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
0.5000
D=
0
num =
0
0.5000
den =
1.0000
2.5000
1.5000
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.
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
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.
= -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
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.
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.
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.
=
39
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.
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.
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.
Natural Frequency
The natural frequency of a second order system is the frequency when the phase of the
response is -90 degrees.
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.
= 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.
(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.
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.
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
= 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.
Open the modeling window with New then Model from the File menu on the Simulink Library
Browser as shown above.
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,
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
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.
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.
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:
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.
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.
EXAMPLE
Model the second order tutorial example system using Simulink.
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.
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.
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.
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.
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
>> 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.
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.
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.
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
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.
Mathematical Expression
Direction
Applied Force
f(t) (positive)
Friction Force
b v (negative)
Inertial Force
m v' (negative)
Drawing the vectors in the table above yields the following free body diagram.
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
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.
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:
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
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
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)
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.
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:
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
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
Equations
Heat Transfer q
Thermal Resistance R
Flow Rate
Density
Specific Heat
Temperature
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
Designation
Mathematical Expression
Direction
Cooling coil
Qf2c_d
1/Rc2f * (c-f)
Out
Qr2f_v
***r
Out
Qe2f_d
1/Re2f * (e-f)
In
Qr2f_d
1/Rr2f * (r-f)
In
Qf2r_v
***f
In
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
Qr2f_d
1/Rr2f (r-f)
Out
Qf2r_v
***f
Out
Qe2r_d
1/Re2r (e-r)
In
Qp2r_d
1/Rp2r (p-r)
In
Qr2f_v
***r
In
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
Qp2r_d
1/Rp2r (p-r)
Out
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.
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.
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 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.
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.
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.
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