Advertisement
Guest User

Untitled

a guest
May 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. public class Debug {
  2.  
  3.     public static void main(String... args) {
  4.        
  5.         // R(t) = tsin(0.2t)
  6.         Function function = new Function() {
  7.             public double calculate(double input) {
  8.                 // R(t) = tsin(0.2t)
  9.                 return input * Math.sin(0.2 * input);
  10.             }
  11.         };
  12.  
  13.         System.out.println(areaUnderCurve(0, 50, 1000000, function));
  14.     }
  15.  
  16.     /**
  17.      * This method will calculate the area under the curve of the provided function, which in this case, is
  18.      * R(t) = tsin(0.2t), representing by [input * Math.sin(0.2 * input)], input being 't' or 'time.'
  19.      *
  20.      * @param from - the beginning year
  21.      * @param to - the ending year
  22.      * @param rectangles - how many rectangles, or in this case how accurate it is
  23.      * @param function - the function
  24.      *
  25.      * @return the average of the rectangles, calculated from the right-edge and left-edge.
  26.      */
  27.     private static double areaUnderCurve(double from, double to, int rectangles, Function function) {
  28.         double interval = (to - from) / rectangles;
  29.  
  30.         double left = 0.0;
  31.         for (int i = 0; i < rectangles; i++) { // LEFT
  32.             System.out.println(((double) i / rectangles) * 100 + "% done.");
  33.             left += interval * function.calculate(from + (interval * i));
  34.         }
  35.  
  36.         System.out.print("\n\nFinished Left! Onto Right!\n\n");
  37.  
  38.         double right = 0.0;
  39.         for (int i = 1; i <= rectangles; i++) { // RIGHT
  40.             System.out.println(((double) i / rectangles) * 100 + "% done.");
  41.             right += interval * function.calculate(from + (interval * i));
  42.         }
  43.  
  44.         return (right + left) / 2;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement