lecture simulink [相容模式]
TRANSCRIPT
(C) 2014 CPSE Lab. J. Chen1
陳榮輝 中原大學化工系e-mail : [email protected]
http://wavenet.cycu.edu.tw/
(C) 2014 CPSE Lab. J. Chen2
Stateflow Coder
Blocksets Simulink RTW
Toolboxes MATLAB Compiler
• Founded in 1984• Based on Linpack & Eispack• Founders still active:
Cleve Moler, Jack Little, Steve Bangert
Pioneers in interactive technical software
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen3
(C) 2014 CPSE Lab. J. Chen4
Open System
Spreadsheet Models(e.g. Excel)
Future technologies
PFD models, CFD models(e.g. HYSYS, CHEMAD
AspenPlus, Fluent)
Equation-based models(e.g.MATLAB, gPROMS)
Chemical Process Simulation
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen5
Example: Single-Variable Differential Equation
,in Ainv C
ACv ,
V
Reaction: A → BRate Kinetics: (-rA) = kCA
Initial Condition: at t=0, CA = CA,initial = 2 mol/L
Objective: Solve differential mole balance on a CSTR using MATLAB integration routine.
Problem description: A CSTR initially filled in 2mol/L of A is started up with specified conditions of inlet concentration, inlet and outlet flow rates. The reactor volume and fluid density are considered to be constant.
( ) inAA Ain
vdC vk C C
dt V V
inv v
(C) 2014 CPSE Lab. J. Chen6
Ordinary Differential Equations: IVP
MATLAB has several routines for numerical integration ode45, ode23, ode113, ode15s, ode23s, etc.
Here we will introduce ode45 only. ode23 uses 2nd-order and ode45 uses 4th-order
Runge-Kutta integration.
( ), ( )
dy tf t y t
dt
0 0( )y t y
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen7
Example: Scalar ODE
( )( ) 5 sin 5tdy t
y t e tdt
(0) 1y
0 0.5 1 1.5 2 2.5 3-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
t
y(t)
(C) 2014 CPSE Lab. J. Chen8
Example 1: Scalar ODE
( )( ) 5 sin 5tdy t
y t e tdt
(0) 1y
0 0.5 1 1.5 2 2.5 3-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
t
y(t)
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen9
Integration by ode23 and ode45
[t, y] = ode45 (‘@fcn’, [t0,tf], y0)
where
@fcn is a string variable containing the name of the m-file for the derivatives.
t0 is the initial timetf is the final time
y0 is the initial condition vector for the state variablest a (column) vector of time
y an array of state variables as a function of time
(C) 2014 CPSE Lab. J. Chen10
Example: Multivariable ODE
2
2
( )sin ( ) 0
d tt
dt
1( ) ( )y t tDef:
2
( )( )
d ty t
dt
12
21
( )( )
( )sin ( )
dy ty t
dtdy t
y tdt
The first important point to note is that y is a vector of 2 variables, y1 and y2
Also, dy is a vector of two differential equations associated with the 2 variables
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen11
Example: Multivariable ODE
2
2
( )sin ( ) 0
d tt
dt
-5 -4 -3 -2 -1 0 1 2 3 4 5-3
-2
-1
0
1
2
3
y1(t)
y 2(t
)
(C) 2014 CPSE Lab. J. Chen12
Example: Rossler System
12 3
21 2
33 1
( ) ( )
( ) ( )
( )( ( ) )
dyy t y t
dtdy
y t ay tdt
dyb y t y t c
dt
a, b and c are problem parameters that are passed on the ode functions
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen13
Example: Rossler System
12 3
21 2
33 1
( ) ( )
( ) ( )
( )( ( ) )
dyy t y t
dtdy
y t ay tdt
dyb y t y t c
dt
-4-2
02
46
-5
0
50
1
2
3
4
y1(t)y
2(t)
y3(t
)
-10-5
05
1015
-10
-5
0
5
100
5
10
15
20
y1(t)y
2(t)
y3(t
)
option can be set via odeset
(C) 2014 CPSE Lab. J. Chen14
»M_DigitPID.m & M_Plant.m
ts=0.001; %Sampling timexk=zeros(2,1); e_1=0; u_1=0;for k=1:1:2000
time(k) = k*ts;yd(k)=0.50*sin(1*2*pi*k*ts); para=u_1;tSpan=[0 ts];[tt,xx]=ode45(‘PlantModel',tSpan,xk,[],para);xk = xx(length(xx),:);y(k)=xk(1);
e(k)=yd(k)-y(k);de(k)=(e(k)-e_1)/ts; u(k)=20.0*e(k)+0.50*de(k);%Control limitif u(k)> 1.0, u(k)= 1.0; endif u(k)< 0.0, u(k)= 0.0; end
u_1=u(k);e_1=e(k);end
0
( ) ( ) ( ) ( ) ( 1)k
ds p i s
i s
ku k u k e k k e i t e k e k
t
PID Feedback Control Simulation: MATLAB
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen15
»M_DigitPID.m & M_Plant.m
function dy = PlantModel(t,y,flag,para)u=para;J=0.0067;B=0.1;
dy=zeros(2,1);dy(1) = y(2);dy(2) = -(B/J)*y(2) + (1/J)*u;
figure(1);plot(time,yd,'r',time,y,'k:','linewidth',2);xlabel('time(s)');ylabel('yd,y');legend('Ideal position signal','Position tracking');figure(2);plot(time,yd-y,'r','linewidth',2);xlabel('time(s)'),ylabel('error');
Plant 2
1( )G s
Js Bs
2
2 2
( ) 1 ( ) ( )( )
( )
Y s d y t dy tJ B u t
U s Js Bs d t dt
1
12
22
1
y y
dyy
dtdy B
y udt J J
PID Feedback Control Simulation: MATLAB
(C) 2014 CPSE Lab. J. Chen16
Simulink is an interactive, block-orientated-based tool. It is a block program that allows the simulation and analysis of dynamic systems in a block diagram format whether they are linear or nonlinear, in continuous or discrete forms.It is tightly coupled with MATLAB and supported by Blocksets and Extensions.
MATLAB
Blocksets / StateflowToolboxes
SimulinkReal-Time Workshop
What is SIMULINK
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen17
» simulink
Starting SIMULINK
(C) 2014 CPSE Lab. J. Chen18
In this example we will demonstrate what Simulink is capable of by simulating the motion of a car. We are interested in analyzing the relationship between the force applied to the car, f, and the resultant velocity, v, of the car.
f (force)
bv(friction)
v(velocity)
m(mass) kgm 1000 mNsb /50
EXAMPLE: SIMULINK the Motion of Car
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen19
Although it is possible to model the above system in Simulink directly, it is simpler if we convert the system into the Laplace domain.
f (force)
bv(friction)
v(velocity)
m(mass)
)()(50)(1000
)()()(
sFsVssV
sFsbVsmsV
fbvdt
dvm
)1
(120
50
1
501000
1
)(
)(
Ts
K
s
ssF
sV
EXAMPLE: SIMULINK the Motion of Car
(C) 2014 CPSE Lab. J. Chen20
We will now find out what happens when we increase the force applied to the car from 0 N to 500 N.
• Sources Step (a step time of 10, an initial value of 0 and a final value of 500.)
• Continuous Transfer Fcn (the numerator should be equal to [1/50] and the denominator should be [20 1].)
• Sinks Scope
EXAMPLE: SIMULINK the Motion of Car
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen21
We will now find out what happens when we increase the force applied to the car from 0 N to 500 N.
• Sources Step (a step time of 10, an initial value of 0 and a final value of 500.)
• Continuous Transfer Fcn (the numerator should be equal to [1/50] and the denominator should be [20 1].)
• Sinks Scope• Draw a line from the output of any block to
the input of any other block.• Select Simulation-Simulation Parameters.
(In the stop time, enter a value of 100.)• Double click on the scope block• click on the play button ( )
EXAMPLE: SIMULINK the Motion of Car
(C) 2014 CPSE Lab. J. Chen22
We will now find out what happens when we increase the force applied to the car from 0 N to 500 N.
• Sources Step (a step time of 10, an initial value of 0 and a final value of 500.)
• Continuous Transfer Fcn (the numerator should be equal to [1/50] and the denominator should be [20 1].)
• Sinks Scope• Draw a line from the output of any block to
the input of any other block.• Select Simulation-Simulation Parameters.
(In the stop time, enter a value of 100.)• Double click on the scope block• click on the play button • Click on autoscale button to resize the
scale to fit the entire range of vales
EXAMPLE: SIMULINK the Motion of Car
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen23
The simulink model could have been developed directly from the differential equation in the time domain.
• The inertia and damping are gain blocks (located in the math folder). Double-click them, setting the parameter variables (m and b) (I prefer this way for easily checking your model).
• The damping block has been flipped around. To flip a block, right click on it and then select format-flip block from the pull down menu.
• The text for each of the blocks can be edited by simply clicking on the text and entering whatever you like
• The m-file is built up to edit the values of parameters.
• The integration is integration block (locate in the continuous folder). Double-click it, setting initial condition to 0 (default value).
50 1000
bm
fbvdt
dvm
EXAMPLE: SIMULINK the Motion of Car
(C) 2014 CPSE Lab. J. Chen24
The simulink model could have been developed directly from the differential equation in the time domain.
• The inertia and damping are gain blocks (located in the math folder). Double-click them, setting the parameter variables (m and b) (I prefer this way for easily checking your model).
• The damping block has been flipped around. To flip a block, right click on it and then select format-flip block from the pull down menu.
• The text for each of the blocks can be edited by simply clicking on the text and entering whatever you like
• The m-file is built up to edit the values of parameters.
• The integration is integration block (locate in the continuous folder). Double-click it, setting initial condition to 0 (default value).
EXAMPLE: SIMULINK the Motion of Car
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen25
The simulink model could have been developed directly from the differential equation in the time domain.
• The inertia and damping are gain blocks (located in the math folder). Double-click them, setting the parameter variables (m and b) (I prefer this way for easily checking your model).
• The damping block has been flipped around. To flip a block, right click on it and then select format-flip block from the pull down menu.
• The text for each of the blocks can be edited by simply clicking on the text and entering whatever you like
• The m-file is built up to edit the values of parameters.
• The integration is integration block (locate in the continuous folder). Double-click it, setting initial condition to 0 (default value).
EXAMPLE: SIMULINK the Motion of Car
(C) 2014 CPSE Lab. J. Chen26
You can pass the data into the MATLAB workspace using the To Workspace icon
EXAMPLE: SIMULINK the Motion of Car
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen27
You can pass the data into the MATLAB workspace using the To Workspace icon
EXAMPLE: SIMULINK the Motion of Car
(C) 2014 CPSE Lab. J. Chen28
Ordinary differential equations» cstr1.mdl
0
11AA
A CCdt
dC
Exercise: Single Isothermal CSTR Reactor
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen29
» cstr2.mdl
11101
1:tank1 AAAA kCVCCF
dt
dCVst
22212
2:tank2 AAAA kCVCCF
dt
dCVnd
Exercise: Two Isothermal CSTR Reactors
(C) 2014 CPSE Lab. J. Chen30
» cstr2.mdl
Exercise: Two Isothermal CSTR Reactors
2014/10/12 PM 12:19
(C) 2014 CPSE Lab. J. Chen31
133
s +25s2
Transfer FcnSumSignal
Generator Scope
PID
PID Controller
Mux
Mux
1
Out_1Sum
P
Proportional
I
s
Integral
du/dt
Derivative
D
D
1
In_1
»SimLTI_PID1.mdl
0
( ) ( ) ( )
1( ) ( ) ( ') '
t
c dI
E t SP t CV t
dEMV t K E t E t dt T I
T dt
PID Feedback Control Simulation: SIMULINK
(C) 2014 CPSE Lab. J. Chen32
1
Out_1Sum
P
Proportional
I
s
Integral
du/dt
Derivative
D
D
1
In_1
133
s +25s2
Transfer FcnSumSignal
Generator Scope
PID
PID Controller
Mux
Mux
»SimLTI_PID1.mdl
PID Feedback Control Simulation: SIMULINK
2014/10/12 PM 12:19