Professional Documents
Culture Documents
Simulation
Prof.Ing.Petr Noskievi, CSc.
[xn, fn]
x
[xi, fi]
x
(x)
[x1, f1]
[x0, f0]
x
x
[xi+1, fi+1]
y = f(x) (x) = ?
Approximation
Aproximac funkce f(x)
Error of the approximation
E( x) = f ( x) ( x)
n
1
2
2n ( f , ) = f (x i ) ( x i ) i
i =1
Weighted criterion
Linear regression
pro x a, b
( x) = a + b x
1
2 2
2n ( f , ) = f ( xi ) (a + b xi )
i =1
]2
S = f (xi ) (a + b xi )
i =1
S
= 0.
b
Linear regression
n
n
n
S
= 2 yi (a + bxi ) ( 1) = 2 y i n a + b xi
a
i =1
i =1
i =1
n
n
n
n
S
= 2 yi (a + bxi ) ( xi ) = 2 x i y i a xi b x2i
b
i =1
i =1
i =1
i =1
System of
linear
equations
i =1
i =1
n a + b x i = f ( x i )
n
a x i + b
i =1
xi f ( xi )
i =1
b=
1 n
xi f ( xi ) n xi f ( xi )
i =1 i =1
i =1
i =1
x2i =
n
2 1
xi n xi
i =1
i =1
a = y b x
1 n
x = xi ,
n i =1
1 n
y = f ( xi )
n i =1
Linear regression
n
Correlation coefficient:
A measure for the
quality of the
approximation
i =1
i =1
n x i f ( x i ) x i f ( x i )
rxy =
i =1
n
n
n
n
2
n x2i xi n f 2 ( xi ) f ( xi )
i =1
i =1
i =1
i =1
Linear Regression
Multiple regression:
Polynomial regression:
[ (
Criterion:
i =1
)]
[ (
)]
a0
i=0
xik + a1
i=0
xik +1 + ... + a n
i=0
xik + n =
yi xik
i=0
(xi ) = f (xi ),
for i = 0, 1, 2, ..., n
n
Ln (x ) = f (xi ) li (x )
i =0
pro k i,
li (x k ) = 1
pro k = i.
li ( x) =
i = 0, 1, 2, ..., n
a0
= f ( x0 )
a 0 + a1( x1 x0 )
= f ( x1)
a 0 + a1( x2 x0 ) + a 2 ( x2 x0 )( x2 x1)
= f ( x2 )
.
.
.
a1 =
f (x1) a 0
x1 x0
Spline function
The given set of points is
interpolated using the
series of the of the unique
cubic polynomials.
Between each of two given
points the cubic polynomial
has another set of
coefficients. The whole
interpolation is done using
the set of the interpolating
functions called spline
function.
y = f(x)
(x)
y = f(x) {(x)}
i = 0, 1, ..., n-1
fn-1 fn
i(x) = ai+bi(x-xi)+ci(x-xi)2+di(x-xi)3
fn-2
fi+1
f1
f2
fi
x0 x1
x2
xi xi+1
f0
xn-2
xn-1 xn
i ( x) = a i + bi (x xi ) + ci (x xi ) + d i (x xi )
Cubis spline-function
Each cubic polynomial has degree 3 and is described by the set of four
coefficients: ai, bi, ci, di. For n intervals it is to design 4n coefficients.
Spline function
The set of n+1 inperpolation conditions will be used for the derivation of the coefficients.
( xi ) = f ( xi ),
i = 0, 1, ..., n
The spline function fullfils 3(n-1) conditions of the continuity of the cubic
polynomials in form:
i 1(xi ) = i (xi ),
i = 1, 2, ...n -1
i1 (xi ) = i(xi ),
i1 (xi ) = i(xi ),
i = 1, 2, ...n -1
i = 1, 2, ...n -1
Now we have 4n-2 conditions for 4n coefficients, which values should be derived.
The last two conditions can be set as following condition in the end points :
First derivative:
a)
(x0 ) = f0
(x n ) = fn
Second derivative:
b)
(x0 ) = f 0
(xn ) = f n
c)
(x0 ) = 0
(xn ) = 0
Spline function
The derivatives of the polynomials i(x) are continuos functions.
i ( x) = bi + 2ci (x xi ) + 3d i (x xi )
i( x) = 2ci + 6d i (x xi )
(xi ) = M i ,
i = 0, 1, ..., n
hi = xi +1 xi
i = 0, 1, ..., n
i(xi ) = 2ci ,
resp. 2ci = Mi
resp. 2ci + 6d i hi = Mi +1
Mi
2
di =
M i +1 M i
6h i
ai = fi
Spline function
Next for x=xi+1: i (xi +1) = a i + bi hi + ci h2i + d i h3i
fi +1 = a i + bi hi + ci h2i + d i h3i
After substitution for the coefficients ai, ci, di we obtain the expression for the
coefficient bi:
f f 2M + M
bi =
i +1
hi
i +1
hi
The equations for the coefficient of the polynomials expressed using Mi for the
second derivative: a = f (x )
i
bi =
f ( x i +1 ) f ( x i )
hi
Mi
2
M i+1 M i
di =
6h i
ci =
2 M i + M i +1
hi
6
Spline function
The calculation of the second derivatives Mi is based on the use of the
condition for the continuity of the first derivative :
i1 (x i ) =
f i f i1 1
1
+ M i1h i1 + M i h i1
h i1
6
3
1 (x i ) =
f i +1 f i 2M i + M i +1
+
hi
hi
6
The equation must be fullfiled in the inner given points: for i = 1, 2, ..., n-1 :
f i+1 f i f i f i1
1
1
1
M i1h i1 + (h i1 + h i )M i + M i+1h i =
6
3
6
hi
h i1
2( h 0 + h 1 )
h1
.
h1
2( h 1 + h 2 ) h 2
.
.
.
.
.
0
.
.
.
.
h i 1 2( h i 1 + h i ) h i
.
.
.
.
.
.
.
.
h n2
f 2 f1 f1 f 0
6
M 0 h 0
h
h 0
M 1 1
0
f
f
3
2
2 f1
M
.
h 1
h 2
.
.
.
.
.
.
.
M i = f i +1 f i f i f i 1
h i 1
.
. h i
.
.
.
.
.
.
.
2( h n 2 + h n 1 ) M n 1
6 f n f n 1 f n 1 f n 2
h n 1
h n 2
Spline function
The calculation of the spline function can be described in the next steps:
1. Input data: n, x0, x1, ..., xn, f(x0), f(x1), ..., f(xn).
2. calculation hi = xi+1 - xi, pro i = 0, 1, ..., n-1.
3. Creating of the systm of the linear algebraic equations, settinf of M0, Mn.
4. Calculation Mi, i = 1, 2, ..., n-1 solution of the equations.
5. Calculation of the coefficients in the for each interval.
The calculation of the value of the spline function in the given point is realized in
two steps:
1. The interval (index i) in which the value x is located.
2. Calculation of the of the cubic function i (x) for given value x.
If also the values of the first derivative fi(xi) are given, the spline function
is called Hermits spline function and in the given points should be also
fulfilled the condition:
f i = i
Next applications
y = f(x)
y = f(x)
y = f(x)
x
periodick kivka
Periodic function
x
smyka
General curve in 2D
x
uzaven kivka
Closed figure
yn = y0
y0
x0
0 (x0 ) = n 1(xn )
0 (x0 ) = n 1(x n )
0(x0 ) = n 1(x n )
x1
yn
x2
.
.
h0
.
0
h n 1
.
.
.
2( h 0 + h1) h1
.
.
.
.
hn2
M 0 6 y1 y 0 y n y n 1
h
h n 1
0
.
.
.
.
.
.
.
y
y
y
1
M =
.
6 2
1 0
1
h1
h 0
.
.
.
.
.
. y y
y
n
n 1
n 1 n 2
2( h n 2 + h n 1) M n 1 6 h
hn2
n 1
h n 1
Curve parameterization
Let we have a set of given points [xi, yi] and the variable x is not
monotonously growing up for growing index i, that means, it is not valid:
x n > x n 1 > x n 2 > ... > x2 > x1 > x0
For the derivation of the spline we divide th set of points into the two sets:
Curve parameterization
t0 = 0
t i +1 = t i + d i ,
i = 0, 1, 2, ..., n -1
The parameterization can be done using one of the following formulas for the
difference (distance) between two points di:
di =
(xi+1 xi )2 + (yi+1 yi )2
2
d i = ( xi +1 xi ) + ( yi +1 yi )
di = xi +1 xi + yi +1 yi
d i = max xi +1 xi , yi +1 yi , i = 0, 1, 2, ..., n -1
6
x
[x1, y1]
x
[x0, y0]
x
x
x
x
x
3=8
[x3, y3] = [x8, y8]
10
8x
x 6
10 x
x
0 = 11
x 4
1
x 3
x
It is valid :
x 0 = x n , y0 = y n
Numerical Integration
Methods
The numerical methods of integration are based on the use of approximation
methods. The given function which should be integrated f(x) is substituted by
their approximation (x). The integration is calculated for this function:
b
f ( x)dx
( x)dx + R
y = f(x)
(x) = f(xi)
n 1
f ( x)dx h f (xi ) + R
i= 0
R=
ba
f ( x n ) f ( x0 )
n
n 1
f ( x)dx
a
h f xi + + R
2
i=0
ba 2
h max f ( x)
24
a, b
y0
y1 yi-1
x0=a x1
xi-1
yi yi+1
xi
xi+1
yn-2 yn-1
xn-2
yn
xn-1 xn=b x
y = f(x)
i(x)
y0
yi yi-1
x0=a xi
xi+1
After integration:
x i+1
f (x)dx
xi
h
f ( x i ) + f ( x i +1 )
2
n 1
1
1
ba 2
h max f ( x)
12
a, b
yn-2 yn-1
xn-2
yn
xn-1 xn=b x
xi
x i+1
xi
f ( x)dx
a
y = f(x)
x i+2
f (x)dx
f (x)dx
i(x)
x xi 2 fi ( x xi )( x xi +1)
dx
fi + fi h + 2
2
h
xi
yi
h
[ f + 4 fi + 1 + fi + 2 ]
3 i
xi-1
h
f + 4 f1 + 2 f2 + 4 f3 + ... + 2fn-2 + 4 fn 1 + fn + R
3 0
xi
yi+1 yi+2
xi+1 xi+2
xn
ba 4
( )
h max f 4 ( x)
180
a, b
By this method the number n must be even number, that means, that the
function f(x) must be given in odd number of points, in n+1points .
[
]
+ f [ x0 , x1, ..., x n ](x x0 )(x x1) ... (x x n-1).
dk
dx k
dk
dx k
f ( x + h) f ( x) 1
hM 2
h
2
f ( x)
f ( x) f ( x h) 1
+ hM 2
h
2
f ( x)
f ( x + h) f ( x h) 1 2
+ h M3
2h
6
f ( x)
- 3f ( x) + 4 f ( x + h) f ( x + 2 h) 1 2
+ h M3
2h
3
f ( x)
3f ( x) 4 f ( x h) + f ( x 2 h) 1 2
+ h M3
2h
3
f ( x)
f ( x)
f ( x + 2h) 2 f ( x + h) + f ( x)
h2
f ( x + h) 2 f ( x) + f ( x h)
hM3
h2
f ( x) 2 f ( x h) + f ( x 2 h)
h2
h2 M 4
12
+ hM 3
Mi = max f ( i) ( x)
a, b
f ( x)
f ( x)
) (
) + 0(h2 )
( )
+ 0 h4
F: i ( x) = a i + bi (x xi ) + ci (x xi ) + d i (x xi )
i = 0, 1, 2, ..., n -1
Derivation:
2
F: i ( x) = bi + 2ci (x xi ) + 3d i (x xi )
i = 0, 1, 2, ..., n -1
with initial condition y0 = y(t) for time t from the interval <t0, tk>.
Where ti is from the mentioned interval. The value hi is a step of the numerical
solution:
hi = t i +1 t i ,
i = 0, 1, 2, ...
The values of the independent variable ti, at which the numerical solution of
the differential equation is calculated, define so called node points of the
solution.
yi
yi+1
t
hi=ti+1-ti
E L ( t i ) = ET ( t i ) + E R ( t i )
ET(ti) - error of the method - trunsaction error, ER(ti) - rounding error - this
error is caused by the limited length of the word in the computer (given by the
number of the decimals used by the numerical computation).
The value of the transaction error is proportional to the power of the step h, for
example h2 , like 0(h2). That means, that the error ET is order hi. The error in one
step of the solution influences the solution in the next steps. The inaccuracy of
result in the next i steps is characterized by the accumulative error global
error.
i = Y(t i ) y(t i ) ,
where Y(ti) is the exact accurate solution at the point ti.
One-step Methods
Corrector methods
(implicit methods)
Predictor methods
(explicit methods)
Combined methods
One-Step Methods
One step method can be expressed in the form:
y i +1 = y i + h ( t i , y i , h )
The expression h(ti, yi, h) defines the increment of the solution between the
nodes i a i+1
We express the increment of the solution using the Taylor series in
dependence on the step h and its power h = ti+1 - ti (fixed step h) as follows:
h2
h n ( n1)
yi +1 yi = h f ( t i , yi ) + f ( t i , yi ) + ... + f
t i , yi ) + 0 h n +1
(
2!
n!
( )
The computation of the increment using the Taylor series needs the calculation of the derivatives
higher order of the function f(t, y) what is difficult to express or calculate. It is also assumed that the
function f(t, y) is differentiable.
The method which calculated the increment of the solution from the Taylor series is called Taylor series
method. Because of the need of the calculation of the derivatives higher order this approach is
practically not used.
This approach is the basis for the derivation of the One Step Methods like Eulers method or Runge Kutta method.
Eulers method
The Eulers method uses only the fist term of the Taylor series:
y i +1 = y i + h f ( t i , y i )
yi+1
yi
yi
h2
h3
ET =
f (ti , yi ) +
f (ti , yi ) + ...
2!
3!
ti
hi
ti+1
y i +1 = y i + h w jk j
j=1
The increments kj are calculated between the nodes ti a ti+1 using the
expressions:
k1 = f (t i , yi )
k j = f t i + jh, yi + jhk j1
j = 2, 3, ..., r.
The variables wj, j, j are the constant parameters. Their values assure
that the numerical solution of the differential equation in the node ti+1 is
equal to the value which can be calculated from the r-terms of the Taylor
series in the node ti .
h r +1 (r )
f ( , y ),
(r + 1)!
ti , t i + h
This expression is not possible to use for the practical calculation of the
estimated value of the transaction error. For that reason the following approach
based on the half step is very frequently used.
Half-step method for the estimation of the occurring error:
1. The solution y(ti+h) in the next node ti+1 is computed using the chosen
one-step method with the step length h.
2. We repeat the calculation with the half step h = h/2 and calculate the
solutions y(ti+h) and y(ti+2h), (two times use of the method).
3. If the difference between the values of the solutions y(ti+h) and y(ti
+2h) is less than the accepted maximal error we continue in the
solution with the step h. Otherwise with the half step.
y
y(ti+h/2)
y(ti)
ti
y(ti+2*h/2)
ti+2*h/2
ti+h/2
y i +1 = y i + h i k 2
k1 = f ( t i , y i )
h
h
k 2 = f t i + i , yi + i k1
2
2
Heuns method
k +k
yi +1 = yi + hi 1 2
2
k1 = f ( t i , y i )
k 2 = f (t i +1, yi + hi k1)
k + 2k 2 + 2k 3 + k 4
yi +1 = yi + hi 1
6
k1 = f ( t i , y i )
h
h
k 2 = f t i + i , yi + i k1
2
2
h
h
k 3 = f t i + i , yi + i k 2
2
2
k4 = f (ti +1 , yi + hi k3 )
The simple change of the step of the solution is the advantage of the Runge-Kutta
Methods. It is possible to change the step in dependence on the estimated value of
the error of the solution. The disadvantage is the multiple calculation of the function
f(t, y) in dependence on the order of the method. If this calculation is enough fast, it
is suitable to do the whole solution using the Runge-Kutta method. If the function f(t,
y) is more difficult and the computation of their value takes more time, it is
recommendable to start the solution with the Runge-Kutta method and continue
with the multi-step method.
Multi-step Methods
Let we assume that we know the solution of the differential equation
y = f ( t , y),
y0 = y(t 0 )
in k+1 nodes
yi = y( t i ), yi 1 = y( t i 1), ..., yi k = y( t i k )
t i +1
f (t , y)dt
ti
It is impossible to calculate the integral at the interval ti, ti+1 , because we dont know the the solution y.
but we know the solution in the previous nodes. The multistep methods are based on the approximation
of the function f(t, y) using the interpolation polynomial p(t), which is defined by the values f(ti, yi), f(ti-1,
yi-1), ..., f(ti-k, yi-k), and on the approximation of the integral from f(t,y) by the integral from the p(t,y). Then
we can write:
y( t i +1) = y( t i ) +
t i +1
p(t, y)dt + ET
ti
Multi-step methods
Because of the use the polynomial p(t, y) for the computation of the integral
at the interval ti, ti+1 outside the given values, we will use the polynomial for
the extrapolation of the function f(t, y). The formulas of the methods for the
numerical solution of the differential equations derived in this way are called
explicit methods and are expressed in explicit forms.
If we use by the derivation of the interpolation polynomial p(t, y) the values
f(ti+1, yi+1), f(ti, yi), f(ti-1, yi-1), f(tik, yi-k) we obtain for the computation of the
solution y the interpolation formula and so called implicit formula. Because
of the computation of the value of the solution yi+1 from the value f(ti+1, yi+1)
the derived formula is implicit.
The order of the multistep methods is given by the order of the polynomial
p(t).
Multi-step methods
The explicit methods Adams-Bashforth methods (A-B), predictor formulas
Derivation of the two steps method A-B
Let we replace the function f(t, y) in the equation for the integral
increment of the solution by the Newton interpolation:
f ( t , y) p(t i , yi ) = f (t i , yi ) +
f (t i-1, yi-1)
(t t i )
h
After substitution into the formula for the numerical solution of the differential
equation we obtain:
t i +1
f
yi +1 = yi + fi + i 1 ( t t i )dt
h
ti
y i +1 = y i + f i ( t i + 1 t i ) +
fi 1
2
t i +1 t i )
(
2h
The final expression of the explicit method 2nd order we obtain after the
substitution h = ti+1 - ti and fi-1 = fi - fi-1 and transformation:
y i +1 = y i +
h
(3f f )
2 i i 1
Multi-step methods
The explicit multistep method A-B order k+1 can be expressed by the formula:
k
y i +1 = y i + h b kj fi j
j= 0
b0j
b1j
b2j
b3j
3
2
1
2
16
12
5
12
59
24
37
24
23
12
55
24
9
24
Multi-step methods
Implicit methods (corrector methods)
The implicit methods are described using the implicit formulas and are also
called Adams - Moulton methods or A-M methods.
The implicit A-M method order k can be expressed using the formula:
k
yi +1 = y i + h b kj fi +1 j
j= 0
The values of the coefficients bkj of the implicit methods A-M for k = 0, 1, 2, 3
are summarized in the table:
j
b0j
b1j
1
2
1
2
5
12
8
12
b2j
b3j
9
24
19
24
1
12
5
24
1
24
Multi-step methods
Predictor corrector methods
The introduced multistep methods are not mostly used stand-alone but in
combination as the predictor-corrector methods. Using the predictor
method the next value of the solution is calculated. The result is used for
the evaluation of the corrector formula which gives the value of the solution.
It is important to use the predictor and corrector formulas same order. The
local errors of both formulas are same order).
The estimation of the local error of the predictor-corrector methods can be
done using the formula:
P
C
If the value of the error is less than the maximal error, we use the value as the
solution at the point ti+1 . If not, the value of the corrector formula is used as a
prediction of the solution and is used for the second evaluation of the corrector
formula and computation of the new value of the solution in the point ti+1 . The
error is estimated in the mentioned way and the described procedure can be
repeted again.
x! = f (x, t),
Eulers method
x i +1 = x i + h f t i , x i
k1 = f t i , x i
Heuns method
h
h
k 2 = f t i + i , xi + i k1
2
2
h
xi +1 = xi + i k1 + k 2
2
k1 = f t i , x i
k1 = f t i , x i
k 2 = f t i +1 , x i + h i k 1
h
xi +1 = xi + i k1 + 2 k 2 + k 3 + k 4
6
h
h
k 2 = f t i + i , xi + i k1
2
2
h
h
k 3 = f t i + i , xi + i k 2
2
2
k 4 = f t i +1, xi + hi k 3
j = 1, 2, ..., n
xi +1 = xi + h b kj f t i j , xi j
j= 0
x i +1 = x i + h k kj f t i +1 j , x i +1 j
j= 0
The accuracy is estimated in the same way like by the solution of one equation.
The solution of each equation must full fill the condition.
y! = y
Im (h)
y i +1 = y i + h y i
yi +1 = (1 + h ) yi
-2
Re (h)
-1
y i +1 y i
-1
1+ h 1
2 h2
p h p
+ ... +
1
2!
p!
Im( h)
RK 4..
2
RK 2..
RK 3..
1
RK 1..
-3
-2
-1
Re(h)
A-B 2..
A-B 3..
A-B 4..
A-B 5..
Re(h)
-2
-1
A-M 2..
0
A-M 3..
A-M 4..
A-M 5..
min Re i
i
T
= min
Tmax
It is suitable to solve the stiff systems using the numerical methods which have
the stability area the whole left half plane of the complex plane : Re h < 0 .
These methods are called A-stabil methods :
1. The explicit multistep method cannot be the A - stable method.
2. The highest order of the implicit multistep method is 2, the smallest local
error has the trapezium method.
i +1
= y + hf y
i
i +1
, t i+1
1
1
1 h
Im (h)
-1
Re (h)
i +1
=y +
i
h
f ( yi , t i ) + f ( yi +1, t i+1)
2
1
h
2
1
1
1 h
2
1+
The trapezium and implicit Eulers methods need thepredicted values of the
slution. These can be calculated from using the direct Eulers method or other
explicit method.
Example
It is to create a state model of the system shown in Figure. Next do the
analysis of the use of the numerical methods for the solution of the differential
equations for the given values of the model parameters.
m = 1 kg
k = 100 Nm-1
b = 1000 Nsm-1
b
Initial conditions:
x0 = 0
x0 = 1 ms-1
x, x
Solution
The state space model has form:
d x1 0
=
dt x2 k
m
1 x
1
b x2
m
x1(0) 0
x(0) =
=
x2 (0) 1
d x1 0
=
dt x 2 100
x1
1000 x 2
det(A I) = 0
1 1 0
0
det
= 0
100 1000 0 1
2 + 1000 + 100 = 0
1 = 0,1
2 = 999,899
999,899
0,1
=! 104
The ratio of the stiff system is order 4 (104 ), that means the model represents
the stiff system.
Solution
Analysis of the numerical solution:
In the case of the real eigen values the following condition must be full filled:
max h 2
2
999,899
h 0,002
h
2,7
999,899
h 0,0027
h
Because of the numerical stability it is to use very short step. Next the achieving
of the given accuracy will need to use more shorter step. The use of the A- stabil
method is suitable. The whole left half plane is a stable area for these methods.