-
Notifications
You must be signed in to change notification settings - Fork 0
/
bisection
66 lines (58 loc) · 1.29 KB
/
bisection
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
#include <stdio.h>
#include <math.h>
#define EPSILON 0.001
float func (float x)
{
float y = (x*x*x) - (x*x) + 2;
return y;
}
void bisection(float a, float b)
{
float c=a;
if(func(a) * func(b) > 0)
{
printf("Incorrect Initial Guess\n");
return;
}
else if(func(a) * func(b) == 0)
{
printf("Root is either %f or %f\n", a, b);
if(func(a) == 0)
{
printf("Root is %f\n", a);
}
else
{
printf("Root is %f\n", b);
}
return;
}
while((b-a) >= EPSILON){
//printf("%d %d", a,b);
c = (a + b)/2.0;
printf("Mid point is %f\n", c);
if(func(c) == 0.0)
{
printf("Root is %f\n", c);
break;
}
else if(func(a) * func(c) < 0)
{
b = c;
}
else
{
a = c;
}
}
printf("Root is %f ", c);
}
int main()
{
float a, b;
printf("f(x) = x^3 - x^2 + 2\n");
printf("Enter the interval (values of a and b): ");
scanf("%f %f", &a, &b); //(-200,300) = -1
bisection(a, b);
return 0;
}