-2

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.

    }
}
peter_gent
  • 175
  • 2
  • 4
  • 17
  • 2
    There are some questions which should be asked to classmates or responsible professor before being asked on StackOverflow. For questions on StackOverflow, we prefer that you show some progress of understanding the problem and show what you have tried. – Simon Forsberg Oct 23 '13 at 15:21
  • numerical methods in Java sounds so painful... That being said I don't think this is an SO related question... – Mateusz Dymczyk Oct 23 '13 at 15:22
  • @MateuszDymczyk that's why they have an interface for it, instead of making you interpret it from a string. You write the function right in java code. – Cruncher Oct 23 '13 at 15:23
  • It seems you don't know what a Java Interface is. http://stackoverflow.com/questions/504904/how-are-java-interfaces-actually-used – But I'm Not A Wrapper Class Oct 23 '13 at 15:23
  • @SimonAndréForsberg that might even be an official close reason - separate from the "did not do anything to solve it". Meta? – Thorbjørn Ravn Andersen Oct 23 '13 at 15:26
  • `public interface Function {B apply(A arg); }` What I don't understand is why isn't this: `public interface Function {double apply(double arg);}`. And if it does use generics, why doesn't the interface accept generic arguments? That lab posted by your professor could be greatly improved... – Cruncher Oct 23 '13 at 15:27
  • @ThorbjørnRavnAndersen Not sure what you mean there. Are you suggesting I should add a feature-request on meta to add "There are some questions which should be asked to classmates or responsible professor before being asked on StackOverflow" as an official close reason? – Simon Forsberg Oct 23 '13 at 15:28
  • @Cruncher you can use all the interfaces you want to, doesn't change the fact that I feel sympathetic for everyone who has to take a numerical methods/analysis class in Java ;-) – Mateusz Dymczyk Oct 23 '13 at 15:30
  • @MateuszDymczyk I like java :(, but perhaps a language with function pointers would be better for this particular job/course. – Cruncher Oct 23 '13 at 15:43
  • @SimonAndréForsberg something ikke that, yes. – Thorbjørn Ravn Andersen Oct 23 '13 at 16:10
  • I've added progress towards the goal. – peter_gent Dec 18 '13 at 18:47

1 Answers1

1

It looks like you are being asked to first create an interface for functions. Your functions take something of type A as a parameter and return something of type B. It might look something like this:

// here I am using Float for both A and B
Function<Float, Float> myFunc = new SineFunction();
float result = myFunc.apply(1.0f);

Once you've done this, you are asked to write a function to integrate any other function. Let's say I want to integrate sin(x) between x=0 and x=pi/2. That is what is meant by lower and upper bounds (0 is lower, pi/2 is upper). So your integrate function should act on some Function over some range like 0 to pi/2 using some n number of steps.

That's as much as I'm going to say without giving you any answers. good luck!

wrongu
  • 560
  • 3
  • 13