Modelling and simulation of engineering systems Introduction to

advertisement
Modelling and simulation of engineering systems
Introduction to Matlab and Simulink
Dr. M. Turner (mct6@sun.engg.le.ac.uk)
1
Motivation
For complex, high order systems, it is difficult to determine the behaviour of the system analytically the differential equations are just to complicated to solve by hand. Thus it is common for engineers to
use computers to solve these differential equations numerically.
Matlab and Simulink are tools which can be used to solve a wide variety of differential equations
numerically. Matlab is the computational package and Simulink, simply put, is the ‘front end’ which
allows simple construction of various mathematical models of varying complexity.
2
Installation
To install Matlab, in your CFS account, navigate to Installable Software and scroll down until you find
Matlab. Installation is straightforward and when this process is complete you should have a Matlab
icon on your desktop or in your list of programs.
3
Quick tour
To start Matlab simply click on the Matlab icon or select it from your list of programs. Matlab should
start and a large Window should open on your desktop. Typically this window is divided into three
panes (this will vary from version to version and will depend on your Matlab startup file):
1. The largest pane is normally that of the command line. This controls most of the user interaction
with Matlab. You can instruct Matlab to do something by entering a command and pressing
return (in much the same way as a UNIX or MS-DOS command line).
Type
>> ver
followed by the return key. Matlab should display information about its version number and the
number of toolboxes installed.
2. Normally the top-left Window shows the Matlab workspace. Essentially this is the collection of
variables (matrix, vectors, strings etc.) which Matlab has in its memory and you can access.
Type
>> A=1
followed by return in the command line pane. The (1x1) variable A should now appear in your
workspace pane.
3. The third pane (normally the bottom-left pane) shows your current directory. This shows what
files appear in this directory and allows you to navigate through your computer’s file structure.
The current directory is where Matlab attempts to load and save files to if no other specific
information is given. It will also search this directory for any Matlab script and function files
(covered later in course).
In fact the two left most windows are superfluous to Matlab’s operation. You can navigate throught
your computer’s file structure using standard UNIX and DOS commands such as cd, ls, dir etc.
Also by typing
>>who
or
>> whos
you can display information about your workspace in the command window. Try these commands and
also
>> what
4
Simulink overview
Invoke Simulink by typing
>> simulink
into the command line. A new window should appear with various blocks inside it. The new window
is the main Simulink window; the blocks within are various libraries.
If you double-click on a library block a further new window should appear, displaying the contents of
the library. Explore various libraries and their contents. The contents of these libraries are ‘blocks’
which can be combined to construct mathematical models of systems. The main libraries we shall use
are
1. Continuous. This library mainly has blocks which represent the mathematical behaviour of
continuous time linear systems e.g. pole-zero, state-space, transfer function blocks as well as
certain other elements.
2. Math [sic] functions. This library contains elementary and common mathematical operations
such as addition, multiplication and trigonometric functions.
3. Signal routing. Simulink is based on the flow of signals constructed in block diagrams. Elements
of this library can be used to manipulate signals within Simulink. For instance you can multiplex
signals - combine several scalar signals to form a vector signal (or several vector signals to form
a larger vector signal) - de-multiplex signals and such like.
4. Sources/Sinks. Elements in this library describe the types of signals which can be input to
models and dictate how the output is to be stored. The output can be saved in the workspace,
written to a file, or displayed on a scope.
5
New models
To begin a new model, select New model from the File tab in the Simulink window. A new, blank
window should be displayed. Blocks can be dragged and dropped from libraries into this new window
and blocks can be connected by clicking your mouse on a block output and dragging the cursor to
another block’s input. This should be quite easy.
This new window should have several tabs at the top. Normal file loading, savingand other operations
are catered for in the File tab. One of the important tabs is the simulation tabs. This controls how
Simulink runs the simulation of the mathematical model (not surprisingly). If you select simulation
parameters from this window, yet another window should appear. The solver tab of this window
is perhaps the most important. This tab dictates how long (in simulation time, not real time) the
simulation is run for and the type of numerical routine used to solve the differential equations. For
now, leave this tab alone.
6
Simulation of a second order system
A second order linear system (with no zeros) can be described, using Lalpace Transforms, by the
following transfer function:
G(s) =
ωn2
s2 + 2ζωn s + ωn2
(1)
where wn is the undamped natural frequency of the system and ζ is the damping ratio.
In Matlab this can be simulated using the transfer function block in the continuous library. Start a new
model, as described above, and drag this block into your new window. To enter the parameters of the
transfer function you need to double-click on the block.
For the numerator enter [0 0 wn*wn]
For the denominator enter [1 2*z*wn wn*wn ]
i.e. the elements of these vectors represent the powers of s in the transfer function, in descending order
from left to right. Note that as matlab is essentially a matrix/vector manipulation tool, most variables
are entered in vector form (as above) or matrix form. It is also possible to enter data as scalars, strings
and various other data structures.
To allow Simulink to simulate such a block you must define numerical value of the parameters. In the
command line enter
>> wn=2 ; z=1;
Connect a step block and a scope block to the input and output of this transfer function respectively
(these can be found in the sources and sinks libraries). Use the simulation tab in your window to
simulate the system for ten seconds. Double click on the scope to observe response.
Tasks
• Experiment with changing the damping ratio. Initially use positive values, but also try a negative
one later. What is the correspondance between the damping ratio and the system’s behaviour?
How is this related to the system’s poles? Use the Matlab function roots to find the poles of
your denominator.
• Experiment with changing the natural frequency of the system. Use a positive damping ratio less
than one for these experiments and only use positive natural frequencies. What is the effect of
natural frequency on the system’s response and how is this related to the poles of the system?
• An alternative way of representing a transfer function in simulink is through the pole-zero block.
Using the initial values for wn and z and the pole zero block, convince yourself that the pole-zero
and transfer function blocks are equivalent. Again, double click on the pole-zero block to enter
the appropriate parameters.
• Using the File menu on your diagram, save your simulink diagram. Be careful to save it in a
directory where you have permission to write (e.g. your Z drive on CFS).
• If you have time, click on the Demo libraries block in the main Simulink window and run some
of the Simulink demonstrations.
Other useful commands
Note all of these commands may be invoked with or without left-hand arguments.
v = [1 2 3]
V = [1 2; 3 4]
w = v’
L = eig(A)
conv([1 2],[1 3])
a = A(1,1)
x = sqrt(y)
x = norm(X)
X = abs(Y)
Define vector [1
2 3]
#
1 2
Define matrix
3 4
Define a new vector/matrix to be transpose of existing vector/matrix
Define L to contain eigenvalues of (square!) matrix A.
Convolve two vectors (gives s 2 + 5s + 6)
Defines a as (1,1) element of matrix/vector A
Defines x to be square root of y
Defines x to be ‘magnitude’
of vector/matrix X.
q
2
For vector, x = |x| = x1 + x22 + . . .
For matrix, x = |Xx|
x
Defines X to be a vector/matrix whose elements are the magnitudes
of the elements of the vector/matrix Y
"
Using simple vectors and matrices, make sure you are able to use all of these commands.
Download