Numerical integration: Difference between revisions
Content added Content deleted
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: | Line 4,285: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Note: there was virtually no difference between '''numeric digits 9''' (the default) and '''numeric digits 20'''. |
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.*/ |
<lang rexx>/*REXX pgm performs numerical integration using 5 different algorithms and show results.*/ |
||
numeric digits 20 /*use twenty decimal digits precision. */ |
numeric digits 20 /*use twenty decimal digits precision. */ |
||
Line 4,293: | Line 4,293: | ||
if test==2 then do; L= 1; H= 100; i= 1000; end |
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==3 then do; L= 0; H= 5000; i= 5000000; end |
||
if test==4 then do; L= 0; H= 6000; i= |
if test==4 then do; L= 0; H= 6000; i= 6000000; end |
||
say center('test' test, |
say center('test' test, 79, "═") /*display a header for the test suite. */ |
||
say ' left rectangular('L", "H', 'i") ──► " left_rect(L, H, i) |
say ' left rectangular('L", "H', 'i") ──► " left_rect(L, H, i) |
||
say ' midpoint rectangular('L", "H', 'i") ──► " midpoint_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 ' right rectangular('L", "H', 'i") ──► " right_rect(L, H, i) |
||
say ' Simpson('L", "H', 'i") ──► " Simpson(L, H, i) |
say ' Simpson('L", "H', 'i") ──► " Simpson(L, H, i) |
||
say ' trapezium('L", "H', 'i") ──► " trapezium(L, H, i) |
say ' trapezium('L", "H', 'i") ──► " trapezium(L, H, i) |
||
end /*test*/ |
end /*test*/ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
Line 4,305: | Line 4,305: | ||
f: if test==1 then return arg(1) **3 /*choose the cube function. */ |
f: if test==1 then return arg(1) **3 /*choose the cube function. */ |
||
if test==2 then return 1 / arg(1) /* " " reciprocal " */ |
if test==2 then return 1 / arg(1) /* " " reciprocal " */ |
||
return arg(1) /* " " " |
return arg(1) /* " " "as─is" " */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
left_rect: procedure expose test; parse arg a,b,n; $= 0; |
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*/ |
do x=a by h for n; $= $ + f(x); end /*x*/ |
||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
midpoint_rect: procedure expose test; parse arg a,b,n; $= 0; |
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*/ |
do x=a+h/2 by h for n; $= $ + f(x); end /*x*/ |
||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
right_rect: procedure expose test; parse arg a,b,n; $= 0; |
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*/ |
do x=a+h by h for n; $= $ + f(x); end /*x*/ |
||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Simpson: procedure expose test; parse arg a,b,n; |
Simpson: procedure expose test; parse arg a,b,n; h= (b-a)/n |
||
$= f(a + h/2) |
$= f(a + h/2) |
||
@= 0; do x=1 for n-1; $= $+f(a+h*x+h*.5); @= @+f(a+x*h); end /*x*/ |
@= 0; do x=1 for n-1; $= $+f(a+h*x+h*.5); @= @+f(a+x*h); end /*x*/ |
||
Line 4,330: | Line 4,330: | ||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
════════════════════════════════════test 1═════════════════════════════════════ |
|||
═════════════════════════════test 1══════════════════════════════ |
|||
left rectangular(0, 1, 100) ──► 0.245025 |
left rectangular(0, 1, 100) ──► 0.245025 |
||
midpoint rectangular(0, 1, 100) ──► 0.2499875 |
midpoint rectangular(0, 1, 100) ──► 0.2499875 |
||
right rectangular(0, 1, 100) ──► 0.255025 |
right rectangular(0, 1, 100) ──► 0.255025 |
||
Simpson(0, 1, 100) ──► 0.25 |
Simpson(0, 1, 100) ──► 0.25 |
||
trapezium(0, 1, 100) ──► 0.250025 |
trapezium(0, 1, 100) ──► 0.250025 |
||
════════════════════════════════════test 2═════════════════════════════════════ |
|||
═════════════════════════════test 2══════════════════════════════ |
|||
left rectangular(1, 100, 1000) ──► 4.6549910575146761473 |
left rectangular(1, 100, 1000) ──► 4.6549910575146761473 |
||
midpoint rectangular(1, 100, 1000) ──► 4.604762548678375185 |
midpoint rectangular(1, 100, 1000) ──► 4.604762548678375185 |
||
right rectangular(1, 100, 1000) ──► 4.5569810575146761472 |
right rectangular(1, 100, 1000) ──► 4.5569810575146761472 |
||
Simpson(1, 100, 1000) ──► 4.6051703849571421725 |
Simpson(1, 100, 1000) ──► 4.6051703849571421725 |
||
trapezium(1, 100, 1000) ──► 4.605986057514676146 |
trapezium(1, 100, 1000) ──► 4.605986057514676146 |
||
════════════════════════════════════test 3═════════════════════════════════════ |
|||
═════════════════════════════test 3══════════════════════════════ |
|||
left rectangular(0, 5000, 5000000) ──► 12499997.5 |
left rectangular(0, 5000, 5000000) ──► 12499997.5 |
||
midpoint rectangular(0, 5000, 5000000) ──► 12500000 |
midpoint rectangular(0, 5000, 5000000) ──► 12500000 |
||
right rectangular(0, 5000, 5000000) ──► 12500002.5 |
right rectangular(0, 5000, 5000000) ──► 12500002.5 |
||
Simpson(0, 5000, 5000000) ──► 12500000 |
Simpson(0, 5000, 5000000) ──► 12500000 |
||
trapezium(0, 5000, 5000000) ──► 12500000 |
trapezium(0, 5000, 5000000) ──► 12500000 |
||
════════════════════════════════════test 4═════════════════════════════════════ |
|||
═════════════════════════════test 4══════════════════════════════ |
|||
left rectangular(0, 6000, |
left rectangular(0, 6000, 6000000) ──► 17999997 |
||
midpoint rectangular(0, 6000, |
midpoint rectangular(0, 6000, 6000000) ──► 18000000 |
||
right rectangular(0, 6000, |
right rectangular(0, 6000, 6000000) ──► 18000003 |
||
Simpson(0, 6000, |
Simpson(0, 6000, 6000000) ──► 18000000 |
||
trapezium(0, 6000, |
trapezium(0, 6000, 6000000) ──► 18000000 |
||
</pre> |
</pre> |
||