my way - how to teach computer simulation€¦ · my way - how to teach computer simulation heikki...
TRANSCRIPT
-
MY WAY -
HOW TO TEACH COMPUTER
SIMULATION
Heikki Koivo
Aalto University
ESPOO, FINLAND
-
Objectives
LEARN BASICS OF
• Modelling
• Simulation
• Automatic control
• Optimization
• Tools: MATLAB, SIMULINK
-
Beginning of COMPUTER SIMULATION course
1983 Slides about contents
-
Course Content in 19831 MODELLING
– Basic philosophy
– How to determine the model structure from experimental results
– Parameter estimation
– Examples: paper machine, heat recovery system for a TMP line,
industrial glass oven, plastics extruder, flotation circuit,
magnetic levitation vehicle, main control loops of a nuclear
power plant, training simulator
2 NUMERICAL METHODS FOR SOLVING DIFFERENTIAL EQUATIONS
– Integration methods
– Difficulties: Functions given as tables, discontinuities, delays in
differential equations, selection of step size and its automatic
control
– Library programs (e.g. IMSL, NAG)
-
Course Content in 1983, cont.
3 INTERACTION
– Interaction with computer
– Software solutions, example given
– Graphics
4 PROGRAMMING THE SIMULATOR
– Structure
– General principles for software design and implementation
– Special features of FORTRAN
– Documentation
-
Course Content in 1983, cont.
5 HARDWARE USED IN SIMULATION
– Parallel processors
– Personal computers (PC’s)
– Real-time simulation
6 FURTHER EXAMPLES OF SIMULATED SYSTEMS
– Elevator
– TMP (Thermo Mechanical Pulping) process
– Cancerous cell
– National economy
Lectures, exercises in microcomputer class,
take-home exam
-
Course Content in 2006
7
-
Computer tools
-
Basics in simulation
• Mathematical model building– Use of the model
– Profitability in mathematical modelling
– Model users
– What is computed and how accurately ()
– How detailed a model is needed
-
Basics in simulation
• Different phases in model building– Preplanning
– Selection of variables
– Model structure selection
– Planning experiments, if needed
– Performing the experiments
– Analyzing the results from experiments
– Finalizing the model and testing it
-
Basics in simulation
• Use of model
• E.g. paper machine
-
Short cycle of a paper machine
-
Microfluidics simulation
-
MS MS
PSTN
M S C
RNCRNC RNC
BS BS
Wireless radio network – Radio resource
management
-
Chernobyl
• Sometimes, even with computer-controlled processes thingsgo wrong
• In Chernobyl, engineers in early morning hours of 1986, in ’wolf’s hour’, started making experiments.
• They turned the main controllers off, and started making whatcould be called (standard) step experiments thinking that theycould manually control the process.
• Unfortunately the process was inherently unstable, and the rest we know.
• Once it had happened, my PhD student reminded me of ourRussian visitor – visit in 1983.
-
16
Chernobyl
16
-
MIR Submersible – Institute of Oceanology,
Russian Academy of Sciences
17
• Used in filming TITANIC
• Finnish built and designed
• Max diving depth 6000 m
• Reserve power generation
simulation (our conribution)
-
Model structures
• Static models
• Dynamic models
• SIMULINK – starters
• Stochastic processes to generate noise
-
Model structures
• STATIC MODELS
Linear, scalar
y = k u + b
y output; u input; k gain; b bias
EXAMPLE: OHM’s Law
U=RI
U = voltage (output)
I = current (input)
R = resistor
Nonlinear, scalar
y = f(u; a),
a is a parameter
OHM’s Law with nonlinear resistor
R = R(I) = R0 I2 ; Resistor depends on current
0
3
; ; ;
( ; )
y U u I a R
y f u a au
2 3
0 0( )U R I I R I I R I
-
Model structures
• STATIC MODELS
– Linear, multi-input multi-output (MIMO)
y = Au + b
y is output vector, u input vector, b constant vector
-
Model structures
• DYNAMIC MODELS
– Linear, scalar
0 0, ( )dx
x ax bu x t xdt
a and b are constants
-
Model structures
• DYNAMIC MODELS - EXAMPLE
– 1798: Model of population dynamics (Malthus, 1766-1834) :
Population year 1960 was 3.04.109 and its growth 2% .
Compute how population evolved up to year 2008.
90.02 , (1960) 3.04 10x x x
0.02( 1960)
0
9 0.02(2008 1960) 9
( )
(2008) 3.04 10 7.9 10
tx t x e
x e
-
MODEL STRUCTURESYear Population
Average
annual
growth
rate (%)
Average annual
population change
1960 3,039,669,330 1.33 40,792,172
1969 3,632,780,614 2.05 75,286,491
1970 3,708,067,105 2.07 77,587,001
1971 3,785,654,106 2.01 76,694,660
1972 3,862,348,766 1.95 76,183,283
1973 3,938,532,049 1.90 75,547,218
2000 6,085,478,778 1.21 74,220,528
2001 6,159,699,306 1.18 73,002,863
2002 6,232,702,169 1.16 72,442,511
2003 6,305,144,680 1.14 72,496,962
2004 6,377,641,642 1.13 72,578,164
2005 6,450,219,806 1.12 72,540,568
2006 6,522,760,374 1.10 72,466,183
2007 6,595,226,557 1.09 72,442,792
2008 6,667,669,349 1.08 72,368,570
2009 6,740,037,919 1.07 72,210,364
-
Model structures
• MATLAB – SIMULINK SOLUTION
• Instead of differential equation –
integrate it resulting in:
• In SIMULINK, integral operator is
9
0.02 ,
(1960) 3.04 10
x x
x
0 0 0
0( ) ( ) ( )
t t t
t t t
dxx t dt dt dx x t x t
dt
( )x t x(t)
𝑥(𝑡0
-
Model structures
9
0.02 ,
(1960) 3.04 10
x x
x
( )x t x(t) 0.02x(t)
• What about x(t0)? Click the the integrator block open.
In the space Initial condition type: 3.04*10^9 ;
• Until now, we have handled the left-hand side of the equation.
In order to have balance in the equation, we need to complete the SIMULINK
diagram with the term 0.02x(t).
𝑥(1960 = 3.04 ⋅ 109
-
Model structures
9
0.02 ,
(1960) 3.04 10
x x
x
( )x t x(t)
0.02x(t)
• MATLAB – SIMULINK SOLUTION –
SOLUTION DISPLAYED WITH A SCOPE
Simulation result
-
Model structures• DYNAMIC STATE-SPACE MODEL
– Nonlinear, multi-input multi-output (MIMO)
0( ( ), ( ), ), ( )t t t t
0x f x u x x
𝐲(𝑡 = 𝐡(𝐱(𝑡 , 𝐮(𝑡 , 𝑡
𝑡 has been droppedto simplify notation
State space model
First order derivatives on the
left hand side, no derivatives
on the right hand side
Output equation
Remark: Discrete time state space equations are similar
-
Model structures
• DYNAMIC STATE-SPACE MODEL
– Nonlinear, multi-input single-output (MISO)
23 , Drop the argument x x x u t
y x
2( ) 3 ( ) ( ) ( )
( ) ( )
x t x t x t u t
y t x t
1
2
x x
x x
To get state - space model
solve for the highest order
derivative and define
1 2
2
2 1 2
1
3
x x
x x x u
y x
1 1 21 22
2 1 22 1 2 1
, ,= ,
, ,3
( , )
f x x ux x
f x x ux x x u
u
y x h x u
x f x u
u
y
-
Model structures
• LINEAR STATE-SPACE MODEL MODELS
– Linear, multi-input multi-output (MIMO)
A (nxn), B (nxm),
C (pxn) and D (pxm) real, constant
,x Ax Bu
y Cx
-
TRANSFER FUNCTION
GU Y
Y = G*U
Linear, time-invariant state equation
,x Ax Bu
y Cx
Corresponding MIMO (multi input - multi output) transfer function
G( ) C( I A) B1s s continuous ,
CONTINUOUS CASE
Scalar transfer
function
-
To generate noisy data for measurements:
WHITE NOISE or RANDOM NUMBERS
White noise: Current state
depends
neither on history nor future
values
Noise spectrum is constant
Random number generator:
Uniformally or Normally distributed
-
Noisy data generation, example
-
Numerical solution of
differential equations
-
Summary of solvers
-
Initial value problem
0
( ( ), ), 0
(0)
x f x t t t
x x
1 1
1 1( ) ( ) ( ( ), ) or ( ) ( ) ( ( ), ) m m
m m
t t
m m m m
t t
x t x t f x s s ds x t x t f x s s ds
Let
Integrating both sides
0
0
( ) ( ( ), ) ,
t
x t x f x s s ds
-
Initial value problem (scalar case)
Assume f depends only on x
t (time)1mt mt
1( )
mf x
( )m
f x
Area
1m mh t t
Rectangular
approximation
of the integral
ErrorKnown Unknown
h integration step
1( )mf x h
𝑡𝑚−1
𝑡𝑚
𝑓(𝑥(𝑠 𝑑𝑠 ≈ 𝑓 𝑥 𝑡𝑚−1 𝑡𝑚 − 𝑡𝑚−1 = 𝑓(𝑥(𝑡𝑚−1 ℎ
-
Euler method
t (time)0t 4t
0 0( , )f x t4 4( , )f x t
( , )f x t
4 ( )p t
4 4
0 0
4( ) ( ) , m=4
t t
t t
f s ds p s ds
Euler method-First order method:
Global error (error at a given time) is proportional to the step size.
1 1 1( , ) , ( )m m m m m mx x f x t h x x t
Can use higher order
polynomials for better
approximation:InterpolationExtrapolationLagrangeNewton’s differenceor orthogonal polynomialsLegendreTscebychevJacobiLaguerreHermite
-
Runge-Kutta methods (open)
• Runge-Kutta methods use
only function values – not
derivatives
• General form of Runge-Kutta
method1
1
1
1
( , )
v
m m i i
i
i
i m ij j m i
i
x x w k
k hf x a k t c h
Approximates derivatives
• Classical 4. order Runge-Kutta
1 1 1 2 2 3 3 4 4
1
2 21 1 2
3 31 1 32 2 3
4 41 1 42 2 43 3 4
( , )
( , )
( , )
( , )
m m
m m
m m
m m
m m
x x w k w k w k w k
k hf x t
k hf x a k t c h
k hf x a k a k t c h
k hf x a k a k a k t c h
1 4 2 3 2 3 4
21 31 32 41 42
1 1 1, , , 1
6 3 2
1, 0
2
w w w w c c c
a a a a a
-
Predictor-corrector method
• General form
1 1 2 1 1
0 1 1 1
m m m k m k
m m k m k
x x x x
h x x x
1 1 2 1 1
0 1 1 1 1( , )
m m m k m k
m m m k m k
x x x x
h f x t x x
-
Predictor Euler – Corrector trapezoidal
1. Compute xm+1 with predictor formula
2. Compute corrected xm+1 from corrector formula
Requires starting values, e.g. Runge-Kutta
Multiple step method
e.g. Adams-Moulton
1ˆ
m m mx x hf
1 1 1ˆ( , ) ( , )2
m m m m m m
hx x f x t f x t
-
Stiff equations
• Definition
Equation is stiff (in linear, time-invariant case) when
max
min
Re1
Re
-
Stiff equations
• Example
• Characteristic equation
• Roots of polynomial
• roots([1, 100, .9999]) ans =-99.9900 -0.0100
• Solution
100 0.9999 0x x x
(0) 1; (0) 0x x
2 100 0.9999 0r r
99.99 0.001( ) 0.0001 1.001t tx t e e
Fast mode disappears
quickly, cannot be seen
in the figure
Slow
mode
dominates
Slow mode
Fast mode
-
Errors in numerical integration
• Truncation error
– Only finite number of terms
are taken into account
• Rounding error
– Floating point does not represent
real number accurately, because
computers have finite word lengths.
Error grows, when step size
decreases
• Cumulative error
• Truncation error – e.g. Taylorin
series
1
2 3
1 1 1 1
( ) ( )
( ) ( ) ( ) ( )2! 3!
m m
m m m m
x t x t h
h hx t x t h x t x t
11( )
( 1)!
kkhT h x
k
Truncation error
-
Bottlenecks in simulation
Torque T
Speed n
Field voltage e
Two-phase induction servomotor
torque-speed characteristics.
Torque function characteristics
not given explicitly as a function.
It is given as a graph or in table
form (very common in practice).
Dependence is often nonlinear.
A system component creates a
nonlinear dynamic equation.
How to solve these problems in
simulation?
0
-
Linearization example
Pendulum (assume no friction)
L is constant,
mass point mass m
mg
T (external
torque)
mg sin mg cos
L
m
-
Pendulum - equation of motion
L
TmgorcesfmL sin
Assuming constants to simplify this
sin u
Define state variables:
angle α and angular
velocity
1
2
:
:
x
x
1 2
2 1
:
: sin
x x
x x u
1 2 1 1 2
2 1 2 1 2
: ( , , )
: sin ( , , )
x x f x x u
x x u f x x u
Remark: State definition not unique!
Could define state variables as
State equation becomes
2
1
:
:
x
x
-
Pendulum (u=0) - Equilibrium state
Exact solution
0 0
1 2
0 0
2 1
0
0 sin
x x
x x
0 0( ) x 0 f x
Equilibrium state
satisfies
0
1
0
2
, 0, 1, 2,...
0
x n n
x
Pendulum
Assume u=0
for simplicity
1 2 1 1 2
2 1 2 1 2
: ( , )
: sin ( , )
x x f x x
x x f x x
-
Equilibrium state – SIMULINK/ TRIM
TRIM Finds steady state parameters
for a Simulink system.TRIM solves for steady-state parameters that
satisfy certain input, output and state conditions.
Set initial condition.
Is there another equilibrium point?
One has to make another initial guess.
Set initial guess to be (1,1)
[X,U,Y,DX]=TRIM(' Pendulum ',[1 1]')
X =1.0e-12 * [ 0 0.2922]’
U = Empty matrix: 0-by-1
Y = Empty matrix: 0-by-1
DX = 1.0e-12 * [ 0.2922 0]’
Remark: SIMULINK uses states
2
1
:
:
x
x
TRIM(‘Pendulum‘,[2 2]’)
X = 0 angular velocity
3.1416 angle α
𝛼
-
Linearized state space model corresponding to
computed equilibrium states
MATLAB Command
[A,B,C,D]=LINMOD('SYS',X,U)
[A,B,C,D]=LINMOD(‘Pendulum', [0 0])
x Ax Bu
y Cx Du
[A,B,C,D]=LINMOD('Pendulum', [0 0])
A = 0 -1.0000
1.0000 0
B = Empty matrix: 2-by-0
C = Empty matrix: 0-by-2
D = []
[A,B,C,D]=LINMOD('Pendulum', [0 pi ])
A = 0 1.0000
1.0000 0
-
Difficulties in simulation
• Models not given
• Use parameterized, empirical models
• Parameters determined from
measurements
-
Examples of parameterized models
• STATIC - LINEAR– Guess the model
y = a u+b
Determine parameters a and b, when a measurement (ni noise) is available.
– Form cumulative error (constant weights)
i i iy a u b n
2
1
1( , )
2
Ni i
i
J a b y au b
Minimize cost function w.r.t. a and b.
Linear -multivariable
2
1
1( ) ( ; )
2
Ni i
i
J
a y f u a Minimize cost function w.r.t. a.
-
Curve fitting – fit polynomial
• Data generation
• MATLAB or SIMULINK
20.3+2sin(t)+0.1ty
>> [t y]ans =
0 0.3000
0.5000 1.2839
1.0000 2.0829
1.5000 2.5200
2.0000 2.5186
... ...
8.5000 9.1220
9.0000 9.2242
9.5000 9.1747
10.0000 9.2120
Table
0 1 2 3 4 5 6 7 8 9 100
1
2
3
4
5
6
7
8
9
10
x
y
Graph
POLYFIT Fit polynomial to data.POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of degree N that fits the data,
P(X(I))~=Y(I), in a least-squares sense.
-
Result of 3rd order polynomial fit
[p,s]=polyfit(t,y,3)
ans =-0.0152 0.3768 -1.4204 2.2505
y1=polyval(p,t);
e=y-y1;
3 2 1 0 3 2 1 0
2213 2
3 2 1 0 3 2 1 0, , , , , ,
1
min ( , , , ) min i i i ia a a a a a a a
i
J a a a a y a t a t a t a
3 2
3 2 1 0y a t a t a t a
0 1 2 3 4 5 6 7 8 9 10-4
-2
0
2
4
6
8
10
12
Error
Data
Not a good fit!
-
Result of 6th order polynomial fit
p=polyfit(t,y,6);
y1=polyval(p,t);
e=y-y1;
Much better!0 1 2 3 4 5 6 7 8 9 10
-2
0
2
4
6
8
10
Do not fit one high-order polynomial
globally but rather many low order
polynomials locally. WHY?
tt = 0:.25:10;
yy=spline(t,y,tt);
SPLINE Cubic spline data interpolation.
YY = SPLINE(X,Y,XX) uses cubic spline interpolation to find
YY, the values of the underlying function Y at the points in the
vector XX. The vector X specifies the points at which the data
Y is given. If Y is a matrix, then the data is taken to be vector-
valued and interpolation is performed for each column of Y and
YY will be length(XX)-by-size(Y,2).
0 1 2 3 4 5 6 7 8 9 100
1
2
3
4
5
6
7
8
9
10
p=polyfit(t,y,6) yy=spline(t,y,tt)
Error
Excellent fit!
-
Polynomial fit on noisy data
• Noisy data, repeat curve fitting
>> plot(tout,y)
[p,s]=polyfit(tout,y,6);
y1=polyval(p,t);
plot(tout,y,tout,y1,tout,y-y1)
Data
Polyno-
mial fit
polyfit(tout,y,6)
Common mistake: Model fit very good
but model follows noise.
Use prefiltering and lower order
models.
-
Look-up table in simulation
Look-up table can be used in interpolation of dataSimulate the system
with different initial conditions, when is given in the previous example.
Show the result in phase plane.
. ( )x x f x 0 6 y f x ( )
Previous noisy data was generated in MATLAB
using sampling interval 1.
x=[-10:1:10]; y=0.3+2*sin(x)+0.1*x.^2;
0 1 2 3 4 5 6 7 8 9 100
1
2
3
4
5
6
7
8
9
10
x
y
Graph
-
Look-up table in simulation
EXAMPLE 2.
Simulate the system
with different initial conditions,
when is given in the
previous example.
Show the result in phase plane.
. ( )x x f x 0 6
y f x ( )
Look-up
table
-
Look-up table simulation,
initial condition (1,1)States x1(t) and x2(t)
0 5 10 15 20-1.5
-1
-0.5
0
0.5
1
1.5
Time (s)
x1
x2
-1 -0.5 0 0.5 1 1.5-1.5
-1
-0.5
0
0.5
1
x1
x2
Phase plane (x1,x2)
-6 -4 -2 0 2 4 6-5
-4
-3
-2
-1
0
1
2
3
x1
x2
Phase plane plot with different initial conditions
-
Discontinuity in time
• State equation
0
( ( ), ( ), ),
( )
t t t
t
0
x f x u
x x
u(t)
ttk tdisctk+1
tdisc-tk
tdisc known
• Discontinuity in time (or in
known input)
• Example: relay in the system
0
( ( ), ( ), ),
( )
t t t
t
0
x f x u
x x
-
60
Discontinuity in time –
not a big problem
Dynamics of a satellite in space , one-degree of freedom
Input u(t) assumes values -1, 1 at relay output.
u
-
Discontinuity in state can generate problems
• State equation
0
( ( ), ( ), ),
( )
t t t
t
0
x f x u
x x
u(t)
ttk tdisctk+1
tdisc-tk
tdisc not known
dx/dt=f1(x,u,t)
Old dynamics
New dynamics
dx/dt=f2(x,u,t)
-
Discontinuity in state -Backlash
62
Automatic step size
Choose e.g. smaller stepsize
-
Algebraic loop
2 3 1 3
3 2 1
1 2 34, 2, 0.5
x u k k x k k x
u k k x k x
k k k
Same variable on both sides of equation
• E.g. derivative of state – not a proper state equation
x f (x , x, u, t ), x(t0 ) x 0
Algebraic loop
2 3 1 3
2 3 1 3
1 3
2 3 2 3
1
1
1 1
x u k k x k k x
k k x u k k x
k kx x u
k k k k
Fixed
-
Find the minimum of J.
Static optimizationCost function
1 2( ) ( , , , )nJ J x x xx Global maximum
1
min ( ),
[ , , ]
nR
n
J
x x
xx
x
Global minimum
Example[x,y,z]=peaks(40);meshc(x,y,z)
Several local max and min
-
Numerical optimization methods -
unconstrained
• ’Gradient’ methods
– Gradient
– Conjugate gradient
– Davidon-Fletcher-Powell
– Broyden-Fletcher-Goldfarb-Shanno
– Newton or Gauss-Newton
– Marquardt-Levenberg
Need gradientinformation,
Hessian approximation!
X=fminunc(FUN,X0,OPTIONS)
Choice of quasi-Newton methods
FUN = Cost function; XO=Initial guess
-
Numerical optimization methods
• All ’gradient’ methods can be written in the form
1i i i i x x s
step size
search direction
-
Numerical optimization methods –
unconstrained direct search
• Direct search methods– Hooke-Jeeves
– Rosenbrock
– Simplex (Nelder-Mead)
Need only cost function values!
• X =
fminsearch(FUN,X0,OPTIONS)
(Nelder-Mead)
• FUN = Cost function;
• XO=Initial guess
Uses a polytope of N+1
vertices in N dimensions
-
Computer controlled traffic –
streets, (intelligent) highways
-
MATLAB Optimization
Test exampleUNCONSTRAINED: Gradient method - Steepest Descent
DOES NOT CONVERGE! THERE IS NOT ENOUGH CHANGE IN THE
GRADIENT IN THE BOTTOM OF THE VALLEY!
REMARK: In feedforward
neural networks:
Backpropagation algorithm
is a gradient algorithm –
Is not a very efficient
method.
-
MATLAB Optimization
Test example[x,fval,exitflag,output] = fminunc({f,GRAD},x,OPTIONS);
• Broyden-Fletcher-Goldfarb-Shanno quasi-Newton method
Value of the function at the solution: 8.98565e-009
Number of function evaluations: 105
-
MATLAB Optimization
Test exampleChoose any of the following methods to minimize the banana function
UNCONSTRAINED:
4) Simplex Search (NELDER-MEAD) – uses only function
values
-
Dynamic systems and optimization
– Combining SIMULINK simulation and optimization
– PID controller optimization
-
Principle of feedback
Temperature control
-
Block diagram
Temperature control of sauna
PI(D)Resistor
wires
Sauna
temperature
SensorFeedbackMeasured
output
Error
e(t)Set point Output
+
Control Actuator Sauna
-
PID control
• ”Textbook” version of PID
• Control variable u is a sum of:
– P-term (proportional to error e) - present
– I-term (proportional to integral of error e) - history
– D-term (proportional to derivatice of error e) – future
• Cost function
– Many cost functions can be used to optimize PID control
parameters.
( )( ) ( ) ( )
t
p i d
o
de tu t K e t K e d K
dt
0
=
, , ; , ,p i d p i d
ITAE Integral Time Absolute Error
J K K K t e t K K K dt
-
yref
e uy
2
Out2
1
Out1
Transport
Delay
5s+1
0.4
Transfer FcnStep
Product
1
s
Integrator1
1
s
Integrator
Ki
Gain1
Kp
Gain
Clock
|u|
Abs
MATLAB realization – System and cost
function configuration in SIMULINK
Cost function
realization
0
( , ) ; ,p i p iJ K K t e t K K dt
Replace with known, large enough T
• fminsearch (N-M)
• fminunc (BFGS)
-
MATLAB realization – Optimized step
responses
• fminsearch (N-M) the cost function has reached steady-state faster than
• fminunc (BFGS), which still searches for the optimal values.
0 5 10 15 20 25 30 35 40 45 500
0.2
0.4
0.6
0.8
1
1.2
1.4Step responses
fminsearch
fminunc
0 20 40 60 80 100 120 1400
20
40
60
80
100
120
140Cost function values
fminsearch
fminuncfminsearch
fminunc
-
Further comments
• Toolboxes have been used in my courses on
– Automatic Control: Analog and Digital control
– System Identification
– Neuro-Fuzzy Systems
– Wireless Automation
• PiccSim simulator has been developed for
(wireless) networked control systems
– MATLAB/Simulink and ns-2.
– PiccSIM stands for Platform for integrated
communications and control design, simulation,
implementation and modeling.
-
PiccSim
79