// rderiv.java // compute formulas for numerical derivatives on hx equal spaces points // order is order of derivative, 1 = first derivative, 2 = second // points is number of points where value of function is known // f(x0), f(x1), f(x2) ... f(x points-1) // term is the point where derivative is computed // f'(x0) = a(0)*f(x0) + a(1)*f(x1) // + ... + a(points-1)*f(x points-1) // // algorithm: use divided differences to get polynomial p(x) that // approximates f(x). f(x)=p(x)+error term // f'(x) = p'(x) + error term' // substitute xj = x0 + j*h // substitute x = x0 to get p'(x0) etc public class rderiv // for function deriv { rderiv(int order, int npoints, int point, double hx, double c[]) { // compute the exact coefficients to numerically compute a derivative // of order 'order' using 'npoints' at ordinate point, // where order>=1, npoints>=order+1, 0 <= point < npoints, // 'a' array returned with numerator of coefficients, // 'bb' returned with denominator of h^order int a[]=new int[npoints]; // integer coefficients of solution int h[]=new int[npoints+1]; // coefficients of h int x[]=new int[npoints+1]; // coefficients of x, variable for differentiating int numer[][]=new int[npoints][npoints]; // numerator of a term numer[term][pos] int denom[]=new int[npoints]; // denominator of coefficient int jj, b; int k = 0; int ipower, isum, iat, r; int debug = 0; double bh; if(npoints<=order) { System.out.println("ERROR in call to deriv, npoints="+npoints+ " < order="+order+"+1"); return; } for(int term=0; term0) for(int i=0; i0) System.out.println(" "); b = 0; for(int jterm=0; jtermb) b=denom[jterm]; // largest denominator } for(int jterm=0; jterm0) System.out.println("f^("+order+")(x["+iat+"])=(1/"+b+"h^"+order+ ")("+a[jterm]+" f(x["+jterm+"]) +"); } // end computing terms of coefficients bh = 1.0/(double)b; for(int i=0; iMath.abs(b)) { a1 = Math.abs(a); b1 = Math.abs(b); } else { a1 = Math.abs(b); b1 = Math.abs(a); } r=1; while(r!=0) { q = a1 / b1; r = a1 - q * b1; a1 = b1; b1 = r; } return a1; } // end gcd } // end class rderiv.java