/*********************************************************************** * Test program: Numerical differentation according to Romberg * * -------------------------------------------------------------------- * * SAMPLE RUN: * * (Find first derivative of function f(x)=1/x, for x=0.12). * * * * Numerical differentation according to Romberg * * * * Test function f(x) = 1/x * * * * Put in x-value at which you want to evaluate derivative: 0.12 * * Put in desired accuracy: 1e-10 * * Maximal number of columns in Romberg scheme: 4 * * Starting step size: 0.005 * * * * Results: * * * * er_app= 8.8917317953019e-11 * * res = -6.9444444444444e+01 * * nend = 3 * * hend = 6.2500000000000e-04 * * * * -------------------------------------------------------------------- * * "Numerical Algorithms with C, By Gisela Engeln-Muellges * * and Frank Uhlig, Springer-Verlag, 1996" [BIBLI 11]. * * * * C++ Release 1.0 By J-P Moreau, Paris. * * (www.jpmoreau.fr) * ************************************************************************ Note: to link with files: basis_r.cpp, difrom.cpp, vmblock.cpp. --------------------------------------------------------------- */ #include // For REAL, etc. //test function to derivate REAL func (REAL x) { return (1 / x); } //print results void aus (int n, REAL prec, REAL h, REAL x, int error, REAL schaetz, REAL res, int nend, REAL hend) { if (error != 1) { printf ("\n Results:\n\n"); printf (" er_app= %20.13"LZP"e\n" " res = %20.13"LZP"e\n" " nend = %2d\n" " hend = %20.13"LZP"e\n\n", schaetz, res, nend, hend); } } //input one data item #define input(Question,EinFormat,AusFormat,Variable) \ ( printf (Question" "), /* Write out question */ \ scanf (EinFormat, &Variable)) /* read value */ \ int difrom (REAL x0, REAL eps, int n, REAL h, REAL* res, REAL* er_app, int* nend, REAL* hend); void main() { int error, nend, n; REAL x, prec, h; REAL res, schaetz, hend; printf ("\n Numerical differentation according to Romberg\n\n"); printf (" Test function f(x) = 1/x\n\n"); input (" Put in x-value at which you want to evaluate derivative:", "%"LZS"f", "%"LZP"f", x); input (" Put in desired accuracy:", "%"LZS"f", "%"LZP"f", prec); input(" Maximal number of columns in Romberg scheme:", "%d", "%d", n); input (" Starting step size:", "%"LZS"f", "%"LZP"f", h); error = difrom (x,prec,n,h,&res,&schaetz,&nend,&hend); aus (n,prec,h,x,error,schaetz,res,nend,hend); return; } //end of file tdifrom.cpp