-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisection.cpp
81 lines (79 loc) · 1.38 KB
/
bisection.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <limits.h>
#include <set>
#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <utility>
#define maxn 1005
using namespace std;
int n;
double co[maxn];
int flag;
double f(double x) {
double v=1,ans=0;
for(int i=0; i<=n; i++) {
ans+=v*co[i];
v*=x;
}
return ans;
}
double bisection(double a,double b,double tol) {
flag=true;
if (f(a)*f(b)>=0) {
cout << "You have not assumed right a and b\n";
flag=false;
return 0;
}
int tf=0;
double c,pc;
while(true) {
c=(a+b)/2;
if (f(c) == 0.0)
break;
else if (f(c)*f(a)<0.000)
b=c;
else
a=c;
if(tf) {
double ftol=fabs(pc-c);
if(ftol<0) {
ftol*=(-1.00);
}
ftol/=fabs(c);
if(ftol<=tol) {
return c;
}
}
tf=1;
pc=c;
}
}
int main() {
FILE *fpw,*fpr;
fpr = fopen("input.txt", "r+");
fpw = fopen("output.txt", "w+");
double a=-5000,b=5000;
fscanf(fpr, "%d", &n);
cout<<n<<endl;
for(int i=n; i>=0; i--) {
fscanf(fpr, "%lf", &co[i]);
cout<<co[i]<<" ";
}
puts("");
double ret=bisection(a,b,0.0000005);
cout<<"Root : "<<ret<<endl;
printf("Approximate value : %.8lf\n",f(ret));
fprintf(fpw, "Root : %.8lf \nApproximate value : %.8lf\n",ret,f(ret));
return 0;
}