|
New algorithm for numerical integration +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (https://www.hpmuseum.org/forum/forum-10.html) +--- Forum: HP Prime Software Library (https://www.hpmuseum.org/forum/forum-15.html) +--- Thread: New algorithm for numerical integration (/thread-24433.html) |
New algorithm for numerical integration - Namir - 2025-12-08 11:53 Here is a new algorithm for numerical integration. The method divides the main integration internal into small sub-intervals. For each sub-interval, the function picks three points (lower limit, middle point, and upper limit) and gets a quadratic fit for these points. It then calculates the area under the sub-interval using the quadratic approximation. The approximation for the total integral is the sum of all the sub-interval areas. The function FX(x) define the function to be integrated. The number of interval divisions is influenced by the smoothness of the integrated function. Functions that show much variations and fluctuation require a higher number of sub-intervals. Code: EXPORT FX(x)fitArea_LSQ(1,10,10) gives 2.30356496042 fitArea_LSQ(1,10,40) gives 2.30259027917 compared to LN(10) = 2.30258509299 RE: New algorithm for numerical integration - Albert Chan - 2025-12-08 14:26 (2025-12-08, 11:44)Namir Wrote: fitArea_LSQ(1,10,10) gives 2.30356496042 It looked like Simpson's Rule, not new. SHARP EL-506W, showing all digits X [x-1] [∫dx] a=1 b=10 n=10 --> 2.303564960418 X [x-1] [∫dx] a=1 b=10 n=40 --> 2.302590279162 RE: New algorithm for numerical integration - Valentin Albillo - 2025-12-08 20:29 Hi, Namir, (my highlights) (2025-12-08, 11:44)Namir Wrote: Here is a new algorithm for numerical integration. This new algorithm of yours is akin to ye olde Simpson's rule so not exactly new, and as a matter of fact it's implemented inefficiently. For instance, you're calling the function FX three times per subinterval Quote:y1 := FX(x1); without taking account of the fact that the y1 of the current subinterval after the first one is just the y3 of the previous subinterval so there's no need to call FX again, it's wasteful, just assign the already computed value. Also, you say Quote:// Simplified version using built-in least squares fitting but computing a least squares fit of a polynomial of degree 2 (a parabola) to exactly 3 points is not a least squares *regression* fit, it's just plain *collocation* as the polynomial will pass through all 3 points alright and the "least squares" error is guaranteed to be zero. Thus, calling a built-in least squares procedure is unneeded and again wasteful. Last but not least, your algorithm evaluates FX at the extremes of the interval, which begets the well-known problems when FX is undefined or infinite there, usually resulting in show-stopping errors. Anyway, even a perfectly implemented Simpson's rule is just too obsolete and inefficient nowadays, there are much better, much more accurate and faster methods available. Regards. V. All My Articles & other Materials here: Valentin Albillo's HP Collection |