-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bisection_Transcendental.cpp
51 lines (48 loc) · 1.13 KB
/
Bisection_Transcendental.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <math.h>
using namespace std;
double func(double x) {
return x*exp(x)-1;
}
double find_root(double, double, double, int);
int main() {
int maxc,valid=1;
double a,b,root,prec;
do {
cout<<"\nEnter starting interval : ";
cin>>a;
cout<<"\nEnter ending interval : ";
cin>>b;
if (func(a)*func(b)>=0)
cout<<"\nWrong assumption!\nTry again";
else
valid = 0;
}while(valid==1);
cout<<"\nEnter precision : ";
cin>>prec;
cout<<"\nEnter Max no. of iterations : ";
cin>>maxc;
root = find_root(a,b,prec,maxc);
cout<<"\nRoot of equation is : "<<root;
cout<<endl;
return 0;
}
double find_root(double a, double b, double prec, int maxc) {
int c=1, root;
double m=0;
cout<<"\nIteration\tRoot";
while (c<=maxc && (b-a)>=prec) {
m = (a+b)/2;
if (func(m)==0.00)
root = m;
if ((func(m)>0 && func(b)<0) || (func(m)<0 && func(b)>0)) {
a = m;
}
else {
b = m;
}
cout<<"\n"<<c<<"\t\t"<<m;
c++;
}
return root;
}