central differences in fdjac


nevermind
04-09-2004, 08:23 PM
i was wondering if anyone has successfully changed the fdjac routine to include central difference formulas rather than forward difference formulas. i have attempted to do so myself, but with very limited success thus far.

thank you very much for any help you may have,

kyle demars

nevermind
04-15-2004, 12:18 AM
i found a way to make central differences work, so i thought that i would just leave a modified version of fdjac here for anyone else who might want it. it actually turns out to be quite simple. i was just being a bit dense before getting there.

#include <cmath>
#include "nr.h"
using namespace std;

void NR::fdjac(Vec_IO_DP &x, Vec_I_DP &fvec, Mat_O_DP &df,
void vecfunc(Vec_I_DP &, Vec_O_DP &))
{
const DP EPS=1.0e-8;
int i,j;
DP h,temp;

int n=x.size();
Vec_DP f(n);
Vec_DP g(n);
Vec_DP y(n);
for (j=0;j<n;j++) {
temp=x[j];
h=EPS*fabs(temp);
if (h == 0.0) h=EPS;
x[j]=temp+h;
y[j]=temp-h;
h=x[j]-temp;
vecfunc(x,f);
vecfunc(y,g);
x[j]=temp;
y[j]=temp;
for (i=0;i<n;i++)
df[i][j]=(f[i]-g[i])/(2*h);
}
}