SimulinkExx
description
Transcript of SimulinkExx
![Page 1: SimulinkExx](https://reader036.fdocuments.in/reader036/viewer/2022071920/55cf99d6550346d0339f6aec/html5/thumbnails/1.jpg)
International Automotive Engineering University of Applied Sciences Ingolstadt
WS 2009/10 Faculty Electr. Engin. and Computer Science
Prof. Dr. A. Hagerer
Introduction to Simulink
Exercise 1
We are going to build a Simulink model file, approximately the same as a m-file but using symbols instead.
Open up a editor file to create a Simulink model (.mdl-file). Look for "File->New ->Model". Then open
Simulink's library browser (e.g. "View->Library Browser" from the model window's menu). Select
"Simulnk->Sinks" and mark the "Scope" symbol. Drag it to your open model window. Also get a signal
generator which can be found under "Simulink->Sources" choose "Pulse generator". Drag it over to your
model file. Finally connect the two blocks by clicking the "Pulse generator" and using the left mouse button.
It should then look like this:
Run the model by clicking the arrow in the toolbar. Double click the "Scope" symbol in order to see a simu-
lation. It seems to bee a square wave with amplitude 1 and frequency 1 Hz. This can bee changed by double
click the "Pulse generator" and modifying the parameters. Run the model once again. Does it seem correct?
Maybe it's hard to see. Use the three different zoom possibilities that can be found in the tools menu of the
"Scope". Zoom, zoom x-scale and zoom y-scale.
Also try to change pulse width to 4% of a 5 seconds period. Run the model again!
Exercise 2
Mark the "Pulse generator" and delete it. Instead get a "Signal Generator" from "Simulink->Sources". It
should be connected as the previous example. Here we have the possibility to choose different signals as
square wave, saw tooth, sine wave or a random waveform. Select a sine wave with amplitude 2 and fre-
quency 1000 Hz. Run it!
It looks a bit strange does it not. I can see no resemblance with a sine wave what so ever. Every signal being
simulated is using more or less the same number of points. Suppose we would like simulate a sine signal
(1000 Hz). It would be sufficient to simulate this one during 0.002 sec. The simulation time can be altered
under "Simulation->Simulation Parameters->Stop Time". Change the "Stop Time" and start the simulation.
Look on the curve, now we have 2 periods ( if nothing can be seen press the binocular).
Replace the "Signal Generator" above with a "Signal Builder" and try to construct your very own signal.
Open the "Signal builder, click and drag with the mouse. Perhaps you can get something similar to the figure
below.
![Page 2: SimulinkExx](https://reader036.fdocuments.in/reader036/viewer/2022071920/55cf99d6550346d0339f6aec/html5/thumbnails/2.jpg)
Exercise 3
We are now going to construct our own function. We get "Matlab Fcn" from "Simulink->User defined
Functions". Put it together with a "Scope" and use "Ramp as an input signal. We can find it in "Simulink-
>Sources". Click on "Ramp" and in this symbol you can find three arguments: slope, start time and initial
output. Use the following settings, slope=1, start time=2 and initial output=2. Afterwards set the Matlab
function to a cosine function. Just write cos. Run the model!
Add more functions to the model above. Enter "Simulink->Math Operations" and drag the blocks "Abs" and
"Gain" to the current model window. Double click the "Gain" and change the value to 3. Add also some ex-
tra "Scopes" in order to see precisely what happens in every step. Mark "Scope" and press the right mouse
button and drag how many scopes you desire.
What we have here is a full wave rectifier with an amplifier. Run the model! Does it work as expected? In
scope 2 we can see a sine wave though with a time delay of 2 seconds. Thereafter we take the absolute value
of the sine and get a rectified signal as can be seen in scope 1. Finally we amplify the amplitude with gain 3
and get the output signal in scope.
Exercise 4
Remove all blocks except Ramp and Scope. Now we are going to take the time derivative of an input signal
and to integrate the same signal. Get an "Integrator" from "Simulink->Continuous" and in the same library
we also find "Derivative". If you double click on the "Integrator" you can set a parameter initial condition.
From the start it has a default value 0, but it can be altered to whatever you want. Integrate and take the time
derivate for 10 seconds.
Also change the input signal to a "Step" that can be found under "Simulink->Sources". You are surely famil-
iar with the step function. There are three parameters in the block: initial value, step time and final value. Set
the step time=2 and the rest should be as it is. What value do the integration and the time derivative give if
we simulate for 10 seconds?
Sometimes you want the signals to appear in the same "Scope". This can be arranged by choosing a multi-
plexer. Get a "Mux" from "Simulink->Signal Routing". Delete one of the scopes. Connect the signals that
shall be displayed in the scope with the "Mux"'s inputs and the "Mux"-output to the scope.
To display two signals by means of a single scope but separately can be obtained by changing the scope's
parameter "Number of axis" to 2.
![Page 3: SimulinkExx](https://reader036.fdocuments.in/reader036/viewer/2022071920/55cf99d6550346d0339f6aec/html5/thumbnails/3.jpg)
Exercise 5
Let's introduce the idea of subsystem. A subsystem makes it possible to hide details of a model and to have a
overview of the model. It can be found in "Simulink->Ports&Subsystems". Hide the time derivative and the
integrator in the subsystem and switch "Step" to a "Sine wave" (from "Simulink->Sources "). Execute!
Exercise 6
In this example we are going to investigate a differential equation and look on its solution. We have a simple
model of bacteria growth in a jam pot. Assume that the number of "born" bacteria is increasing proportional
to the existing number (x) of bacteria and the number dying is proportional to the existing number in square:
{ 321growthdeath
2
growthbirth
xpxbdtdx ⋅−⋅=
The constants are: b =1/hour and p=0.5/(bacteria*hour). We have 100 bacteria from the start.
The blocks "Gain", "Product" and "Sum" can all be found in "Simulink-> Math Operations". Use the blocks
above to show how the solution to the differential equation looks like. Make a graphical reading from the
Scope. The initial number of bacteria is written in the integrator. The constants b and p can be assigned in
the MATLAB command window or by just replacing the letters by their numerical value. Double click on
the Gains and switch value. After how long time will only half the initial bacteria remain?
Exercise 7
In this example we will try to illustrate how a difference equation will look like:
[ ] [ ] [ ]nxnyny +−⋅= 15.0
and an initial condition y[-1]=1. Further we know that our input signal is a step applied at n=0 with ampli-
tude 1.
In the scope we can find the solution y[n]. The initial condition is inserted in a "Delay block". Furthermore,
select the sampling time in seconds. This can be inserted in both the "Delay" and "Step"-block. The "Integer
Delay" can be found in library "Simulink-> Discrete".
Before you run the simulation, change "Simulation-> Configuration Parameters". Choose the following:
"Solver Options-> Fixed-step" and "Solver-> Discrete". Notice that the difference equation can be formu-
lated from the sum block. Choose the sample time in each block to 1 sec (Integer Delay, Step and Gain).
Exercise 8
In this problem we will try to illustrate the following: a wagon is linked to a wall with a spring and a
damper. The position of the wagon is given by x(t). The wagon can move without any friction on the
ground. The mass is M. The differential equation describing the dynamics of the system is the following:
FxcxbxM =⋅+⋅+⋅ &&&
M = 5 [kg] mass of the wagon
b = 1 [Ns/m] damper constant
c = 2 [N/m] spring constant
F - force acting on the wagon, zero from the beginning and 2[N] after 5 [sec].
xx &, - position and velocity of the wagon.
Assume velocity and positions are zero from the start. Decide the stationary position of the wagon after a
long time by reading from the scope in your model. Show the velocity and the acceleration as well. What
stationary value will these have after long time?
![Page 4: SimulinkExx](https://reader036.fdocuments.in/reader036/viewer/2022071920/55cf99d6550346d0339f6aec/html5/thumbnails/4.jpg)
Exercise 9
Solve the following pair of coupled differential equations in Simulink:
0)0(,2)0(
22
3
==
−⋅=
=⋅+
xy
yxy
uxx
&&
&
u(t) is a step input signal and y(t) is the output, x(t) is solely an intermediate signal. What value does the out-
put signal have after 15 seconds?
Exercise 10 Simulating Control of a DC Motor
The objective of this exercise is to introduce you to many of the commonly-used blocks in Simulink while at
the same time providing a real-world example. For now, you will be simulating a "virtual" DC motor.
A basic feedback control system is shown above, drawn in the very common block diagram form. The
unique feature of Simulink is that simulation models appear in almost exactly the same format. If you pro-
gram carefully, it will be very easy for an experienced engineer to quickly understand your model. The Ref-
erence Signal is what you want the Output to be. Your job is to design the controller so that the output signal
matches the reference signal.
For our system, our Plant will be a DC motor. Because of inertia in the motor, we cannot instantaneously
reach a desired velocity or position – the motor has to accelerate and decelerate the mass of the rotor. The
motor is assumed to have a sensor attached to its output shaft to measure its position and velocity. A so
called PID controller will be implemented in software to follow a square-wave motor position reference sig-
nal. It should be possible to control either the velocity of the motor (for example, if you are controlling the
speed of a motor for an application like cruise control) or its position (for example, if you are controlling the
position of a robot joint), though the controller for each is different.
1. If you haven’t already, open a blank model window.
2. Start by creating a reference signal using one of the built-in signal generators and display it to the
screen.
a. To generate the signal, look for the "Signal Generator" block in the "Simulink->Sources" category
and drag it into your blank model window.
b. To view or save signals, use a "Sink" block. Drag a "Scope" block into your model and connect
signal generator and scope.
c. To setup the Signal Generator to output a square wave, double-click on the block to open the
"Source Block Parameters: Signal Generator" dialog. Change the "wave form" to "square" and the
frequency to "0.25" Hz and click "OK."
d. Now we need to adjust the time step for the simulation. To do this, click on Simulation-
>Configuration Parameters. In the Solver menu, change the Solver type to "Fixed-step" and the
Solver to "ode4 (Runge-Kutta)". Then set the Fixed-step size to "0.001". Then click "OK."
e. Run the simulation. Have a look at the signal in the scope. You should see an output like what is
shown below.
![Page 5: SimulinkExx](https://reader036.fdocuments.in/reader036/viewer/2022071920/55cf99d6550346d0339f6aec/html5/thumbnails/5.jpg)
f. In order to view the entire history of from the scope, you will have to open the scope parameters by
clicking on the second icon (a little folder) in the scope window. Click on the "data history" tab and
uncheck "limit data points…".
3. Now we will create our DC motor model and see what happens when we don’t use a controller. This
should be exactly the same as if we connected a square wave signal generator (plus current amplifier)
up to a real motor and watched what happened. Controlling a motor like this is called "open-loop," be-
cause there is no feedback or compensation.
a. We will create a very simple model of the DC motor using a subsystem. Add a subsystem block
from "Ports&Subsystems" to the model. If you double-click on the subsystem block, you will open
what appears to be a new model window. However, if you look at the name of the window you will
see "[modelname]/Subsystem," where [modelname] is the name of your model (what you saved it
as). You do not have to save this model separately; it is included in your base model.
You can also rename the block in the main model window to be anything you want. Change the
name of the block to "plant" by clicking on the text "Subsystem" below the model and typing over
it. Now see the change reflected in the name of the subsystem model window.
The plant is quite empty. As you can see, there are "Ports" defining the inputs and outputs of the
subsystem. You can create more of these using the "In" and "Out" blocks also found in the "Ports
&Subsystems" category. We don’t need any more. Also, you can rename these ports to make things
easier to read. Rename the input port to u and the output port to x. Then, model the following dif-
ferential equation in the subsystem:
�� � � � �� b. Now to apply the square wave, drag the plant over the line connecting the Signal Generator and the
Scope. If you lined it up correctly, the line will break and the plant will be added into the series.
c. Run the simulation and observe the output on the scope just as before. Now the scope is showing
the position of our virtual motor. Not quite what we wanted, right? That’s why we need a controller
(or "compensator").
d. Now it is also nice to be able to view both the input and output on the same scope graph. This is ac-
complished through multiplexing, or combining signals. For this you will use the "Mux" block
found in the Signal Routing category. Drag this block into the model. You can see that the block
has two inputs and one output. The number of inputs can be changed by double-clicking on it, but
we only need two. Now, delete the signal line going into the scope and connect the output of the
Mux instead. To connect the signal generator signal, you will need to drag the input hat from the
Mux to somewhere on the signal connecting the Signal Generator to the plant. Then connect the
output from the plant to the other input. It will look something like this:
![Page 6: SimulinkExx](https://reader036.fdocuments.in/reader036/viewer/2022071920/55cf99d6550346d0339f6aec/html5/thumbnails/6.jpg)
e. Now run the simulation again and view the scope. It will show two different coloured lines.
4. To try to match these two signals, we will create a control system using a feedback loop and a compen-
sator. We could use a transfer function again like before, but instead we will create our PID compensa-
tor using a Subsystem. Subsystems make it easy to organize your model by grouping blocks into a
common block.
a. First we need to create our feedback loop. This loop goes from the output of our motor (the angle)
and is subtracted from the reference signal to get a position error. To do the subtraction, you will
need the "Sum" block or the "Add" block.
b. Edit the sum/add block so that it subtracts the motor angle from the reference angle. You may have
to recreate some of your signal lines. And make sure you keep your reference signal going into the
scope. Your model should look something like this:
c. Now we will create our compensator subsystem. Select another subsystem block in the "Ports &
Subsystems" category and drag it into the model. Open the subsystem, rename "In1" to "e" and
"Out1" to "u".
d. Now to create our PID control. Such a controller has the format:
� � �� ��� �� �� where u is the controller output, e is the error you are trying to compensate, and the K’s are gains.
In our PID subsystem, the input is the error and the output is the control, so we need to add some
mathematical blocks in between to get our PID controller. Create the proportional term by adding a
"Gain" block (found in the "Math Operations" category).
e. Now to create the other terms, you will need to use the "Derivative" and "Integrator" blocks from
the "Continuous" category. Also, these terms have gains associated with them, so you will need to
add more "Gain" blocks. Remember, putting blocks in series just means that they are multiplied.
f. Once you have all three terms built, you will need to add them together using the "Add" block. To
do this, you will have to edit the "Add" block so that it will add three numbers. Double-click on the
block and edit it so that it has three plus signs ("+++"). Then connect everything together.
g. To start with, set the "Kd" and "Ki" gains to zero. Also, you might need to edit the "Integrator"
block to add limits. If you let the integral term add forever, it can cause large instabilities. Double-
![Page 7: SimulinkExx](https://reader036.fdocuments.in/reader036/viewer/2022071920/55cf99d6550346d0339f6aec/html5/thumbnails/7.jpg)
click and check the box, "Limit Output" and change the limits so that the value is [-1,+1] and click
"OK." In your model, you can see the Integrator block has changed appearance to show that you’ve
added saturation limits.
5. Now run the simulation again. Again it won’t look good, but not as bad as the first time you ran it. Now
it’s time to tweak the gains – called "PID tuning." Below you will see a description of a step response
and a table describing how the three gains affect it.
Adjust the gains until you can get a nice step response – short rise time, low overshoot, short settling
time, low steady-state error. Note: you may leave the integral gain Ki equal to zero, or at least it will
likely be small.