68
Introduction to Simulink Dr. Mohammed F. Alsayed

Introduction to Simulink Dr. Mohammed F. Alsayed

Embed Size (px)

Citation preview

Page 1: Introduction to Simulink Dr. Mohammed F. Alsayed

Introduction to Simulink

Dr. Mohammed F. Alsayed

Page 2: Introduction to Simulink Dr. Mohammed F. Alsayed

Introduction

• In the last few years, Simulink has become the most widely used software package in academia and industry for modeling and simulating dynamical systems.

• Simulink environment encourages you to pose a question, model it, and see what happens.

• You can move beyond idealized linear models to explore more realistic nonlinear models. Thus, describing real-world phenomena.

• It turns your computer into a lab for modeling and analyzing systems.

Page 3: Introduction to Simulink Dr. Mohammed F. Alsayed

• It is a software package for modeling, simulating, and analyzing dynamical systems.

• It supports linear and nonlinear systems, modeled in continuous time, sampled time, or a hybrid of the two.

• Systems can also be multirate, i.e., have different parts that are sampled or updated at different rates.

What is Simulink?

Page 4: Introduction to Simulink Dr. Mohammed F. Alsayed

• For modeling, Simulink provides a graphical user interface (GUI) for building models as block diagrams, using click-and-drag mouse operations.

• With this interface, you can draw the models just as you would with pencil and paper.

• You can also customize and create your own blocks (S-Functions).

• Models are hierarchical, so you can build models using both top-down and bottom-up approaches.

What is Simulink?

Page 5: Introduction to Simulink Dr. Mohammed F. Alsayed

• After you define a model, you can simulate it, using a choice of integration methods, either from the Simulink menus or by entering commands in MATLAB’s command window.

• You can change parameters and immediately see what happens, for “what if” exploration.

• The simulation results can be put in the MATLAB workspace for postprocessing and visualization

What is Simulink?

Page 6: Introduction to Simulink Dr. Mohammed F. Alsayed

Running a Demo model

• An interesting demo program provided with Simulink models the thermodynamics of a house.

• To run this demo, type thermo in the MATLAB command window. This command starts up Simulink and creates a model window that contains this model.

or

>> demo ‘simulink’

Quick Start

Page 7: Introduction to Simulink Dr. Mohammed F. Alsayed

Quick Start

Page 8: Introduction to Simulink Dr. Mohammed F. Alsayed

• When you open the model, Simulink opens a Scope block containing two plots labeled Indoor vs. Outdoor Temp and Heat Cost ($), respectively.

• To start the simulation, pull down the Simulation menu and choose the Start command (or, on Microsoft Windows, press the Start button on the Simulink toolbar).

• As the simulation runs, the indoor and outdoor temperatures appear in the Indoor vs. Outdoor Temp plot and the cumulative heating cost appears in the Heat Cost ($) plot.

Quick Start

Page 9: Introduction to Simulink Dr. Mohammed F. Alsayed

Quick Start

Page 10: Introduction to Simulink Dr. Mohammed F. Alsayed

• To stop the simulation, choose the Stop command from the Simulation menu (or press the Pause button on the toolbar).

• When you’re finished running the simulation, close the model by choosing Close from the File menu.

Quick Start

Page 11: Introduction to Simulink Dr. Mohammed F. Alsayed

Description of the Demo• The demo models the thermodynamics of a house

using a simple model. The thermostat is set to 70 degrees Fahrenheit and is affected by the outside temperature, which varies by applying a sine wave with amplitude of 15 degrees to a base temperature of 50 degrees. This simulates daily temperature fluctuations.

Quick Start

Page 12: Introduction to Simulink Dr. Mohammed F. Alsayed

• The model uses subsystems to simplify the model

diagram and create reusable systems.

• A subsystem is a group of blocks that is represented

by a Subsystem block.

• This model contains five subsystems: Thermostat,

House, and three Temp Convert subsystems (two

convert Fahrenheit to Celsius, one converts Celsius to

Fahrenheit).

Quick Start

Page 13: Introduction to Simulink Dr. Mohammed F. Alsayed

• The internal and external temperatures are fed into

the House subsystem, which updates the internal

temperature.

• Double-click on the House block to see the

underlying blocks in that subsystem.

Quick Start

Page 14: Introduction to Simulink Dr. Mohammed F. Alsayed

• The Thermostat subsystem models the operation of a

thermostat, determining when the heating system is

turned on and off.

Quick Start

Page 15: Introduction to Simulink Dr. Mohammed F. Alsayed

• Both the outside and inside temperatures are

converted from Fahrenheit to Celsius by identical

subsystems.

• When the heat is on, the heating costs are computed

and displayed on the Heat Cost ($) plot on the

Thermo Plots Scope. The internal temperature is

displayed on the Indoor Temp Scope.

Quick Start

Page 16: Introduction to Simulink Dr. Mohammed F. Alsayed

Some Things to Try

• The Constant block labeled Set Point (at the top left

of the model) sets the desired internal temperature.

Open this block and reset the value to 80 degrees

while the simulation is running. See how the indoor

temperature and heating costs change. Also, adjust

the outside temperature (the Avg Outdoor Temp

block) and see how it affects the simulation.

Quick Start

Page 17: Introduction to Simulink Dr. Mohammed F. Alsayed

• Adjust the daily temperature variation by opening the

Sine Wave block labeled Daily Temp Variation and

changing the Amplitude parameter.

Quick Start

Page 18: Introduction to Simulink Dr. Mohammed F. Alsayed

• Running the simulation involves specifying parameters

and starting the simulation with the Start command.

• You can encapsulate complex groups of related blocks

in a single block, called a subsystem.

• You can create a customized icon and design a dialog

box for a block by using the masking feature.

• Scope blocks display graphic output much as an actual

oscilloscope does.

What this Demo Illustrates?

Page 19: Introduction to Simulink Dr. Mohammed F. Alsayed

• Other demos illustrate useful modeling concepts.

• Type simulink3 in the MATLAB command window.

• The Simulink block library window appears.

• Double-click on the Demos icon.

Other Useful Demos

Page 20: Introduction to Simulink Dr. Mohammed F. Alsayed

• The model integrates a sine wave and displays the

result, along with the sine wave.

• To create the model, first type simulink in the

MATLAB command window.

• Note: The window might differ based on the operating

system you are using.

Building a Simple Model

Page 21: Introduction to Simulink Dr. Mohammed F. Alsayed

Building a Simple Model

Page 22: Introduction to Simulink Dr. Mohammed F. Alsayed

• To create a new model, select the New Model button on

the Library Browser’s toolbar.

Building a Simple Model

Page 23: Introduction to Simulink Dr. Mohammed F. Alsayed

• To create this model, you will need to copy blocks into

the model from the following Simulink block libraries:

– Sources library (the Sine Wave block)

– Sinks library (the Scope block)

– Continuous library (the Integrator block)

– Signals & Systems library (the Mux block)

Building a Simple Model

Page 24: Introduction to Simulink Dr. Mohammed F. Alsayed

• To copy the Sine Wave block from the Library

Browser, first expand the Library Browser tree to

display the blocks in the Sources library. Do this by

clicking first on the Simulink node to display the

Sources node, then on the Sources node to display the

Sources library blocks. Finally click on the Sine Wave

node to select the Sine Wave block.

Building a Simple Model

Page 25: Introduction to Simulink Dr. Mohammed F. Alsayed

• Now drag the Sine Wave node from the browser and

drop it in the model window.

• Copy the rest of the blocks in a similar manner from

their respective libraries into the model window.

Building a Simple Model

Page 26: Introduction to Simulink Dr. Mohammed F. Alsayed

• Now it’s time to connect the blocks. Connect the Sine

Wave block to the top input port of the Mux block.

Position the pointer over the output port on the right

side of the Sine Wave block.

Building a Simple Model

Page 27: Introduction to Simulink Dr. Mohammed F. Alsayed

Building a Simple Model

Page 28: Introduction to Simulink Dr. Mohammed F. Alsayed

• Drawing a branch line is slightly different. To weld a

connection to an existing line, follow these steps:

• First, position the pointer on the line between the Sine

Wave and the Mux block.

• Second, Press and hold down the Ctrl key. Press the

mouse button, then drag the pointer to the Integrator

block’s input port or over the Integrator block itself.

Building a Simple Model

Page 29: Introduction to Simulink Dr. Mohammed F. Alsayed

• Finish making block connections. When you’re done,

your model should look something like this.

Building a Simple Model

Page 30: Introduction to Simulink Dr. Mohammed F. Alsayed

• Now, open the Scope block to view the simulation

output. Keeping the Scope window open, set up

Simulink to run the simulation for 10 seconds.

• First, set the simulation parameters by choosing

Parameters from the Simulation menu.

• On the dialog box that appears, notice that the Stop

time is set to 10.0 (its default value).

Building a Simple Model

Page 31: Introduction to Simulink Dr. Mohammed F. Alsayed

• Close the Simulation Parameters dialog box by clicking

on the Ok button.

• Simulink applies the parameters and closes the dialog

box.

• Choose Start from the Simulation menu and watch the

traces of the Scope block’s input.

Building a Simple Model

Page 32: Introduction to Simulink Dr. Mohammed F. Alsayed

Creating a Model

Page 33: Introduction to Simulink Dr. Mohammed F. Alsayed

• You can start Simulink in two ways:

1. Click on the Simulink icon on the MATLAB toolbar.

2. Enter the simulink command at the MATLAB prompt.

Starting Simulink

Page 34: Introduction to Simulink Dr. Mohammed F. Alsayed

• To create a new model, click the New button on the

Library Browser’s toolbar (Windows only) or choose

New from the library window’s File menu and select

Model.

Creating a New Model

Page 35: Introduction to Simulink Dr. Mohammed F. Alsayed

• To edit an existing model diagram, either:• Choose the Open button on the Library Browser’s

toolbar (Windows only) or the Open command from the Simulink library window’s File menu and then choose or enter the model filename for the model you want to edit.

• Enter the name of the model (without the .mdl extension) in the MATLAB command window. The model must be in the current directory or on the path.

Editing an Existing Model

Page 36: Introduction to Simulink Dr. Mohammed F. Alsayed

Summary of Mouse and Keyboard Actions

Page 37: Introduction to Simulink Dr. Mohammed F. Alsayed

Summary of Mouse and Keyboard Actions

Page 38: Introduction to Simulink Dr. Mohammed F. Alsayed

Summary of Mouse and Keyboard Actions

Page 39: Introduction to Simulink Dr. Mohammed F. Alsayed

Summary of Mouse and Keyboard Actions

Page 40: Introduction to Simulink Dr. Mohammed F. Alsayed

• As your model increases in size and complexity, you can simplify it by grouping blocks into subsystems. Using subsystems has these advantages:

1. It helps reduce the number of blocks displayed in your model window.

2. It allows you to keep functionally related blocks together.

3. It enables you to establish a hierarchical block diagram, where a Subsystem block is on one layer and the blocks that make up the subsystem are on another.

Creating Subsystems

Page 41: Introduction to Simulink Dr. Mohammed F. Alsayed

• You can create a subsystem in two ways:

1. Add a Subsystem block to your model, then open that block and add the blocks it contains to the subsystem window.

2. Add the blocks that make up the subsystem, then group those blocks into a subsystem.

Creating Subsystems

Page 42: Introduction to Simulink Dr. Mohammed F. Alsayed

Here are some model-building hints you might find useful:• Memory issues: In general, the more memory, the

better Simulink performs.• Using hierarchy: More complex models often benefit

from adding the hierarchy of subsystems to the model. Grouping blocks simplifies the top level of the model and can make it easier to read and understand the model.

Tips for Building Models

Page 43: Introduction to Simulink Dr. Mohammed F. Alsayed

• Cleaning up models: Well organized and documented models are easier to read and understand.

• Modeling strategies: If several of your models tend to use the same blocks, you might find it easier to save these blocks in a model. Then, when you build new models, just open this model and copy the commonly used blocks from it. You can create a block library by placing a collection of blocks into a system and saving the system. You can then access the system by typing its name in the MATLAB command window.

Tips for Building Models

Page 44: Introduction to Simulink Dr. Mohammed F. Alsayed

• One of the most confusing issues for new Simulink users is how to model equations. Here are some examples that may improve your understanding of how to model equations.

Modeling Equations

Page 45: Introduction to Simulink Dr. Mohammed F. Alsayed

• Converting Celsius to Fahrenheit• To model the equation that converts Celsius

temperature to Fahrenheit:

TF = 9/5(TC) + 32

• First, consider the blocks needed to build the model:• A Ramp block to input the temperature signal, from the

Sources library• A Constant block, to define a constant of 32, also from

the Sources library• A Gain block, to multiply the input signal by 9/5, from

the Math library

Modeling Equations

Page 46: Introduction to Simulink Dr. Mohammed F. Alsayed

• A Sum block, to add the two quantities, also from the Math library

• A Scope block to display the output, from the Sinks library

• Next, gather the blocks into your model window.

Modeling Equations

Page 47: Introduction to Simulink Dr. Mohammed F. Alsayed

• The Ramp block inputs Celsius temperature. Open that block and change the Initial output parameter to 0.

• The Gain block multiplies that temperature by the constant 9/5. The Sum block adds the value 32 to the result and outputs the Fahrenheit temperature.

• Open the Scope block to view the output. Now, choose Start from the Simulation menu to run the simulation. The simulation will run for 10 seconds.

Modeling Equations

Page 48: Introduction to Simulink Dr. Mohammed F. Alsayed

Modeling a Simple Continuous System• To model the differential equation

• where u(t) is a square wave with an amplitude of 1 and a frequency of 1 rad/sec.

• The Integrator block integrates its input, x’, to produce x.

• Other blocks needed in this model include a Gain block and a Sum block.

Modeling Equations

Page 49: Introduction to Simulink Dr. Mohammed F. Alsayed

• To generate a square wave, use a Signal Generator block and select the Square Wave form but change the default units to radians/sec. Again, view the output using a Scope block. Gather the blocks and define the gain.

Modeling Equations

Page 50: Introduction to Simulink Dr. Mohammed F. Alsayed

• In this model, to reverse the direction of the Gain block, select the block, then use the Flip Block command from the Format menu. Also, to create the branch line from the output of the Integrator block to the Gain block, hold down the Ctrl key while drawing the line.

Modeling Equations

Page 51: Introduction to Simulink Dr. Mohammed F. Alsayed

• An important concept in this model is the loop that includes the Sum block, the Integrator block, and the Gain block.

• In this equation, x is the output of the Integrator block. It is also the input to the blocks that compute x’, on which it is based. This relationship is implemented using a loop.

Modeling Equations

Page 52: Introduction to Simulink Dr. Mohammed F. Alsayed

Modeling Equations

Page 53: Introduction to Simulink Dr. Mohammed F. Alsayed

• A Math Example• The figure below shows a bouncing ball.

Lets Get Familiar

Page 54: Introduction to Simulink Dr. Mohammed F. Alsayed

• The ball is dropped from a meters above a flat surface, and each time the ball hits the ground after falling a distance h it rebounds a distance rh, where r is a positive number less than one.

• We will derive an expression for the computation of the total distance that the ball travels and we will create a Simulink model.

Lets Get Familiar

Page 55: Introduction to Simulink Dr. Mohammed F. Alsayed

• The distance is computed with the geometric series

• We can obtain a closed form for this series we begin with the infinite series

• The sum of the n+1 terms is

Lets Get Familiar

Page 56: Introduction to Simulink Dr. Mohammed F. Alsayed

• Multiplication both sides by r yields

• Subtracting the last equation from the one before results in cancellation of many terms

• And for r ≠ 1

Lets Get Familiar

Page 57: Introduction to Simulink Dr. Mohammed F. Alsayed

• and since in our example r < 1, as n→∞, the term rn approaches zero, that is,

• Therefore for our example, the total distance traveled by the ball is

Lets Get Familiar

Page 58: Introduction to Simulink Dr. Mohammed F. Alsayed

• Now, we can use Simulink to model the relation with three Constant blocks and one each Add, Subtract, Multiply, and Divide blocks.

• The result can be displayed in a Display block. • Enter: a = 6; r = 0.1:0.1:0.9;

Lets Get Familiar

Page 59: Introduction to Simulink Dr. Mohammed F. Alsayed

Lets Get Familiar

Page 60: Introduction to Simulink Dr. Mohammed F. Alsayed

• Some modifications

Lets Get Familiar

Page 61: Introduction to Simulink Dr. Mohammed F. Alsayed

• Chemical solutions example• Let us consider a tank with 1000 gal capacity

initially holding 100 gal. of a salt solution in which 50 lbs of salt are dissolved. Suppose that 3 gal. of brine, each containing 2 lbs of dissolved salt, are added to the tank per minute, and that the mixture, kept uniform by stirring, leaves the tank at the rate of 2 gal. per minute. We wish to compute the amount of salt in the tank at any time t.

Lets Get Familiar

Page 62: Introduction to Simulink Dr. Mohammed F. Alsayed

• Let Q be the number of lbs of salt present at the end of minutes t.

Lets Get Familiar

Page 63: Introduction to Simulink Dr. Mohammed F. Alsayed

Lets Get Familiar

Page 64: Introduction to Simulink Dr. Mohammed F. Alsayed

• Modeling a second order ODE.• Consider the following model

Lets Get Familiar

Page 65: Introduction to Simulink Dr. Mohammed F. Alsayed

• Preparation

Lets Get Familiar

Page 66: Introduction to Simulink Dr. Mohammed F. Alsayed

Lets Get Familiar

Page 67: Introduction to Simulink Dr. Mohammed F. Alsayed

• Class work• Build a Simulink model that solves the

differential equation

• Initial condition

Lets Get Familiar

tx 2sin3.1)0( x

Page 68: Introduction to Simulink Dr. Mohammed F. Alsayed

• Class work

Lets Get Familiar