(02-07-2020 05:08 PM)Albert Chan Wrote: Since trapezoids and midpoints are using the same points, I tried to tease out the algorithm, using f(x) = x*log(1+x)
10 DEF FNF(X)=X*LOGP1(X)
20 DEF FNG(U)=1.5*(1-U*U)*FNF(U*(3-U*U)/2)
30 M1=2*FNG(0)
40 M2=FNG(-.5)+FNG(.5)
50 M4=.5*(FNG(-.75)+FNG(-.25)+FNG(.25)+FNG(.75))
60 T1=0
70 T2=(T1+M1)/2
80 T4=(T2+M2)/2
90 T8=(T4+M4)/2
100 DISP "MIDPOINTS=";(M1-20*M2+64*M4)/45
110 DISP "TRAPEZOID=";(-T1+84*T2-1344*T4+4096*T8)/2835
120 DISP "INTEGRAL =";INTEGRAL(-1,1,1,FNF(IVAR))
>RUN
MIDPOINTS= 1.02693666365
TRAPEZOID= .978017069767
INTEGRAL7= .97801706977
Besides showing INTEGRAL algorithm using romberg's method with trapezoids, I noticed a few things.
- Integrand that evaluated to zero for end points meant the outside trapezoids is approximated as triangles.
Area is 0 if there no sample points in between. Thus trapezoid area, T1 = 0
- Even though the midpoint numbers does not incorprate previous sample points, it will, when we do the extrapolation.
For 7 sample points, midpoints extrapolated estimate = (M1-20*M2+64*M4)/45
- For this example, extrapolated trapezoid numbers seems to converge with half the error.
We may improve the estimate wih a 2:1 weighted average of both approaches.
(unfortunately, in general, we have no idea what the weight is)
Code:
points t=trapezoids m=midpoints (t+m)/2 (2t+m)/3
7 0.9780170698 1.0269366637 1.0024768667 0.9943236011
15 0.9945010276 1.0108562045 1.0026786161 0.9999527533
31 0.9986339048 1.0027587099 1.0006963073 1.0000088398
63 0.9996599242 1.0006854427 1.0001726835 1.0000017637
127 0.9999151794 1.0001704034 1.0000427914 1.0000002541
255 0.9999788206 1.0000424598 1.0000106402 1.0000000336
511 0.9999947084 1.0000105961 1.0000026523 1.0000000043
1023 0.9999986775 1.0000026466 1.0000006621 1.0000000005
2047 0.9999996694 1.0000006613 1.0000001654 1.0000000001
Note: \(\int _{-1}^1 x \log(1+x)\;dx = 1\)
We can prove using integration by parts, but is easier with symmetry
\(\log(1+x) = \int _0 ^x {dt\over1+t} = \int _0 ^x (1-t+t^2-t^3+t^4+\;...) dt
= {x\over1} - {x^2\over2} + {x^3\over3} - {x^4\over4} +\; ... \)
\(\int _{-1}^1 x \log(1+x)\;dx
= \int _{-1}^1 ({x^2\over1}-{x^3\over2}+{x^4\over3}-{x^5\over4}+\; ... )\; dx
= 2 \int _0 ^1 ({x^2\over1} + {x^4\over3} + {x^6\over5} +\;...) dx
\)
Summing integrated terms, we have a
telescoping series
\({2\over1×3} + {2\over3×5} + {2\over5×7} + \;... = {1\over1}-{1\over3} + {1\over3}-{1\over5}+{1\over5}-{1\over7}+\;... = 1\)