-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBisection method in c
59 lines (46 loc) · 1.16 KB
/
Bisection method in 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
#include <stdio.h>
#include <math.h>
double f(double x)
{
return (x*log10(x))-1.2;
}
int main()
{
double a,b,e;
printf("Enter 1st root = ");
scanf("%lf",&a);
printf("\nEnter 2nd root = ");
scanf("%lf",&b);
printf("\nDefine the input accuracy: = ");
scanf("%lf",&e);
double x;
if(f(a)*f(b)<=0)
{
int iter = 1;
printf (" Iteration \t a \t \tb \t\tx \t\tf(x) \t |a - b| \n ");
do {
x = (a+b) /2.0;
printf (" %d \t %lf \t %lf \t %lf \t %lf \t %lf \n", iter, a, b, x, f(x), fabs(a- b));
if (f(x) == 0)
{
x = f(x);
break;
}
if (f(a)*f(x)>0)
{
a = x;
}
else if (f(a)* f(x) < 0)
{
b = x;
}
iter++;
}
while (fabs(a-b)>=e );
printf (" \nThe root of the given equation is: %lf", x);
}
else
{
printf (" \n The root doesn't exist in the given interval. ");
}
}