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 ' |
say ' left rectangular('L", "H', 'i") ──► " left_rect(L, H, i) |
||
say ' |
say ' midpoint rectangular('L", "H', 'i") ──► " midpoint_rect(L, H, i) |
||
say ' |
say ' right rectangular('L", "H', 'i") ──► " right_rect(L, H, i) |
||
say ' Simpson('L", "H', 'i") |
say ' Simpson('L", "H', 'i") ──► " Simpson(L, H, i) |
||
say ' 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)) |
do x=a by h for n; $=$+(f(x)+f(x+h)); end /*x*/ |
||
return $*h/ |
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 |
|||
midpoint rectangular(0, 1, 100) ──► 0.2499875 |
|||
right rectangular(0, 1, 100) ──► 0.255025 |
|||
Simpson(0, 1, 100) |
Simpson(0, 1, 100) ──► 0.25 |
||
trapezium(0, 1, 100) |
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) |
Simpson(1, 100, 1000) ──► 4.6051703849571421725 |
||
trapezium(1, 100, 1000) |
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) |
Simpson(0, 5000, 5000000) ──► 12500000 |
||
trapezium(0, 5000, 5000000) |
trapezium(0, 5000, 5000000) ──► 12500000 |
||
─────────────────────────────test 4────────────────────────────── |
─────────────────────────────test 4────────────────────────────── |
||
left rectangular(0, 6000, 5000000) ──► 17999996.4 |
|||
midpoint rectangular(0, 6000, 5000000) ──► 18000000 |
|||
right rectangular(0, 6000, 5000000) ──► 18000003.6 |
|||
Simpson(0, 6000, 5000000) |
Simpson(0, 6000, 5000000) ──► 18000000 |
||
trapezium(0, 6000, 5000000) |
trapezium(0, 6000, 5000000) ──► 18000000 |
||
</pre> |
</pre> |
||