Numerical methods of integration

Click For Summary

Discussion Overview

The discussion revolves around implementing numerical methods of integration, specifically focusing on the rectangle method (midpoint rule) and the trapezoid rule. Participants are sharing code snippets, troubleshooting issues, and providing programming advice related to calculating the area under a graph.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a code snippet for the midpoint rule but encounters incorrect results, prompting a request for help.
  • Another participant suggests that the area is being computed incorrectly and provides a correction regarding the midpoint calculation for the rectangles.
  • A later reply acknowledges the correction and expresses relief at identifying the issue.
  • Another participant proposes using the trapezoid rule as a potentially better method for integration.
  • Multiple hints are provided regarding programming practices, such as using meaningful variable names, understanding signed vs unsigned integers, and the implications of integer division versus floating-point division.
  • One participant expresses familiarity with C programming and discusses type promotion in Java, suggesting that the original code may not have the issues previously mentioned.
  • A participant admits to being new to programming and acknowledges difficulties with integer division, but later confirms that their code produces the expected results.

Areas of Agreement / Disagreement

Participants generally agree on the need for corrections in the original code, but there are differing opinions on the implications of integer versus floating-point division and the effectiveness of the proposed methods. The discussion remains unresolved regarding the best approach to take for accurate integration.

Contextual Notes

Limitations include potential misunderstandings about variable types and division methods in programming languages, as well as the lack of consensus on the best numerical integration method to use.

Who May Find This Useful

Readers interested in numerical methods of integration, programming practices in Java, and troubleshooting coding issues related to mathematical computations may find this discussion beneficial.

whatisreality
Messages
286
Reaction score
1
Mod note: Removed the color formatting, and surrounded the code with [ code ] tags.
@whatisreality, please use a
Java:
 tag at the beginning of your code, and a
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
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   Reactions: whatisreality
Oh, I knew it would be something stupid. Thanks, that was the problem!
 
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   Reactions: whatisreality
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   Reactions: whatisreality and jim mcnamara
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   Reactions: 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!
 

Similar threads

  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
10
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K