Numerical methods. Note 9
[~Home~] Systems of Ordinary Differential Equations Numerical Methods. Note~ « 9 »

NB: Send me an email when you are done with a given obligatory exercise. Check that there's well commented source code (with a short description), an informative output, and a graph (when needed).

Integration of systems of differential equations. We shall discuss here the generalization of the Runge-Kutta methods for a system of differential equations Y'(x) = F(x,Y), where Y = {y1,y2,...,yn} is a column of unknown functions and F = {f1,f2,...,fn} is a column of the right-hand-sides. The initial condition is now also a column Y(x0) = Y0. The user-supplied subroutine to calculate the right-hand-sides should be modified like, for example

void dydx(real x,real y[],real f[]){ f[1]=... ; ...; f[n]=...;}

where the column of the right-hand-sides is returned in an array f[]. The midpoint Runge-Kutta method for a system of differential equations can be implemented, e.g. like

K0=F(x, Y0); K1/2=F(x+h/2; Y0+h/2K0); Y1=Y0+hK1/2; err=max(K0-K1/2)h/3

The Predictor-Corrector method can be generalized as, e.g.
Prediction: Y(x) ≈ Y1+F1⋅(x-x1)+C⋅(x-x1)2, where C=[Y0-Y1-F1⋅(x0-x1)]/(x0-x1)2 (from Y(x0)=Y0); Y2=Y(x2); F2=F(x2,Y2)
Correction: Y(x) ≈ Y1+F1⋅(x-x1)+C⋅(x-x1)2 +D⋅(x-x1)2(x-x0),
where D=(F2-F1-2C⋅(x2-x1))/(2(x2-x1)(x2-x0)+(x2-x1)2), (from Y'(x2)=F2); Y2=Y(x2)

The correction can serve as an estimate of the error. \dt Problems \dd

  1. Generalize your routines to system of differential equations. Your step-size controlling (driver) subroutine may have the following prototype
    void driver(real a,real b,real(*f)(real,real*,real*),int n,real *y0
    	,real *x,real **y,int *k,int kmax
    	,real h,real eps,real acc,real *gerr,real *k0,real *k12)
    
    where y[k][i] = yi(xk) is the solution, \tt{k0} and \tt{k12} is the storage for e.g. K0 and K1/2, \tt{gerr} is the estimated global error and \tt{h} is the initial step.

    The driver calls the stepper -- the subroutine that performs the very step:

    void stepper(int n,real x,real* y0,real* y1,real(*f)(real,real*,real*)
    	,real h,real* err,real* k0,real* k12)
    
  2. Integrate the equation #{ y''(x)=-y , y(0)=0 , y'(0)=1 } from #{a}=0 to #{b}=(4+1/2)π.

"Copyleft" © 2004 D.V.Fedorov (fedorov at phys dot au dot dk)