473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

bisection method for finding roots

1
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. }
Apr 10 '07 #1
1 14088
sicarie
4,677 Expert Mod 4TB
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?
Apr 10 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: uli | last post by:
Hi all, I've learned native C and I'm trying to write now in C++. I want to use a simple routine from the "Numerical Recipes in C" for using a Newton-Raphson Method within my c++ class. The...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
18
by: anand | last post by:
*********************************************************************************************************** #include<stdio.h> #include<conio.h> #include<math.h> void main() { double...
8
by: aruna | last post by:
to write a c program to find the roots of the equation using bisection method that too using array or pointers
6
by: sekitoleko | last post by:
c program for newton raphson algorithm for finding roots of a polynomial
2
by: t4zone58 | last post by:
Hey guys! I am beginner at VB studying. I got confused with solve the functions by applying bisection method in VB. Basically I can understand the bisection method mathmatically. However i got...
6
by: Cappo | last post by:
Hello, I just started programming in C++ and i have a problem with a task.I need to write a program which illustrates the Bisection method in C++. I have some codes, which use the bisection code,...
1
by: lhbrown | last post by:
Hi. I'm looking for some help with the Bisection method in Visual Basic, which I'm using for the first time as part of a university project. I am using the bisection method to work out the water...
1
by: lionkng | last post by:
I need a simple code for finding roots in c programme using Newton-raphson method
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.