Numerical integration: Difference between revisions

→‎{{header|REXX}}: changed whitespace, changed to use six million approximations instead of five million for the 4th test suite.
m (→‎{{header|REXX}}: changed whitespace, aligned some statements better.)
(→‎{{header|REXX}}: changed whitespace, changed to use six million approximations instead of five million for the 4th test suite.)
Line 4,285:
 
=={{header|REXX}}==
Note:   there was virtually no difference between   '''numeric digits 9'''   (the default)   and   '''numeric digits 20'''.
<lang rexx>/*REXX pgm performs numerical integration using 5 different algorithms and show results.*/
numeric digits 20 /*use twenty decimal digits precision. */
Line 4,293:
if test==2 then do; L= 1; H= 100; i= 1000; end
if test==3 then do; L= 0; H= 5000; i= 5000000; end
if test==4 then do; L= 0; H= 6000; i= 50000006000000; end
say center('test' test, 6579, "═") /*display a header for the test suite. */
say ' left rectangular('L", "H', 'i") ──► " left_rect(L, H, i)
say ' midpoint rectangular('L", "H', 'i") ──► " midpoint_rect(L, H, i)
say ' right rectangular('L", "H', 'i") ──► " right_rect(L, H, i)
say ' Simpson('L", "H', 'i") ──► " Simpson(L, H, i)
say ' trapezium('L", "H', 'i") ──► " trapezium(L, H, i)
end /*test*/
exit /*stick a fork in it, we're all done. */
Line 4,305:
f: if test==1 then return arg(1) **3 /*choose the cube function. */
if test==2 then return 1 / arg(1) /* " " reciprocal " */
return arg(1) /* " " "as-isas─is" " */
/*──────────────────────────────────────────────────────────────────────────────────────*/
left_rect: procedure expose test; parse arg a,b,n; $= 0; h= (b-a) / n
do x=a by h for n; $= $ + f(x); end /*x*/
return $*h/1
/*──────────────────────────────────────────────────────────────────────────────────────*/
midpoint_rect: procedure expose test; parse arg a,b,n; $= 0; h= (b-a) / n
do x=a+h/2 by h for n; $= $ + f(x); end /*x*/
return $*h/1
/*──────────────────────────────────────────────────────────────────────────────────────*/
right_rect: procedure expose test; parse arg a,b,n; $= 0; h= (b-a) / n
do x=a+h by h for n; $= $ + f(x); end /*x*/
return $*h/1
/*──────────────────────────────────────────────────────────────────────────────────────*/
Simpson: procedure expose test; parse arg a,b,n; h= (b-a) / n
$= f(a + h/2)
@= 0; do x=1 for n-1; $= $+f(a+h*x+h*.5); @= @+f(a+x*h); end /*x*/
Line 4,330:
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
════════════════════════════════════test 1═════════════════════════════════════
═════════════════════════════test 1══════════════════════════════
left rectangular(0, 1, 100) ──► 0.245025
midpoint rectangular(0, 1, 100) ──► 0.2499875
right rectangular(0, 1, 100) ──► 0.255025
Simpson(0, 1, 100) ──► 0.25
trapezium(0, 1, 100) ──► 0.250025
 
════════════════════════════════════test 2═════════════════════════════════════
═════════════════════════════test 2══════════════════════════════
left rectangular(1, 100, 1000) ──► 4.6549910575146761473
midpoint rectangular(1, 100, 1000) ──► 4.604762548678375185
right rectangular(1, 100, 1000) ──► 4.5569810575146761472
Simpson(1, 100, 1000) ──► 4.6051703849571421725
trapezium(1, 100, 1000) ──► 4.605986057514676146
 
════════════════════════════════════test 3═════════════════════════════════════
═════════════════════════════test 3══════════════════════════════
left rectangular(0, 5000, 5000000) ──► 12499997.5
midpoint rectangular(0, 5000, 5000000) ──► 12500000
right rectangular(0, 5000, 5000000) ──► 12500002.5
Simpson(0, 5000, 5000000) ──► 12500000
trapezium(0, 5000, 5000000) ──► 12500000
 
════════════════════════════════════test 4═════════════════════════════════════
═════════════════════════════test 4══════════════════════════════
left rectangular(0, 6000, 50000006000000) ──► 17999996.417999997
midpoint rectangular(0, 6000, 50000006000000) ──► 18000000
right rectangular(0, 6000, 50000006000000) ──► 18000003.6
Simpson(0, 6000, 50000006000000) ──► 18000000
trapezium(0, 6000, 50000006000000) ──► 18000000
</pre>