ODE Runge-Kutta methods The theory (very short excerpts from lectures) First-order initial value problem We want to approximate the solution Y(x) of a system of first-order ordinary differential equations where Y(x) = Y (x) = F(x, Y(x)) with an initial condition Y(x (0) ) = Y (0), (1) y 1 (x) y 2 (x) y n (x), Y (x) = y 1(x) y 2(x) y n(x), F(x, Y) = f 1 (x, y 1, y 2,, y n ) f 2 (x, y 1, y 2,, y n ) f n (x, y 1, y 2,, y n ) Explicit Runge-Kutta methods - numerical methods for finding an approximation of the solution, using explicit formulas (there is no need to solve any equations) Euler method The simplest and least accurate method (first-order accuracy) is Euler method, which extrapolates the derivative at the starting point of each interval to find the next function value: for i = 0, 1, 2, 1 compute the derivative K of the vector function Y as K = F(x (i), Y (i) ) 2 compute Y (i+1) = Y (i) + h K Collatz (or midpoint) method An example of a second-order Runge-Kutta method (with second-order accuracy) is Collatz method, also called midpoint method First, initial derivative at the starting point of each interval is used to find a trial point halfway across the interval Second, this midpoint derivative is computed and used to make step across the full length of the interval The trial midpoint is discarded once its derivative has been calculated and used 1 c Certik
for i = 0, 1, 2, 1 compute the trial midpoint [x p, Y p ] using Euler method with 1 2 h: K 1 = F(x (i), Y (i) ) x p = x (i) + 1 2 h Y p = Y (i) + 1 2 h K 1 2 compute the derivative K 2 at the trial midpoint [x p, Y p ] as K 2 = F(x p, Y p ) 3 compute Y (i+1) using derivative at the trial midpoint: Y (i+1) = Y (i) + h K 2 The classical fourth-order Runge-Kutta method (RK4) This is the most often used, fourth-order, Runge-Kutta formula It uses three trial points to make a guess of the direction, which is then used to make step across the full length of the interval These trial points are discarded once their derivatives have been calculated and used for i = 0, 1, 2, 1 compute the first trial midpoint [x p, Y p ] using Euler method with step size 1 2 h: K 1 = F(x (i), Y (i) ) x p = x (i) + 1 2 h Y p = Y (i) + 1 2 h K 1 2 compute the second trial midpoint [x p, Y q ], using the derivative at the first trial midpoint [x p, Y p ] and a step size 1 2 h: K 2 = F(x p, Y p ) Y q = Y (i) + 1 2 h K 2 3 compute trial endpoint [x (i+1), Y e ], using the derivative at the second trial midpoint [x p, Y q ] and a step size h: K 3 = F(x p, Y q ) Y e = Y (i) + h K 3 4 compute Y (i+1) using weighted average of derivatives at the initial point and at all three trial points: K 4 = F(x (i+1), Y e ) Y (i+1) = Y (i) + 1 6 h (K 1 + 2K 2 + 2K 3 + K 4 ) 2 c Certik
Example 1 - from the previous tutorial, with RK4 added Consider Cauchy problem y = y x 2, y(1) = 2 Compute an approximate value of y(14) using RK4 with step sizes h = 02 and h = 04 and compare its performance with previous results summarized in the first four columns of Table 1: exact solution, Euler method with step size h = 01 and Collatz method with step size h = 02 k = 0 step 1: The solution Computation for h = 02 : x (0) = 1, y (0) = 2 k 1 = y(0) (x (0) ) 2 = 2 1 2 = 2, x p = x (0) + 1 2h = 1 + 01 = 11, y p = y (0) + 1 2 h k 1 = 2 + 01 2 = 22 step 2: step 3: step 4: k 2 = y p x 2 = 22 p 11 2 = 18182 y q = y (0) + 1 2 h k 2 = 2 + 01 18182 = 21818 k 3 = y q x 2 = 21818 p 11 2 = 18032 x (1) = x (0) + h = 1 + 02 = 12, y e = y (0) + h k 3 = 2 + 02 18032 = 23606 k 4 = y e (x (1) ) = 23606 2 12 2 = 16393 y (1) = y (0) + 1 6 h (k 1 + 2k 2 + 2k 3 + k 4 ) = = 2 + 1 30 (2 + 2 18182 + 2 18032 + 16393) = 23627 y(12) is approximately equal to y (1) = 23627 This is the second value at the fifth column 3 c Certik
k = 1 step 1: k 1 = y(1) (x (1) ) = 23627 2 12 2 = 16408 x p = x (1) + 1 2h = 12 + 01 = 13 y p = y (1) + 1 2 h k 1 = 23627 + 01 16408 = 25268 step 2: step 3: step 4: k 2 = y p x 2 = 25268 p 13 2 = 14952 y q = y (1) + 1 2 h k 2 = 23627 + 01 14952 = 25122 k 3 = y q x 2 = 25122 p 13 2 = 14865 x (2) = x (1) + h = 12 + 02 = 14 y e = y (1) + h k 3 = 23627 + 02 14865 = 26600 k 4 = y e (x (2) ) = 26600 2 14 2 = 13572 y (2) = y (1) + 1 6 h (k 1 + 2k 2 + 2k 3 + k 4 ) = = 23627 + 1 30 (16408 + 2 14952 + 2 14865 + 13572) = 26614 y(14) is approximately equal to y (2) = 26614 The last two values at fifth column are computed by the same process for k = 2 and k = 3 Using similar process with h = 04 we obtain values presented at the last column of Table 1 Our results show that Collatz method gives more precise solution than Euler method, even in the case when double step size is used (which represents comparable work, because within every step the derivative is computed twice) RK4 method gives the best results, even in the case when quadruple step size is used (which represents comparable work, because within every step the derivative is computed four times) 4 c Certik
exact h = 01 Euler h = 02 Collatz h = 02 RK4 h = 04 RK4 x (i) y(x (i) ) y (i) y (i) y (i) y (i) 1 20000 20000 20000 20000 20000 11 21903 22000 12 23627 23818 23636 23627 12 25191 25472 14 26614 26979 26628 26614 26617 15 27912 28356 16 29100 29616 29115 29100 17 30190 30773 18 31192 31838 31209 31193 31196 Table 1: Example 1 The first column represents values of x, where the approximate solution is computed At the second column there is exact solution, the third column presents approximate solution obtained by Euler method with step size h = 01, at the fourth column there is Collatz method with the step size h = 02 and the last two columns present approximate solution obtained by RK4 method with step sizes h = 02 and h = 04, respectively 5 c Certik