-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbisection 2.c
50 lines (43 loc) · 917 Bytes
/
bisection 2.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
// C++ program for implementation of Bisection Method for
// solving equations
#include<stdio.h>
#include<stdlib.h>
#define EPSILON 0.01
// An example function whose solution is determined using
// Bisection Method. The function is x^3 - x^2 + 2
double func(double x)
{
return x*x*x - x*x + 2;
}
// Prints root of func(x) with error of EPSILON
void bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
printf("You have not assumed right a and b\n");
return;
}
double c = a;
while ((b-a) >= EPSILON)
{
// Find middle point
c = (a+b)/2;
// Check if middle point is root
if (func(c) == 0.0)
break;
// Decide the side to repeat the steps
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
printf("The value of root is : %f",c);
}
// Driver program to test above function
int main()
{
// Initial values assumed
double a =-200, b = 300;
bisection(a, b);
return 0;
}