Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static double findAreaUnderCurve(double from, double to, int rectangles, Function function) {
- double area = 0.0; // Current Area
- double length = (to - from) / rectangles; // Length of each rectangle
- // HIGHER
- // r is the beginning + length, and while it is less than the end (to), continue to preform the equation and add
- // the length every time it loops.
- for (double r = from + length; r < to; r += length) {
- area += length * function.calculate(r);
- // function.calculate(r) means it uses the respective equation in https://pastebin.com/kpGC5mCN
- }
- // LOWER
- // r is the beginning + length, and while it is less than the end (to), continue to preform the equation and add
- // the length every time it loops.
- for (double r = from; r <= to; r += length) {
- area += length * function.calculate(r);
- // function.calculate(r) means it uses the respective equation in https://pastebin.com/kpGC5mCN
- }
- return area / 2; // Divide by 2 for average
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement