-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRidderSolver.java
78 lines (59 loc) · 2.32 KB
/
RidderSolver.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class RidderSolver {
public static double calculateRoot(double xLowerBound, double xUpperBound, double Tolerance, double maxIterations,
RootFinder myRoot) {
//Setting the error to a large value being 1.00 and initialize the number of iterations to 0.
double error = 1.00;
int numberofIterations = 0;
//Initializing XM, XR, newXR.
double XM = calculateXM(xLowerBound, xUpperBound);
double XR = calculateXR(xLowerBound, xUpperBound, myRoot);
double newXR = 0;
//Do-While loop for the computation of the Ridder's algorithm.
do {
if ((XR < XM) && ((myRoot.findX_AI(xLowerBound) * myRoot.findX_AI(XR)) < 0)) {
xUpperBound = XR;
} else if ((XR < XM) && (myRoot.findX_AI(XR) * myRoot.findX_AI(XM)) < 0) {
xLowerBound = XR;
xUpperBound = XM;
} else if (XR < XM && (myRoot.findX_AI(XM) * myRoot.findX_AI(xUpperBound)) < 0) {
xLowerBound = XM;
}
else if (XR > XM && (myRoot.findX_AI(xLowerBound) * myRoot.findX_AI(XR)) < 0) {
xUpperBound = XM;
} else if (XR > XM && (myRoot.findX_AI(XR) * myRoot.findX_AI(XM)) < 0) {
xLowerBound = XM;
xUpperBound = XR;
} else if (XR > XM && (myRoot.findX_AI(XM) * myRoot.findX_AI(xUpperBound)) < 0) {
xLowerBound = XR;
}
else {
return xUpperBound;
}
newXR = calculateXR(xLowerBound, xUpperBound, myRoot);
error = Math.abs((newXR - XR) / (newXR));
numberofIterations++;
XR = newXR;
XM = calculateXM(xLowerBound, xUpperBound);
//Do-While Loop flow control to
} while (error > Tolerance && numberofIterations < maxIterations);
//returning the XR_a
return XR;
}
//Method to calculate XR.
public static double calculateXR(double xLowerBound, double xUpperBound, RootFinder myRoot) {
double XM = calculateXM(xLowerBound, xUpperBound);
double numerator = Math.signum((myRoot.findX_AI(xLowerBound) - myRoot.findX_AI(xUpperBound)))
* myRoot.findX_AI(XM);
double denominator = Math
.sqrt(Math.pow(myRoot.findX_AI(XM), 2) - myRoot.findX_AI(xLowerBound) * myRoot.findX_AI(xUpperBound));
double XR = XM + (XM - xLowerBound) * numerator / denominator;
return XR;
}
//Method to calculate XM.
public static double calculateXM(double xLowerBound, double xUpperBound) {
return (xLowerBound + xUpperBound) / 2;
}
public RidderSolver clone() {
return new RidderSolver();
}
}