Skip to content

Commit e911396

Browse files
committed
Made integals class fully functional, implementing an adaptive simpson's rule algorithm
1 parent d235f64 commit e911396

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

GraphingCalculator/src/jonah/Integral.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,28 @@ public Integral(double xVal, ArrayList<Object> function, double lowerBound, doub
1717
equation = new Function(function);
1818
}
1919

20-
public double evaluate(){
21-
int lower = (int)(lowerBound);
22-
int higher = (int)(higherBound);
23-
double sum = 0;
24-
return sum;
20+
public double evaluate(){
21+
double originalValue = findIntegral(lowerBound, higherBound);
22+
return integrate(lowerBound, higherBound, originalValue, 1);
23+
}
24+
25+
public double integrate(double lower, double higher, double originalValue, int depth) {
26+
double midpoint = (lower + higher)/2;
27+
double s1 = findIntegral(lower, midpoint);
28+
double s2 = findIntegral(midpoint, higher);
29+
if(Math.abs(s1 + s2 - originalValue) < 0.001 | depth > 15) {
30+
return s1 + s2;
31+
} else {
32+
return integrate(lower, midpoint, s1, depth + 1) + integrate(midpoint, higher, s2, depth + 1);
33+
}
34+
}
35+
36+
private double findIntegral(double lower, double higher) {
37+
double deltaX = (higher - lower)/6;
38+
double midpoint = (higher + lower)/2;
39+
double fa = equation.evaluate(lower, new ArrayList<Object>(function), 0);
40+
double fmidpoint = 4 * equation.evaluate(midpoint, new ArrayList<Object>(function), 0);
41+
double fb = equation.evaluate(higher, new ArrayList<Object>(function), 0);
42+
return deltaX * (fa + fmidpoint + fb);
2543
}
2644
}
621 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)