Simulink Intro

6
1 Engineering Mechanics – Simulink Introduction Tutorial By the end of this workshop, you must understand the basics of Simulink and be able to build simple block diagrams in Simulink to perform simple integrations of functions. Part 1 – Creating a model for a flying ball in 1-D In this section, you will learn how to create a Simulink model that integrates a function twice (with supplied initial conditions). 2 2 9.81 '(0) 15 (0) 10 dy dt y y =- = = which corresponds to: 2 1 acceleration 9.81 ms velocity acceleration , initial velocity 15 ms position velocity , initial position = 10 m dt dt - - =- = = = Open up MATLAB, and type Simulink into the command window and press enter. 1) Create New Model 2) Drag a Constant Block onto your Model then double-click on it and set it to -9.81 3) Drag an Integrator Block onto your Model 4) Hold on to Ctrl then drag and drop your integrator block to create a copy of it 5) Link up the blocks as shown below by clicking on a block you want to connect from, then holding Ctrl and clicking on the block you want to connect to (Alternatively, you can link it up by dragging your mouse from the output of the ‘from’ block to the input of the ‘to’ block). 6) Now look for a Scope block (also under Commonly Used Blocks) and drag that onto the model. 7) Double-click on the Scope and click on Parameters (2 nd button) to change Number of Axes to 2. 8) Now you will see that you can have 2 arrows going into the Scope instead of just 1, so link up the blocks (drag backwards from the input of Scope, back to the line between the integrators) 9) Relabel the blocks by clicking on the labels and editing the label. 10) Save this Model as SimpleBall.mdl in a folder in your H: drive (Name the folder ‘Ball’) 1 2 3 4 5 6 7 8 9 10

description

how to use simulink

Transcript of Simulink Intro

Page 1: Simulink Intro

1

Engineering Mechanics – Simulink Introduction Tutor ial By the end of this workshop, you must understand the basics of Simulink and be able to build simple block diagrams in Simulink to perform simple integrations of functions.

Part 1 – Creating a model for a flying ball in 1-D In this section, you will learn how to create a Simulink model that integrates a function twice (with supplied initial conditions).

2

29.81

'(0) 15

(0) 10

d y

dty

y

= −

==

which corresponds to:

2

1

acceleration 9.81 ms

velocity acceleration , initial velocity 15 ms

position velocity , initial position = 10 m

dt

dt

= −

= =

=

Open up MATLAB, and type Simulink into the command window and press enter. 1) Create New Model 2) Drag a Constant Block onto your Model then double-click on it and set it to -9.81 3) Drag an Integrator Block onto your Model 4) Hold on to Ctrl then drag and drop your integrator block to create a copy of it 5) Link up the blocks as shown below by clicking on a block you want to connect from, then

holding Ctrl and clicking on the block you want to connect to (Alternatively, you can link it up by dragging your mouse from the output of the ‘from’ block to the input of the ‘to’ block).

6) Now look for a Scope block (also under Commonly Used Blocks) and drag that onto the model. 7) Double-click on the Scope and click on Parameters (2nd button) to change Number of Axes to 2. 8) Now you will see that you can have 2 arrows going into the Scope instead of just 1, so link up the

blocks (drag backwards from the input of Scope, back to the line between the integrators) 9) Relabel the blocks by clicking on the labels and editing the label. 10) Save this Model as SimpleBall.mdl in a folder in your H: drive (Name the folder ‘Ball’)

1 2 3

4 5

6

7

8

9

10

Page 2: Simulink Intro

2

Part 2 – Using the model to simulate the ball Now you have built the model that will integrate acceleration twice to give you position, however the initial conditions have not been dealt with yet. 1) Go back to MATLAB and go to the same folder in which you placed your simulation model

(‘Ball’ folder in your H: drive). 2) Create a new blank m-file in that folder and call it ‘Ball_Initialise.m’ 3) Inside the m-file, put the following then save it:

% Initialisation file for SimpleBall.mdl % Initial Velocity v_initial=15; % Initial Position y_initial=10; 4) Now return to your Simulink model and double-click on the integrator block labelled ‘velocity’ 5) Under ‘Initial Condition:’, replace the ‘0’ with ‘v_initial’ 6) Likewise, change the initial condition of the position to ‘y_initial’ 7) Now go to File � Model Properties � Callbacks � InitFcn, and type ‘Ball_Initialise’ into the

blank space on the right, then click Ok. 8) Change the running time to 4 seconds by changing it in the box (see below) 9) Go to the Simulation menu and select Configuration Parameters then configure the Solver

options to match the settings shown in the diagram on the next page 10) Finally, you are ready to go! Hit Run 11) View your output by double-clicking on Scope, and auto-resize the view. The top graph shows

velocity against time, while the bottom graph shows position against time.

11

5

4 10 8 7 9

Page 3: Simulink Intro

3

Note: The reason why you don’t simply put in values of 10 and 15 into the Initial Condition box in the model is because you want to be able to easily manipulate these values by changing them in the m-file. When models become large and complicated, the last thing you want to have to do is navigate around a complicated block diagram just to change initial conditions.

Note: The reason why the Solver options were configured as shown is to try to ensure accuracy of the numerical integration. This implements ode45 from MATLAB instantly (which you have seen last year) with the additional condition of not allowing the stepsize to ever exceed 0.01. In general, you would have to decide on the configurations yourself, based on the trade-off between computation time and accuracy.

9

Page 4: Simulink Intro

4

Part 3 – Extending model to a bouncing ball Notice that this ball will just continue to drop forever in our current model. Now we would like to incorporate bouncing into the model (suppose the floor is at 0y = ). What we would like to happen is for the simulation to do the following: 1) Keep Position 0≥ (don’t let the ball drop below the floor) 2) At the point when the ball hits the ground, reset the initial condition of the integrator to the same

speed but in the opposite direction. For example, if the ball hit the ground at 15ms−− (downwards), then the initial condition should be reset to 14ms− (upwards) after hitting

the ground (coefficient of restitution of 0.8 for the ball/floor, not a perfectly elastic collision so some kinetic energy is loss with each bounce)

Note: Before editing your model, make sure you have saved all your progress up to this point as ‘SimpleBall.mdl’. Now save the model as new file ‘BounceBall.mdl’. To incorporate bouncing, the following modifications have to be made to the model: 1) Double-click on the position block. Check the Limit output box, then set Lower saturation limit:

to 0.

2) Double-click on the velocity block, and set External reset to ‘rising’ (this will get explained later

on). Set Initial condition source to ‘external’ and check ‘Show state port’. Again, flip and rotate the block accordingly, and resize it so that it looks as below.

3) For neatness, click on the velocity block then use flip (Ctrl-I) and rotate (Ctrl-R) to get the block to look as it is below with the state port pointing downwards. You can find these commands under the format menu.

2

3

1

Page 5: Simulink Intro

5

Now we need to let the velocity block know when it has to reset initial condition (when ball hits the ground). 4) Go back to the Simulink Library Browser (if you have accidently closed it, open it from View �

Library Browser, or click on the button shown below after expanding the model window) 5) Drag a ‘Compare to Constant’ block from the Logic and Bit Operations library onto the model. A

compare to zero block would actually suffice, but later on we may like to change the floor level so being able to manipulate the constant as we please is more suitable. For now, edit the block and make the necessary changes to the model to get it to look as shown below. You will also need to double-click on the comparator block to make sure it outputs a boolean instead of uint8.

Basically, this is saying when the ball is in the air (most of the time), then the input into the Reset checker is simply 0 constant. Since it’s just constant and not rising, it does not reset. However, when the ball hits the ground, the 0 becomes a 1. This is a rise from 0 to 1 and hence the Reset checker picks up a rising value from 0 to 1 and realises that it has to reset the initial condition because the ball has hit the floor. Now, the model knows when to reset the initial condition. However, it does not yet know what to reset it to. This is why we changed the way the Initial Condition to external (the 0x in the velocity

block). 6) First, just think about the initial condition at the very start when we release the ball. This is

simply v_initial. Drag an IC block from the Signal Attributes library and enter v_initial into it.

6

4 Drag to expand window

4 Library Browser

5

Page 6: Simulink Intro

6

7) Now we will make use of the state port to reset the initial condition to 0.8 of the velocity the ball hits the ground, in the opposite direction. In other words, we would like to take the final state of the velocity block, multiply it by -0.8, and send it back into itself as the new initial condition. To do this, drag a gain block from Commonly Used Blocks or Math Operations and set it to -0.8 as shown on the diagram (enlarge the block if the -0.8 is not appearing, for neatness and clarity).

8) Finally, the model is complete! Change the simulation time to 12 seconds, and hit Run. 9) View the scope and auto-resize it as before. The graphs should look like the ones shown below.

Remember to save your file.

7

8

9