/* test_deriv.c high order, many points */ #include "deriv.h" /* basic deriv and rderiv */ #include #include #include #define PI 3.141592653589793238462643 #define nx 17 #undef abs #define abs(x) ((x)<0.0?(-(x)):(x)) #undef max #define max(a,b) ((a)>(b)?(a):(b)) double f(double x) { return sin(x); } double fp(double x) /* first derivative */ { return cos(x); } double fpp(double x) /* second derivative */ { return -sin(x); } double fppp(double x) /* third derivative */ { return -cos(x); } double fpppp(double x) /* fourth derivative */ { return sin(x); } int main(int argc, char *argv[]) { double cx[nx]; double cxx[nx]; double cxxx[nx]; double cxxxx[nx]; int i, j, ip, ij, iPI; int a[nx]; int b[1]; double xg[nx]; /* x values */ double u[nx]; /* sine wave, one cycle, 9 points */ double dx, x, xmin, xmax, hx; double up, upp, uppp, upppp; /* computed */ double yp, ypp, yppp, ypppp; /* exact */ int np, mid; double err, avgerr, maxerr; printf("test_deriv.c sine wave, to 4th order nx=%d \n", nx); printf("np is number of points used to compute derivative\n"); printf("np must be one larger than derivative and 11 too big for third\n"); xmin = 0.0; for(iPI=1; iPI<=2; iPI++) { xmax = iPI*PI; /* run pi or 2*pi */ hx = (xmax-xmin)/(double)(nx-1); printf("nx=%d, xmin=%f, xmax=%f, hx=%f \n", nx, xmin, xmax, hx); for(i=0; inx-1-mid) {ip = np-1-(nx-1-i); ij = nx-np;} else if(i>=mid) {ip = mid; ij = i-mid;} rderiv(1, np, ip, hx, cx); up = 0.0; for(j=0; jnx-1-mid) {ip = np-1-(nx-1-i); ij = nx-np;} else if(i>=mid) {ip = mid; ij = i-mid;} rderiv(2, np, ip, hx, cxx); upp = 0.0; for(j=0; jnx-1-mid) {ip = np-1-(nx-1-i); ij = nx-np;} else if(i>=mid) {ip = mid; ij = i-mid;} rderiv(3, np, ip, hx, cxxx); uppp = 0.0; for(j=0; jnx-1-mid) {ip = np-1-(nx-1-i); ij = nx-np;} else if(i>=mid) {ip = mid; ij = i-mid;} rderiv(4, np, ip, hx, cxxxx); upppp = 0.0; for(j=0; j