Troubleshooting a Trap: Debugging the Trapezium Rule

Click For Summary
SUMMARY

The discussion centers on debugging the implementation of the trapezium rule for numerical integration in Java. The user identified issues related to the loop boundaries and the definition of the variable N, which caused incorrect results. The code calculates the integral using trapezoidal approximation but initially misinterpreted the summation notation and the limits of iteration. The flaw was traced back to a misunderstanding of the variable N's definition in the course notes versus its usage in the code.

PREREQUISITES
  • Understanding of numerical integration techniques, specifically the trapezium rule.
  • Proficiency in Java programming, particularly with loops and methods.
  • Familiarity with mathematical notation and summation concepts.
  • Knowledge of debugging techniques in programming.
NEXT STEPS
  • Review the implementation of the trapezium rule in Java for numerical integration.
  • Learn about the differences between integer and floating-point arithmetic in Java.
  • Explore Java's debugging tools to identify and fix logical errors in code.
  • Study mathematical summation notation and its application in programming loops.
USEFUL FOR

Mathematicians, software developers, and students learning numerical methods who are looking to implement and debug the trapezium rule in Java.

whatisreality
Messages
286
Reaction score
1
This is not giving me the right answer! I have checked for errors like integer division and rounding, but can't find any. I am aware the comments are slightly inaccurate and they will be improved. But what's wrong with the actual code?
Java:
//This method uses the trapezium rule to calculate the integral
   //a is the lower limit, b the upper limit, c is the power of x in the function, N the number of trapezia
   //Returns the result of the integral
   public static double trapeziumRule(double a, double b, double c, int N){

     double h = (b-a)/(N-1);             //h is the width of a single trapezium
     double x = 0.5*(f(a,c)+f(b,c));           //x is the average height of the trapezium
     for(int i=2; i<N-1; i++){

       x += f(a+h*i,c);
     }
     double integral = h*x;
     return integral;
It's probably something in the actual maths... should i run from 2 to N-2? I clearly don't know how to use sum notation properly, since I can't convert ##\sum_{i=2}^{N-1}## into a for statement correctly.

Actually, is that the problem? Should it be i<=N-1? And i starts from 1? That would also affect my formula for h.

Except that doesn't work either.
 
Last edited:
Technology news on Phys.org
Whoops, found the flaw. Difference in the meaning of N in my course notes and the meaning of N in my code!
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
6
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
1
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K