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

School of Engineering, Discipline of Mechanics and Mechatronics, pp.

19

Introduction to Simulink
Laboratory Guide - MCHA2000

The University of Newcastle, AUSTRALIA

MCHA2000 - Mechatronics Systems

School of Engineering, Discipline of Mechanics and Mechatronics

Objectives
The objective of this Lab is to introduce Simulink, which is an graphical environment for numerical
simulation of ODEs based on block diagrams. As an exercise, we will build a simulator for a massspring-damper system.

Laboratory Guide - MCHA2000

Contents
1 Introduction

2 Building Simulink Models (Task 1).

3 Running Simulink from Matlab (Task 2)

4 Mass-spring-damper system (Task 3).

School of Engineering, Discipline of Mechanics and Mechatronics

1 Introduction
Simulink is a Matlab toolbox for simulation of dynamic systems. This toolbox provides a graphical
interface for representing models using block diagrams.
In this Lab, we introduce the basic features of Matlab-Simulink. For further reference, students should
R
, available onrefer to help documentation Simulink - Getting Started with SIMULINK, Mathworks
line at http://www.mathworks.com.au/help/simulink/getting-started-with-simulink.html.
To open the Simulink library browser, click on the button
available in top part of the Matlab
toolbar. This action will open the window shown in Figure 1. To open a window where we can build
a model, select File > New > Model.

Figure 1: Simulink-library browser.

Simulink Libraries
Simulink contains a wide range of elementary components or blocks to build models. These components
are grouped into libraries according to their characteristics. In this Lab, we will focus on the components of the Continuous, Math Operations, Sinks and Sources libraries (see Figure 1). The help documentation of the Simulink blocks is available in Help > Product Help > Simulink > Block Reference.

Laboratory Guide - MCHA2000


The most common blocks of these libraries are
Continuous:Integrator, State-Space, Transfer Fcn, Derivative.
Math Operations: Sum, Gain, Product(Divide), Trigonometric Functions.
Sources: Step, Sine Wave, Band-Limited White Noise, Clock.
Sinks: Scope, To Workspace. The components in this library are mainly used to display simulation results or to store variables and export them from Simulink to the Matlab workspace.

2 Building Simulink Models (Task 1).


Consider the first-order differential equation
x + x = u

(1)

where x is the state, u is the input and is a positive constant.


We would like to simulate the response of (1) to an input step u. We will build the Simulink block
diagram shown in Figure 2, which is a block diagram representation of (1).

Figure 2: Simulink block diagram of a first order ODE.

Follow the procedures below to build the Simulink model shown in Figure 2.
1. Open a new model window (File > New > Model) and save it as mymodel.mdl.
2. Add the necessary blocks from the libraries to the mymodel window. This can be done by dragging
the blocks from the library browser and dropping them into the model window. Alternatively,
right-click on the block in the library and select Add to. We will need the following blocks:
Integrator, Gain, Sum, Step, Scope and To Workspace.
3. Interconnect the blocks in the model by drawing lines between the outputs and inputs. This can
be done as follows, (i) position the mouse pointer over an output, the pointer will change to a
cross shape, (ii) drag a line from the output to an input.

School of Engineering, Discipline of Mechanics and Mechatronics


4. Select the parameters of each block. Open the blocks by double clicking and set the following
parameters:
Step block: Step time =1, Initial value = 0, Final value = 10.
Gain block: Gain = 0.5.
To workspace block: Variable name = x. Save format: Array.
Integrator block: Initial condition = 0.
5. Select the time period for simulation. The parameters associated with the simulation time can
be set in Simulation > Configuration Parameters. In this window, assign Start time = 0
and Stop time = 20 as shown in Figure 3. There are different numerical solver options, which
will discussed in future labs.
6. Select Data Import/Export in the menu of the window shown in Figure 3. Choose a name for the
variable Time, for example mytime. As a result, Simulink will write the values of the simulation
time into the variable mytime. This variable will be available in the Matlab workspace after the
simulation. Deselect the check-box for Limit data points to last and select Format: Array.
7. Save the model.
8. Run the simulation from Simulation > Start.
9. Visualise the simulation results. Open the scope block to display the associated variable. Plot the
variables saved in Matlab workspace. For this example, the plot command is plot(mytime,x).
Exercise. Simulate and plot the response of the system with a initial condition x0 = 5 and input
u = 0.

3 Running Simulink from Matlab (Task 2)


In this section, we will assign the parameters of the Simulink model and run simulations directly from
Matlab.

Parameterising Simulink blocks from Matlab


We can use variables to assign parameters of the Simulink blocks instead of using numbers. The
variables can be assigned in the Matlab workspace. For example, the parameter of the gain block
from the previous section, can be replaced with the variable tau. The value of this variable can be
assigned in Matlab as per usual >> tau = 0.5.

Running simulations from Matlab


The Matlab command sim runs a simulation using the model specified in the instruction. For example:
sim(mymodel)
Further details of the command sim can be found in the help documentation.

Laboratory Guide - MCHA2000

Figure 3: Window for simulation parameter configuration.

Exercise.
1. Replace the parameters of the previous model with the following variables:
Step block. Step time = stept, Initial value = stepi, Final value = stepf.
Integrator block. Initial condition = x0.
2. Create a script in a M-file with the following content:
tau=0.5;
x0=0;
stept=1;
stepi=0;
stepf=10;
sim(mymodel)
plot(mytime,x)
3. Save the script as mymodel_sim.m and run it.

School of Engineering, Discipline of Mechanics and Mechatronics

4 Mass-spring-damper system (Task 3).


The system shown in Figure 4 is composed of a mass located in the middle, a spring attached to
the right-hand side, and a damper on the left-hand side. This system is known as linear massspring-damper system (A video of the system can be found on http://youtu.be/bs-SByPNL6Y ). The
idealised physical model is shown in Figure 5. The dynamics of this system can be described by the
following differential equation
m
x + bx + kx = F (t)
(2)
where x is the position of the mass, x and x
are the velocity and acceleration of the mass respectively
and F is an external force. The mass, spring and damper are characterised by the coefficient m, k
and b respectively.

Figure 4: Mass-spring-damper system.

F(t)
b

k
m
x(t)

Figure 5: Idealized model of the mass-spring-damper system.

The mass-spring-damper system can be represented in the state-space form as follows


   b
   1 
k
z1
z1
m m
=
+ m F (t)
z2
z2
0
1
0
where z1 = x and z2 = x.

(3)

Laboratory Guide - MCHA2000


Exercise.
1. Build a Simulink block diagram of the mass-spring-damper system (3). Export the position,
velocity, external force and simulation time from Simulink to the Matlab workspace using
To Workspace blocks.
2. Write a script to simulate the model with the following specifications:
Define all parameters of the model and the simulation time within the script. The parameters are m = 1kg, k = 1N/m and b = 1N s/m. The initial conditions of the system are
zero. The external force is a step function of amplitude 1N acting on the system at t = 2s.
Plot the position, velocity, acceleration, external force, damper force and spring force.
3. Run the script with different values for the parameters and analyse the results.
4. Add to the script and to the Simulink model the possibility of changing the initial conditions
(velocity and position), and simulate a response to initial conditions and zero input force.

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