forked from veenits123/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBisection_Method.java
59 lines (50 loc) · 1.64 KB
/
Bisection_Method.java
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
//java program to find roots of equation using bisection method
public class BisectionMethod
{
//value for accuracy of root. more smaller it is more accurate will be root
static final float err= 0.01f;
// the equation whose roots are to be determined. in this case it is x^3-x^2+2
static double function(double x)
{
return x*x*x - x*x + 2;
}
// function to find root with error of err
static void bisection(double start, double end)
{
// check if root does not lies in range (start,end)
if (function(start) * function(end) >= 0)
{
System.out.println("You have not assumed"
+ " right start and end");
return;
}
double mid=0;
while ((end-start) >= err)
{
// finding mid point
mid = (start+end)/2;
// check if mid is root
if (function(mid) == 0.0)
break;
// to check te side where root lies.
else if (function(mid)*function(start) < 0)
end = mid;
else
start = mid;
}
//prints mid upto 4 digits of precision
System.out.printf("The value of root for above equation is : %.4f"
,mid);
}
// main program for finding roots
public static void main(String[] args)
{
// values assumed for start and end
double start =-500, end = 500;
bisection(start, end);
}
// This code is contributed by Pankaj Poply
}
//OUTPUT
//
//The value of root for above equation is : -0.9995