These codes are tested in Dev C++ and are error free. If any error occurs, comment below. Other programs will be uploaded as soon as possible.
Thank you! :)

Tuesday, April 12, 2016

Bisection Method

#include<iostream.h>
#include<conio.h>
#include<math.h>
#define f(x) (x*x - 4*x - 10)
#define e 0.05
int main()
{
    static int count;
    float x1, x2, x0, f1, f2, f0, ab;
    cout << "Enter x1: " ;
    cin >> x1;
    cout << "Enter x2: " ;
    cin >> x2;
    cout << endl << "Estimated error: " << e << endl;
    f1 = f(x1);
    f2 = f(x2);
    if((f(x1) * f(x2))>0)
    {
              cout << "No roots " << endl;
              goto down;
              }
    else
    {
              Top:
                  ab = (fabs((x2 - x1)/x2) * 100);
                  if(ab< 0.05)
                  {
                          cout << endl << "The root is " << x1 << endl;
                          cout << "Error : " << ab << "%" << endl;
                          cout << "Total no of iteration is : " << count;
                     
                          goto down;
                          }
                          else{
                          count++;
                          x0 = (x1 + x2) /2;
                          f0 = f(x0);
                          if((f1 * f0)< 0)
                          {
                                 x2 = x0;
                                 goto Top;
                                 }
                          else{
                               x1 = x0;
                               f1 = f0;
                               goto Top;
                             }  }
    }
    down:
         getch();
         return 0;
         }
                       
               


No comments:

Post a Comment

If you have any problem, please leave your message here.