Numerical Methods: Problem 1



Given the equation , create a C++ program that finds the value of x by Bisection Method.
#include <iostream.h>

#include <math.h>


double function(double x);

void input(double& a, double& b, double& error);

void bisection(double a, double b, double error, double& root);

void output(double root);


int main()

{


   double a, b, c, d;

   input(a, b, c);

   bisection(a, b, c, d);

   output(d);

   cin.get();

   cin.get();

}

                double function(double x)

{

                double F;

                 F=(16*pow(x,4))-(40*pow(x,3))+(5*pow(x,2))+(20*x)+6;

                return F;

}

                void input(double& a, double& b, double& error)

{

                int n=1;


                                cout<<"============================================================\n";

                                cout<<"Numerical Methods - Finding Roots\n";

                                cout<<"Given F(x)=16(x^4)-(20x^3)+(5x^2)+20x+6\n";

                                cout<<"We are ask to find the root by bisection method\n";

                                cout<<"============================================================\n";

                                cout<<"Enter the value of A: \n";

                                cin>>a;

                                cout<<"Enter the value of B: \n";

                                cin>>b;

                                cout<<"Enter the criteria for convergence: \n";

                                cin>>error;

   while(n==1)

   {

                cout<<"\n\twith L="<<a<<"    "<<"F="<<b;

                cout<<endl;

                cout<<"---------------------------------------------------\n";

                cout<<"\t      F(A)                       F(B)\n";

                cout<<"              "<<function(a)<<"                      "<<function(b)<<endl;

                cout<<"---------------------------------------------------\n";

      double x;

                                x=function(a)*function(b);

      if (x>0)

      {

                cout<<"\n\n\tTry again another value of B: ";

                cin>>b;

                n=1;

      }

       else

                n=0;

   }

}

void bisection(double a, double b, double error, double& root)

{

                double x, i, fxabs, fx, fa, zero=0;

   int n=1, j=1;

   while(n==1)

   {

                x=(a+b)/2;

      cout<<endl;

      cout<<"with x"<<j<<"="<<x;

      cout<<"\n\tF(xmiddle)="<<function(x);

      cout<<endl;

      cout<<endl;

      fx=function(x);

      fa=function(a);

      i=fx*fa;

      fxabs=fabs(fx);

      if ((fx==zero)||(fxabs<=error))

      {

      root=x;

         n=0;

      }

      else if(i>0)

      {

      a=x;

         j=j+1;

         n=1;

      }

      else

      {

                b=x;

         j=j+1;

         n=1;

      }

   }

}

void output(double root)

{

   cout<<"==============================================\n";

                cout<<"The possible root of the given F(x) is "<<root<<endl;

   cout<<"==============================================\n";

}



 
Share:

No comments:

Post a Comment

Thanks for Suggestion / Comment!

God Bless!

Our Sponsor

Popular Post