Troubleshooting a Trap: Debugging the Trapezium Rule

AI Thread Summary
The discussion revolves around troubleshooting a code implementation of the trapezium rule for numerical integration. The user is experiencing issues with the output, suspecting errors related to integer division and rounding. They initially consider the loop's range, questioning whether to iterate from 2 to N-2 and how to properly use summation notation in their for loop. Ultimately, the user identifies a critical flaw: a misunderstanding of the variable N's definition in their course notes versus its use in the code, which is likely causing the discrepancies in results. This highlights the importance of consistent variable definitions in programming and mathematical applications.
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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top