Anonymous user
Numerical integration/Gauss-Legendre Quadrature: Difference between revisions
Numerical integration/Gauss-Legendre Quadrature (view source)
Revision as of 14:49, 1 November 2013
, 10 years ago→version 2: optimized COS & EXP subroutines, did some cosmetic changes, added more optimization here and there. -- ~~~~
(→{{header|REXX}}: "Premature optimization is the root of all evil." Don. Knuth) |
m (→version 2: optimized COS & EXP subroutines, did some cosmetic changes, added more optimization here and there. -- ~~~~) |
||
Line 1,232:
:::* about 1/6 the computing time
Note that the function values for '''pi''' and '''e''' should have more precision than the number of digits specified.
<lang rexx>/*REXX pgm does numerical integration
digs=40; d2=digs*2; tiny = '1E-'d2 /*equivalent to 1÷10**d2 */
numeric digits digs;
numeric digits d2; true = exp(b) - exp(a)
numeric digits digs+10; times = digs%2 + 1
say 'step' center("iterative value",digs+3) ' difference ' /*show hdr*/
sep='────' copies('─' ,digs+3) '────────────'; say sep
do step=1 for times;
p1z=2; p1.1=1; p1.2=0; r.=0
/*█*/ do L=1 for p1z;
/*█*/ end /*L*/
/*█*/ do L=1 for p0z; L2=L+2; TT.L2=p0.L
/*█*/
/*█*/
end /*L*/▼
/*█*/
/*█*/
▲ p1z=p1z+1; do j=1 for p1z; p1.j= T.j ; end /*j*/
▲ end /*k*/
do
dx=f/df;
▲ r.2.!=2/((1-x*x)*df*df)
sum=0
do m=1
z=bma*sum*.5
say right(step,4)
end /*step*/
say sep; say left('',4) true " {exact}"
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────COS
cos: procedure; x=r2r(arg(1)); numeric fuzz min(9,digits()-9); _=1; z=1
/*──────────────────────────────────E subroutine──────────────────────────────────────*/
e: return 2.7182818284590452353602874713526624977572470936999595749669676277240766303535
/*──────────────────────────────────EXP
exp: procedure; parse arg x;
x=x-ix; z=1; _=1;
if z\==0 then z=z*e()**ix; return z
/*──────────────────────────────────PI subroutine──────────────────────────────────────*/
|