Created
October 14, 2017 17:15
-
-
Save JoseRivas1998/f6642e1e8dcea665b12e0f7264d3e088 to your computer and use it in GitHub Desktop.
Definite Integral in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface Function { | |
| public double f(double x); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainWithLambda { | |
| public static final double INCREMENT = 1E-4; | |
| public static void main(String[] args) { | |
| System.out.println(integral(0, 2, x -> { | |
| return Math.pow(x, 2); | |
| })); | |
| } | |
| public static double integral(double a, double b, Function function) { | |
| double area = 0; | |
| double modifier = 1; | |
| if(a > b) { | |
| double tempA = a; | |
| a = b; | |
| b = tempA; | |
| modifier = -1; | |
| } | |
| for(double i = a + INCREMENT; i < b; i += INCREMENT) { | |
| double dFromA = i - a; | |
| area += (INCREMENT / 2) * (function.f(a + dFromA) + function.f(a + dFromA - INCREMENT)); | |
| } | |
| return (Math.round(area * 1000.0) / 1000.0) * modifier; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MainWithoutLambda { | |
| public static final double INCREMENT = 1E-4; | |
| public static void main(String[] args) { | |
| System.out.println(integral(0, 2, new Function() { | |
| @Override | |
| public double f(double x) { | |
| return Math.pow(x, 2); | |
| } | |
| })); | |
| } | |
| public static double integral(double a, double b, Function function) { | |
| double area = 0; | |
| double modifier = 1; | |
| if(a > b) { | |
| double tempA = a; | |
| a = b; | |
| b = tempA; | |
| modifier = -1; | |
| } | |
| for(double i = a + INCREMENT; i < b; i += INCREMENT) { | |
| double dFromA = i - a; | |
| area += (INCREMENT / 2) * (function.f(a + dFromA) + function.f(a + dFromA - INCREMENT)); | |
| } | |
| return (Math.round(area * 1000.0) / 1000.0) * modifier; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment