Reputation: 95
I am trying to integrate the function 1/((1+x)(x^0.5)) between 0 and infinity in C using the trapezium rule. I understand that this is an improper integral and that the trapezium method isnt ideal but its what Ive been told to use. Rather than try to calculate straight between 0 and infinity I have tried to separate the integral into different limits and sum them up. For some reason the first iteration of the while loop (limits, 0 and 1) works and produces the correct answer (pi/2), but later ones do not.
Can someone please tell me where my code is
Upvotes: 0
Views: 234
Reputation:
sum
is not initialized. You got lucky on the first pass and it was 0. But the next time through the outer loop, it still has the value from the first time. It needs to be set to 0 before you enter the inner loop and start adding things up again.
Upvotes: 2