Chapter 4 Numerical Solution of Initial-Value Problems 4.3 Runge-Kutta Method The Runge-Kutta Method is an explicit method that achieves the accuracy of a Taylor series approach without requiring the calculation of higher derivatives. The general form of RungeKutta method can be written as yn+1 = yn + (xn, yn, h)h (4.3-1) where (xn, yn, h) is the increment function that represents an average slope over the interval h. (xn, yn, h)h = a1k1 + a2k2 + + amkm (4.3-2) In the above expression k1/h is the slope at x1, k2/h is the slope at x2, and km/h is the slope at xm for which x1 < x2 < xm x1 + h. With this definition, the first order Runge-Kutta method is just Euler’s method. (xn, yn, h)h = a1k1 = (1)h f(xn, yn) where a1 = 1 and k1 = h f(xn, yn) The second order Runge-Kutta method is written as yn+1 = yn + ak1 + bk2 (4.3-3) where k1 = h f(xn, yn) and k2 = h f(xn + h, yn+ k1). The four constants a, b, , and can be determined by comparing equation (4.3-3) with Taylor’s method of second order. yn+1 = yn + f(xn, yn)h + where f’(xn, yn) = Therefore 1 f’(xn, yn)h2 2! df f dx f f dy f dy = + = + dx x dx x y dx y dx yn+1 = yn + f(xn, yn)h + 1 f f dy 2 [ + ]h 2! x y dx From Taylor series expansion of a function with two variables g(x + r, y + s) = g(x, y) + r g g +s + x y 4-4 (4.3-4) The constant k2 = h f(xn + h, yn+ h) can be expressed as k2 = h{ f(xn, yn) + h f f + k1 + O(h2) } x y Substitutions of k1 and k2 into yn+1 = yn + ak1 + bk2 yields yn+1 = yn + a hf(xn, yn) + b hf(xn, yn) + bh2 f f + b h2f(xn, yn) + O(h3) x y The equation can be rearranged to yn+1 = yn + [a + b] hf(xn, yn) + [b f f + bf(xn, yn) ]h2 + O(h3) x y (4.3-5) Compare equation (4.3-5) with equation (4.3-4) yn+1 = yn + f(xn, yn)h + 1 f f dy 2 [ + ]h 2 x y dx (4.3-4) We obtain a+b=1 1 b = 2 1 b = 2 Since there are only three equations for four unknowns a, b, , and , we have infinite number of second order Runge-Kutta methods depending on the constants chosen. Solving for a, , and in terms of b we obtain a = 1 b, = 1 1 , and = 2b 2b With different set of constants, the second order Runge-Kutta method is known with different name. Modified Euler method: b= 1 1 ,a= ,==1 2 2 yn+1 = yn + ak1 + bk2= yn + 0.5k1 + 0.5k2 yn+1 = yn + 0.5h [ f(xn, yn) + h f(xn + h, yn+ hf(xn, yn))] 4-5 Midpoint method: b = 1, a = 0, = = 0.5 yn+1 = yn + ak1 + bk2 = yn + k2 = yn + h[f(xn + 0.5h, yn+ 0.5h f(xn, yn))] Heun’s method: b= 3 1 2 ,a= ,== 4 4 3 yn+1 = yn + ak1 + bk2 = yn + 1 3 k1 + k2 4 4 k1 = h f(xn, yn) , k2 = h f(xn + h, yn+ k1) = h f(xn + 2 2 h, yn+ k1) 3 3 Example 4.3-1 _____________________________________________________ Solve the following ordinary differential equation (ODE) using various Runge-Kutta method of order 2 with x = 0.2 dy = x + y0.5, at x = 1, y = 2 dx Solution Modified Euler method: yn+1 = yn + 0.5h [ f(xn, yn) + h f(xn + h, yn+ hf(xn, yn))] f(xn, yn) = 1 + 20.5 = 2.4142 yn+ hf(xn, yn) = 2 + (0.2)(2.4142) = 2.4828 f(xn + h, yn+ hf(xn, yn)) = 2.7757 yn+1 = 2 + (0.5)(0.2)[ 2.4142 + 2.7757] = 2.5190 Midpoint method: yn+1 = yn + h[f(xn + 0.5h, yn+ 0.5h f(xn, yn))] f(xn, yn) = 1 + 20.5 = 2.4142, f(xn + 0.5h, yn+ 0.5h f(xn, yn) = 2.5971 yn+1 = 2 + (0.2)(2.5971) = 2.5194 Heun’s method: yn+1 = yn + 0.25k1 + 0.75k2 k1 = h f(xn, yn) = (0.2)( 2.4142) k2 = h f(xn + 2 2 h, yn+ k1) = (0.2)(2.6471) 3 3 yn+1 = 2 + (0.2)[ (0.25)(2.4142) + (0.75)(2.6471)] = 2.5193 4-6 Fourth order Runge-Kutta method for yn+1 = yn + dy = f(x,y) dx 1 (k1 + 2k2 + 2k3 + k4) 6 where k1 = hf(xn, yn) k3 = hf(xn + 0.5h, yn + 0.5k2) k2 = hf(xn + 0.5h, yn + 0.5k1) k4 = hf(xn + h, yn + k3) k2/h and k3/h are the slopes at the mid point. Each of the k/h represents a slope over the given interval. k1/h is the slope at the beginning of the interval and k4/h is the slope at the end of interval. Therefore the sum (k1 + 2k2 + 2k3 + k4) / (6h) can be interpreted as a weighted average of the slope. dy = x + y ; y(0) = 1, h = 0.1 dx k1 = 0.1*(0 + 1) = 0.1*1 EX: Solve => y = y(0) + 0.5k1 = 1 + 0.5*0.1*1 = 1.05 k2 = 0.1*(0.05 + 1.05) = 0.1*1.10 => y = y(0) + 0.5k2 = 1 + 0.5*0.1*1.10 = 1.055 k3 = 0.1*(0.05 + 1.055) = 0.1*1.105 => y = y(0) + k3 = 1 + 1*1.105 = 1.1105 k4 = 0.1*(0.1 + 1.1105) = 0.1*1.2105 y(0.1) = 1 + 0.1*(1 + 2*1.1 + 2*1.105 + 1.2105)/6 = 1.11034 k1 = 0.1*(0.1 + 1.11034) = 0.1*1.21034 => y = y(0.1) + 0.5k1 = 1.11034 + 0.5*0.1*1.21034 = 1.170857 k2 = 0.1*(0.15 + 1.170857) = 0.1*1.32857 => y = y(0.1) + 0.5k2 = 1.11034 + 0.5*0.1*1.32857 = 1.17638285 Fourth-order Runge-Kutta method for system of first order differential equations dy1 = f1(x, y1, y2, …, ym) dx dy2 = f2(x, y1, y2, …, ym) ...... dx dy m = fm(x, y1, y2, …, ym) dx 1 yin 1 = yin + (k1,i + 2k2,i + 2k3,i + k4,i) , where i = 1, 2, ..., m and 6 k k h k1,i = h*fi(xn, y1n , …, ymn ) k2,i = h*fi(xn + , y1n + 1,i , …, ymn + 1,m ) 2 2 2 k k h k3,i = h*fi(xn + , y1n + 2,i , …, ymn + 2,m ) 2 2 2 k4,i = h*fi(xn + h, y1n + k3,i, …, ymn + k3,m) 4-7 The idea behind the solution to a system of differential equations is similar to the solution of a single differential equation. All the k’s values divided by step size h are just the slopes of the curves evaluated at the appropriate x and yi. EX: Solve dy1 = y1y2 + x dx dy2 = xy2 + y1 dx , y1(0) = 1 , y2(0) = -1 using fourth order Runge-Kutta method with step size h = 0.1 Solution x = xn = 0, y1 = 1, y2 = -1 k1,1 = 0.1*(y1y2 + x) = 0.1*[ (1)(-1) + 0] k1,2 = 0.1*(xy2 + y1) = 0.1*[ (0)(-1) + 1] = -0.1 = 0.1 x = xn + 0.5h = 0 + (0.5)(.1) = 0.05 y1 = y1(0) + 0.5k1,1 = 1 + 0.5(-0.1) = 0.95 y2 = y2(0) + 0.5k1,2 = -1 + 0.5( 0.1) = -0.95 k2,1 = 0.1*(y1y2 + x) = 0.1*[ (0.95)(-0.95) + 0.05] = -0.08525 k2,2 = 0.1*(xy2 + y1) = 0.1*[ (0.05)(-0.95) + 0.95] = 0.9025 x = xn + 0.5h = 0 + (0.5)(.1) = 0.05 y1 = y1(0) + 0.5k2,1 = 1 + 0.5(-0.0852) = 0.9574 y2 = y2(0) + 0.5k2,2 = -1 + 0.5( 0.0902) = -0.9549 k3,1 = 0.1*(y1y2 + x) = 0.1*[ (0.9574)(-0.9549) + 0.05] = -0.0864 k3,2 = 0.1*(xy2 + y1) = 0.1*[ (0.05)(-0.9549) + 0.9574] = 0.0909 x = xn + h = 0 + .1 = 0.1 y1 = y1(0) + k3,1 = 1 + (-0.0864) = 0.9136 y2 = y2(0) + k3,2 = -1 + ( 0.0909) = -0.9091 k4,1 = 0.1*(y1y2 + x) = 0.1*[ (0.9136)(-0.9091) + 0.1] = -0.0730 k4,2 = 0.1*(xy2 + y1) = 0.1*[ (0.1)(-0.9091) + 0.9136] = 0.0823 y1(0.1) = y1(0) + (k1,1 + 2k2,1 + 2k3,1 + k4,1)/6 y1(0.1) = 1 + [(-0.1) + 2(-0.08525) + 2(-0.0864) + (-0.0730)]/6 y2(0.1) = y2(0) + (k1,2 + 2k2,2 + 2k3,2 + k4,2)/6 y2(0.1) = -1 + [(0.1) + 2(0.09025) + 2(0.0909) + (0.0823)]/6 x = xn = 0.1, y1 = 0.9139, y2 = -0.9092 4-8 = 0.9139 = -0.9092 k1,1 = 0.1*(y1y2 + x) = 0.1*[ (0.9139)(-0.9092) + 0.1] = -0.07309 k1,2 = 0.1*(xy2 + y1) = 0.1*[ (0.1)(-0.9092) + 0.9139] = 0.082298 Example 4.3-2. Solve the following first order system for y1 and y2 at x = 1. dy1 = y1y2 + x dx dy2 = xy2 + y1 dx , y1(0) = 1 , y2(0) = -1 using fourth order Runge-Kutta method with step size h = 0.1 Solution The MATLAB routines ode23 and ode45 can be used to solve the system. A MATLAB function must be created to evaluate the slopes as a column vector. The function name in this example is exode(x, y) which must be saved first in the hard drive with the same name exode.m. ------------------------------% Example 4.3-2 function to evaluate the slopes function y12 = exode(x,y) y12(1,1)=y(1)*y(2)+x; y12(2,1)=x*y(2)+y(1); ------------------------------The command ode23 or ode45 is then evaluated from the command windows. MATLAB will set the step size to achieve a preset accuracy that can be changed by user. We will use both ode23 and ode45. [x,y]=ode23('exode',[0; 1],[1; -1]) x= 0 0.0800 0.1800 0.2800 0.3800 0.4800 0.5800 0.6800 0.7800 0.8800 0.9800 1.0000 y= 1.0000 0.9290 0.8628 0.8174 0.7899 0.7779 0.7797 0.7943 0.8207 0.8585 0.9076 0.9188 4-9 -1.0000 -0.9260 -0.8480 -0.7828 -0.7275 -0.6794 -0.6364 -0.5966 -0.5581 -0.5189 -0.4770 -0.4681 The independent variable can also be specified at certain locations between the initial and final values and MATLAB will provide the dependent value at these locations. However, the step size h is still controlled by the error tolerance. >> xspan=0:.1:1; >> [x,y]=ode23('exode',xspan,[1 -1]) x= 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 y= 1.0000 0.9139 0.8522 0.8106 0.7863 0.7772 0.7816 0.7986 0.8274 0.8675 0.9188 -1.0000 -0.9092 -0.8341 -0.7711 -0.7173 -0.6704 -0.6283 -0.5889 -0.5503 -0.5108 -0.4681 y= 1.0000 0.9139 0.8522 0.8106 0.7863 0.7772 0.7817 0.7987 0.8274 0.8675 0.9188 -1.0000 -0.9092 -0.8341 -0.7711 -0.7174 -0.6705 -0.6283 -0.5889 -0.5504 -0.5108 -0.4681 >> [x,y]=ode45('exode',xspan,[1 -1]) x= 0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 4-10 Differential equations of higher order, or systems containing equations of mixed order can be transformed to a set of first order differential equations. For example, consider the following equation and initial conditions 3 2 d 3z dz 2d z + z 2z = 0 3 2 dx dx dx dz d 2z z(0) = 1, (0) = 0, (0) = 1 dx dx 2 We can define new variables as follows z = y1 dy dz = 1 = y2 dx dx dy d 2z = 2 = y3 2 dx dx dy d 3z = 3 3 dx dx The original equation and its initial conditions is now equivalent to the following set of equations and initial conditions dy1 = y2 , dx y1(0) = 1 dy 2 = y3 , dx y2(0) = 0 dy 3 = 2y1 + y23 y12 y3 , dx y3(0) = 1 4-11