57 lines
1.3 KiB
C
57 lines
1.3 KiB
C
|
|
#include <math.h>
|
|
#include "nrutil.h"
|
|
#define MAXSTP 10000
|
|
#define TINY 1.0e-30
|
|
|
|
extern int kmax,kount;
|
|
extern float *xp,**yp,dxsav;
|
|
|
|
void odeint(ystart,nvar,x1,x2,eps,h1,hmin,nok,nbad,derivs,rkqs)
|
|
float eps,h1,hmin,x1,x2,ystart[];
|
|
int *nbad,*nok,nvar;
|
|
void (*derivs)(),(*rkqs)();
|
|
{
|
|
int nstp,i;
|
|
float xsav,x,hnext,hdid,h;
|
|
float *yscal,*y,*dydx;
|
|
|
|
yscal=vector(1,nvar);
|
|
y=vector(1,nvar);
|
|
dydx=vector(1,nvar);
|
|
x=x1;
|
|
h=SIGN(h1,x2-x1);
|
|
*nok = (*nbad) = kount = 0;
|
|
for (i=1;i<=nvar;i++) y[i]=ystart[i];
|
|
if (kmax > 0) xsav=x-dxsav*2.0;
|
|
for (nstp=1;nstp<=MAXSTP;nstp++) {
|
|
(*derivs)(x,y,dydx);
|
|
for (i=1;i<=nvar;i++)
|
|
yscal[i]=fabs(y[i])+fabs(dydx[i]*h)+TINY;
|
|
if (kmax > 0 && kount < kmax-1 && fabs(x-xsav) > fabs(dxsav)) {
|
|
xp[++kount]=x;
|
|
for (i=1;i<=nvar;i++) yp[i][kount]=y[i];
|
|
xsav=x;
|
|
}
|
|
if ((x+h-x2)*(x+h-x1) > 0.0) h=x2-x;
|
|
(*rkqs)(y,dydx,nvar,&x,h,eps,yscal,&hdid,&hnext,derivs);
|
|
if (hdid == h) ++(*nok); else ++(*nbad);
|
|
if ((x-x2)*(x2-x1) >= 0.0) {
|
|
for (i=1;i<=nvar;i++) ystart[i]=y[i];
|
|
if (kmax) {
|
|
xp[++kount]=x;
|
|
for (i=1;i<=nvar;i++) yp[i][kount]=y[i];
|
|
}
|
|
free_vector(dydx,1,nvar);
|
|
free_vector(y,1,nvar);
|
|
free_vector(yscal,1,nvar);
|
|
return;
|
|
}
|
|
if (fabs(hnext) <= hmin) nrerror("Step size too small in odeint");
|
|
h=hnext;
|
|
}
|
|
nrerror("Too many steps in routine odeint");
|
|
}
|
|
#undef MAXSTP
|
|
#undef TINY
|