-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBisectionmethod (1).java
More file actions
53 lines (46 loc) · 1.38 KB
/
Copy pathBisectionmethod (1).java
File metadata and controls
53 lines (46 loc) · 1.38 KB
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
// Java program for implementation of Bisection Method
// for solving equations
class Bisectionmethod{
static final float EPSILON = (float)0.01;
// An example function whose solution is determined using
// Bisection Method. The function is x^3 - x^2 + 2
static double func(double x)
{
return x*x*x - x*x + 2;
}
// Prints root of func(x) with error of EPSILON
static void bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
System.out.println("You have not assumed"
+ " right a and b");
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;
}
//prints value of c upto 4 decimal places
System.out.printf("The value of root is : %.4f"
,c);
}
// Driver program to test above function
public static void main(String[] args)
{
// Initial values assumed
double a =-200, b = 300;
bisection(a, b);
}
// This code is contributed by Nirmal Patel
}