Numerical methods of integration

In summary, the conversation is about a user asking for help with writing a code to calculate the area under a graph using the rectangle or midpoint method. Other users provide tips and suggestions for improving the code, such as making variables more meaningful, using signed vs unsigned ints, and being careful with math operations involving different types of numbers. The conversation also touches on the difference between integer and floating point division and the importance of using type casts. Overall, the conversation provides helpful hints for writing more efficient and accurate code.
  • #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:
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:
Technology news on Phys.org
  • #2
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.
 
  • Like
Likes whatisreality
  • #3
Oh, I knew it would be something stupid. Thanks, that was the problem!
 
  • #4
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.
 
  • Like
Likes whatisreality
  • #5
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:
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.
Java:
double h =(b-a)/N;
This will not give you the result you expect.
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.
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.
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.
 
  • Like
Likes whatisreality and jim mcnamara
  • #6
newjerseyrunner said:
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:
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.
Java:
double h =(b-a)/N;
This will not give you the result you expect.
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.
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.
newjerseyrunner said:
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.
Java:
integral = integral + h*f(a+0.5*(b-a)+h*(i-1));
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.
newjerseyrunner said:
Be careful of this when using numbers too, there is a very big difference between 1 and 1.0 to a computer language.
 
  • Like
Likes whatisreality
  • #7
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!
 

FAQ: Numerical methods of integration

What is numerical integration and when is it used?

Numerical integration is a method for approximating the value of a definite integral using numerical techniques. It is used when the integral cannot be evaluated analytically or when the function is too complex to integrate by hand.

What are the advantages of numerical integration?

Numerical integration allows for the evaluation of complex integrals that cannot be solved analytically. It also provides a more accurate result compared to approximating the integral using basic geometric shapes.

What are the different types of numerical integration methods?

There are several types of numerical integration methods, including the Trapezoidal Rule, Simpson's Rule, and Gaussian Quadrature. Each method has its own advantages and disadvantages and is used depending on the complexity of the function and the desired level of accuracy.

How do numerical integration methods work?

Numerical integration methods work by dividing the interval of integration into smaller subintervals and approximating the area under the curve within each subinterval. The approximation is then summed up to get an estimate of the total area under the curve.

What are some common sources of error in numerical integration?

Some common sources of error in numerical integration include rounding errors, truncation errors from using a finite number of subintervals, and the choice of integration method. Improper selection of the integration method can lead to significant errors in the final result.

Similar threads

Replies
35
Views
3K
Replies
1
Views
963
Replies
36
Views
4K
Replies
7
Views
1K
Replies
7
Views
2K
Back
Top