Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Debug {
- public static void main(String... args) {
- // R(t) = tsin(0.2t)
- Function function = new Function() {
- public double calculate(double input) {
- // R(t) = tsin(0.2t)
- return input * Math.sin(0.2 * input);
- }
- };
- System.out.println(areaUnderCurve(0, 50, 1000000, function));
- }
- /**
- * This method will calculate the area under the curve of the provided function, which in this case, is
- * R(t) = tsin(0.2t), representing by [input * Math.sin(0.2 * input)], input being 't' or 'time.'
- *
- * @param from - the beginning year
- * @param to - the ending year
- * @param rectangles - how many rectangles, or in this case how accurate it is
- * @param function - the function
- *
- * @return the average of the rectangles, calculated from the right-edge and left-edge.
- */
- private static double areaUnderCurve(double from, double to, int rectangles, Function function) {
- double interval = (to - from) / rectangles;
- double left = 0.0;
- for (int i = 0; i < rectangles; i++) { // LEFT
- System.out.println(((double) i / rectangles) * 100 + "% done.");
- left += interval * function.calculate(from + (interval * i));
- }
- System.out.print("\n\nFinished Left! Onto Right!\n\n");
- double right = 0.0;
- for (int i = 1; i <= rectangles; i++) { // RIGHT
- System.out.println(((double) i / rectangles) * 100 + "% done.");
- right += interval * function.calculate(from + (interval * i));
- }
- return (right + left) / 2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement