These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in...

12
These videos and handouts are supplemental documents of paper “X. Li, Z. Huang. An Inverted Classroom Approach to Educate MATLAB in Chemical Process Control, Education for Chemical Engineers, 19, 1-12, 2017.” The copyright of these materials belongs to Dr. Huang and Villanova University, PA, USA. Any use of these materials should be in compliance with U.S. Copyright law, including the TEACH Act. These materials are only allowed for academic purpose, NOT for commercial usage.

Transcript of These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in...

Page 1: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

These videos and handouts are supplemental

documents of paper “X. Li, Z. Huang. An Inverted

Classroom Approach to Educate MATLAB in Chemical

Process Control, Education for Chemical Engineers,

19, 1-12, 2017.” The copyright of these materials

belongs to Dr. Huang and Villanova University, PA,

USA. Any use of these materials should be in

compliance with U.S. Copyright law, including the

TEACH Act. These materials are only allowed for

academic purpose, NOT for commercial usage.

Page 2: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

1

MATLABTraining1-SimulationofODEmodelsinSimulinkObjective

• General introduction of MATLAB • ODE model simulation in Simulink (including plot function)

WhyMATLAB

• MATLAB, which stands for MATrix LABoratory, is a technical computing environment for high-performance numeric computation and visualization.

• With powerful function library • No strict format requirement • Easier for programming than traditional programming languages such as C, C++, and

Fortran GeneralintroductionofMATLAB1.1 Introduction of the MATLAB desktop

• Windows on the MATLAB desktop include: o Current folder window

• Set the current working directory to the folder where you save the programs. MATLAB files must be in either the working directory or in a directory listed in the search path.

o Command window • Run simulations or recall functions by typing the names of the programs or

functions o Workspace window

• Show the data (e.g., variables, array, matrices) existing in the current working window.

1.2 The best teacher you can ask for help: the help function >>helpXYZ=gethelponfunction“XYZ”Example:gettherootsof!" + 2! + 1 = 0• The function to get the roots is: roots. • Type the following code in the command line and get the information on how to use the

function roots helproots

Currentworkingdirectory

Page 3: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

2

• Explanation listed as follows: ROOTS(C)computestherootsofthepolynomialwhosecoefficientsaretheelementsofthevectorC.IfChasN+1components,thepolynomialisC(1)*X^N+...+C(N)*X+C(N+1).Mathematicaltranslationoftheabovecomments:

Polynomial: ()!* + ("!

*+) + ⋯+ (*! + (*-)MATLAB: SYS = ()(" …(*(*-) Thecommand:root(SYS)

>>Sys=[121];%(don’tshowtheresultinthecommandline–theroleofsemicolon)>>Sys=[121]%(showtheresultinthecommandline)>>roots(Sys)Theansweris-1,-1Exercise:understandthefollowingfunctions:exp,log,andlog10.1.3 Overview of MATLAB modules/functions

• Algebra operation for scalar, vector, and matrix • Flow loop for repeating operation (e.g., functions for and while) • Logical Operation (e.g., function if then) • Creation of functions and scripts • Searching and sorting (e.g., functions find and sort) • Data saving (e.g., save data in a *.mat document using function save) • Function library for all engineering disciplines • Friendly user-interface: Simulink

1.4 Introduction of Simulink • SIMULINK is a part of MATLAB that can be used to simulate dynamic systems in block

diagram windows where models are created and edited primarily by mouse-driven commands. (you need to decompose the model into different model components and link them together in Simulink)

• Example: the liquid level in a tank The liquid level in the tank can be controlled by adjusting the inlet flow-rate qin and tuning the outlet valve. The liquid level can be monitored by the level transmitter. The output from the level transmitter can be used as the input of a control system to regulate the liquid level in the tank. The liquid levels can be determined from the following equations:

)K(1 v1 hqAdt

dhin -=

4/2dA p=

where qin is inlet flow-rate, 0.0035 m3/min; Kv1is the valve coefficient, 0.000187; d is the diameter of the tank, 150 mm. The initial value of the liquid level h is 0 m. The Simulink model developed for the liquid level in a tank is shown below:

qin

h

Page 4: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

3

o Developing Simulink models like putting pieces together in the Lego play o Important modules in Simulink models:

- source (e.g., the clock for time, step change and constants), - math operation (e.g, multiply, divide, add, minus, and gain), - integrator, - defined function (e.g., sqrt(h)) - sink (e.g., display the final value, the array and scope recording the time profiles)

o Simulink allows users to select their own simulation parameters and ODE solvers • Introduction of Simulink modules provided in the Simulink module library

o The source blocks

o The math operation blocks

o The integrator blocks

o The defined function blocks

Page 5: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

4

o The sink blocks

SimulationofODEsusingSimulink• Example 1: the CSTR model An exothermal reaction, A->B, takes place in a CSTR reactor which is cooled by the water with temperature Tc. The concentration of species A (cA), the bulk flow temperature (T), and the reaction rate constant (k) are determined by the following equations:

AAiAA kcccVq

dtdc

--= )( ,

)()()( TTVCUAc

CkHTT

Vw

dtdT

cAR

i -+D-

+-=rrr

)/exp(0 RTEkk -=

Parameter Value Parameter Value

q 100L/min E/R 8750K

cAi 1mol/L k0 7.2×1010min-1

τi 350K UA 5×104J/minK

V 100L Tc(0) 300K

ρ 1000g/L cA(0) 0.5mol/L

C 0.239J/gK T(0) 350K

-ΔHR 5×104J/mol

Develop a Simulink model (shown in the following figure) to determine the temperature T profiles for the following two scenarios: 1) Tc decreases to 290 K; and 2) Tc increase to 309 K. Plot the T profiles for these two scenarios in the same figure.

Page 6: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

5

• The procedure to develop the Simulink model for the CSTR model 1) Run MATLAB in Wins 7

Click on the Microsoft “Start” button in the bottom corner of your screen, and MATLAB will be listed under “Programs”. Click the MATLAB icon to run MATLAB.

2) Change the MATLAB working directory to the folder where you will save your programs.

3) Build a new Simulink model

Click New (as shown in the following figure), click Simulink Model, and save the new model as “Model_CSTR”.

Page 7: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

6

4) Open the block library in Simulink by clicking the library browser icon (marked by the

RED rectangle in the following figure)

5) Set up the framework of the Simulink model (mainly by setting the integrators for T, cA,

and the function to calculate rate constant k) o Drag two integrator blocks and one function block into the model. o Double click each integrator block and set up the initial value for T and cA: T(0) =

350 K, and cA (0) = 0.5 mol/L. o Click the label under each block and add the annotation or equation for it. Tip: 1) you can copy an existing block by pressing Ctrl and at the same time using the mouse to drag the target block; 2) double-clicking on a block will open a window filled with more blocks or open the dialog box of the block. The dialog box is used to specify parameters that govern the operation of the block; 3) The names of MATLAB variables, functions or documents, should not include any special characters other than “_”. For example, “C A” is not correct as a space is included in the variable name. Instead, “C_A” is a correct name.

6) Specify the equation to calculate the rate constant k

o Decompose the equation for k into three parts, each of which becomes an input: )//exp(0 TREkk -=

u1 u2 u3

Page 8: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

7

o Build a vector consisting of the three inputs (or parts). Double click the vector

concatenate to change the number of elements to “3”, instead of “2”.

o Build the block for each input and connect them with the vector concatenate: drag two Constant blocks from the Sources category, one for k0 and the other for E/R; double click each Constant block and input the value for each input; connect the Constant blocks to the vector concatenate.

o Input the equation for k in the function block. From now on, the output from the k function block can be used as an input/parameter for other blocks.

7) Input the differential equation for cA

o Decompose the differential equation for cA into different parts:

AAiAA ckccVq

dtdc

´--´= )( ,

o Drag the math operation blocks from the Level 1 to Level 3 into the model, and input the equations for the math operation in the block labels.

o Drag the Constant blocks into the model to specify those model parameters, and connect the blocks from Level 3 to Level 1.

Level1

Level2

Level3

Page 9: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

8

8) Input the differential equation for T o Decompose the differential equation for T into different parts:

)()()( TTCV

UAC

ckHTTVw

dtdT

cAR

i -´´´

´´D-+-´

´=

rrr

o Drag the math operation blocks from the Level 1 to Level 4 into the model, and input

the equations for the math operation in the block labels. o Drag the Constant blocks into the model to specify those model parameters, and

connect the blocks from Level 4 to Level 1.

9) Add the time block, step change for Tc

o Click on “Sources” and select the Step block from the new list and drag the Step block from the Simulink Library Browser

o Double click the Step block, set the initial value for Tc as 300 K, the final value as 290 K, and the Step time as 0.

Level1

Level2

Level3

Parameters/Constants

Level1

Level2

Level3

Level4

Page 10: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

9

o Click the “Sources” and select the Clock block from the new list and drag it to the model.

10) Add the Sink blocks to record the profiles for the time t, cA, and T o Click on the “Sinks” block in the Simulink window and drag the To Workspace

blocks for t, cA, and T. Connect the output of t, cA, and T blocks to the inputs of the To Workspace blocks.

o Double-click on the To Workspace block to open its dialog box. Change Variable name from simout to t. At the same time, set the save format shown at the bottom of the dialog box as “Array”. These arrays will be used to plot the profiles of cA, and T.

o Use the same approach to get add the Scope block (which can show the profile for the variable) and the Display block (which can show the final value of the variable) for cA, and T.

11) Specify the time and the ODE solver to run the simulation

o To select the integration technique and parameters to be used during simulations, pull down the Simulation menu and choose Configuration Parameters. A dialog box is opened showing all the simulation parameters that can be modified.

o Set the Stop Time to 10 for a 10-minute simulation and set the ODE solver as ode15s

o Start the simulation by selecting Start from Simulation menu or click the Run icon

on the Simulink window.

ode15s solver for stiff ODE models

Page 11: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

10

12) Plot the output profiles o For the condition in which Tc is reduced to 290 K, set the

variable name for t, cA, and T to t1, CA1, and T1 in the Simulink model. Run the simulation.

o For the condition in which Tc is increased to 305 K, set the variable name for t, cA, and T to t2, CA2, and T2 in the Simulink model. Run the simulation.

o Use the function plot in the Command window to plot the T profile for both conditions in a figure.

plot(t1,T1,'r--','linewidth',2)%plotTforTcreducedto290Kholdon%keepthet1~T1profileinthefigureplot(t2,T2,'b-','linewidth',2)%plotTforTcincreasedto305Kxlabel('Time(minutes)','fontsize',12)%addx-labelylabel('Temperature(K)','fontsize',12)%addy-label legend('T_cdecreasedto290K','T_cincreasedto305K')%addlegendforthetwocurves%note:in‘r--‘,‘r’representsthelinecolor(RED),and‘--’representsthelinetype.Seenext%formoredetailoftheplotfunction;%everythingbehind‘%’isusedastheannotationandnotexecutable

Runthesimulation Runthesimulationfor10minutes

Savethefigureinthejpgformat

Page 12: These videos and handouts are supplemental … · MATLAB Training 1 - Simulation of ODE models in Simulink ... a *.mat document using ... into different model components and link

CHE 4232: Chemical Process Control, Spring 2015, by Zuyi (Jacky) Huang, Villanova University

11

o Here is more detailed introduction of the command plot: the plot command opens a MATLAB Figure window and generates a plot inside the window.