/* Title: Example of noise in evaluating a function. This is the program in Section 3.1. In the program, the function f(x) = -1 + 3*x - 3*x**2 + x**3 is evaluated in two forms: nested and direct. The interval of evaluation is [a,b] = [1-delta,1+delta], with delta specified in the data statement. For other computers, this value may have to be chosen smaller, in order to obtain the desired demonstration of noise in evaluating f(x). */ #include const int n=20; const float delta = 0.001; main() { float a, b, h, x; float fnestd, fdirct, b1, b2; int j; /* Initialization */ a = 1.0 - delta; b = 1.0 + delta; h = 2.0*delta/n; printf("\n Following are the values of x, f(x):Nested evaluation,\n"); printf(" and f(x):Direct evaluation\n"); for(j=0; j <= n; j++) { /* Begin loop for evaluating function on [a,b] */ x = a + j*h; b1 = -3.0 + x; b2 = 3.0 + x*b1; fnestd = -1.0 + x*b2; fdirct = -1.0 + 3.0*x - 3.0*x*x + x*x*x; printf("\t%9.6f\t%16.6e\t%16.6e\n",x, fnestd, fdirct); } return 0; }