Anonymous user
Numerical integration/Gauss-Legendre Quadrature: Difference between revisions
Numerical integration/Gauss-Legendre Quadrature (view source)
Revision as of 08:00, 25 September 2015
, 8 years ago→version 2: added/changed whitespace and comments, changed output alignment and indentation, used a better method of determining the numeric (decimal digits) precision.
(→{{header|Perl 6}}: glrize) |
(→version 2: added/changed whitespace and comments, changed output alignment and indentation, used a better method of determining the numeric (decimal digits) precision.) |
||
Line 1,378:
===version 2===
This REXX version
:::* a faster '''cos''' function (with full precision)
:::* a faster '''exp''' function (with full precision)
Line 1,384:
:::* some static variables instead of repeated expressions
:::* calculations using full (specified) precision (''numeric digits'')
:::* multiplication using [···'''*.5'''] instead of division using [···'''/2''']
:::* a generic approach for setting the ''numeric digits''
:::* a better test for earlier termination (stopping) of calculations
<br>exists nested
▲<br><br>The use of vertical bars is one of the very few times to use leading comments, as there isn't that many situations where there
▲<br>exists nested DO loops with different (grouped) indentations, and practically no space on the right side of the statements.
<br>It presents a good visual indication of what's what, but it's the dickens to pay when updating the code.
<lang rexx>/*REXX pgm
▲say ' step ' center("iterative value",digs+5) ' difference' /*show hdr*/
▲sep='──────' copies('─' ,digs+5) '─────────────'; say sep
do step=1;
/*█*/ do k=2 to step;
/*█*/
/*█*/ T.y=0;
/*█*/
/*█*/
/*█*/ kkm=k+km; do j=1 for p1z+1; T.j=(kkm*T.j-km*TT.j)/k ;
/*█*/ p0z=p1z; do
/*█*/ p1z=p1z+1; do
/*█*/ end /*k*/
/*▓*/ do !=1 for step
/*▓*/ x=cos(pi*(!-.25)/step_)
/*▓*/ do times
/*▓*/ f=p1.1; df=0; do k=2 to p1z
/*▓*/
/*▓*/
/*▓*/
/*▓*/ dx=f/df;
/*▓*/
/*▓*/ r.1.!=x
/*▓*/ r.2.!=2 / ((1 - x*
/*▓*/ end /*!*/
sum=0
/*▒*/ do m=1 for step; sum=sum + r.2.m * exp(bpaH+r.1.m*bmaH)
/*▒*/ end /*m*/
z=bmaH*sum /*calculate the target value
dif=
if abs(dif)>=oldD then leave; Ndif=translate( format(dif,3,4,2,0), 'e', "E")
say center(step,6) z' '
oldD=abs(dif)
end /*step*/ /* [↑] Z is
say sep; say left('',6+1)
exit /*stick a fork in it, we're all done. */
/*─────────────────────────────────────────────────────────────────────────────────────*/
e: return 2.7182818284590452353602874713526624977572470936999595749669676277240766303535
pi: return 3.1415926535897932384626433832795028841971693993751058209749445923078164062862
/*──────────────────────────────────────────────────────────────────────────────────*/
cos: procedure expose !.; parse arg x; if !.x\==. then return !.x; _=1; z=1; y=x*x
do k=2 by 2 until p==z; p=z; _=-_*y/(k*(k-1)); z=z+_; end; !.x=z; return z
/*────────────────────────────────────────────────────────────────────────────*/
exp: procedure; parse arg x; ix=trunc(x); if abs(x-ix)>.5 then ix=ix+sign(x)
x=x-ix; z=1; _=1; do j=1 until p==z; p=z; _=_*x/j; z=z+_; end
Line 1,451 ⟶ 1,445:
'''output'''
<pre>
step iterative value difference
────── ───────────────────────────────────────────────────────────────────────────────── ─────────────
1 6.0000000000000000000000000000000000000000000000000000000000000000000000000000 -1.4036e+01
2 17.
3 19.8536919968055821921309108927158495960774667319753888929050027075848592516449 -1.8206e-01
4 20.0286883952907008527738054439857661647073363250481518077257887668521514648371 -7.0615e-03
5 20.0355777183855621539285357252750939315016272074471283081673242529514166130212 -1.7214e-04
6 20.0357469750923438830654575585499253741529947892197512571761670590022501037512 -2.8797e-06
7 20.0357498197266007755718729372891903369400657532378489130759167634362318526785 -3.5093e-08
8 20.0357498544945172882260918041683132616236752579944055100693304551390338045253 -3.2529e-10
9 20.0357498548174338368864419454858704839263168086955797931292590585320198343011 -2.3700e-12
10 20.0357498548197898711175766908543458234008349625446568080936795730938134206072 -1.3927e-14
11 20.0357498548198037305529147159697031241993516306485175808291929207610544866595 -6.7396e-17
12 20.0357498548198037976759531014454017742327138984429607438017578771715767588801 -2.7323e-19
13 20.0357498548198037979482458119092690701862659228785307035583081473361900008142 -9.4143e-22
14 20.0357498548198037979491844483599375945130148356706886332919441446027039133617 -2.7906e-24
15 20.0357498548198037979491872317401917248452734118643091749897281356338832737044 -7.1915e-27
16 20.0357498548198037979491872389153958789316129464894982848020715833786709115192 -1.6260e-29
17 20.0357498548198037979491872389316236038179252557440453906282250905385221899362 -3.2517e-32
18 20.0357498548198037979491872389316560624360571301484111974244019477736095816451 -5.7920e-35
19 20.0357498548198037979491872389316561202637283172074241556158972833578634975732 -9.2480e-38
20 20.0357498548198037979491872389316561203560751340857503751994442223163866485105 -1.3311e-40
21 20.0357498548198037979491872389316561203562080727616463861143647576984994505169 -1.7360e-43
22 20.0357498548198037979491872389316561203562082461596244537077863602238434348449 -2.0610e-46
23 20.0357498548198037979491872389316561203562082463655032534484950691669883186951 -2.2368e-49
24 20.0357498548198037979491872389316561203562082463657267060509015976314516120711 -2.2276e-52
25 20.0357498548198037979491872389316561203562082463657269286070017882824926007972 -2.0430e-55
26 20.0357498548198037979491872389316561203562082463657269288111333795426198786576 -1.7312e-58
27 20.0357498548198037979491872389316561203562082463657269288113063661454830066555 -1.3595e-61
28 20.0357498548198037979491872389316561203562082463657269288113065019935767640428 -9.9208e-65
29 20.0357498548198037979491872389316561203562082463657269288113065020927177501970 -6.7460e-68
30 20.0357498548198037979491872389316561203562082463657269288113065020927482013630 -3.7009e-68
────── ───────────────────────────────────────────────────────────────────────────────── ─────────────
20.
</pre>
|