Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1.     public static double findAreaUnderCurve(double from, double to, int rectangles, Function function) {
  2.         double area = 0.0; // Current Area
  3.         double length = (to - from) / rectangles; // Length of each rectangle
  4.  
  5.         // HIGHER
  6.         // r is the beginning + length, and while it is less than the end (to), continue to preform the equation and add
  7.         // the length every time it loops.
  8.         for (double r = from + length; r < to; r += length) {
  9.             area += length * function.calculate(r);
  10.             // function.calculate(r) means it uses the respective equation in https://pastebin.com/kpGC5mCN
  11.         }
  12.  
  13.         // LOWER
  14.         // r is the beginning + length, and while it is less than the end (to), continue to preform the equation and add
  15.         // the length every time it loops.
  16.         for (double r = from; r <= to; r += length) {
  17.             area += length * function.calculate(r);
  18.             // function.calculate(r) means it uses the respective equation in https://pastebin.com/kpGC5mCN
  19.         }
  20.  
  21.         return area / 2; // Divide by 2 for average
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement