/* * newton.c : Newton-Verfahren für * * f(x) := cos(x)-x = 0 * * x0 ....... Startwert * xacc ..... Genauigkeit der Näherungslösung */ #include #include #include #define MAXITER 20 /* * f(x) */ double f( double x ) { return( cos(x) - x ); } /* * df(x)/dx */ double df( double x ) { return( -sin(x) - 1.0 ); } int main( void ) { double x0, xacc, x, xn, dx; int j; printf("x0 : "); scanf("%lf", &x0); printf("xacc : "); scanf("%lf", &xacc); x = x0; /* Iterationsschleife */ for (j = 1; j <= MAXITER; j++) { dx = f(x) / df(x); xn = x - dx; printf("x = %14.7e |xn - x| = %14.7e\n", x, fabs(dx)); if (fabs(dx) < xacc) { printf("s = %14.7e\n", x); break; } x = xn; } if (j > MAXITER) { printf("newton: MAXITER ueberschritten\n"); exit(1); } return(0); }