Taken from a lab :
The aim of this exercise is to implement the midpoint-rule (also called rectangle method) >for the numerical integration of functions.
Declare a generic interface Function as follows:
public interface Function {B apply(A arg); }
Then write a class Maths with a method integrate that implements the midpoint rule for a >function f that is to be integrated between lowerBound and upperBound in n equally-spaced >steps:
public static double integrate(Function f, double lowerBound, double upperBound, int n)
Test your code by numerical integration ofthe square-function between 0 and 1 (result should be approximately 1.0/3.0)
the sinus-function between 0 and PI/2 (see class java.lang.Math, result should be > approximately 1.0) It seems natural to use named local classes (say "Sinus" and "Square") for this exercise, > but you can also use anonymous local classes if you prefer.
I looked on the wikipedia page but I can't really get my head around the entire concept (especially what is meant by upper and lower bounds). Afaik it's just the way to get the area under a line, given N rectangles used to get the area for each tangential point on the line. I've no idea how to relate it to the above function - recursion maybe? and I've also never seen anything like :
B apply(A arg)
--
So far I've made this progress:
public class MidPointRule {
public MidPointRule() {
}
public static double integrate(Function<Double,Double> f, double lowerBound,double upperBound, int n){
double width = upperBound-lowerBound/n; // width is current width-nextwidth/n?
return integrate(f,lowerBound,upperBound,n);
}
public static void main(String[] args) {
// sq function between 0 and 1, sinus function between 0 and PI/2.
}
}