how to write a c program to find the roots of the equation using bisection method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aruna
    New Member
    • Aug 2006
    • 6

    how to write a c program to find the roots of the equation using bisection method

    to write a c program to find the roots of the equation using bisection method
    that too using array or pointers
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    1. You posted a similar thread to this, please refrain from doing that.
    2. For which equation are we finding roots? What is the bijection method? A bijection is a 1-1 onto function, so "1-1 onto function method" really doesn't help. You should be more specific, then we might be able to help you.

    Comment

    • Niheel
      Recognized Expert Moderator Top Contributor
      • Jul 2005
      • 2432

      #3
      Thanks for the headsup D_C.
      Dup has been deleted.
      niheel @ bytes

      Comment

      • aruna
        New Member
        • Aug 2006
        • 6

        #4
        help me pleaseeee

        i just wrote a c program to find the roots of equation using bisection method
        but there is some error prevails .i think my logic is correct please help out
        this my program



        Code:
        /* to find the roots using bisection method*/
        # include <stdio.h>
        # include <conio.h>
        # include<stdlib.h>
        # include <math.h>
        # define EPS .00001
        double F(double x);
        double root(double fVal1,double fVal2);
        
        void main()
        {
        
        
        	double fVal1,fVal2;
        	clrscr();
        	printf("enter the values ");
        	scanf("%f %f",&fVal1,&fVal2);
        	printf("the roots of the euation is %1.9f\n",root(fVal1,fVal2));
        	getch();
        }
        	double root(double fVal1,double fVal2)
        	{
        		int nCtr=0;
        		double fTest1,fTest2,fTest3,fVal,fNum;
        
        		fTest1=F(fVal1);
        		fTest2=F(fVal2);
        		if((fTest1*fTest2)>0)
        		{
        			printf("both are of equal sign");
        			exit(0);
        		}
        		else
        
        		{
        			do
        			{
        				nCtr++;
        				fVal1=(fVal1+fVal2)/2;
        				fTest3=F(fVal);
        			if(fTest1*fTest3>0)
        
        			{
        				fTest1=fTest3;
        				fVal1=fVal;
        			}
        			else
        			{
        				 fTest2=fTest3;
        				 fVal2=fVal;
        
        			}
        			fNum=fabs (fVal2-fVal1);
        
        
        			} while(fNum>EPS);
        		}
        			return fabs(fVal2)>fabs(fVal1)?fVal2:fVal1;
        
        
        
        
        
        	       }
        	       double F(double x)
        	       {
        			return (x*x*x)+(3*x)-5;
                                                                   
        	       }
        Last edited by Banfa; Sep 3 '06, 09:33 PM. Reason: Added code tags

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Firstly, as has already been stated, please do not start multiple threads about the same problem, post to your original thread. I have now merged the 2 relevent threads.

          As to your code.

          strangely I had to change

          double fVal1,fVal2;

          in main to

          float fVal1,fVal2;

          in order to get the correct values into the program.

          A more serious error is that in

          double root(double fVal1,double fVal2)
          {
          ...
          }

          you declare and use a value fVal but you never initialise it to anything.

          Comment

          • aruna
            New Member
            • Aug 2006
            • 6

            #6
            i have changed my program still iam not getting it
            # include <stdio.h>
            # include <conio.h>
            # include<stdlib. h>
            # include <math.h>
            # define EPS .00001
            float F(float x);
            float root(float fVal1,float fVal2);

            void main()
            {


            float fVal1,fVal2,;
            clrscr();
            printf("enter the values ");
            scanf("%f %f",&fVal1,&fVa l2);
            printf("the roots of the euation is %1.9f\n",root(f Val1,fVal2));
            getch();
            }
            float root(float fVal1,float fVal2)
            {
            int nCtr=0;
            float fTest1,fTest2,f Test3,fVal,fNum ;

            fTest1=F(fVal1) ;
            fTest2=F(fVal2) ;
            if((fTest1*fTes t2)>0)
            {
            printf("both are of equal sign");
            exit(0);
            }
            else

            {
            do
            {
            nCtr++;
            fVal=(fVal1+fVa l2)/2;
            fTest3=F(fVal);
            if(fTest1*fTest 3>0)

            {
            fTest1=fTest3;
            fVal1=fVal;
            }
            else
            {
            fTest2=fTest3;
            fVal2=fVal;

            }
            fNum=fabs (fVal2-fVal1);


            } while(fNum>EPS) ;
            }
            return fabs(fVal2)>fab s(fVal1)?fVal2: fVal1;





            }
            float (float x)
            {
            return (x*x)-5;

            }

            Comment

            • aruna
              New Member
              • Aug 2006
              • 6

              #7
              not getting proper o/p help me

              i have just wrote a program to find the roots of the equation using bisection method
              but there is some mistake iam not getting the output.comment iam getting as abnormal termination.so please help me
              here is my program.


              Code:
              /* to find the roots using bisection method*/
              # include <stdio.h>
              # include <conio.h>
              # include<stdlib.h>
              # include <math.h>
              # define EPS .00001
              float F(float x);
              float root(float fVal1,float fVal2);
              
              void main()
              {
              
              
              	float fVal1,fVal2,;
              	clrscr();
              	printf("enter the values ");
              	scanf("%f %f",&fVal1,&fVal2);
              	printf("the roots of the euation is %1.9f\n",root(fVal1,fVal2));
              	getch();
              }
              	float root(float fVal1,float fVal2)
              	{
              		int nCtr=0;
              		float fTest1,fTest2,fTest3,fVal,fNum;
              
              		fTest1=F(fVal1);
              		fTest2=F(fVal2);
              		if((fTest1*fTest2)>0)
              		{
              			printf("both are of equal sign");
              			exit(0);
              		}
              		else
              
              		{
              			do
              			{
              				nCtr++;
              				fVal=(fVal1+fVal2)/2;
              				fTest3=F(fVal);
              				if(fTest1*fTest3>0)
              
              				{
              					fTest1=fTest3;
              					fVal1=fVal;
              				}
              				else
              				{
              					 fTest2=fTest3;
              					 fVal2=fVal;
              
              				}
              				fNum=fabs (fVal2-fVal1);
              
              
              			} while(fNum>EPS);
              		}
              		return fabs(fVal2)>fabs(fVal1)?fVal2:fVal1;
              
              
              
              
              
              	       }
              	       float (float x)
              	       {
              			return (x*x)-5;
              
              	       }

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Please do NOT create multiple threads about the same problem

                Please DO you the &#91;code&#9 3; and &#91;/code&#93; to delimit you code.

                The posted code does not compile, please post compiling code. I would suggest that rather than typeing your code into the message box you copy and paste it from the source file once you have got the code compiling so we are all working from the same base line.

                Comment

                • priya mahajan
                  New Member
                  • Sep 2006
                  • 9

                  #9
                  [


                  /*The Bisection Method to solve x*x*x+3*x-5***********/
                  #include<stdio. h>
                  #include<conio. h>
                  #include<math.h >

                  float f(float x);
                  main()
                  {

                  float a,b,y,r1,r2,r3;
                  do{
                  printf("\nenter approximate interval");
                  scanf("%f%f",&a ,&b);
                  r1=f(a);
                  r2=f(b);
                  }while(r1*r2>0) ;
                  printf("a\t\tb x=(a+b)/2 f(x)\n\n");
                  do{
                  r1=f(a);
                  r2=f(b);
                  y= (a+b)/2.0;
                  r3=f(y);
                  printf( "%.4f %.4f %.4f %.4f",a,b,y,r3) ;
                  if(r1*r3<0)
                  b=y;
                  else
                  a=y;
                  printf("\n");
                  }while(fabs(r3) >0.0001);
                  printf("\n The approx sol is %.4f",y);
                  getch();
                  return 0;
                  }

                  float f(float x )
                  {
                  float f1;
                  f1=(x*x*x)+(3*x )-5;
                  return(f1);
                  }

                  well i hope it will help you.
                  its simple but it works!!.

                  Comment

                  Working...