CSERD


  • userhome
  • catalog
  • resources
  • help

Diffeq class


Shodor > CSERD > Resources > Libraries > Diffeq class

  Download Source Code  •  Documentation  •  View License Agreement


Diffeq class documentation

The Diffeq class is designed to solve for the solution of a system of first order differential equations x'(x,t) = f(x,t).

Algorithms used include Euler's method, improved Euler's method, and the 4th order Runge Kutta method (from MathWorld.)

Languages:

  • C++
  • Java

    C++

    The Diffeq class is called at construction with the number of equations to be solved. The programmer sets the initial values by accessing the member variable x. The programmer provides a routine deriv that calculates the derivatives based on value of x and time.

    Member variables

    
       int neq;           //number of equations
       double *x;         //equation values at a given time
       double time;       //time variable
       double * der;      //workspace for derivative calcualtion
       double * x_old;    //workspace for equation values
       double * der_old;  //workspace for derivative calcualtion
       double * k1;       //workspace for Runge Kutta calculation
       double * k2;       //workspace for Runge Kutta calculation
       double * k3;       //workspace for Runge Kutta calculation
       double * k4;       //workspace for Runge Kutta calculation
    

    Methods

  • Constructor: Diffeq(int j)
  • Destructor: ~Diffeq()
  • updateEuler(double step, void deriv(int,double,double*,double*)) - calculates next values of x by Euler method, increments time step.
  • updateIEuler(double step, void deriv(int,double,double*,double*)) - calculates next values of x by improved Euler method, increments time step.
  • updateRKutta4(double step, void deriv(int,double,double*,double*)) - calculates next values of x by 4th order Runge Kutta method, increments time step.

    Java

    The Diffeq class is called at construction with the number of equations to be solved. The programmer overrides the Diffeq class, and provides a method deriv that calculates the derivatives based on values of x and t. The programmer sets the initial values by accessing the member variable x.

    Member variables

    
       int neq;           //number of equations
       double [] x;       //equation values at a given time
       double time;       //time variable
       double [] der;     //workspace for derivative calcualtion
       double [] x_old;   //workspace for equation values
       double [] der_old; //workspace for derivative calcualtion
       double [] k1;      //workspace for Runge Kutta calculation
       double [] k2;      //workspace for Runge Kutta calculation
       double [] k3;      //workspace for Runge Kutta calculation
    

    Methods

  • Constructor: Diffeq(int j)
  • Destructor: ~Diffeq()
  • updateEuler(double step) - calculates next values of x by Euler method, increments time step.
  • updateIEuler(double step) - calculates next values of x by improved Euler method, increments time step.
  • updateRKutta4(double step) - calculates next values of x by 4th order Runge Kutta method, increments time step.
  • deriv(int n, double time, double [] x, double [] xder) - routine to take position values as an array of doubles x and calculate derivatives stored in array of doubles xder.

  • ©1994-2024 Shodor