Connecting Tech Pros Worldwide Forums | Help | Site Map

bisection method for finding roots

Newbie
 
Join Date: Apr 2007
Posts: 1
#1: Apr 10 '07
Quote:

Originally Posted by salman patni

hi myself patni salman i am studied in amit collage aurangabad.i need solution of bisection method program using c language with algorithm

Hi
Please try the following program nd do tell me if it works or not.
priya
Expand|Select|Wrap|Line Numbers
  1. /* bisection.c: Implements the bisection method for finding roots. 
  2.  *
  3.  *  
  4.  *
  5. /*             
  6.  
  7.     Solves f(x) = 0, given an initial interval [a,b] such that f has
  8.     differing signs at the endpoints. Quits when f((a+b)/2) is close
  9.     enough to zero. Function f must be continuous. It must be implemented
  10.     as double f(double) in this file or elsewhere. We  include an example
  11.     implementation with f(x) = 3x^3 - x - 1. The endpoints may be
  12.     specified with -a and -b options. They default to a = 0, b = 1.0.
  13.     If the endpoints don't have the required property the program quits
  14.     with an error. 
  15.  
  16.     The algorithm bisects the initial interval successively, until
  17.     the value of f at the midpoint differs from zero by less than
  18.     TOLERANCE, which defaults to .00000001. (It can be set on the
  19.     command line with -t option.) The final result is the midpoint
  20.     of the last interval. Which of the two bisected halves is retained
  21.     at a given stage is determined by calculating f at the endpoints,
  22.     so that the condition of different signs at the endpoints is
  23.     preserved. 
  24.  
  25.     The program reports the current interval, and the values of f at the
  26.     endpoints at each stage.
  27.  
  28.     The algorithm is also implemented in a user callable function.
  29.  
  30. */
  31.  
  32. /* compile: cc -o bisection.c  -lm
  33.  
  34.       Use -D_SHORT_STRINGS if your compiler does not support multiline
  35.           string constants.
  36.  
  37.       Use -DHAVEF and link object file containing implementation of 
  38.       function declared as double f(double) if you want to solve a non
  39.       default equation. In this case, also -DEQUATION= so the printout
  40.       will give the correct heading.
  41.  
  42.       Use -DNOMAIN and -DHAVEF if you only want to link in function implemented
  43.       below with declaration
  44.  
  45.   double bisection(double a, double b, double(*f)(double), double tolerance);
  46.  
  47.       It is up to the caller to make sure the endpoints and f are suitable.
  48.       You're toast if they're not.
  49.  
  50. */
  51.  
  52.  
  53. #include<stdio.h>
  54. #include<stdlib.h>
  55. #include<math.h>
  56.  
  57. #ifndef EQUATION
  58. #define EQUATION "3x^3 - x - 1 = 0"
  59. #endif
  60. #define VERSION "1.0"
  61. #define USAGE "bisection [ -a float -b float -t float -h -v]"
  62. #ifndef _SHORT_STRINGS
  63. #define HELP "\nbisection [ -a float -b float -t float -h -v ]\n\n\
  64. Find root of linked function f using bisection method.\n\n\
  65. -a: Use next argument as left endpoint of initial interval.\n\
  66. -b: Use next argument as right endpoint of initial interval.\n\
  67. -t: Use next argument as tolerance. Quit when f(midpoint) dips below this.\n\
  68. -v: Print version number and exit. \n\
  69. -h: Print this helpful information. \n\n"
  70. #else
  71. #define HELP USAGE
  72. #endif
  73.  
  74. /* Default values */
  75. #define TOLERANCE .00000001
  76. #define LEFT 0.0
  77. #define RIGHT 1.0
  78. #define MAXPASS 256
  79.  
  80. #ifndef HAVEF
  81. /* Here is the default function f's implementation. */
  82.  
  83. double f(double x){
  84.  
  85.     return 3.0*x*x*x - x - 1.0; 
  86. }
  87. #else
  88. extern double f(double);
  89. #endif
  90.  
  91. #ifndef NOMAIN
  92. int
  93. main(int argc, char **argv)
  94. {
  95.     double tolerance = TOLERANCE;
  96.     double a = LEFT;
  97.     double b = RIGHT;
  98.     double c,d,e,mid;
  99.     int j=0,n=1;
  100.  
  101.     /* Process command line */
  102.     while(++j < argc){
  103.         if(argv[j][0] == '-')
  104.             switch(argv[j][1]){ 
  105.                 case 'a':
  106.                 case 'A':
  107.                     if(j+1 >= argc){
  108.                         fprintf(stderr,"%s\n",USAGE);
  109.                         exit(1);
  110.                     }
  111.                     a = atof(argv[j+1]);
  112.                     j++;
  113.                     continue;
  114.                 case 'b':
  115.                 case 'B':
  116.                     if(j+1 >= argc){
  117.                         fprintf(stderr,"%s\n",USAGE);
  118.                         exit(1);
  119.                     }
  120.                     b = atof(argv[j+1]);
  121.                     j++;
  122.                     continue;
  123.                 case 't':
  124.                 case 'T':
  125.                     if(j+1 >= argc){
  126.                         fprintf(stderr,"%s\n",USAGE);
  127.                         exit(1);
  128.                     }
  129.                     tolerance = atof(argv[j+1]);
  130.                     if(tolerance <= 0.0){
  131.                         fprintf(stderr,"tolerance must be 
  132.  
  133. positive.\n");
  134.                         exit(1);
  135.                     }
  136.                     j++;
  137.                     continue;    
  138.                 case 'v':
  139.                 case 'V':
  140.                     printf("%s\n",VERSION);
  141.                     exit(0);
  142.                 case '?':
  143.                 case 'h':
  144.                 case 'H':
  145.                     printf("%s\n",HELP);
  146.                     exit(0);
  147.                 default:
  148.                     fprintf(stderr,"bisection: unkown option %s\n",
  149.                         argv[j]);
  150.                     exit(1);
  151.             }
  152.     }
  153.  
  154.  
  155.     /* Check necessary conditions */
  156.  
  157.     if( b < a ){
  158.             fprintf(stderr,"Error: initial endpoints are swapped.\n");    
  159.         exit(1);
  160.     }
  161.     c = f(a); d = f(b);
  162.     if(((c>0.0)&&(d>0.0))||((c<0.0)&&(d<0.0))){
  163.  
  164.         fprintf(stderr,"f has same sign at enpoints. Cannot continue.\n");
  165.         exit(1);
  166.     }    
  167.  
  168.     /* loop until desired tolerance */
  169.  
  170.     printf("\n\nBisection method solution of %s:\n\n",EQUATION);
  171.     while(1){
  172.  
  173.         mid = (a+b)/2.0;
  174.         e = f(mid);
  175.         printf("%2d. f[%10.8f,%10.8f]  =  [%10.8f,%10.8f]\n",
  176.                 n,a,b,c,d,e);
  177.         if(fabs(e) < tolerance){
  178.  
  179.             printf("\nSolution is x = %10.8f at tolerance 
  180.  
  181. %9.8f.\n\n",mid,tolerance);
  182.             exit(0);
  183.         }
  184.         if( c > 0.0)
  185.             if(e < 0.0) b = mid;
  186.             else a = mid;
  187.         if( c < 0.0)
  188.             if(e > 0.0) b = mid;
  189.             else a = mid;
  190.  
  191.             c = f(a); d = f(b);
  192.         n++;
  193.         if(n>MAXPASS) exit(1); /* obviously not converging */
  194.     }
  195.  
  196.     return 0;
  197.  
  198. }
  199. #endif
  200.  
  201. double bisection(double a, double b, double(*f)(double),double tolerance)
  202. {
  203.  
  204.     double mid,c,d,e;
  205.  
  206.     c = f(a); d = f(b);
  207.     while(1){
  208.  
  209.         mid = (a+b)/2.0;
  210.         e = f(mid);
  211.         if(fabs(e) < tolerance)break;
  212.         if( c > 0.0)
  213.             if(e < 0.0) b = mid;
  214.             else a = mid;
  215.         if( c < 0.0)
  216.             if(e > 0.0) b = mid;
  217.             else a = mid;
  218.  
  219.             c = f(a); d = f(b);
  220.     }
  221.     return mid;
  222. }

sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,957
#2: Apr 10 '07

re: bisection method for finding roots


Quote:

Originally Posted by epriya

Hi
Please try the following program nd do tell me if it works or not.
priya

epriya-

This thread has been idle for several months, and posting your own question in it is called 'hijacking' and is considered very rude. I have split your thread from the other.

Just out of curiousity, what stopped you from trying it? Did you try plugging it into a compiler?
Reply