Numerical integration: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed the trapezium function's returned value, changed some comments.)
m (→‎{{header|REXX}}: changed the format of the output to use arrows, used a better/cleaner name for some algorithms in the output.)
Line 3,588: Line 3,588:
say
say
say center('test' test,65,'─') /*display a header for the test suite. */
say center('test' test,65,'─') /*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 3,623: Line 3,623:
trapezium: procedure expose test; parse arg a,b,n; h=(b-a)/n
trapezium: procedure expose test; parse arg a,b,n; h=(b-a)/n
$=0
$=0
do x=a by h for n; $=$+(f(x)+f(x+h))*.5; end /*x*/
do x=a by h for n; $=$+(f(x)+f(x+h)); end /*x*/
return $*h/1 /*return the number with no trailing 0s*/</lang>
return $*h/2 /*return the number with no trailing 0s*/</lang>
'''output'''
'''output'''
<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.6059860575146761454
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, 5000000) = 17999996.4
left rectangular(0, 6000, 5000000) ──► 17999996.4
midpoint_rectangular(0, 6000, 5000000) = 18000000
midpoint rectangular(0, 6000, 5000000) ──► 18000000
right_rectangular(0, 6000, 5000000) = 18000003.6
right rectangular(0, 6000, 5000000) ──► 18000003.6
Simpson(0, 6000, 5000000) = 18000000
Simpson(0, 6000, 5000000) ──► 18000000
trapezium(0, 6000, 5000000) = 18000000
trapezium(0, 6000, 5000000) ──► 18000000
</pre>
</pre>