martes, 31 de julio de 2012

/* PROGRAMA QUE ENCUENTRA LA RAIZ UNA FUNCION POR EL MÉTODO DE BISECCIÓN */

// para compilar: gcc p002.c -o e002.out

#include <stdio.h>
#include <math.h>
#include<stdlib.h> //para que coja exit(1)

double fun(double x);

int main () {
double a,b,xl,xr,xm,tol,yt,wid;
FILE *fcon;
fcon=fopen("condfun.txt","r");
fscanf(fcon,"%lf %lf %lf",&a,&b,&tol);
xl=a;xr=b;
yt=fun(xl)*fun(xr);
if(yt>0.0){
 printf("No hay raiz en el intervalo\n");
 exit(1);
}
wid=(xr-xl);
while(wid>tol){
xm=(xr+xl)/2.0;
yt=fun(xl)*fun(xm);
if(yt<0.0)xr=xm;
else xl=xm;
wid=(xr-xl);
}
printf("la raiz es: %lf \n",xr);
return 0;
}


double fun(double x){
double y;
y=3.0*log(fabs(x))-9.0;
return y;
}

No hay comentarios: