- #1
whatisreality
- 290
- 1
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:
Why does this not work?! It compiles, just gives the wrong value!
@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:
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;
}
Last edited by a moderator: