00001 
00002 
00003 
00004 
00005 
00006 
00007 #include <stdio.h>
00008 #include <math.h>
00009 
00012 double f (double x){
00013   
00014   return (x*x*x - x*x - 41.0 * x + 105.0);
00015 }
00016 
00021 int sameSign(double x, double y){
00022   return x * y >= 0.0;
00023 }
00024 
00028 double middleOf(double x, double y){
00029   return x + (y - x)/2;
00030 }
00031 
00035 int isSmallNumber(double x){
00036   return (fabs(x) < 0.0000001);
00037 }   
00038 
00045 double findRootBetween(double l, double u){
00046  while (!isSmallNumber(f(middleOf(l,u)))){ 
00047    if(sameSign(f(middleOf(l,u)), f(u)))
00048      u = middleOf(l,u);
00049    else 
00050      l = middleOf(l,u);
00051  }
00052  return middleOf(l,u);
00053 }  
00054 
00056 int main (void){
00057     double x, y;
00058     int numbers; 
00059 
00060     do{
00061       printf("%s","Find a ROOT between which numbers: ");
00062       numbers = scanf("%lf%lf", &x, &y);
00063 
00064       if (numbers == 2 && !sameSign(f(x),f(y))){
00065           double solution = findRootBetween(x,y);
00066           printf("\nThere is a root in %lf\n", solution);
00067         }
00068       else if (numbers == 2 && sameSign(f(x),f(y)))
00069           printf("\nf must have different signs in %lf and %lf\n",
00070                   x, y);
00071       else if (numbers != 2)
00072           printf("\nBye\n\n");
00073     }
00074     while (numbers == 2);
00075 }
00076 
00077