Numerical integration: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (→{{header|Raku}}: Move some calculations out of hot loops, more efficient looping constructs, arguably less idiomatic, but less horribly slow) |
m (→{{header|REXX}}: changed whitespace, aligned some statements better.) |
||
Line 4,289: | Line 4,289: | ||
numeric digits 20 /*use twenty decimal digits precision. */ |
numeric digits 20 /*use twenty decimal digits precision. */ |
||
do test=1 for 4 |
do test=1 for 4; say /*perform the 4 different test suites. */ |
||
if test==1 then do; L= 0; H= 1; i= 100; end |
if test==1 then do; L= 0; H= 1; i= 100; end |
||
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= 5000000; end |
if test==4 then do; L= 0; H= 6000; i= 5000000; end |
||
⚫ | |||
say |
|||
⚫ | |||
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) |
||
Line 4,308: | Line 4,307: | ||
return arg(1) /* " " "as-is" " */ |
return arg(1) /* " " "as-is" " */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
left_rect: procedure expose test; |
left_rect: procedure expose test; parse arg a,b,n; $= 0; h= (b-a) / n |
||
⚫ | |||
$= 0 |
|||
⚫ | |||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
midpoint_rect: procedure expose test; |
midpoint_rect: procedure expose test; parse arg a,b,n; $= 0; h= (b-a) / n |
||
⚫ | |||
$= 0 |
|||
⚫ | |||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
right_rect: procedure expose test; |
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*/ |
|||
$= 0 |
|||
⚫ | |||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Simpson: procedure expose test; |
Simpson: procedure expose test; parse arg a,b,n; h= (b-a) / n |
||
$= f(a + h/2) |
$= f(a + h/2) |
||
@= 0; |
@= 0; do x=1 for n-1; $= $+f(a+h*x+h*.5); @= @+f(a+x*h); end /*x*/ |
||
return h*(f(a) + f(b) + 4*$ + 2*@) / 6 |
return h * (f(a) + f(b) + 4*$ + 2*@) / 6 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
trapezium: procedure expose test; |
trapezium: procedure expose test; parse arg a,b,n; $= 0; h= (b-a) / n |
||
⚫ | |||
$= 0 |
|||
⚫ | |||
return $*h/2</lang> |
return $*h/2</lang> |
||
{{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 |
||
Line 4,342: | Line 4,337: | ||
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 |
||
Line 4,349: | Line 4,344: | ||
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 |
||
Line 4,356: | Line 4,351: | ||
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 |