Name-Akshay Bora ROLL NO. - 7682 QUESTION 2. Root of A Transcendental Equation Using Bisection Method Program
Name-Akshay Bora ROLL NO. - 7682 QUESTION 2. Root of A Transcendental Equation Using Bisection Method Program
PROGRAM:
#include <iostream>
#include<stdlib.h>
#include<math.h>
#include<iomanip>
#define f(x)(x*tan(x)-1) /*Defining the function whose roots are to be found*/
using namespace std;
int main()
{
double a, b,tol;
double c = a;
int n=1;
cout<<"This Program Prints the root of a Transcendental Equation in a given
interval using Bisection method"<<endl;
s:cout<<"\nEnter initial guesses :\n\t";cout<<"a :";cin>>a;cout<<"\tb :";cin>>b;
if (f(a)==0)
{
cout<<"The root of the equation is: "<<a<<endl;
goto ab;}
if (f(b)==0){
cout<<"The root of the equation is: "<<b<<endl;
goto ab;}
if (f(a) * f(b) >= 0)
{
cout << "\t\t Wrong Intervals chosen. Please give the Range again.\n";
goto s;
}
cout<<"Enter tolerance value : ";
cin>>tol;
cout<<"Sl.no."<<setw(6)<<"a"<<setw(15)<<"f(a)"<<setw(15)<<"b"<<setw(15)<<"f(
b)"<<setw(15)<<"c"<<setw(15)<<"f(c)"<<endl;/*gives a proper heading*/
cout<<"____________________________________________________________
__________________________"<<endl;
/*The following would do the bisection procedure and then print the required
data and finallly finds out the root upto the required accuracy*/
do
{
c = (a+b)/2;
cout<<n<<setw(10)<<a<<setw(15)<<f(a)<<setw(15)<<b<<setw(15)<<f(b)<<setw(1
5)<<c<<setw(15)<<f(c)<<endl;
if (f(c) == 0.0)
break;
else if (f(c)*f(a) < 0)
b = c;
else
a = c;
n++;
}
while (fabs(b-a) >= tol);
cout << "The value of root is : " << c;
ab:
return 0;
}
OUTPUT:
This Program Prints the root of a Transcendental Equation in a given interval using Bisection
method