You are on page 1of 11

Simulink Tutorial

Simulink is a blockorientated or iconorientated addin to Matlab that simulates dynamic (timevarying) systems. It uses a block diagram approach, which you are familiar with from 3rd year Process Control. All processes that will be simulated have an input, the system, and an output: Input SYSTEN SYSTEM Output

A quick example just input and output Open Matlab and at the command prompt type: simulink

Click here to start a new simulation

All the inputs that you will require are in the list under Sources while all possible output options are listed under Sinks. The actual system will be built up from the other options, most commonly the Continuous and Discrete dropdowns. Other Functions and Nonlinear models are available. 1

1. 2. 3. 4. 5.

Start a new simulation by clicking the white page. Select an input (Source) choose a sine wave for now (drag and drop the icon to the blank area of the simulation) Choose an output, or a Sink the Scope Link them up by dragging and dropping the connecting lines Push the play button Your system should look something like this:

6.

Doubleclick the Scope to view a graph of the output. Double click on the Sine Wave and change the amplitude and frequency settings.

8.

Click on Simulation/Parameters (or just push Ctrl-E) to change the following options: Time length change this to 100 seconds Which ODE solver is used to integrate the ODE equation system Step sizes Relative and absolute tolerances (these determine the accuracy of the ODE integration) Refine factor how many data points used in the graphs

9.

Rerun the simulation for a Time Length of 100 seconds. You will notice that the output is quite ragged, which is not expected as a sine curve is smooth. This is because the Refine Factor is small. (The Refine Factor option is located in the Data Import/Export tab -> Save Options box) Increase it to 4, which means that four times as many data points are used in the simulation output. This should result in a smoother curve. 2

10.

One last change is to investigate different inputs. Try the following inputs: A step input adjust the magnitude, initial value and final value A ramp input play around with different slopes and initial values The clock input The constant input Random noise inputs try both the Uniform Random Number and plain Random Number (this should really be called a Normal Random Number Input)

Extending the example: Adding a system 1. Select a continuoustime transfer function (Transfer Fcn) and drag into the previous simulation. First delete the connecting line then reconnect up the system as shown:

2.

We would like to use a different transfer function of the form: G ( s ) =

! 4s + 2 . To change 10 s 2 + 19 s + 1

the transfer function in the simulation, doubleclick the Transfer Fcn block in your simulation. The numerator is entered as: [ -4 2 ] and the denominator as [ 10 19 1 ]. 3. Rerun the simulation. Your Scope output should appear as shown if your amplitude and frequency are both set to unity: Notice that it takes a while for the system to settle down to steady state. Also at time t = 0 the sine curve heads downwards, rather that up. This is because the transfer function has modified the incoming signal. 3

Feedback example : This example will model a simple feedback system operating under PI control to maintain performance despite external disturbances impacting on the process. A stirredtank heater is used to preheat a reactant containing a solid catalyst. The inlet flowrate is

w = 0.2 kg/s and the tank volume is 1 m3. The incoming mixture has an average density of 900 kg/m3
and an average specific heat of 1 000 J/(kg.K). The nominal inlet temperature is 373 K and the desired outlet temperature that must be maintained is 403 K. Passing the mixture over heating coils provides the temperature increase required. The inlet temperature may vary around 373 K and consequently the controller must adjust the valve supplying the heating to maintain the outlet temperature at 403 K.
dT (t ) = wC p (Tin (t ) ! T (t ) )+ Q dt

An energy balance for the process is given by: At steady state the process equation is:

V" C p

(1) (2)

0 = wC pTin ! wC pT + Q

Given the above values as the nominal steady state values, we can compute that Q = 6 000 J/s Subtracting (2) from (1):
V" C p dT ' (t ) = wC p (T 'in (t ) ! T ' (t ) )+ Q' (t ) (3) dt

where the dashed variables are deviation variables, for example: Q' (t ) = Q(t ) ! Q Taking the Laplace transform: Simplifying: where K = 1

V" 1 sT ' ( s ) = T 'in ( s ) ! T ' ( s ) + Q' ( s) w wC p


T ' (s) = K 1 Q' ( s) + T 'in ( s ) !s + 1 !s + 1

(4) (5)

wC p

= 0.005

K kJ s

and

# = V"

= 4500 seconds.

Note that the first term in (5) is the model for our plant and the second term is the model for the disturbance. We can build this in Simulink by using two transfer functions and the Sum block (look under Math). 4 Plant Disturbance

Using a standard method for controller design, the Direct Synthesis method with finite settling time, we can compute the tuning constants for a PI controller of the form:
& Q' ( s) 1 # = Kc $ 1 + $ ( s! ! e( s ) ' " %

(6)

where Q' ( s ) is the signal manipulating the amount of heat provided and e( s ) is the error, that is the difference between the desired temperature and the measured temperature, e( s ) = Tset ( s ) ! T ' ( s ) . The tuning constants are found to be KC = 2250 and " ! = 4500, for a settling time of 400 seconds. Simulink has a model for PID controllers (found under the dropdown for Simulink Extras/Additional Linear/PID Controller), however it takes the following form only:

Q' ( s) I = P + + Ds e( s ) s
which implies that P = KC = 2250, but I =

(7)

KC = 0.5. This is the one block that you must take care with as "!

many textbooks define their tuning rules for a specific form of the PID controller. Now we are in a position to link the blocks together. The following notes will help to complete your diagram (see the completed version on the next page) Right click on a connection line to split off the signal this will complete your feedback loop Adjust the simulation time horizon to 30 000 seconds, that is about 8 hours worth of time. Adjust the relative tolerance to 1E-8. To get the feedback subtraction, drag another Sum icon into your simulation and doubleclick on it to change the symbols. For the feedback the order of the list of signs should be: |+ The | indicates where the + and where the signs appear in the circle. Add a step input into the disturbance model. Doubleclicking on it allows you to set the step magnitude and change when the step occurs. Set the following values: Step Time = 5 000, Initial value = 0 and final value 10. This implies that at time t = 5 000 seconds the inlet temperature will decrease by 10 K. Since we wish our outlet temperature to remain constant the set point should have a constant input of 0. Drag a Sources/Constant block into your simulation and change its value to zero.

Your block diagram should now appear as follows: Disturbance signal Setpoint Error e( s ) Input Q' ( s )

T ' (s)
Running your simulation will then produce the following output in the Scope:

Notice that the disturbance occurs at 5 000 seconds into simulation, but before that the output is zero. Since we know that output should be 403 K, what has gone wrong ? The answer is nothing, remember that we are working in deviation variables from steady state. This means that the scope is actually showing you the value of T ' (t ) = T (t ) ! T , where T = 403 K This means that a zero deviation is 403 K in reality; it further implies that when the disturbance of a 10 K change in the inlet temperature occurs, the worst deviation from 403 K is a drop of about 0.7 K in the outlet temperature. Our controller is therefore doing pretty well under these circumstances, but it does take almost 8 hours to reject this disturbance, during which time another disturbance would almost certainly have impacted the process. The following two pages contains some simulation work that you are to complete in your own time. It will help revise work from the 3P03 course concerning process stability, influence of PI controller tuning parameter KC and I, influence of process deadtime influence of measurement noise. 6

1. Add a Scope to the signal leaving the controller to see what sort of heat adjustment is made by the controller to overcome the effect of this step disturbance. You should find that 2 000 J/s of extra heat is required in order to compensate for this 10 K reducing in inlet temperature. 2. Set P=0 and I=0, that is you are effectively turning the controller off (check this in the Scope you added in the previous step) to see the effect of this step change. Confirm that without the controller the outlet temperature in the tank eventually is 10 K less than required. To emphasize the deviation variables again, an input of zero into the plant from the controller does not imply that we are adding zero heat into the process, we are in fact adding the steadystate amount of heat, 6 000 J/s, determined by Equation (2). 3. Recall that the basic PI settings found were P =2 250 and I = 0.5. Look at the outlet temperature scope and record the worst deviation from the setpoint of 403 K. Now keep P constant, but reduce I in the range of I = [0.05, 0.25]. Since I = K C / " ! it implies that you have increased the reset rate, , as the controller gain, KC, is fixed at 2 250. How does this response differ ? 4. This time increase the value of I in the range I = [5, 20] which implies that we have decreased the reset rate. What type of behaviour is observed now ? Would the sort of behaviour at say I = 50 be acceptable both to the process manager and plant operator in practice ? 5. Now so far it has really taken quite a while to reject our disturbance. Increase the P somewhere in the range of P = [40 000, 80 000] and I to [ 200, 400]. How does this response differ and compare the worst deviation from setpoint with that obtained in step 3 above. Is this a better controller and can you improve it further ? Remember when you do this to also check the controller action taken (that is the amount of heat added to overcome the disturbance) would these controller actions be realistic in practice ? 6. Now we wish to investigate the effect of deadtime on the process. Turn the controller settings to values of P = 5 000 and I to 200. From the Simulink dropdown menu under Continuous select a Transport Delay. Drag it and drop it onto the line connect the controller to the plant. If it does not automatically connect itself between the two, remove the line first, then add the transport delay and reconnect your system. See the diagram on the following page.

Double click the transport delay and change the value to 30, which implies that there is pure delay of 30 seconds between the time the controller makes the change and when it actually occurs. This is very realistic in practice and this delay element may also be used to simulate a measurement delay, that is, the reading we currently see on the measurement device is actually the reading from 30 seconds ago.

Now rerun the simulation and you will notice that the ODE integrator takes longer, but this is not because of the time delay. Observe the scope output and describe what has happened. Can you now explain why the integration took this long ? 7. If we required acceptable behaviour do you think we should increase or decrease the length of deadtime ? Confirm your answer by simulation and find the length of deadtime that results in behaviour that is just acceptable. 8. Return your PI settings back to the base case values P =2 250 and I = 0.5. Now add a normal random number generator to simulate noisy signals. Leave the mean at 1, change the variance to 0.1 and set the sampling time to 5 seconds. First send this random signal directly to a Scope. Do you think that this seems reasonable for a noisy measurement ? Join up the measurement noise so that the true measurement signal is corrupted by this noise as shown in the diagram alongside. (You can use CtrlR to rotate blocks) 9. Look at the controller action taken when both noise and deadtime are present. Do the negative values of heat alarm you ? If not, why not ? The above notes were compiled from various sources further Simulink Tutorials may be found from www.google.com by typing Simulink Tutorial. 8

SIMULINK Guide to plotting graphs in the MATLAB workspace


Thursday, November 1, 2007 Consider the following Simulink model:

Sending Scope results to the workspace To send the results of a Scope to workspace, do the following: 1. Double-click on the Scope. Click on the Parameters icon. (2nd icon from the left)

2.

Select the Data History tab. Check the Save data to workspace box. Assign a name to your variable (in this case, y1) and choose Array as the format. Then click OK.

3.

Run your simulation (hit Ctrl-T or press the Play button). (Do not leave out this step!) The results of your Scope will now appear in the MATLAB workspace as a two-column matrix named y1 (in this case). y1 = [ t output ] The first column of y1 holds the time-vector. The second column holds the output values.

Overlaying graphs from Simulink Output Let us assume that the output values from Scope are stored in the matrix y1 and the results from Scope1 are in y2 . In the MATLAB workspace, issue the following commands: >> time1 = y1(:,1) >> output1 = y1(:,2) >> plot(time1, output1) This will plot the results of Scope. To overlay the results of Scope1 on it, do the following: >> hold; Current plot held >> time2 = y2(:,1) >> output2 = y2(:,2) >> plot(time2,output2, :) Explanation: The hold command holds the current plot. All subsequent plot commands will plot over the current plot. Note: the hold command only needs to be issued once. The 3rd parameter of the plot command specifies the style of the plot. : tells MATLAB to plot a dotted line. For further information, type: >> help plot

10

Demonstration: Digital Controller applied to a Continuous Plant


Consider a plant modeled by the following transfer function:

g (s) =

1 2s + 1

We apply the direct method to design the following controller, with = 0.2:

g c (s) =

1 1 2s + 1 = !s g ( s ) 0.2 s
10 z ! 9.5 z !1

Using MATLABs c2d function to discretize the controller (with t=0.1), we obtain:

g c ( z) =
Simulink Model

To check how effective this controller is at rejecting disturbances, construct the following Simulink model.

Notes: 1. 2. 3. Use 0 as the setpoint of the closed-loop system. Set 0.1 as the sampling time on the Discrete Transfer Fcn block. Use the transfer function a unit step disturbance.

g d (s) =

1 in the disturbance transfer function block. Connect a Step block to produce s +1

You should see the following responses on your Scopes. (Left: controller output (u), Right: system output (y) )

11

You might also like