i have made a program that makes an approximation of the integral with Gaussian Quadrature method. The question I got in my homework is why the integral in the function integr(x) from 0 to 1 is difficult to approximate? The value of approximation is around 156.08. I thought it had something to do with the range, but no matter if I set n to be 25 or 90, the output at the last iteration will always be around 156. Sorry if the question don't belong here.
Asked
Active
Viewed 98 times
0
-
1oh yes! will do now:) – AlphaList Oct 28 '21 at 22:34
-
2Why it's difficult to approximate in general or difficult for this method to approximate? – CJR Oct 28 '21 at 22:35
-
1Your code gives 156.07966 for n = 100. WolframAlpha [says 156.08](https://www.wolframalpha.com/input/?i=integrate+1%2F%28x**2%2B0.0001%29+from+0+to+1). That looks pretty good to me. What's wrong about it? – Marco Bonelli Oct 28 '21 at 22:37
-
1@CJR with this method:) – AlphaList Oct 28 '21 at 22:37
-
@MarcoBonelli, yes the code is working, but in my assignement there is a question "Why is this integral difficult to approximate?", and I'm a bit lost what the answer to that is :) – AlphaList Oct 28 '21 at 22:39
-
Does this method require your function to be well defined across an interval? Because you are very close to a division by zero. – CJR Oct 28 '21 at 22:39
-
2@AlphaList well, not sure if we can even answer that. This website is about programming, your question seems more about math than programming... – Marco Bonelli Oct 28 '21 at 22:39
-
@CJR yes the interval is important :) – AlphaList Oct 28 '21 at 22:46
-
@MarcoBonelli yes sorry, it's both math/programming question so when I asked on a math site they couldn't answer because of the code. My teacher wrote that the hint is to look at the range, so therefore I tried to ask here:) – AlphaList Oct 28 '21 at 22:46
-
Your teacher is giving you an unclear question to be honest. In any case, any program that has to deal with the approximation of integrals near asymptotes (like yours which has an asymptote in 0) will have an hard time using floating point math (e.g. `float` in Python) because such large values of y cannot be represented precisely enough using floating point numbers. See [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). – Marco Bonelli Oct 28 '21 at 22:50
-
I'm not giving you the answer cause it's for homework but you should think about what small changes in function interval or weight near an infinite slope would do – CJR Oct 28 '21 at 22:52
-
@MarcoBonelli Alright, thank you, I will take a look at the link also:) – AlphaList Oct 28 '21 at 22:54
-
@CJR thank you! I will try to edit my code and see what happens:) – AlphaList Oct 28 '21 at 22:55
-
*cough* *cough* this is how you get plagiarized , im taking the same course as you and i don't recommend putting code up like this because someone is bound to search for the solution online and use this :) – Marcus F Oct 29 '21 at 07:05
-
also btw, the *n* value is degrees of the polynomial youre trying to approximate, therefore setting the n value to a 100 doesn't make any logical sense because it has to be set to 2, thats how the gaussian quadrature algorithm works. – Marcus F Oct 29 '21 at 07:08
1 Answers
3
Probably due to basic floating-point arithmetic. Up to a point, if you set n
sufficiently high, you'll end up at something like 156.0796666... A good alternative for numerical stability is to perform your calculations in log-space instead, if/whenever possible.

hyit
- 496
- 4
- 10
-
1Okay, so if I understood correctly, the reason it is difficult to approximate is because if I want to have a good approximation, I have to set n to a high number and my function will end up making many iterations and therefore take longer time? – AlphaList Oct 28 '21 at 22:49
-
1It is more whatever big `n` is the next iterations are too small to be summed properly due to float arithmetic error. Therefore it cannot be improved to a certain threshold only by increasing `n`. – jlandercy Oct 28 '21 at 23:07
-
1Exactly as @jlandercy said. At some point, the delta u,v values you're summing are so small, they lead to underflow and effectively contribute nothing to the overall integral. A good place to start reading is the [accuracy section](https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems) on floating point arithmetic. There are otherwise many sources on numerical stability online, most of which (to the best of my knowledge) suggest log-space calculations if such accuracy is desired. – hyit Oct 29 '21 at 13:00