The ODE Class


The ODE Class

As you certainly know by this time, everything in Java is defined within a class. If we are working with ODEs we need to define a class that will encapsulate an ODE. We will write the ODE class to represent a generic ODE. It will be the superclass for specific ODE subclasses. The ODE class will declare fields and methods used by all ODE classes. Since an ODE is a mathematical entity, we will place the ODE class in the TechJava.MathLib package.

When writing a class you must always consider the state and behavior of the item you are modeling. Let us first consider the fields that will define the state of an ODE. The ODE class will represent its associated ODE by one or more first-order differential equations. The ODE class will declare a field to store the number of first-order equations. Another field is needed to store the number of free variables in the ODE. Free variables are those that are not specified by boundary conditions at the beginning of the integration range. For initial value problems, the number of free variables will be zero. Two-point boundary problems will have one or more free variables.

The coupled set of first-order ODEs is solved by integrating each of the ODEs step-wise over a certain range. The values of the independent and dependent variables will have to be stored at every step in the integration. To facilitate this, the ODE class declares two arrays ” x[] which stores the values of the independent variable at each step of the integration domain and y[][] which stores the dependent variable or variables. The y[][] array is 2-D because an ODE might represent a system of first-order differential equations and therefore have more than one dependent variable.

The ODE class constructor will take two input arguments that specify the number of first-order differential equations and number of free variables used by the ODE. Because the required number of steps along the integration path is not a fixed value, the x[] and y[][] arrays are allocated to a maximum number of steps. This approach may waste a little memory but is the simplest way to do things.

Now let's turn to the behavior of an ODE class. What does an ODE class have to do? It must declare a method to return the right-hand sides of the first- order differential equations that describe the ODE. The ODE class will declare methods to return the number or first-order equations and free variables as well as methods to return the values of the x[] and y[][] arrays. There will be one method to return the entire array and another to return a single element of the array.

The ODE class also declares methods to set the conditions at the start of the integration range and to compute the error at the end. These methods and the right-hand side method are ODE-specific. Since the ODE class represents a generic ODE, they are implemented as stubs. ODE subclasses will override these methods according to their needs.

The ODE class code listing is shown next .

 package TechJava.MathLib; public class ODE {   //  This is used to allocate memory to the   //  x[] and y[][] arrays   public static int MAX_STEPS = 999;   //  numEqns = number of 1st order ODEs to be solved   //  numFreeVariables = number of free variables   //                     at domain boundaries   //  x[] =  array of independent variables   //  y[][] = array of dependent variables   private int numEqns, numFreeVariables;   private double x[];   private double y[][];   public ODE(int numEqns, int numFreeVariables) {     this.numEqns = numEqns;     this.numFreeVariables = numFreeVariables;     x = new double[MAX_STEPS];     y = new double[MAX_STEPS][numEqns];   }   //  These methods return the values of some of   //  the fields.   public int getNumEqns() {     return numEqns;   }   public int getNumFreeVariables() {     return numFreeVariables;   }   public double[] getX() {     return x;   }   public double[][] getY() {     return y;   }   public double getOneX(int step) {     return x[step];   }   public double getOneY(int step, int equation) {     return y[step][equation];   }   //  This method lets you change one of the   //  dependent or independent variables   public void setOneX(int step, double value) {     x[step] = value;   }   public void setOneY(int step, int equation,                       double value) {     y[step][equation] = value;   }   //  These methods are implemented as stubs.   //  Subclasses of ODE will override them.   public void getFunction(double x, double dy[],                           double ytmp[]) {}   public void getError(double E[], double endY[]) {}   public void setInitialConditions(double V[]) {} } 


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