MATLAB 입문 CHAPTER 8 Numerical Calculus and Differential Equations ACSL, POSTECH 1 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Numerical Methods for Differential Equations The Euler Method Consider the equation dy r (t ) y dt (8.5-1) From the definition of derivative, dy y(t t ) y(t ) lim dt t 0 t r(t) : a known function ( t is small enough) dy y (t t ) y (t ) dt t (8.5-2) Use (8.5-2) to replace (8.5-1) by the following approximation: y(t t ) y (t ) r (t ) y (t ) t y (t t ) y (t ) r (t ) y (t )t (8.5-3) 2 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Numerical Methods for Differential Equations Assume that the right side of (8.5-1) remains constant over the time interval (t , t t ) . Then equation (8.5-3) can be written in more convenient form as follows: y(tk 1 ) y(tk ) r (tk ) y(tk )t (8.5-4) , where tk 1 tk t t : step size . The Euler method for the general first-order equation y f (t , y) is y(tk 1 ) y(tk ) tf [tk , y(tk )] (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. 3 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech Euler method for the free response of dy/dt = –10y, y(0) = 2 r= -10; deltaT = 0.01 ; % step size t=[0:deltaT:0.5]; % our vector of time values y(1)=2; % initial value at t=0 for k=1: length(t)-1 % for every value in t vector y(k+1) = y(k) + r*y(k)*deltaT; end 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-19 Euler method solution of dy/dt sin t, y(0) 0. Figure 8.5–2 8-20 More? See pages 490-492. Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations 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 t . dy f (t , y ) (8.5-7) dt y(tk 1 ) y(tk ) tf [tk , y(tk )] (8.5-8) Suppose instead we use the average of the right side of (8.5-7) on the interval (tk , tk 1 ) . y (t k 1 ) y (t k ) t ( f k f k 1 ) 2 (8.5-9) , where f k f [tk , y(tk )] (8.5-10) 7 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Numerical Methods for Differential Equations Let h t, yk y (tk ), yk 1 y (tk 1 ) Euler predictor: yk 1 yk hf (tk , yk ) h y y [ f (tk , yk ) f (tk 1 , yk 1 )] Trapezoidal corrector: k 1 k 2 (8.5-11) (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 g1 hf (t k , yk ) g 2 hf (t k h, yk g1 ) yk 1 yk 1 ( g1 g 2 ) 2 (8.5-13) g1 is deltaY given by f(tk,yk) (8.5-14) g2 is deltaY given by f(tk+1,yk+1) (8.5-15) 8 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech Modified Euler solution of dy/dt = –10y, y(0) = 2 r= -10; deltaT = 0.01 ; % same step size as before y(1)=2; t=[0:deltaT:0.5]; % our vector of time values for k=1:length(t)-1 g1 = deltaT*r*y(k); % estimate of deltaY at t(k),y(k) g2 = deltaT*r*(y(k) + g1); % est of deltaY at t(k+1),y(k+1) y(k+1) = y(k) + 0.5*(g1 + g2); end 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-21 Modified Euler solution of dy/dt sin t, y(0) 0. Figure 8.5–4 8-22 More? See pages 493-496. MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech Numerical Methods for Differential Equations Runge-Kutta Methods The second-order Runge-Kutta methods: yk 1 yk w1 g1 w2 g 2 g1 hf (tk , yk ) g 2 hf (tk h, yk hf k ) (8.5-17) , where w1 , w2 : constant weighting factors (8.5-18) (8.5-19) To duplicate the Taylor series through the h2 term, these coefficients must satisfy the following: w1 w2 1 1 2 1 w2 2 w1 (8.5-19) (8.5-19) (8.5-19) 12 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Numerical Methods for Differential Equations The fourth-order Runge-Kutta methods: yk 1 yk w1 g1 w2 g2 w3 g3 w4 g4 (8.5-23) g1 hf (t k , yk ) g 2 hf (t k h, yk 1 g1 ) g 3 hf [t k 2 h, yk 2 g 2 ( 2 2 ) g1 ] g 4 hf [t k 3h, yk 3 g 2 3 g 3 ( 3 3 3 ) g1 ] (8.5-24) w1 w4 1 6 w2 w3 1 3 1 2 1 2 2 1 2 3 3 1 3 0 Apply Simpson’s rule for integration 13 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech 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 ode23 ode45 ode113 ode23s ode23t ode23tb ode15s Description Nonstiff, low-order solver. Nonstiff, medium-order solver. Nonstiff, variable-order solver. Stiff, low-order solver. Moderately stiff, trapezoidal-rule solver. Stiff, low-order solver. Stiff, variable-order solver. 14 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech Stiff? 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 loworder method), ode23t (a trapezoidal method). 15 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations 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 y f (t , y) specified . in function file ydot, whose inputs must be t and y and whose output must be a column vector representing dy dt . ; that is, y 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 syntax is identical for the other solvers. 16 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech Lets try these on the problem we've been solving 17 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Numerical Methods for Differential Equations <example> Response of an RC Circuit R + v c y 2 1.8 . y 10 y (RC=0.1s, v(0)=0V, y(0)=2V) numerical solution analytical solution 1.6 1.4 Capacitor Voltage dy RC y v(t ) dt 1.2 1 0.8 0.6 0.4 0.2 0 0 0.05 0.1 0.15 0.2 Time(s) 0.25 0.3 0.35 0.4 18 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech 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. 19 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech 20 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech Extension to Higher-Order Equations The Cauchy form or the state-variable form Consider the second-order equation .. . 5 y 7 y 4 y f (t ) .. y If 1 4 7 . f (t ) y y 5 5 5 x1 y (8.6-1) (8.6-2) . , x2 y , then . x1 x2 1 4 7 x2 f (t ) x1 x2 5 5 5 . The Cauchy form or state-variable form 21 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Extension to Higher-Order Equations . <example> Solve (8.6-1) for 0 t 6 with the initial conditions y(0) 3, y(0) 9 . ( Suppose that f (t ) sin( t ) 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)]; The time interval of interest %then, use it: [t, x] = ode45('example1',[0,6],[3,9]); plot(t,x); The initial condition for the vector x. 10 8 6 4 2 0 -2 . xdot(1) xdot(2) x(1) x(2) x.1 x2 x1 x2 -4 0 1 2 3 4 5 6 22 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Extension to Higher-Order Equations Matrix Methods < a mass and spring with viscous surface friction > y .. . m y c y ky f (t ) (8.6-6) . f(t) x1 x2 k ( Letting m . x2 c 0 x.1 k x m 2 . , x2 y ) 1 k c f (t ) x1 x2 m m m (Matrix form) . x1 y . (Compact form) 1 x 0 c 1 1 f (t ) x2 m m f (t ) 0 k m 1 c m (8.6-7) 0 1 m x 1 x2 23 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech 24 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech 25 The following is extra credit (2 pts) 26 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech Extension to Higher-Order Equations Characteristic Roots from the eig Function . x1 3x1 x2 (8.6-8) x2 x1 7x2 (8.6-8) . Substituting x1 (t ) A1e st , x2 (t ) A2e st sA1e st 3 A1e st A2e st sA2e st A1e st 7 A2e st Cancel the e st A nonzero solution will exist for A1 and A2 if and only if the deteminant is zero terms ( s 3) A1 A2 0 A1 ( s 7) A2 0 s 3 1 s 2 10s 22 0 1 s7 Its roots are s = -6.7321 and s = -3.2679. 27 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech 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 3 1 1 7 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. 28 Acsl, Postech MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Extension to Higher-Order Equations Programming Detailed Forcing Functions < An armature-controlled dc motor> R ▪ Apply Kirchhoff’s voltage low and Newton’s low L di Ri K e w v(t ) dt dw I KT i cw dt L i cw + v I Kew i - T KT i Letting w x1 i, x2 w : motor’s current , (8.6-11) : rotational velocity ( matrix form ) R . x.1 L x KT 2 I L: inductance, R: resistance, I: inertia, KT : torque constant, c: viscous damping constant, w (8.6-10) Ke Ke 1 L x1 v(t ) c x2 L 0 I : back emf constant, v(t ) : applied voltage 29 MATLAB 입문 : Chapter 8. Numerical Calculus and Differential Equations Acsl, Postech 30