(PC-12xx~14xx) qasi Adaptive Simpson quadrature
|
03-28-2021, 01:40 PM
Post: #1
|
|||
|
|||
(PC-12xx~14xx) qasi Adaptive Simpson quadrature
ADAPTIVE SIMPSON QUADRATURE
Estimate definite integral over closed interval with adaptive Simpson's rule For SHARP PC-12xx to 14xx 465 bytes BASIC image (PC-1350) See also: https://en.wikipedia.org/wiki/Adaptive_S...27s_method Precision: 1E-9 (adjustable) recommended 1E-5 Recursive subdivision steps: up to 21 (adjustable at line 160) Example: 300 "F1" Y=4/(1+X*X): RETURN RUN f=F1 a=0 b=1 3.141592654 Code: ' ADAPTIVE SIMPSON QUADRATURE "I count on old friends to remain rational" |
|||
04-15-2022, 01:45 AM
Post: #2
|
|||
|
|||
RE: (PC-12xx~14xx) qasi Adaptive Simpson quadrature
Hi, robve
I translated your BASIC code for HP71B Example is for ∫(surd(x,-3), x=-2..3) ≈ 0.739024156626 10 DEF FNF(X)=SGN(X)*ABS(X)^(-1/3) 20 A=-2 @ B=3 @ E=.0001 @ DIM Z(210) 30 T2=TIME @ I=FNF(A) @ J=FNF((A+B)/2) @ K=FNF(B) 40 U=(B-A)*(I+4*J+K)/6 @ M=A @ N=B @ T=0 @ O=1 100 H=(N-M)/2 @ P=FNF(M+H/2) @ Q=FNF(N-H/2) 110 L=H*(I+4*P+J)/6 @ R=H*(J+4*Q+K)/6 @ S=L+R @ D=(S-U)/15 120 IF E<1.E-15 OR ABS(D)<E THEN T=T+S+D @ GOTO 160 130 Z(O+1)=N @ N=M+H @ Z(O)=N @ Z(O+2)=J @ Z(O+3)=Q @ Z(O+4)=K @ Z(O+5)=R 140 E=E/2 @ Z(O+6)=E @ K=J @ J=P @ U=L @ O=O+7 150 GOTO 100 160 O=O-7 @ IF O<1 THEN DISP T,TIME-T2 @ END 170 M=Z(O) @ N=Z(O+1) @ I=Z(O+2) @ J=Z(O+3) @ K=Z(O+4) @ U=Z(O+5) @ E=Z(O+6) 180 GOTO 100 >RUN .739024205098 10.99 HP71B can do recursive calls, without us handle recursive calling stacks. I think this version is simpler, which I posted a few days ago. |
|||
04-16-2022, 01:45 PM
Post: #3
|
|||
|
|||
RE: (PC-12xx~14xx) qasi Adaptive Simpson quadrature
(04-15-2022 01:45 AM)Albert Chan Wrote: Hi, robve Very nice. A bit surprised to see that the recursive version on the HP 71B takes much longer, about 1.7 times the execution time of the stack version: >RUN .73902427614 18.76 The quadrature should be comparable, but not necessarily identical as the summation order differs (in-order traversal sum versus a bottom-up sum in the recursive version that is typically more precise). Applying tail-recursion optimization to the recursive version should speed this up some. - Rob "I count on old friends to remain rational" |
|||
04-16-2022, 04:09 PM
(This post was last modified: 04-16-2022 04:39 PM by Albert Chan.)
Post: #4
|
|||
|
|||
RE: (PC-12xx~14xx) qasi Adaptive Simpson quadrature
(04-16-2022 01:45 PM)robve Wrote: A bit surprised to see that the recursive version on the HP 71B takes much longer, If we replace stack version FNF(x) as subroutine, 11s goes up to 14s. About half of the slowdown is because of the more efficient FNF(x). SUB SIMPSON() does not see FNF(x), thus required slower SUB F(x,y) The other half is due to different recurse conditions. (see below) Quote:The quadrature should be comparable, but not necessarily identical as the summation order differs Also, recurse condition of two versions are close, but not identical. Stack version recurse if E = [1E-15, ABS((S-U)/15)] SUB SIMPSON recurse if E = [1E-15/16, ABS((S-U)/16)] For our test example, with undefined value at x=0, SIMPSON version recurse deeper. With SIMPSON version matching stack version E recurse condition, it produce similar result. > RUN .73902420507 15.44 Compare apples to apples, both using SUB F(x,y), timing difference is now minor. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 3 Guest(s)