Skip to content

Instantly share code, notes, and snippets.

@vvavrychuk
Last active January 28, 2016 13:11
Show Gist options
  • Save vvavrychuk/7e9b01040d1bb2ffee18 to your computer and use it in GitHub Desktop.
Save vvavrychuk/7e9b01040d1bb2ffee18 to your computer and use it in GitHub Desktop.
maven-sub-projects/na-lib/src/main/java/Integrate.java
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