-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBisection Method.cpp
More file actions
49 lines (43 loc) · 1.08 KB
/
Bisection Method.cpp
File metadata and controls
49 lines (43 loc) · 1.08 KB
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
#include <bits/stdc++.h>
using namespace std;
#define error 0.0005
int a[100],n;
double f(double x){ // Using Horner's
double ans=0.0;
for(int i=n;i>=0;i--){
ans=(ans*x)+a[i];
}
return ans;
}
double bisection(double x, double y){
double x1=x, x2=y;
int i=1;
while(fabs(x1-x2)>error){
printf("I-%d: \n",i);
printf("X1: %lf F(x1): %lf\n",x1,f(x1));
printf("X2: %lf F(x2): %lf\n",x2,f(x2));
double x0=(x1+x2)/2.0;
printf("X0: %lf F(x0): %lf\n\n",x0,f(x0));
if(f(x1)*f(x0)>0)
x1=x0;
else if(f(x1)*f(x0)<0)
x2=x0;
i++;
}
return (x1+x2)/2.0;
}
int main(){
printf("Enter the degree of eqn: ");
scanf("%d",&n);
for(int i=n;i>=0;i--){
printf("Enter coefficient of x^%d: ",i);
scanf("%d",&a[i]);
}
printf("Enter Your Initial Value: ");
double x1,x2;
scanf("%lf%lf",&x1,&x2);
double ans=bisection(x1,x2);
printf("Root: %lf\n",ans);
printf("F: %lf\n",f(ans));
return 0;
}