Last active
January 28, 2016 13:11
-
-
Save vvavrychuk/7e9b01040d1bb2ffee18 to your computer and use it in GitHub Desktop.
maven-sub-projects/na-lib/src/main/java/Integrate.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
import java.util.function.Function; | |
public class Integrate { | |
private double a; | |
private double b; | |
private int n; | |
public Integrate(double a, double b, int n) { | |
this.a = a; | |
this.b = b; | |
this.n = n; | |
} | |
public double weight(int j) { | |
double h = (b - a) / n; | |
if ((j == 1) || (j == n + 1)) | |
return 0.5 * h; | |
else | |
return h; | |
} | |
public double node(int j) { | |
return a + (b - a) / n * (j - 1); | |
} | |
public double integrate(Function<Double, Double> f) { | |
double value = 0; | |
for (int j = 1; j <= n + 1; j++) { | |
value += weight(j) * f.apply(node(j)); | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment