-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrkqs.cpp
37 lines (34 loc) · 974 Bytes
/
rkqs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cmath>
#include "nr.h"
using namespace std;
void NR::rkqs(Vec_IO_DP &y, Vec_IO_DP &dydx, DP &x, const DP htry,
const DP eps, Vec_I_DP &yscal, DP &hdid, DP &hnext,
void derivs(const DP, Vec_I_DP &, Vec_IO_DP &))
{
const DP SAFETY=0.9, PGROW=-0.2, PSHRNK=-0.25, ERRCON=1.89e-4;
int i;
DP errmax,h,htemp,xnew;
double er, sc;
int n=y.size();
h=htry;
Vec_DP yerr(n),ytemp(n);
for (;;) {
rkck(y,dydx,x,h,ytemp,yerr,derivs);
errmax=0.0;
for (i=0;i<n;i++){
er = yerr[i];
sc = yscal[i];
errmax=MAX(errmax,fabs(yerr[i]/yscal[i]));
}
errmax /= eps;
if (errmax <= 1.0) break;
htemp=SAFETY*h*pow(errmax,PSHRNK);
h=(h >= 0.0 ? MAX(htemp,0.1*h) : MIN(htemp,0.1*h));
xnew=x+h;
if (xnew == x) nrerror("stepsize underflow in rkqs");
}
if (errmax > ERRCON) hnext=SAFETY*h*pow(errmax,PGROW);
else hnext=5.0*h;
x += (hdid=h);
for (i=0;i<n;i++) y[i]=ytemp[i];
}