Sie sind auf Seite 1von 1

#include <stdio.

h>
#include <math.h>
#define JMAX 50
#define xacc 1e-12
main()
{
double x1,x2, fx, df, dx, rtn;
int j;
/* these guess for the range in which a root exists could come from a */
/* bracketing and bisection algorithm */
x1 = 1.0;
x2 = 4.0 ;
rtn = 0.5*(x1+x2); /* first guess = midpoint of the interval [x1,x2] */
for (j=1;j<=JMAX; j++)
{
fx = rtn*rtn - rtn -1.0; /* in this case the function is f(x) = x^2-x-1*/
df = 2*rtn - 1.0; /* the first derivative is f'(x) = 2x */
/* note that you can change this to be any function
*/
/* you want. Also you could write a C function to
/* evaluate fx and df
*/
dx = -fx/df;
rtn += dx;
printf("%e\n", dx);
if ((x1-rtn)*(rtn-x2) < 0.0){
printf("error, jumped outside bounds");
exit(1);}
if (fabs(dx) < xacc){
printf("found root after %d attempts, at %lf\n", j, rtn);
exit(0); }
}
printf("error - exceeded max tries no root");
exit(0);
}

Das könnte Ihnen auch gefallen