SimuLink Tutorial 1

8
Modelling and simulation of engineering systems Simulink Exercise 1 - translational mechanical systems Dr. M. Turner ([email protected]) 1 Overview of Simulink Simulink is a package which runs in the MATLAB environment. It is used for simulating the behaviour of dynamic systems and today forms a vital part of the design and validation processes in many, often diverse, industries. Simulink allows one to construct block-diagrams of systems using standard blocks such as integrators, differentiators, gains, summing elements and such like. These blocks can be used to build up complex systems which can then be simulated using Simulink’s differential equation solver. This first exercise will introduce you to Simulink and useful concepts such as state-space modelling, which can simplify the use of Simulink and enhance its power. 2 State-space equations Often when the equations of motion are derived for a system they appear as high-order differential equations. For example, modelling two masses connected by a spring yields a fouth order differential equation which may be expressed in the form: (1) where is independent of and often represents either constants, externally acting forces, or inputs of some sort. However, if we define the variables: (2) (3) (4) (5) it then follows that we can write equation (1) as (6) or (7)

description

matlab

Transcript of SimuLink Tutorial 1

Page 1: SimuLink Tutorial 1

1

Modelling and simulation of engineering systemsSimulink Exercise 1 - translational mechanical systems

Dr. M. Turner ([email protected])

1 Overview of Simulink

Simulink is a package which runs in the MATLAB environment. It is used for simulating the behaviourof dynamic systems and today forms a vital part of the design and validation processes in many, oftendiverse, industries.

Simulink allows one to construct block-diagrams of systems using standard blocks such as integrators,differentiators, gains, summing elements and such like. These blocks can be used to build up complexsystems which can then be simulated using Simulink’s differential equation solver.

This first exercise will introduce you to Simulink and useful concepts such as state-space modelling,which can simplify the use of Simulink and enhance its power.

2 State-space equations

Often when the equations of motion are derived for a system they appear as high-order differentialequations. For example, modelling two masses connected by a spring yields a fouth order differentialequation which may be expressed in the form:

������������ �������������������������������������! (1)

where � is independent of � and often represents either constants, externally acting forces, or inputs ofsome sort.

However, if we define the variables:

��"$#�� � (2)��% #�� �� (3)��& #�� �� (4)�(' #�� � �������) (5)

it then follows that we can write equation (1) as

�*��('+�� ��('+���,��&-���.��%-������"/�����0 (6)

or

��('1�3254�6 ��('7���8��&7������%-������"/���:9 (7)

Page 2: SimuLink Tutorial 1

2

As it is this is not useful, but then recall that, from the definitions of � " � ��% , etc we have that � %*� ���" ,��& �$���% and ��' � ���& , so we can write the above relationships, together with equation (7) as

���" #�� ��% (8)���% #�� ��& (9)���& #�� �(' (10)��(' #�� 2 4�

6 ��('7���8��&7���.��%7������" ��� 9 (11)

In other words we have transformed our fourth order ODE into four first order ODE’s. Defining thevector � # � 6 ��"7��% ��& �('89�� (where

���� � denotes the transpose of a vector) we could write this as

�� �� � � � � � (12)

which is a much more compact form than equation (1).

In fact this situation holds quite generally, and assuming that the high-order differential equationssatisfy certain properties, we can always write an � 2� �� order differential equation as � first-orderdifferential equations of the form:

�� �� � � � � � (13)

where the state vector, x has � rows. Each of the variables � " , ��% etc is said to be a state variable ofthe system. It is not too difficult to see that this form can greatly simplify complex, high-order ODE’s.This form is also very convenient for computation and, in fact, is the type of format in which much ofthe computation associated with simulation is carried out.

3 State-space realisations of linear systems

If the system which we are considering is linear, we can simplify its description even further. Thestandard procedure is to define a set of inputs, u, and a set of outputs, y and then write the system inthe following form:

�� � ��� ����� (14)� � ��� ����� (15)

where the matrices A,B,C,D are of compatible dimensions with the vectors. The first equation, (14) isoften called the state equation. In terms of our previous example, the state equation could be easilywritten in this form with the matrices A,B given by

� �

������ 4 4 42 �! 2#"! 2%$! 2'&!

(*))))+ � �

������ 2 "!

(*))))+ (16)

Page 3: SimuLink Tutorial 1

3

and the input � being taken as � (which is only a scalar in this case).

Now, imagine we would like the output to be the state variable � " , then the output equation (equation(15) would be written as

� � ��" (17)

This would mean the matrices C,D would be given by

�3� 64 � 9 � � � (18)

The matrices A,B,C,D are often called the state-space matrices of the system.

Summary - An � -th order differential equation can be re-written as a set of � first order differentialequations. Given an input vector and an output vector, one can then write a description of a system interms of one vector first order differential equation and one algebraic vector equation. These equationsare parametrised by four matrices.

4 An example system

Let us consider a very simple system to begin with. Consider the mass-spring damper system depictedbelow. Let us take the force acting on the first mass as the input to the system and the velocity of thesecond mass as the output.

� �

���

� ��

� �

��� ���

It is easily verified that the equations of motion governing the system are

� ".���"/��� ���"/��� ��" � � ���%-��� ��%-��� ! � � (19)� % ���%-��� ���%-��� ��% � � ���"/��� ��" (20)

Defining ���" ��#���" and ���% ��#�� % we can write these coupled second order differential equations as a setof four first order differential equations, viz:

���" � 2 �� " ��"-2�� " ��"/�

�� " � %7��� " ��%-� 4� " � ! � � (21)

���" � � " (22)

�� % � 2 �� % � % 2�� % ��%-�

�� % ��" ��� % ��" (23)

���% � � % (24)

We shall now consider two different ways of describing such a system using Simulink.

Page 4: SimuLink Tutorial 1

4

4.1 Realisation using basic blocks

Such a system as above can be realised in SIMULINK using very basic elements: integrators, gainsand summing elements, together with the user’s favourite input and output elements.

The first step in this realisation process is to notice that an integrator block can be considered assomething which ‘integrates the derivative of a signal to recover the signal’. It is perhaps easier tothink of this pictorially as shown below. Here the input to the integrator is �� and thus the output willbe � .

� ���

�� �

Now, in state-space systems we have expressions for �� � ’s in terms of ������ . So for example, if we hadthat �� � ���� �� , where is another variable and � � are constants, we could “write” this in Simulinkas

�� � �

���

� �� ��

Note that the output of a summing block is equal to the sum of its inputs. The blocks containing � and are gain blocks.

Using only these elements we can thus draw the Simulink version of the example system as shown onthe last page of this worksheet

It can be easily seen that although the elements used to construct the Simulink block daigram aresimple, the block diagram itself is not. This is only a realisation of a very simple system. Imagine thecomplexity of a complete car suspension system.....

4.2 Realisation using state-space blocks

In Simulink there is also a state-space block which takes an input signal and produces an output. Touse this, one must simply specify the state-space matrices of the system.

So, turning to our example, we can work out the state-space matrices of the system as

� �

������2����� 2����� ���� ����

4 ���� ���� 2 ���� 2 ���� 4

(*))))+ � � �

������

"� �

(*))))+ (25)

Page 5: SimuLink Tutorial 1

5

� � 6 4 9 � � 6 9 (26)

where we have selected the velocity of the second mass as the output.

These matrices can simply be entered into the Matlab workspace and used in the Simulink block tosimulate the system.

To enter a variable, say � , into Matlab workspace, at the command prompt type:

>>B=1

To enter matrix variables, say A, into the workspace, type:

A=[-B/M1 -K/M1 B/M1 K/M1 ; 1 0 0 0 ; B/M2 K/M2 -B/M2 -K/M2; 0 0 1 0]

The elements of the rows are separated by a space. The end of a row (and the start of the next) isindicated by using a semi-colon.

A nice property of the system in this form is that it’s stability can be analysed by looking at the eigen-values of the � -matrix. Typically these eigenvalues will be complex - an imaginary part indicating thatthe system is under-damped and hence will contain oscillatory modes. For (asymptotic) stability it isa requirement that all of the eigenvalues have negative real part.

In Matlab you can view the eigenvalues of a matrix, A, by typing

>> eig(A)

4.3 A comparison

By now, the strengths of the state-space approach are probably becoming clear: they allow a generaland convenient way of simulating dynamic systems in Simulink. The ‘individual blocks’ approachseems complex, even for simple systems, and may be error prone. In addition the structure of theseconnections will vary from system to system, whereas with a state-space block there is no change inconnections, only in the form of the state-space matrices.

This is not the whole story however. The simple example we have looked at is a linear system andhence the differential equations describing it are linear. This is not true of every system and complexsystems are frequently nonlinear in some way. So while, parts of the system (which are linear or almostlinear) may be realised through Simulink state-space blocks, other parts of the system will have to bedescribed using individual elements.

5 Working with Simulink

To invoke Simulink, simply type

>> simulink

at the Matlab command prompt. This will bring up the Simulink library window. The library containsmost common blocks which are needed in simulation, grouped under several categories (Explore theseto familiarise yourself). Once this window has been opened, under the File tab there are options toload/save block diagrams or to create a new diagram.

Page 6: SimuLink Tutorial 1

6

To start a new diagram under File, click New. This should bring up a blank Simulink window. Tocreate a block diagram simply drag blocks over from the library to the new window. The blocks canalso be connected with the mouse. Double clicking on a block in the new window will display someof the block’s properties. This is used, amongst other things for entering parameters. Note that theseparameters must correspond to variables which exist in the Matlab workspace, or are simply numbersor matrices of numbers.

Normally one wants to apply inputs to the system and observe the outputs. There are dedicated inputblocks, such as the step block, which will apply a step function to the system and the sine block whichwill apply a sine wave to the system. These behave much the same way as any other block and requirecertain parameters, such as the magnitude of the sine wave for example, to be set.

There are several different ways to observe the outputs of a system, but they are all enabled throughvarious different blocks. For example, connecting a scope block to a certain variable shows the time-evolution of this variable as the system simulates it. The block To workspace saves the variable to theworkspace after simulation. It can then be plotted using.

>> plot(tout,x)

assuming the variable’s name is � (you can change the variables name by double-clicking on the block).On later versions of Matlab, make sure that the variable � is saved as a matrix or array type - doubleclick on the To workspace block to make sure this is the case. You should also check that the variabletout in Simulation/Workspace I/O is set to a matrix or array type.

Before starting a simulation, there are several parameters which need to be set.To set these parameters,click on the Simulation tab and select Parameters. A small window will be displayed containing aselection of system parameters. The most important of these are

� Stop-time - the point at which the simulation will stop.

� Solver options - determines the ODE solver to find the solutions to the underlying differentialequations which describe the system. For now, leave this as it is and only change if you experi-ence problems.

To run the simulation make follow the following steps:

1. Make sure all required variables have been entered into the Matlab workspace.

2. Ensure all connections in the Simulink diagram are correctly made and have compatible dimen-sions.

3. Ensure the time and solver properties are properly set.

4. Click Simulation and select Start

Page 7: SimuLink Tutorial 1

7

6 Exercise

Construct block Simulink models of the example system discussed earlier using both a connectionof individual elements and just one state-space block. Test the response of the system using variousdifferent inputs: steps, impulses, etc. Verify that the response of the systems using the two differentrealisation approaches is the same.

For your tests use the following parameter values:� " ��� � � % ��� � � � 4 � � � 4Vary � to between � � and � and compare the responses.

Note you can save variables to a .mat file by using the following command:

>> save filename variable1 variable2, .....

Alternatively you can save all the variables in the workspace by simply typing:

>> save filename

A .mat file can be loaded to the workspace by typing:

>> load filename

Any current workspace variables of the same name as those in filename.mat will be over-written.

To display more than one graph at a time use

hold

to toggle the figure hold function on and off.

What do you notice about the system’s response as you vary � ? Do you notice anything surprising?What does this suggest about the system’s transfer function? Use the function

>> [num,den]=ss2tf(A,B,C,D)

to verify this.

What can one say about the stability of this system?

Instead of the output � % , now take the output as � % . What can we now say about the stability of thesystem? Is this consistent with the physical interpretation. How can this be for a passive system?

Page 8: SimuLink Tutorial 1

8

Fa(t)

v1_dot v1 x1_dot x1

v2_dot v2 x2_dot x2

s

1

Integrator3s

1

Integrator2

s

1

Integrator1s

1

Integrator

B/M2

Gain7K/M2

Gain6

K/M2

Gain5

B/M2

Gain4

B/M1Gain3

K/M1Gain2 K/M1

Gain1

B/M1

Gain

Figu

re1:

Sim

ulin

kdi

agra

mof

mas

s-sp

ring

syst

emus

ing

basi

cel

emen

ts