(PC-12xx~14xx) qthsh Tanh-Sinh quadrature
|
04-26-2021, 03:08 PM
(This post was last modified: 04-27-2021 07:31 PM by Albert Chan.)
Post: #72
|
|||
|
|||
RE: (PC-12xx~14xx) qthsh Tanh-Sinh quadrature
An improvement to previous post Q.peak().
Instead of calculating f(a+x)*x, this version use f(a+x), saving some computations. Bonus: this may avoid overflow/underflow issues, if x is huge/tiny. Code: function Q.peak(f, a, d) -- find peak of f(a+x)*x, guess=d Code assumed peak is bell-shaped, not bumpy. Also, code is optimized for peak close to user inputted d. Example, first integral from qthsh.pdf Appendix C: lua> f = function(x) return exp(-x)/x end lua> Q.peak(f, 1) -- default d=1 0.5980678336446792 3.218395385172793 With default d=1, code does 3 function evaluations. (note: code optimized away last column) Code: N x f(1+x) f(1+x)*x lua> Q.quad(Q.count(f), 1, huge), Q.n -- d = 1 (default) 0.2193839343909888 129 lua> Q.quad(Q.count(f), 1, huge, 3.218), Q.n -- d ≈ extrapolated x-intercept 0.21938393439552076 69 Update: code adjusted to quit early, if detected sign changes. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 12 Guest(s)