bisection method by c

Discussion in 'Programmer's Corner' started by swty_todd, Feb 7, 2009.

  1. swty_todd

    Thread Starter Active Member

    Aug 3, 2008
    82
    0
    here is the c program for arriving at a solution using bisection method.
    The ouput is going infinte..pls help ....i think it has more of syntax errors than logical errors...

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include<stdio.h>
    4. #include<math.h>
    5. main()
    6. {float a,b,c,x1,x2,x,series;
    7. double d;
    8.  
    9. printf("enter a,b,c and x1(pos) & x2(neg)");
    10. scanf("%f%f%f%f%f",&a,&b,&c,&x1,&x2);
    11.  
    12. x=0;
    13. d=1;
    14.  while(d>0.0001)
    15.     {
    16.       x=(x1+x2)/2;
    17.      
    18.      series=a*x*x+b*x+c;
    19.      
    20.      d=fabs(series);
    21.      
    22.          if(x*x1 < 0)
    23.              x2=x;
    24.          else
    25.              x1=x;
    26.     }
    27.      
    28. printf("ans=%f",x);
    29.  
    30. return 0;
    31. }
    32.  
     
    Last edited: Feb 8, 2009
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    You can't have any syntax errors if the compiler is producing an executable, so any errors you have must be logic errors.

    Apparently you're trying to find roots of a quadratic equation by choosing x values that straddle the root you're trying to find.

    One suggestion I would make is to do your input one value at a time instead of all five at once. Can you verify that a, b, c, x1, and x2 are being set appropriately? You can do that using a debugger, or if you don't have one available, you can put printf statements after your input line(s).

    What are your input values for a, b, c, x1, and x2? If I knew these, I could tell what your program is doing.

    Mark
     
Loading...