problem with the bisection method

This is a discussion on problem with the bisection method within the C Programming forums, part of the General Programming Boards category; HI! I wrote a program that finds a root with the bisection method. The problem is that i cant find ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    Exclamation problem with the bisection method

    HI!
    I wrote a program that finds a root with the bisection method.
    The problem is that i cant find the bug :P
    here is the code...
    can you give me your ideas for solving the problem?



    Code:
    #include<stdio.h>
    double my_function(double x);
    double a,b,e,meso,fm,fa,fb;
    int main()
    {
        printf("GIVE {a} AND {b} OF THE INTERVAL [a,b] \n,AND   ALSO THE TOLERANCE e:");
        printf("EXAMBLE:IF a=1 AND b=2 GIVE 1 2\n");
        scanf("%lf%lf%lf ",&a,&b,&e);
    	 fa=my_function(a);
    	 fb=my_function(b);
        while((fa>0)&&(fb>0) || (fa<0)&&(fb<0))
            {
        printf("THE VALUES A AND B DO NOT DIFFER IN SIGN\n");
                printf(" (+a)*(+b) OR (-a)*(-b) WORNG\n");
                printf("GIVE {a} AND {b} OF THE INTERVAL [a,b] \n,AND ALSO THE TOLERANCE e:");
                scanf("%lf%lf%lf",&a,&b,&e);
    	 fa=my_function(a);
    	 fb=my_function(b);
            }
                    meso=(a+b)/2;
        		
        			 while(my_function(meso)!=0 || ((b-a)>e) )
            		{	
            	               fm=my_function(meso);
    	 	  fa=my_function(a);			               fb=my_function(b);
    	 				
    	        		if((fa>0)&&(fm>0) || (fa<0)&&(fm<0))
    		        		{
    			        		b=meso;
    			        	}	
    		        		else 
    		        		{
    			        		a=meso;
    		        		}
    		        		meso=(a+b)/2;
    		      }
    		      
    		      	if(my_function(meso)==0)
    		     		 printf("%lf IS THE ROOT OF THE FUNCTION F(X)",meso);
    		        	else if(((b-a)<e))
    		        	printf("THE ROOT APPROXIMATIVE IS %lf",meso);
    return 0;
    }
    
    double my_function(double x)
    {   double fx;
        fx=x*x*x+4*x*x-10;
    return fx;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    I just answered this on another board!!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in pass a variable to a class
    By nima_pw in forum C# Programming
    Replies: 3
    Last Post: 06-09-2009, 07:30 AM
  2. bisection method
    By swty_todd in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 11:33 PM
  3. bisection method
    By bar5037 in forum C++ Programming
    Replies: 13
    Last Post: 10-30-2007, 04:26 PM
  4. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

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