Numerical integration: Difference between revisions
Content added Content deleted
(→{{header|REXX}}: optimized the ƒ function (it's about 4% faster).) |
m (→{{header|REXX}}: aligned statements better, optimized the Simpson function (about 6% faster).) |
||
Line 4,307: | Line 4,307: | ||
return 1/y /* " " reciprocal " */ |
return 1/y /* " " reciprocal " */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
left_rect: procedure expose test; parse arg a,b, |
left_rect: procedure expose test; parse arg a,b,#; $= 0; h= (b-a)/# |
||
do x=a by h for |
do x=a by h for #; $= $ + f(x) |
||
end /*x*/ |
|||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
midpoint_rect: procedure expose test; parse arg a,b, |
midpoint_rect: procedure expose test; parse arg a,b,#; $= 0; h= (b-a)/# |
||
do x=a+h/2 by h for |
do x=a+h/2 by h for #; $= $ + f(x) |
||
end /*x*/ |
|||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
right_rect: procedure expose test; parse arg a,b, |
right_rect: procedure expose test; parse arg a,b,#; $= 0; h= (b-a)/# |
||
do x=a+h by h for |
do x=a+h by h for #; $= $ + f(x) |
||
end /*x*/ |
|||
return $*h/1 |
return $*h/1 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
Simpson: procedure expose test; parse arg a,b, |
Simpson: procedure expose test; parse arg a,b,#; h= (b-a)/# |
||
hh= h/2; $= f(a + hh) |
|||
@= 0; do x=1 for |
@= 0; do x=1 for #-1; hx=h*x + a; @= @ + f(hx) |
||
$= $ + f(hx + hh) |
|||
end /*x*/ |
|||
return h * (f(a) + f(b) + 4*$ + 2*@) / 6 |
return h * (f(a) + f(b) + 4*$ + 2*@) / 6 |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
trapezium: procedure expose test; parse arg a,b, |
trapezium: procedure expose test; parse arg a,b,#; $= 0; h= (b-a)/# |
||
do x=a by h for #; $= $ + (f(x) + f(x+h)) |
|||
end /*x*/ |
|||
return $*h/2</lang> |
return $*h/2</lang> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |