Numerical integration: Difference between revisions

m
→‎{{header|REXX}}: aligned statements better, optimized the Simpson function (about 6% faster).
(→‎{{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:
return 1/y /* " " reciprocal " */
/*──────────────────────────────────────────────────────────────────────────────────────*/
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*/
end /*x*/
return $*h/1
/*──────────────────────────────────────────────────────────────────────────────────────*/
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*/
end /*x*/
return $*h/1
/*──────────────────────────────────────────────────────────────────────────────────────*/
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*/
end /*x*/
return $*h/1
/*──────────────────────────────────────────────────────────────────────────────────────*/
Simpson: procedure expose test; parse arg a,b,n#; h= (b-a)/n; hh= h/2#
hh= h/2; $= f(a + h/2hh)
@= 0; do x=1 for n#-1; hx=h*x; $=$+f( a+hx+hh); @= @ + f(a+hx); end /*x*/
$= $ + f(hx + hh)
end /*x*/
 
return h * (f(a) + f(b) + 4*$ + 2*@) / 6
/*──────────────────────────────────────────────────────────────────────────────────────*/
trapezium: procedure expose test; parse arg a,b,n#; $= 0; h= (b-a) / n#
do x=a by h for n#; $= $ + (f(x) + f(x+h)); end /*x*/
end /*x*/
return $*h/2</lang>
{{out|output|text=&nbsp; when using the default inputs:}}