bisection method

This is a discussion on bisection method within the C Programming forums, part of the General Programming Boards category; here is the c program for arriving at a solution using bisection method. The ouput is going infinte..pls help ....i ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    bisection method

    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:
    main()
    {
    	float a,b,c,x1,x2,x,series;
    	double d;
    
    	printf("enter a,b,c and x1(pos) & x2(neg)");
    	scanf("%f%f%f%f%f", &a, &b, &c, &x1, &x2);
    
    	read:
    
    	x = (x1 + x2) / 2;
    	series = a * x * x + b * x + c;
    	d = fabs(series);
    
    	if (d > 0.0001)
    	{
    		if (x * x1 < 0)
    			x = x2;
    		else
    			x = x1;
    
    		goto read;
    	}
    	else
    	{
    		printf("ans=%f", x);
    	}
    
    	return 0;
    }
    Last edited by swty_todd; 02-07-2009 at 08:39 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    main() - read FAQ
    it should be

    int main(void)

    you should work on your indentation

    if (x*x1<0) - you shoudl really think a little about this line

    goto read; - there are several cases where usage of goto has its reasons. Your case is not one of these. So write your code using while loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    hey thnx vart.....!!!

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    3
    hey i edited it ....bt its still not workin...help...

    Code:
    #include<stdio.h>
    #include<math.h>
    main()
    {float a,b,c,x1,x2,x,series;
    double d;
    
    
    
    printf("enter a,b,c and x1(pos) & x2(neg)");
    scanf("%f%f%f%f%f",&a,&b,&c,&x1,&x2);
    
    
    x=0;
    d=1;
    
     while(d>0.0001)
        {
          x=(x1+x2)/2;
          
    
         series=a*x*x+b*x+c;
          
    
         d=fabs(series);
          
    
             if(x*x1 < 0)
                 x2=x;
             else
                 x1=x;
        }
    
         
    
    printf("ans=%f",x);
    
    
    
    return 0;
    }

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    you have doe notinnk with the line
    if(x*x1 < 0)

    suppose x1 = 1, x2 = 3, than x = 2

    think about right condition to get rid of the half of the interval
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  2. Replies: 2
    Last Post: 01-22-2008, 03:22 PM
  3. bisection method
    By bar5037 in forum C++ Programming
    Replies: 13
    Last Post: 10-30-2007, 04:26 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. problem with the bisection method
    By dionys in forum C Programming
    Replies: 1
    Last Post: 04-01-2004, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21