matlab 입문 chapter 8 numerical calculus and differential equations
DESCRIPTION
MATLAB 입문 CHAPTER 8 Numerical Calculus and Differential Equations. Numerical Methods for Differential Equations. The Euler Method Consider the equation (8.5-1) r(t) : a known function From the definition of derivative, - PowerPoint PPT PresentationTRANSCRIPT
ACSL, POSTECH 1
MATLAB 입문
CHAPTER 8 Numerical Calculus and Differential Equations
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
2
Numerical Methods for Differential Equations
The Euler Method Consider the equation
(8.5-1) r(t) : a known function
From the definition of derivative,
( is small enough)
(8.5-2)
Use (8.5-2) to replace (8.5-1) by the following approximation:
(8.5-3)
ytrdt
dy)(
t
tytty
dt
dy
t
)()(lim
0
t
t
tytty
dt
dy
)()(
)()()()(
tytrt
tytty
ttytrtytty )()()()(
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
3
Numerical Methods for Differential Equations
Assume that the right side of (8.5-1) remains constant over the time interval . Then equation (8.5-3) can be written in more convenient form as
follows:
(8.5-4) , where
: step size
The Euler method for the general first-order equation is
(8.5-5)
The accuracy of the Euler method can be improved by using a smaller step size. However, very small step sizes require longer runtimes and can result in a large accumulated error because of round-off effects.
),( ttt
ttytrtyty kkkk )()()()( 1 ttt kk 1
t
),(.
ytfy
)](,[)()( 1 kkkk tyttftyty
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
Euler method for the free response of dy/dt = –10y, y(0) = 2 r= -10;
delta = 0.01 ; % tau = 0.1, step size 20% of tau
y(1)=2;
k=0;
for time=[delta:delta:0.5]
k=k+1;
y(k+1) = y(k) + r*y(k)*delta;
end
t=[0:delta:0.5];
y_true = 2*exp(-10*t);
plot(t,y,'o',t,y_true), xlabel('t'), ylabel('y');
4
Euler method solution for the free response of dy/dt –10y, y(0) 2. Figure 8.5–1
8-198-19
Euler method solution of dy/dt sin t, y(0) 0. Figure 8.5–2
8-208-20 More? See pages 490-492.
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
7
Numerical Methods for Differential Equations
The Predictor-Corrector Method The Euler method can have a serious deficiency in problems where the variables
are rapidly changing because the method assumes the variables are constant over time interval .
(8.5-7)
(8.5-8)
Suppose instead we use the average of the right side of (8.5-7) on the interval
.
(8.5-9) , where (8.5-10)
t
),( ytfdt
dy
)](,[)()( 1 kkkk tyttftyty
),( 1kk tt
)(2
)()( 11
kkkk fft
tyty )](,[ kkk tytff
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
8
Numerical Methods for Differential Equations
Let
Euler predictor: (8.5-11)
Trapezoidal corrector: (8.5-12)
This algorithm is sometimes called the modified Euler method. However, note that any algorithm can be tried as a predictor or a corrector.
For purposes of comparison with the Runge-Kutta methods, we can express the modifided Euler method as
(8.5-13)
(8.5-14)
(8.5-15)
)(),(, 11 kkkk tyxtyyth
),(1 kkkk ythfyx
),(),([2 111 kkkkkk ytfytfh
yy
)(2
1
),(
),(
211
12
1
ggyy
gyhthfg
ythfg
kk
kk
kk
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
Modified Euler solution of dy/dt = –10y, y(0) = 2
r= -10; delta = 0.02 ; % same tau as before
y(1)=2;
k=0;
for time=[delta:delta:0.5]
k=k+1;
x(k+1) = y(k) + delta*r*y(k);
y(k+1) = y(k) + (delta/2)*(r*y(k) + r*x(k+1));
end
t=[0:delta:0.5];
y_true = 2*exp(-10*t);
plot(t,y,'o',t,y_true), xlabel('t'), ylabel('y');
9
Modified Euler solution of dy/dt –10y, y(0) 2. Figure 8.5–3
8-218-21
Modified Euler solution of dy/dt sin t, y(0) 0. Figure 8.5–4
8-228-22 More? See pages 493-496.
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
12
Numerical Methods for Differential Equations
Runge-Kutta Methods The second-order Runge-Kutta methods:
(8.5-17) , where : constant weighting factors
(8.5-18)
(8.5-19)
To duplicate the Taylor series through the h2 term, these coefficients must satisfy the following:
(8.5-19)
(8.5-19)
(8.5-19)
22111 gwgwyy kk
),(
),(
2
1
kkk
kk
hfyhthfg
ythfg
21,ww
2
12
1
1
2
1
21
w
w
ww
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
13
Numerical Methods for Differential Equations
The fourth-order Runge-Kutta methods:
(8.5-23)
(8.5-24)
Apply Simpson’s rule for integration
443322111 gwgwgwgwyy kk
])(,[
])(,[
),(
),(
1333332334
1222223
112
1
gggyhthfg
ggyhthfg
gyhthfg
ythfg
kk
kk
kk
kk
0
1
21
21
31
61
3
33
2
21
32
41
ww
ww
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
14
Numerical Methods for Differential Equations
MATLAB ODE Solvers ode23 and ode45 MATLAB provides functions, called solvers, that implement Runge-Kutta methods
with variable step size. The ode23 function uses a combination of second- and third-order Runge-Kutta
methods, whereas ode45 uses a combination of fourth- and fifth-order methods.
<Table 8.5-1> ODE solvers
Solver name Description
ode23 Nonstiff, low-order solver.
ode45 Nonstiff, medium-order solver.
ode113 Nonstiff, variable-order solver.
ode23s Stiff, low-order solver.
ode23t Moderately stiff, trapezoidal-rule solver.
ode23tb Stiff, low-order solver.
ode15s Stiff, variable-order solver.
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
15
Numerical Methods for Differential Equations
Solver Syntax <Table 8.5-2> Basic syntax of ODE solvers
Command Description
[t, y] = ode23( ‘ydot’, tspan, y0) Solves the vector differential equation specified
in function file ydot, whose inputs must be t and y and
whose output must be a column vector representing
; that is, . The number of rows in this column
vector must equal the order of the equation. The vector
tspan contains the starting and ending values of the
independent variable t, and optionally, any intermediate
values of t where the solution is desired. The vector y0
contains . The function file must have two input
arguments t and y even for equations where is not
a function of t. The syntax is identical for the other
solvers.
),(.
ytfy
dtdy
),(.
ytfy
)( 0ty),( ytf
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
Lets try these on the problem we've been solving
16
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
17
Numerical Methods for Differential Equations
<example> Response of an RC Circuit
(RC=0.1s, v(0)=0V, y(0)=2V)
)(tvydt
dyRC
R
c yv
-
+
yy 10.
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.40
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
Time(s)
Capacitor
Voltage
numerical solution
analytical solution
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
18
Numerical Methods for Differential Equations
Effect of Step Size The spacing used by ode23 is smaller than that used by ode45 because ode45
has less truncation error than ode23 and thus can use a larger step size. ode23 is sometimes more useful for plotting the solution because it often gives a
smoother curve.
Numerical Methods and Linear Equations It is sometimes more convenient to use a numerical method to find the solution. Examples of such situations are when the forcing function is a complicated
function or when the order of the differential equation is higher than two.
Use of Global Parameters The global x y z command allows all functions and files using that command to
share the values of the variables x, y, and z.
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
19
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
20
Extension to Higher-Order Equations
The Cauchy form or the state-variable form Consider the second-order equation
(8.6-1)
(8.6-2)
If , , then
)(475...
tfyyy ...
5
7
5
4)(
5
1yytfy
yx 1.
2 yx
21
.
2
2
.
1
5
7
5
4)(
5
1xxtfx
xx
The Cauchy form
or state-variable form
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
21
Extension to Higher-Order Equations<example> Solve (8.6-1) for with the initial conditions .
( Suppose that and use ode45.)
%Define the function
function xdot=example1(t,x)
xdot(1)=x(2);
xdot(2)=(1/5) * (sin(t) - 4*x(1) - 7*x(2));
xdot = [xdot(1) ; xdot(2)];
%then, use it:
[t, x] = ode45('example1',[0,6],[3,9]);
plot(t,x);
60 t 9)0(,3)0(.
yy)sin()( ttf
xdot(1)
xdot(2)
x(1)
x(2)
.
1x.2x
1x
2x
The initial condition for the vector x.
0 1 2 3 4 5 6-4
-2
0
2
4
6
8
10
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
22
Extension to Higher-Order Equations
Matrix Methods
< a mass and spring with viscous surface friction >
y
f(t)m
k
c
)(...
tfkyycym
2
.
1 xx
21
.
2 )(1
xm
cx
m
ktf
mx
( Letting , )yx 1.
2 yx
)(1010
2
1.
2
.
1 tfmx
x
m
c
m
kx
x
(Matrix form))(
.
tf
m
c
m
k10
m
10
2
1
x
x
(Compact form)
(8.6-6)
(8.6-7)
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
23
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
24
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
25
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
26
Extension to Higher-Order Equations
Characteristic Roots from the eig Function
Substituting ,
Cancel the terms
Its roots are s = -6.7321 and s = -3.2679.
21
.
1 3 xxx
21
.
2 7xxx (8.6-8)
(8.6-8)
ststst
ststst
eAeAesA
eAeAesA
212
211
7
3
steAtx 11 )( steAtx 22 )(
ste
0)7(
0)3(
21
21
AsA
AAs02210
71
13 2
sss
s
A nonzero solution will exist for A1 and A2 if and only if the deteminant is zero
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
27
Extension to Higher-Order Equations
MATLAB provides the eig function to compute the characteristic roots. Its syntax is eig(A)
<example> The matrix A for the equations (8.6-8) and (8.6-9) is
To find the time constants, which are the negative reciprocals of the real parts of the roots, you type tau = -1./real (r). The time constants are 0.1485 and 0.3060.
71
13
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
28
Extension to Higher-Order Equations
Programming Detailed Forcing Functions< An armature-controlled dc motor>
v
R L
I
+
-
wKe
iKT Tw
cw
i
cwiKdt
dwI
tvwKRidt
diL
T
e
)(
)(0
1
2
1.
2
.
1 tvLx
x
I
c
I
KL
K
L
R
x
x
T
e
▪ Apply Kirchhoff’s voltage low and Newton’s low
(8.6-10)
(8.6-11)
( matrix form )
: motor’s current , : rotational velocityi w
L: inductance, R: resistance, I: inertia, : torque constant, : back emf constant,
c: viscous damping constant, : applied voltage
TK eK
)(tv
Letting wxix 21 ,
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
29
The following is extra credit (2 pts per problem)
30
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
31
ODE Solvers in the Control System Toolbox
Model Forms The reduced form
The state-model of the same system
)(532...
tfxxx (8.7-1)
2
1
.
2
10
2
3
2
510
x
x
u
(8.7-5)
(8.7-3)
(8.7-4)
(8.7-2)
Both model forms contain the same information. However, each form has its own advantages, depending on the purpose of the analysis.
The specification of the output:
DuCy
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
32
ODE Solvers in the Control System Toolbox
< Table 8.7-1> LTI object functions
Command Description
sys = ss (A, B, C, D) Creates an LTI object in state-space form, where the matrices A, B, C, and D correspond to those in the model .
[A, B, C, D] = ssdata (sys) Extracts the matrices A, B, C, and D corresponding to those in the model
.
sys = tf (right, left) Creates an LTI object in transfer-function form, where the vector right is the vector of coefficients of the right-hand side of the equation, arranged in descending derivative order, and left is the vector of coefficients of the left-hand side of the equation, also arranged in descending derivative order.
[right, left] = tfdata (sys) Extracts the coefficients on the right- and left-hand sides of the reduced-form model.
DuCyu ,.
DuCyu ,.
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
33
ODE Solvers in the Control System Toolbox
ODE Solvers<Table 8.7-2> Basic syntax of the LTI ODE solvers
LTI Viewers: It provides an interactive user interface that allows you to switch between different types of response plots and between the analysis of different systems. The viewer is invoked by typing ltiview.
Command Description
Impulse (sys) Computes and plots the unit-impulse response of the LTI object sys.
Initial (sys, x0) Computes and plots the free response of the LTI object sys given in state-model form, for the initial conditions specified in the vector x0.
lsim (sys, u, t) Computes and plots the response of the LTI object sys to the input specified by the vector u, at the times specified by the vector t.
step (sys) Computes and plots the unit-step response of the LTI object sys.
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
34
ODE Solvers in the Control System Toolbox
Predefined Input Functions< Table 8.7-3 > Predefined input functions
Command Description
[u, t] = gensig (type, period, tf, dt) Generates a periodic input of a specified type, having a period period. The following types are available: sine wave (type = sin), square wave (type = square), and narrow-width periodic pulse (type = pulse). The vector t contains the times, and the vector u contains the input values at those times. All generated inputs have unit amplitudes. The optional parameters tf and dt specify the time duration tf of the input and the spacing dt between the time instants.
sawtooth (t, width) Returns a vector the same length as t corresponding to a sawtooth wave of period 2π. The optional parameter width generates a modified sawtooth wave where width determines the fraction between 0 and 2π at which the maximum occurs (0 ≤ width≤ 1).
square (t, duty) Returns a vector the same length as t corresponding to a square wave of period 2π. The optional parameter duty generates a square wave with a duty cycle specified by duty, which is the percentage of the period over which the function is positive.
stepfun (t, t0) Returns a vector the same length as t with zeros where t < t0 and ones where t≥ t0.
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
35
Advanced Solver Syntax
The odeset Function< Table 8.8-1 > Complete syntax of ODE solvers
Command Description[t, y] = ode23 (‘ydot’, tspan, y0, options, p1, p2, …)
Solves the differential equation specified in the function the ydot, whose inputs must be t and y and whose output must be a column vector representing dy/dt; that is, f(t,y). The number of rows in this column vector must equal the order of the equation. The vector tspan contains the starting and ending values of the independent variable t, and optionally, any intermediate values of t where the solution is desired. The vector y0 contains y(t0). The function file must have two input arguments t and y even for equations where f(t,y) is not a function of t.
The options argument is created with the odeset function, and p1, p2, … are optional parameters that can be passed to the function file ydot every time it is called. If these optional parameters are used, but no options are set, use options = [ ] as a placeholder.
The function file ydot can take additional input arguments. It has the form ydot (t, y, flag, p1, p2, …), where flag is an argument that notifies the function ydot that the solver is expecting a specific kind of information. The syntax for all the solvers is identical to that of ode23.
Options = odeset (‘name1’, ‘value1’, ‘name2’, ‘value2’, …)
Creates an integrator options structure options to be used with the ODE solver, in which the named properties have the specified values, where name is the name of a property and value is the value to be assigned to the property. Any unspecified properties have default values. odeset with no input arguments displays all propery names and their possible values.
),(.
ytfy
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
36
Advanced Solver Syntax
Stiff Differential Equations A stiff differential equation is one whose response changes rapidly over a time
scale that is short compared to the time scale over which we are interested in the solution.
A small step size is needed to solve for the rapid changes, but many steps are needed to obtain the solution over the longer time interval, and thus a large error might accumulate.
The four solvers specifically designed to handle stiff equations: ode15s (a variable-order method), ode23s (a low-order method), ode23tb (another low-order method), ode23t (a trapezoidal method).
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
37
Acsl, PostechMATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations
38