P04700 Simulink 2nd Order 2018
P04700 Simulink 2nd Order 2018
P04700 Simulink 2nd Order 2018
Introduction
In this session we are going to look at modelling second order systems using a number of different
techniques, but mainly using transfer function blocks and integrator blocks.
The transfer function block is found in the Continuous Library. The numerator and denominator
coefficients can be entered in the block parameters.
P04700 45
Note that if the constant term is zero, a 0 will need to be entered along with coefficients for higher
powers of s. The model can be completed by adding a step function input, value 10, and a scope to
view the results.
P04700 46
By changing the coefficients to m, b and k, different values can then be input to tune the model
through the MATLAB Command Window.
And different values of damping and stiffness can be tried to see the effect on the response.
P04700 47
As you can see this gives a much better settling response.
P04700 48
1.1 ODE Solvers
We can investigate some of the parameters associated with the solver used. The Simulink solver takes
time steps at discrete intervals to obtain simulation results.
We can observe the step-size by looking at the variables in the MATLAB workspace.
P04700 49
If we now click on Configuration Parameters, we can change the maximum step size to 0.1 s.
Remember that ODE45 is a variable step solver.
P04700 50
When we zoom in on the response it now looks like this.
ODE 45 computes numerical approximation using 4th order and 5th order approximations. The
difference between these is used to compute the integration error.
P04700 51
If the integration error satisfies the tolerance setting, the integration proceeds.
Otherwise the solver tries a smaller time step and repeats the computation.
The relative and absolute error tolerances define the upper limits of errors – relative error is a
percentage. Setting the error to auto is the same as 1 x 10-3.
P04700 52
To find out more out about the various solvers:
P04700 53
One of the concerns that we have when constructing the model using integrator blocks, it looks as
though this is a continuous loop and it isn’t clear to see how the model starts. If we look at the
Parameter dialogue box associated with an integrator block, we can see that it allows us to input initial
conditions.
There are a number of steps required to implement this model using integrator blocks.
P04700 54
The first is that we need to rearrange the differential equation so that the highest order derivative
term is the subject.
P04700 55
Right-click and drag to add another integrator, and then connect and label the 2 integrators.
We now use gain blocks and add blocks to construct the right hand side of the equation.
P04700 56
Use the Ctrl + I (the letter ‘i’) to flip the gain block.
The gain blocks will represent the mass, spring and damper coefficients and will be defined shortly.
A key step is that the output of the Add block is used as the source block for the highest order
derivative.
Another way of thinking of this, is that the output of the Add block is the right hand side of the
equation:
If we wanted to apply any external forces to our model eg sine or step, this should also be an input to
the Add block.
The initial conditions then need to be set by clicking on the relevant integrator block.
P04700 57
And setting the integrator for the higher order derivative to zero.
We now define the variables for the gain constants within the MATLAB Command Window and run
the model.
P04700 58
In summary:
P04700 59
3. Incorporating a Controller
It is also possible to incorporate a PI controller with our mass-spring damper transfer function, to tune
the response of the system.
P04700 60
Worked Example no 1 – Modelling a pendulum
mg
ml 2 mg l sin( ) 0
g sin( ) l.
g
and so:- sin( )
l
d 2 g
Or 2
sin( )
dt l
P04700 61
The SIMULINK model of this is as below:-
Differentiate Differentiate
1 1
sin -9.8/2
Sin(Theta) s s
Sin Int1 Int2 Scope
X -g/l
θ
Pendulum angle, theta
l
T 2 seconds
g
P04700 62
4. Outputting data from SIMULINK
It is possible to output the results from a SIMULINK model into vectors, for use in MATLAB. You might
want to do this because the graph plotting in MATLAB gives much better results than the simple
“Scope” used in SIMULINK. A good example of where we might do this is the Electric Formula Student
we began the model for last week.
Last week we had the car accelerating from a standing start in a straight line on level ground, and the
equation of motion is:-
F m.a
For F we just had the force on the wheels from the electric motor. However, there is also the rolling
resistance and the aero drag. Using the normal vehicle dynamics equations, this gives:-
Fte 12 A Cd v 2 rr m g m.a
Where Fte is the tractive effort force from the drive wheels, and all the other symbols are as before.
Last week we build Stage I of the model. For this refinement let us suppose the following values:-
Density of air ρ = 1.25 kg.m-3
Cd = 0.5
A = 0.6
m = 300 kg (Car and driver)
μrr = 0.040
To put this into SIMULINK, we construct a model as shown on the next page. This has a number of
extra features over the model made last week.
At the bottom are the extra blocks constructing the nett driving force – subtracting the aero force and
rolling resistance from the driving force provided by the wheels.
Over on the right are two blocks of the type “To Workspace” from the “Sinks” folder. Their set up is
explained on the pages after the diagram.
The Clock block is used to give Time values. This is in the “Sources” folder.
P04700 63
Electric FS car Model. Acceleration from standing start.
F/m = a
1
Motor torque in Nm
4 Axle torque/Nm
1/0.24 1/300 acceleration s speed in m/sec
3.6 Speed in kph
Cd A 0.5
Rho
Time
Clock
To Workspace1
P04700 64
To output data to MATLAB you use a “To Workspace” block, which you find in the Sinks
folder.
The “To Workspace” blocks must be edited carefully. As well as giving the name of the array
taking the data, you must also say that it is going to an array, as there are other more
complicated ways of dealing with this.
This will create an array called Speed, holding the speed values.
You will nearly always also want to have the time values, as simulations do not normally use
even time steps. The times are generated using the Clock block, which you find in the
Sources folder. This will normally be connected directly to a “To Workspace” block, which
also needs to be set up as above.
When you run the model on the previous page, and then go to MATLAB, you will see arrays
holding Speed and Time values. You can use these arrays to plot graphs of Speed against
Time which are much more professional looking than those got with the Scope block.
P04700 65
4.2 Simulating an acceleration event
While we are at it we will make another change to the model. We will simulate the
acceleration event at FS, which is a 75 yard dash. (75 yards = 68.7 m). We will remove the
“To Workspace” blocks, and then put a simple display after the clock.
Rather than run the simulation for the full 20 seconds, we will stop it when the car has
travelled 68.7m. This is done over on the right as follows:-
Integrate speed to get distance
Add a “Hit Crossing” block from the “Discontinuities” folder, and set it to 68.7. When
the distance reaches this value, the output goes to 1.
Add a “Stop Simulation” block from the “Sinks” folder.
The model now looks like that on the next page. When you run it, the Display shows the time
to travel the 75 yards.
Now change G to 4.2 by just going to the MATLAB command window and typing G=4.2.
What happens to the time? Try changing this to find the optimum gearing?
Modify the model so that you can change the mass just as easily.
P04700 66
motor speed in rpm axle angular speed
G 60 1/(pi*2*0.24)
Scope
G:1 gearing rev/sec
to rpm convert speed
1
to axle rev/sec, s STOP
diameter =0.25
convert Hit Stop Simulation
to distance Crossing,
in metres sense reaching
68.7 m
F/m = a
1
G 1/0.24 1/300 3.6
Motor torque in Nm Axle torque/Nm acceleration s speed in m/sec Speed in kph
118 u2
Rolling Add square
resistance
the
velocity
Aero Drag
0.5 0.6 1.25 0.5
Cd A 0.5
Rho
6.046
Clock
Display time
to travel 75
yards
P04700 67
5. Getting arrays of data into SIMULINK
We can also take the values from normal MATLAB data arrays into a SIMULINK model. A very good
example of this would be a driving cycle.
However, this must be done in the way SIMULINK expects, which involves using the *.mat type of file.
The *.mat files used in SIMULINK must be of a very specific format. This is:-
They must be an array with TWO rows.
The data in the top row must be the time values.
Let us try an example. Suppose we want to study a driving cycle like the European urban driving cycle. In
MATLAB we would load the data into an array:-
>>V=load(‘EuropeUDC.txt’);
Looking over in the top left window, we could see that the vector V has 201 values, corresponding to a
time of 0 to 200 seconds. We create the vector of time values:-
>>T=[0:1:200];
We now want to join these two together, but we see that the velocity array V is a column, whereas T is a
single row. So we must transpose the velocities:-
>>V=transpose(V);
SIMULINK wants an array which has two rows, the top one of which is time. So we join the two (T and V)
together:-
>> EUDC=[T;V];
This array is now saved as a *.mat file, and it has the right format – two rows, top row is time values.
This is a very useful technique and way of saving data in MATLAB. You go to the workspace, and click on
the variable name, then right click, and choose “save selection”. You then get another window, and you
give a suitable name, such as EUDC.mat. This *.mat file can be saved for later use, and it is a good way of
saving data.
P04700 68
The driving cycles can then be used as an input or data source in a SIMULINK model like the one below.
The data is “pulled in” using the From File block, which is in the Sources folder. (The set up is on the next
page.)
1
EUDC.mat 1/3.6
s
European Integrator Distance
Convert
urban travelled
cycle to m/sec
du/dt
Derivative Acceleration
Make sure the simulation is set to last the same length of time
as the driving cycle, 200 seconds in this case.
Also, set to fixed time step, and w ith a "continuous" solver,
such as Runge Kutta
P04700 69
To set-up the From File block you just double click on it, and write the file name of the *.mat file you
created, as shown below:-
Thought. This last SIMULINK example is a classic case of some work that could equally well be done in
ordinary MATLAB using commands like cumtrapz, to get the distance, and gradient to get the
acceleration. Which method is better?
P04700 70
6. Additional Examples
There are many examples of more complex models available on the Mathworks website.
Two interesting models to look at are the ‘Friction model with hard stops’ and the heating system of a
house.
P04700 71
P04700 72
This shows one way of modelling friction in Simulink.
The two integrators in the model calculate the velocity and displacement of the system. These are then
used in the Friction model subsystem to calculate the friction force.
The hard stops are modelled by limiting the output displacement of the right integrator (limits are set).
P04700 73
The resulting force is:
P04700 74
A sub-system represents the thermal characteristics of the house – the block labelled ‘House’.
P04700 75
And the house heater:
P04700 76
If we run the model these are the results:
The Foucault pendulum, or Foucault's pendulum, named after the French physicist Léon Foucault, is a
simple device conceived as an experiment to demonstrate the rotation of the Earth. The vertical plane
of the motion of the pendulum rotates as the earth rotates.
P04700 77
Note that this model uses a different solver (ODE23t, rather than ODE45 previously), and that it is
possible to change variables in the MATLAB workspace.
P04700 78
7. Modelling a full vehicle including gearbox and engine
We will now develop a full vehicle model, including engine, gearbox and powertrain, which can then be
connected to the cruise controller that you developed last week. This will be based on an Alfa Romeo
147 GTA. We are just going to model longitudinal speed with the car travelling in a straight line on flat
ground.
Open a new Simulink model and save it as Alfa147GTA.slx. There will be one input to the model,
the throttle (an output from the cruise controller), and one output – the vehicle speed, which is an input
to the cruise controller.
As with our earlier electric car example, we need to consider all the forces acting on the car and then
use Newton’s second law to find the acceleration. We can then integrate this to give us the speed of the
vehicle.
𝑡 𝐹(𝜏)
And 𝑣(𝑡) = ∫0 𝑚
𝑑𝜏
The mass of the Alfa 147 GTA is constant, and so can be declared in the workspace by typing:
Mass = 1360;
We can then add the relevant blocks into the root system in Simulink. We need to add thee Subsystem
blocks, one for each of the applied forces, from the Ports & Subsystems blockset. We also include one
Add block, one Divide block, both from the Math Operations blockset; one Integrator block from the
Continuous blockset; and one Constant block from the Sources blockset for the mass.
Because we will be working with the SI system inside the model, we need to include a Gain block (from
the Math operations blockset) to convert from km/h to m/s, as with our previous electric vehicle model.
The three subsystems need to be named Aerodynamic drag, Rolling resistance, and Engine Force.
By clicking on the Add block, we can edit the list of signs. The engine force will be positive, and should
be the last input, so the signs are --+.
The Constant value of the Constant block should be set to Mass, and the Gain of the Gain block
should be set to 3.6. A title (annotation) should be added as a comment.
P04700 79
The model should now look like this:
We now need to deal with the computation of each of these forces in turn. We will start with the
aerodynamic drag.
Aerodynamic drag becomes a significant force at higher vehicle speeds and is given by the drag
equation:
1
𝐹𝑑𝑟𝑎𝑔 = 𝐾𝑑𝑟𝑎𝑔 × 𝑣 2 (𝑡) = 𝜌𝐶𝐷 𝐴 × 𝑣 2 (𝑡)
2
Hence we can declare the drag coefficient in the MATLAB workspace using the following:
Because the Aero drag block needs vehicle speed as an input, we will use a Goto and From block from
the Signal Routing blockset. The Goto and From blocks help us to avoid drawing connections between
two or more subsystems, which helps us to keep views uncluttered. These are often used instead of
connecting input and output ports directly to subsystems.
If a view becomes too complex, and there are too many tags, the same background colours can be
assigned to matching Goto and From blocks. To do this right-click on the block and use the Format menu
option.
P04700 80
The root system should now look like this.
We now need to double click on the Aerodynamic drag subsystem. The drag equation can be
implemented with one Gain block, one Constant block (from Sources), and a Math function block (from
the Math Operations blockset).
We need to input Kdrag as the Gain parameter in the Gain block, and select the pow function in the
Math function block (double-click and set the Function drop-down list to pow). A second input appears,
which is the exponent to apply. This needs to be connected to the Constant block and the parameter to
be set to 2. We also need to rename and connect the input and output ports. The subsystem should look
like this.
P04700 81
7.2 Rolling resistance computation
The rolling resistance is proportional to vehicle speed, and at lower speeds it is the main form of
resistance.
𝐹𝑟𝑒𝑠 = 𝐾𝑟𝑒𝑠 × 𝑣
The values of resistance depend on tyre properties, but a simplified calculation can be made by
assuming that:
At roughly 30 km/h (100 m/s), the rolling resistance is equal to the drag force
The proportional factor is constant at other speeds
Hence at 30 km/h
But
𝐹𝑑𝑟𝑎𝑔 = 𝐾𝑑𝑟𝑎𝑔 × 302
If
𝐹𝑟𝑒𝑠 = 𝐹𝑑𝑟𝑎𝑔
𝐾𝑟𝑒𝑠 × 30 = 𝐾𝑑𝑟𝑎𝑔 × 302
𝐾𝑟𝑒𝑠 = 30 × 𝐾𝑑𝑟𝑎𝑔
Hence Kres is simply 30 times Kdrag and so we don’t even need to declare a new variable in the MATLAB
workspace. Inside the Rolling Resistance subsystem the Gain is set to 30*Kdrag.
Another speed From block needs to be connected to the input port of the subsystem in the root system.
P04700 82
7.3 Engine force – engine, wheels and transmission
Every engine has a characteristic torque-speed curve which shows the maximum torque available (at
maximum throttle) at selected engine speeds. This is measured at the crankshaft. The curve for the
Alpha 147 GTA is shown below:
In addition, between the engine and wheels, there is the gearbox and the differential. The gearbox
applies a variable gear ratio, KGear, depending on the gear selected, and the differential gives a fixed final
ratio, KFinal.
For this block overall, we are wanting to find the linear force that the engine provides to the vehicle,
FEngine, for a given vehicle speed. To calculate this, there are four stages, each of which will be
represented by a sub-system:
Gearbox and differential, for a given vehicle speed (in m/s), we need to determine the
transmission ratio set by the gearbox and differential;
Wheel to RPM, given the transmission ratio, we can calculate the corresponding engine RPM;
Engine, from the engine torque-speed curve, we can find the maximum torque available, for the
given RPM;
Torque to force, given the maximum engine torque, this can be converted to the engine force,
FEngine, that is then applied to the car.
For the overall subsystem there are two inputs, vehicle speed in m/s and throttle input, and the output
is Engine force. Building these blocks in the Engine Force subsystem gives:
P04700 83
7.4 Gearbox and differential
For this model we are going to implement the simplest gearbox possible, that is an automatic gearbox
which chooses the gear based on the current vehicle speed, and which doesn’t have a reverse gear.
Remember that the overall ratio is given by the ratio of the gearbox, and then the differential ratio
(which is fixed).
The following table gives the optimal speed ranges for each gear for the Alfa 147 GTA, along with the
gear ratio:
The gear ratio is the ratio between the engine crankshaft and the output drive shaft:
As an example, in first gear, the engine’s crankshaft is rotating 3.500 times faster than the output drive
shaft.
The differential on the Alfa 147 GTA then applies a gear ratio, KFinal, of 3.733. Hence, in first gear, the
final wheel axis rotates at a speed given by
ie 13 times faster than the engine RPM, whereas in the final gear it is only (0.818 x 3.733 = 3.056) ie 3
times faster.
P04700 84
The data in the table above needs to be entered into the workspace as follows:
To build the automatic gearbox ie the Gearbox and differential block, the input is vehicle speed in m/s,
which we first need to convert to km/h by using a Gain block, with the gain parameter set to 3.6.
We then need to use a very important programming construct, the if()… elseif()… else
construct. Some of you might have met this in the C programming language (or a similar conditional
statement). We can select this in Simulink in the Ports & Subsystems blockset, using the If block.
As we have 6 gears, we will need the 1st output port (IF expression) connected to gear 1, the 2nd to 5th
output ports (ELSEIF expressions) connected to gears 2 to 5, and the 6 th output port (ELSE expression)
connected to gear 6.
P04700 85
Given the values in the previous table, we can set the parameters:
The Number of inputs will be set to 1, and this is the vehicle speed (the corresponding label
inside the block is u1);
The If expression is u1 < 39 (the first gear);
The Elseif expressions will be u1 < 60, u1 < 86, u1 < 110, u1 < 130 (gears 2 to
5);
The Show else condition will be checked (gear 6).
For each output of the If block, there needs to be one If Action Subsystem block (found in the Ports &
Subsystems library blockset). These blocks need to contain the code which is to be executed when the
corresponding condition in the If block is verified (these blocks are called conditionally executed
subsystems). In this case these subsytems will output the gear numbers.
In the If Action Subsystem, we don’t need the input port. Instead we replace it with a Constant block
with the gear number instead.
The next two screenshots show the If Action Subsystem, and the Gearbox and differential subsystems
for all the gears. We need to run the Simulation | Update Diagram action (the keyboard shortcut is Ctrl
+D).
P04700 86
You can also see that the connections between the If block and the subystems are dotted. This means
that they are not carrying signals, but that this is a ‘function-call’. What this means is that when a
condition is satisfied in the If block, the If Action subsystem connected to the corresponding port will be
executed.
There are now six possible gear values coming out of these subsystems. These need to be put together
using the Merge block from the Signal Routing blockset. This block tells the connected subsystems to
write their outputs to the one output signal.
If we place a Merge block in our model, we then need to double click on it and set its Number of inputs
parameter to 6. We then connect each subsystem to one of the inputs.
P04700 87
The model should then look like this:
What we now have is a system which outputs a gear number, depending on the vehicle speed input in
m/s. We now need to include a Look up table, so that this gear number is converted into a Gear ratio
(through the array stored in the MATLAB workspace).
We are going to use the Direct Lookup Table (n-D) from the Lookup Tables blockset. Double click on the
block and set the Number of table dimensions parameter to 1, and Table data parameter to
GearRatio. Note that the table must be accessed with a zero-based index, and so we must subtract
one from the gear number coming from the Merge block.
The final step in this sub-system is to include the final ratio due to the differential. This is achieved by
including a Gain block before the output port, and setting the Gain parameter to FinalRatio.
P04700 88
The output from this sub-system (the combined gear and final drive ratio) will then be used to convert
vehicle speed to engine RPM, and then engine torque to find the forward force which the engine
provides.
Using the output from the last sub-system ie the overall transmission ratio, the vehicle speed can be
converted to the engine speed in rpm. There are 2 inputs for this sub-system: the vehicle speed (in
m/s), and the combined gear and differential reduction ratio.
It is easiest to first think of the rotational speed of the car wheel (in RPM). If vis in m/s, the distance the
wheel travels in 1 minute is v*60.
The circumference of the wheel is given by 2πrwheel, which is the distance travelled in 1 revolution. The
number of revolutions per minute is therefore
𝑣 × 60
𝑅𝑃𝑀𝑊ℎ𝑒𝑒𝑙 =
2𝜋𝑟𝑊ℎ𝑒𝑒𝑙
We are trying to find the engine speed in RPM. This is found from the combined gear and differential
ratio:
𝑣 × 60
𝑅𝑃𝑀𝐸𝑛𝑔𝑖𝑛𝑒 = 𝑅𝑃𝑀𝑊ℎ𝑒𝑒𝑙 × 𝐺𝑒𝑎𝑟𝑅𝑎𝑡𝑖𝑜 × 𝐹𝑖𝑛𝑎𝑙𝑅𝑎𝑡𝑖𝑜 = × 𝐺&𝐷𝑅𝑎𝑡𝑖𝑜
2𝜋𝑟𝑊ℎ𝑒𝑒𝑙
These can be implemented in Simulink, but we need to know the parameters for the Alfa 147 GTA
wheels.
The Alfa 147 GTA uses 185/45R17 tyres. What this gives is that:
This gives a total wheel radius of 0.4318/2 + 0.0833 = 0.2992 m. This needs to be entered in the
Workspace
WheelRadius = 0.2992;
P04700 89
This is now implemented using the following:
Once we know the engine RPM, the engine sub-system can be modelled to find the engine torque.
7.6 Engine
Given the Engine RPM as the output from the previous sub-system, this sub-system will now calculate
the available Engine torque. The inputs are the Engine RPM and the throttle command (this will come
from the cruise control system that we modelled in the previous week).
To find the maximum torque available at a particular engine speed, we need to interpolate using a look-
up table block. The values to define the break points (x – values) in the look-up table are those given
earlier in section 7.3:
RPM 1000 1500 2000 2500 3000 3500 4000 4500 5000
ΤMax 200 220 242 258 260 261 268 285 300
EngineRPM = [1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000
6500 7000];
EngineTRQ = [200 220 242 258 260 261 268 285 300 296 290 250 220];
P04700 90
As part of the Engine sub-system, we need to place a 1-d Lookup Table (from the Lookup Tables
blockset). This needs to be configured as follows:
Table dimensions: 1 (for each RPM value, there is only one torque value)
Table data: EngineTRQ (the y – values)
Breakpoints 1: Engine RPM (the x – values)
Interpolation method (in Algorithm tab): Cubic spline (how to calculate values between two
points)
Extrapolation method (in Algorithm tab): Linear (how to calculate values outside the RPM
range)
This gives us the torque output from the engine, that we can then convert into the linear force provided
by the engine.
To convert the torque provided by the engine, τEngine, to the torque provided at the wheel, τWheel, we
again need to use the combined gear and differential transmission ratio (remembering that gear ratio is
the reduction ratio between the engine and wheels – speed is reduced, and so torque increased):
Also
Hence
P04700 91
This is implemented by the blocks given below:
Don’t forget to connect up all the relevant connections in the parent subsystems. This is the Engine
Force subsystem:
P04700 92
And this is the root sub-system:
Finally, we should group all the root level sub-systems into one subsystem called Alfa Romeo 147 GTA,
and save the current workspace as alfa147gta.mat
To finish off, we can load the workspace automatically by setting its PreLoadFcn callback to:
Load (‘alfa147gta.mat’)
As a reminder, we navigate to the root subsystem, right click on white space, and then select Model
Properties. In the new window, click on the Callbacks tab, and select the PreLoadFcn from the list. The
type the MATLAB command above.
To ensure that we haven’t made any trivial mistakes, run the Update Diagram (from the Simulation
menu, or type Ctrl-D).
P04700 93
The next step is that you could combine together the cruise controller, that we built last week, and the Alfa Romeo vehicle model. A screenshot of the
combined model is given below.
P04700 94
P04700 95