(PC-12xx~14xx) qthsh Tanh-Sinh quadrature
|
04-14-2021, 03:17 PM
Post: #54
|
|||
|
|||
RE: (PC-12xx~14xx) qthsh Tanh-Sinh quadrature
Lua implementation of quad(), using qthsh code to handle finite intervals.
Note: Q.quad is just a shorthand for Q["quad"], a function stored in table Q Code: function Q.quad(f, a, b, d, n, eps) --- I noticed exp-sinh is actually doing 2 integrals, simultaneously. \(\int_a^∞ = \int_a^{a+d} + \int_{a+d}^∞ \) Both integrals are using same number of points, but the last one is much harder. If possible, we should pick d so that first one do most of the work. This version allowed to input d, instead of hard-coded d=1. If we have a clue about the shape of curve, it helps. Example, Standard Normal Distribution. lua> Q = require 'quad' lua> k = 1/sqrt(2*pi) lua> Inf = math.huge lua> pdf = function(x) return k*exp(-x*x/2) end lua> Q.quad(Q.count(pdf), 1, Inf), Q.n 0.15865525392847651 129 lua> Q.quad(Q.count(pdf), 1, Inf, 4), Q.n 0.15865525393145705 69 Above is now as efficient as integrating closed interval of [0, 1] lua> 1/2 - Q.quad(Q.count(pdf), 0, 1), Q.n 0.15865525393145719 58 Same idea for \(\int_{-∞}^∞\), we wanted \(\int_{-d}^d\) to do the most work. lua> Q.quad(Q.count(pdf), -Inf, Inf), Q.n 0.999999999999999 119 lua> Q.quad(Q.count(pdf), -Inf, Inf, 4), Q.n 0.9999999999999996 43 Had we optimized specifically for even functions, above only need (n+1)/2 points ! |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 1 Guest(s)