odeint.c
来自「Numerical Recipes Software 提供的算法子程序集」· C语言 代码 · 共 57 行
C
57 行
#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
/* (C) Copr. 1986-92 Numerical Recipes Software . */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?