# Numerical methods of integration

1. Nov 2, 2015

### whatisreality

Mod note: Removed the color formatting, and surrounded the code with [ code ] tags.
@whatisreality, please use a [code=java] tag at the beginning of your code, and a [/code] tag at the end. Inserting colors by hand is distracting and makes your code difficult to read.

I have to write a piece of code to calculate the area under a graph, using the 'rectangle method' or midpoint method, where you divide the area into lots of little rectangles and take the height to be the value of the midpoint. I've written:
Code (Java):
public static double midpointRule (double a, double b, int N){  // a is the lower limit, b the upper
int i;
double integral = 0;

for(i=0; i<N; i++){
double h = (b-a)/N;        //  h is the width of each rectangle, N the no. of rectangles
integral = integral + h*f(a+0.5*(b-a)+h*(i-1));  //  width*height = area
//  height = f(midpoints) = f(a+0.5*(b-a) + h*(i-1))]
}

return integral;

}
Why does this not work?! It compiles, just gives the wrong value!

Last edited by a moderator: Nov 2, 2015
2. Nov 2, 2015

### RUber

It looks like you are computing the area incorrectly.
for i = 0, you want to take the rectangle from a to a+h, which has midpoint a + h/2.
for i = 1, you want to take the rectangle from a+h to a+2h which has midpoint a + h + h/2, and so on.
So in your line for integral, where you have integral + h* f(a+ .5*(b-a)+h*(i-1)), you are starting halfway through your interval. Try replacing (b-a) with h.

3. Nov 2, 2015

### whatisreality

Oh, I knew it would be something stupid. Thanks, that was the problem!

4. Nov 3, 2015

### Svein

A better way is to use the trapezoid rule: The area between a and a+h is approximated by hâ‹…(f(a)+f(a+h))/2.

5. Nov 3, 2015

### newjerseyrunner

You still aren't going to get the right answer, why will be explained later, but since I can tell that you're fairly new to programming, I'll give you a bunch of hints.

Here is a couple of helpful hints:

1) Make your variables' names more meaningful, a, b, N don't mean anything. Your IDE will often autocomplete function names for you and give you the list of parameters, you should be able to know what the parameters mean without having to look at the code.

2) Utilize the signed vs unsigned ints, they are different for a reason, and as it stands right now you have a potential infinite loop in your code if you pass a negative value for N.

3) This is a personal preference, but most programmers prefer self modifying assignments rather than assigning a variable to itself plus something:
Code (Java):
//Assignment of a temp
integral = integral + value;

//Self modification
integral += value;

4) This is why you won't get the right answer: be careful when doing math with numbers of different types.
Code (Java):
double h =(b-a)/N;
This will not give you the result you expect.
Code (Java):
double b_minus_a = 7.0;
int N = 2;
double h = b_minus_a / N;  //What value will h be?
h will have the value... 3, not 3.5. Why? Because you divided by an int, which is done differently than floating point division. You need a type cast.
Code (Java):
double h = b_minus_a / (double)N;
This i-1 in here, will also give you an integer, which will propagate throughout your math and is quite difficult to debug.
Code (Java):
integral = integral + h*f(a+0.5*(b-a)+h*(i-1));

Be careful of this when using numbers too, there is a very big difference between 1 and 1.0 to a computer language.

6. Nov 3, 2015

### Staff: Mentor

I'm much more familiar with C and its direct descendants than I am with Java, but I don't think the above is a problem. b and a are declared as type double, so the division that is performed in the first line above will be floating point division, as the value of N will be promoted to type double.
I don't think that multiplying h by i - 1 will be a problem, either. h is declared as type double, so the expression i - 1 is promoted to a double before the multiplication is done. The assignment h = (b - a)/N; would be a problem if a and be were of some integral type.

7. Nov 7, 2015

### whatisreality

I am very new to it. Yeah, I've had a few problems with integer division! Anyway, I had an expected answer to compare the answer from my code to, and it seems to work. Thanks!