Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Last active June 4, 2017 16:06
Show Gist options
  • Save LuxXx/1ed78935f8216cb18774182ff981f4c6 to your computer and use it in GitHub Desktop.
Save LuxXx/1ed78935f8216cb18774182ff981f4c6 to your computer and use it in GitHub Desktop.
Riemann Integral
public class Partition {
private double a;
private double b;
private double N;
public Partition(double a, double b, double N) {
super();
this.a = a;
this.b = b;
this.N = N;
}
public double x(int n) {
double len = b - a;
double z = len / N;
return a + n * z;
}
public double integrate(Function f) {
double sum = 0;
for (int i = 1; i < N; i++) {
sum += f.f(x(i)) * (x(i) - x(i-1));
}
return sum;
}
@FunctionalInterface
interface Function {
public double f(double x);
}
public static void main(String[] args) {
double d = new Partition(0, Math.PI, 100000).integrate(x -> Math.sin(x));
System.out.println(d);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment