Numerical integration: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed spelling of trapezoid to trapezium, fixed trapezium (function) returned value.)
m (→‎{{header|REXX}}: changed the trapezium function's returned value, changed some comments.)
Line 3,603: Line 3,603:
$=0
$=0
do x=a by h for n; $=$+f(x); end /*x*/
do x=a by h for n; $=$+f(x); end /*x*/
return $*h/1 /*return a normalized number. */
return $*h/1 /*return the number with no trailing 0s*/
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
midpoint_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
midpoint_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
$=0
$=0
do x=a+h/2 by h for n; $=$+f(x); end /*x*/
do x=a+h/2 by h for n; $=$+f(x); end /*x*/
return $*h/1 /*return a normalized number. */
return $*h/1 /*return the number with no trailing 0s*/
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
right_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
right_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
$=0
$=0
do x=a+h by h for n; $=$+f(x); end /*x*/
do x=a+h by h for n; $=$+f(x); end /*x*/
return $*h/1 /*return a normalized number. */
return $*h/1 /*return the number with no trailing 0s*/
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
Simpson: procedure expose test; parse arg a,b,n; h=(b-a)/n
Simpson: procedure expose test; parse arg a,b,n; h=(b-a)/n
Line 3,619: Line 3,619:
@=0; do x=1 for n-1; $=$+f(a+h*x+h*.5); @=@+f(a+x*h); end /*x*/
@=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 /*return the number with no trailing 0s*/
/*────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
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 to b by h; $=$+h*(f(x)+f(x+h))*.5; end /*x*/
do x=a by h for n; $=$+(f(x)+f(x+h))*.5; end /*x*/
return $*(b-a)/1 /*return a normalized number. */</lang>
return $*h/1 /*return the number with no trailing 0s*/</lang>
'''output'''
'''output'''
<pre>
<pre>
Line 3,632: Line 3,632:
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.260176505
trapezium(0, 1, 100) = 0.250025


─────────────────────────────test 2──────────────────────────────
─────────────────────────────test 2──────────────────────────────
Line 3,639: Line 3,639:
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) = 456.09058122698523643
trapezium(1, 100, 1000) = 4.6059860575146761454


─────────────────────────────test 3──────────────────────────────
─────────────────────────────test 3──────────────────────────────
Line 3,646: Line 3,646:
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) = 62500025000.0025
trapezium(0, 5000, 5000000) = 12500000


─────────────────────────────test 4──────────────────────────────
─────────────────────────────test 4──────────────────────────────
Line 3,653: Line 3,653:
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) = 108000043200.00432
trapezium(0, 6000, 5000000) = 18000000
</pre>
</pre>