Anonymous user
Numerical integration/Gauss-Legendre Quadrature: Difference between revisions
Numerical integration/Gauss-Legendre Quadrature (view source)
Revision as of 02:28, 24 August 2018
, 5 years ago→version 2: added/changed comments and whitespace, used a template for the output section.
m (→{{header|Sidef}}: updated code) |
m (→version 2: added/changed comments and whitespace, used a template for the output section.) |
||
Line 1,913:
<br>visual indication of what's what, but it's the dickens to pay when updating the source code.
<lang rexx>/*REXX program does numerical integration using an N-point Gauss─Legendre quadrature rule. */
pi= pi(); digs= length(pi)-1; numeric digits digs; reps= digs % 2
!.= .; b= 3;
trueV= exp(b)-exp(a); bpa= b + a; bpaH= bpa / 2
say ' step ' center("iterative value", digs+3) ' difference'
sep='──────' copies("─" , digs+3) '─────────────';
do #=1 until dif>0; p0z= 1; p0.1= 1; p1z= 2; p1.1= 1; p1.2= 0;
/*█*/
/*█*/
/*█*/
/*█*/
/*█*/
/*█*/
/*█*/
$= 0▼
/*▒*/ do m=1 for #; $=$ + r.2.m * exp(bpaH + r.1.m*bmaH); end /*m*/
▲ $=0
z= bmaH * $
▲ if #\==1 then say center(#, 6) z' ' Ndif /*don't display if not computed.*/
end /*#*/
say sep; xdif= compare( strip(z), trueV); say right("↑", 6 + 1 + xdif)
say left('', 6 + 1)
say 'Using ' digs " digit precision, the" ,
'N-point Gauss─Legendre quadrature (GLQ) had an accuracy of ' xdif-2 " digits."
Line 1,958 ⟶ 1,957:
pi: return 3.141592653589793238462643383279502884197169399375105820974944592307816406286286209
/*───────────────────────────────────────────────────────────────────────────────────────────*/
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;
/*───────────────────────────────────────────────────────────────────────────────────────────*/
exp: procedure; parse arg x; ix= x % 1; if abs(x-ix) > .5 then ix= ix + sign(x)
x= x-ix; z=1; _=1;
{{out|output|text= when using the default inputs:}}
<pre>
step iterative value difference
|