Runge-Kutta Schemes


Runge -Kutta Schemes

One of the oldest and still most widely used groups of ODE integration algorithms is the Runge-Kutta family of methods . These are step-wise integration algorithms. Starting from an initial condition, the ODE is solved at discrete steps over the desired integration range. Runge-Kutta techniques are robust and will give good results as long as very high accuracy is not required. Runge-Kutta methods are not the fastest ODE solver techniques but their efficiency can be markedly improved if adaptive step sizing is used.

Runge-Kutta methods are designed to solve first-order differential equations. They can be used on a single first-order ODE or on a coupled system of first-order ODEs. If a higher-order ODE can be expressed as a coupled system of first-order ODEs, Runge-Kutta methods can be used to solve it. To understand how Runge-Kutta methods work, consider a simple first-order differential equation.

Equation 20.4

graphics/20equ04.gif


To solve for the dependent variable y , Eq. (20.4) can be integrated in a step-wise manner. The derivative is replaced by its delta-form and the D x term is moved to the right-hand side.

Equation 20.5

graphics/20equ05.gif


Eq. (20.5) is the general form of the equation that is solved. Starting at an independent variable location x n where the value of y n is known, the value of the dependent variable at the next location, y n + 1 , is equal to its value at the current location, y n , added to the independent variable step size, D x , times the right-hand side function.

There is one question left to be resolved ”where should we evaluate the right-hand side function? With the Euler method, as shown in Eq. (20.6), the function is evaluated at the current location, x n .

Equation 20.6

graphics/20equ06.gif


The value of y at the next step is computed using the slope of the f ( x,y ) function at the current step. If you perform a Taylor series expansion on Euler's method you will find that it is first-order accurate in D x . The Euler method is really only useful for linear or nearly linear functions. What happens, for instance, if the slope of the f ( x,y ) curve changes between x n and x n + 1 ? The Euler method will compute an incorrect value for y n + 1 .

This is where the Runge-Kutta methods come into play. The Runge-Kutta methods perform a successive approximation of y n + 1 by evaluating the f ( x,y ) function at different locations between x n and x n + 1 . The final computation of y n + 1 is a linear combination of the successive approximations. For example, the second-order Runge-Kutta method evaluates f ( x,y ) at two locations, shown in Eq. (20.7).

Equation 20.7

graphics/20equ07.gif


The first step of the second-order Runge-Kutta algorithm is the Euler method. A value for D y is computed by evaluating f ( x,y ) at x n . The second step calculates the value of y n + 1 by evaluating f ( x,y ) midway between x n and x n + 1 using a y value halfway between y n and y n + D y 1 . The result is a second-order accurate approximation to y n + 1 . The two-step Runge-Kutta scheme is more accurate than Euler's method because it does a better job of handling potential changes in slope of f ( x,y ) between x n and x n + 1 .

There are numerous Runge-Kutta schemes of various orders of accuracy. The most commonly used scheme and the one we will implement in this chapter is the fourth-order Runge-Kutta algorithm. As the name implies, it is fourth-order accurate in D x . The algorithm consists of five steps, four successive approximations of D y and a fifth step that computes y n + 1 based on a linear combination of the successive approximations. The fourth-order Runge-Kutta solution process is;

  1. Find D y 1 using Euler's method.

  2. Compute D y 2 by evaluating f ( x,y ) at graphics/20inl01.gif .

  3. Calculate D y 3 by evaluating f ( x,y ) at graphics/20inl02.gif .

  4. Evaluate D y 4 by evaluating f ( x,y ) at ( x n + D x,y n + D y 3 ).

  5. Compute y n + 1 using a linear combination of D y 1 through D y 4 .

The mathematical equations for the five steps are shown in Eq. 20.8.

Equation 20.8

graphics/20equ08.gif


Now that we have gone over the derivation of the fourth-order Runge-Kutta method, let's write a method to implement it. The method will be named rungeKutta4() . As Runge-Kutta solvers are used by a wide variety of applications, we will define the rungeKutta4() method to be public and static , so it can be universally accessed. We will define rungeKutta4() in a class named ODESolver and place the ODESolver class in the TechJava.MathLib package.

The rungeKutta4() method takes three arguments. The first argument is an ODE object (or an ODE subclass object). If you recall, the ODE class will define the number of coupled first-order equations that characterize the ODE and will provide arrays to store the dependent and independent variables . The ODE class also defines the getFunction() method that returns the f ( x,y ) function for a given x and y .

The other two input arguments are the range over which the integration will take place and the increment to the independent variable. This increment will be held constant throughout the entire integration. The number of steps that will be performed is not a user -specified value but is computed based on the range and dx arguments. The integration will stop if the step number reaches the MAX_STEPS parameter defined in the ODE class.

The integration follows the steps shown in Eq. (20.8). When the integration is complete, the x[] and y[][] fields of the ODE object will contain the values of the integrated independent and dependent variables. The return value of the rungeKutta4() method is the number of steps computed. The rungeKutta4() code is shown next.

 package TechJava.MathLib; public class ODESolver {   public static int rungeKutta4(ODE ode,                    double range, double dx) {   //  Define some convenience variables to make the   //  code more readable     int numEqns = ode.getNumEqns();     double x[] = ode.getX();     double y[][] = ode.getY();   //  Define some local variables and arrays     int i,j,k;     double scale[] = {1.0, 0.5, 0.5, 1.0};     double dy[][] = new double[4][numEqns];     double ytmp[] = new double[numEqns];   //  Integrate the ODE over the desired range.   //  Stop if you are going to overflow the matrices     i=1;     while( x[i-1] < range && i < ODE.MAX_STEPS-1) {   //  Increment independent variable. Make sure it   //  doesn't exceed the range.       x[i] = x[i-1] + dx;       if (x[i] > range) {         x[i] = range;         dx = x[i] - x[i-1];       }   //  First Runge-Kutta step       ode.getFunction(x[i-1],dy[0],y[i-1]);   //  Runge-Kutta steps 2-4       for(k=1; k<4; ++k) {         for(j=0; j<numEqns; ++j) {           ytmp[j] = y[i-1][j] + scale[k]*dx*dy[k-1][j];         }         ode.getFunction(x[i-1]+scale[k]*dx,dy[k],ytmp);       }   //  Update the dependent variables       for(j=0; j<numEqns; ++j) {         y[i][j] = y[i-1][j] + dx*(dy[0][j] +                   2.0*dy[1][j] + 2.0*dy[2][j] +                   dy[3][j])/6.0;       }   //  Increment i       ++i;     }   //  end of while loop   //  Return the number of steps computed     return i;   } } 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net