-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1_Bisection.c
70 lines (61 loc) · 1.47 KB
/
day1_Bisection.c
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
/*
abhishek madhu
*/
#include <stdio.h>
double f(double x){
return ( (x*x*x) - (3*x) + (1.06) );
}
int main(){
double c,a,b,ans,approx,k;
int flag=0;
char choice = 'N';
approx=0.000010;
a=0.0;
b=1.0;
printf("Enter the approximation range you wish to attain:\t");
scanf("%f", &approx);
printf("Want to provide custom interval?(Y/N):\t");
fflush(stdin);
scanf("%c", &choice);
if(choice=='Y' || choice=='y'){
printf("Enter the value of a and b separated by a space:\t");
fflush(stdin);
scanf("%f%f", &a,&b);
}
else{
printf("Set to defaults ( a=0 | b=1 )\n");
a=0.0;
b=1.0;
}
do{
c=(a+b)/2;
k=f(c);
printf("c=%f\n", c);//
if(k==0){
ans=c;
flag=1;
break;
}
else if(fabs(k) <= approx){
printf("%f is an approximate root with range +-%f\n", c,approx);
printf("The value of f(x) for this root is %f.\n", k);
return 0;
}
else{
if(k>0){
a=c;
}
else{
b=c;
}
}
}while(fabs(k) > approx && a<b);
if(flag){
printf("Root is %f.\n", ans);
}
else{
printf("Root Not Found in the given approximation.\n");
printf("Last value of f(c) = %f | c=%f | a=%f | b=%f |\n", k,c,a,b);
}
return 0;
}