2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,6 +1,17 @@
|
|||
Write functions to calculate the definite integral of a function (<span style="font-family: serif">''f(x)''</span>) using [[wp:Rectangle_method|rectangular]] (left, right, and midpoint), [[wp:Trapezoidal_rule|trapezium]], and [[wp:Simpson%27s_rule|Simpson's]] methods. Your functions should take in the upper and lower bounds (<span style="font-family: serif">''a''</span> and <span style="font-family: serif">''b''</span>) and the number of approximations to make in that range (<span style="font-family: serif">''n''</span>). Assume that your example already has a function that gives values for <span style="font-family: serif">''f(x)''</span>.
|
||||
Write functions to calculate the definite integral of a function <big><big> <span style="font-family: serif">''ƒ(x)''</span> </big></big> using ''all'' five of the following methods:
|
||||
::* [[wp:Rectangle_method|rectangular]]
|
||||
::::* left
|
||||
::::* right
|
||||
::::* midpoint
|
||||
::* [[wp:Trapezoidal_rule|trapezium]]
|
||||
::* [[wp:Simpson%27s_rule|Simpson's]]
|
||||
|
||||
Simpson's method is defined by the following pseudocode:
|
||||
<br>
|
||||
Your functions should take in the upper and lower bounds (<span style="font-family: serif">''a''</span> and <span style="font-family: serif">''b''</span>), and the number of approximations to make in that range (<span style="font-family: serif">''n''</span>).
|
||||
|
||||
Assume that your example already has a function that gives values for <big> <span style="font-family: serif">''ƒ(x)''</span>. </big>
|
||||
|
||||
Simpson's method is defined by the following pseudo-code:
|
||||
<pre>
|
||||
h := (b - a) / n
|
||||
sum1 := f(a + h/2)
|
||||
|
|
@ -14,11 +25,14 @@ answer := (h / 6) * (f(a) + f(b) + 4*sum1 + 2*sum2)
|
|||
</pre>
|
||||
|
||||
Demonstrate your function by showing the results for:
|
||||
* f(x) = x^3, where x is [0,1], with 100 approximations. The exact result is 1/4, or 0.25.
|
||||
* f(x) = 1/x, where x is [1,100], with 1,000 approximations. The exact result is the natural log of 100, or about 4.605170
|
||||
* f(x) = x, where x is [0,5000], with 5,000,000 approximations. The exact result is 12,500,000.
|
||||
* f(x) = x, where x is [0,6000], with 6,000,000 approximations. The exact result is 18,000,000.
|
||||
* <big> ƒ(x) = x<sup>3</sup>, </big> where '''x''' is [0,1], with 100 approximations. The exact result is 1/4, or 0.25.
|
||||
* <big> ƒ(x) = 1/x, </big> where '''x''' is [1,100], with 1,000 approximations. The exact result is the natural log of 100, or about 4.605170
|
||||
* <big> ƒ(x) = x, </big> where '''x''' is [0,5000], with 5,000,000 approximations. The exact result is 12,500,000.
|
||||
* <big> ƒ(x) = x, </big> where '''x''' is [0,6000], with 6,000,000 approximations. The exact result is 18,000,000.
|
||||
|
||||
|
||||
<br>
|
||||
'''See also'''
|
||||
* [[Active object]] for integrating a function of real time.
|
||||
* [[Numerical integration/Gauss-Legendre Quadrature]] for another integration method.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -82,4 +82,37 @@ BEGIN
|
|||
OD;
|
||||
h / 6 * (f(a) + f(b) + 4 * sum1 + 2 * sum2)
|
||||
END # simpson #;
|
||||
|
||||
# test the above procedures #
|
||||
PROC test integrators = ( STRING legend
|
||||
, F function
|
||||
, LONG REAL lower limit
|
||||
, LONG REAL upper limit
|
||||
, INT iterations
|
||||
) VOID:
|
||||
BEGIN
|
||||
print( ( legend
|
||||
, fixed( left rect( function, lower limit, upper limit, iterations ), -20, 6 )
|
||||
, fixed( right rect( function, lower limit, upper limit, iterations ), -20, 6 )
|
||||
, fixed( mid rect( function, lower limit, upper limit, iterations ), -20, 6 )
|
||||
, fixed( trapezium( function, lower limit, upper limit, iterations ), -20, 6 )
|
||||
, fixed( simpson( function, lower limit, upper limit, iterations ), -20, 6 )
|
||||
, newline
|
||||
)
|
||||
)
|
||||
END; # test integrators #
|
||||
print( ( " "
|
||||
, " left rect"
|
||||
, " right rect"
|
||||
, " mid rect"
|
||||
, " trapezium"
|
||||
, " simpson"
|
||||
, newline
|
||||
)
|
||||
);
|
||||
test integrators( "x^3", ( LONG REAL x )LONG REAL: x * x * x, 0, 1, 100 );
|
||||
test integrators( "1/x", ( LONG REAL x )LONG REAL: 1 / x, 1, 100, 1 000 );
|
||||
test integrators( "x ", ( LONG REAL x )LONG REAL: x, 0, 5 000, 5 000 000 );
|
||||
test integrators( "x ", ( LONG REAL x )LONG REAL: x, 0, 6 000, 6 000 000 );
|
||||
|
||||
SKIP
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
use MONKEY-SEE-NO-EVAL;
|
||||
|
||||
sub leftrect(&f, $a, $b, $n) {
|
||||
my $h = ($b - $a) / $n;
|
||||
$h * [+] do f($_) for $a, $a+$h ... $b-$h;
|
||||
|
|
@ -15,7 +17,8 @@ sub midrect(&f, $a, $b, $n) {
|
|||
|
||||
sub trapez(&f, $a, $b, $n) {
|
||||
my $h = ($b - $a) / $n;
|
||||
$h / 2 * [+] f($a), f($b), |do f($_) * 2 for $a+$h, $a+$h+$h ... $b-$h;
|
||||
my $partial-sum += f($_) * 2 for $a+$h, $a+$h+$h ... $b-$h;
|
||||
$h / 2 * [+] f($a), f($b), $partial-sum;
|
||||
}
|
||||
|
||||
sub simpsons(&f, $a, $b, $n) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
def faster_simpson(f, a, b, steps):
|
||||
h = (b-a)/steps
|
||||
h = (b-a)/float(steps)
|
||||
a1 = a+h/2
|
||||
s1 = sum( f(a1+i*h) for i in range(0,steps))
|
||||
s2 = sum( f(a+i*h) for i in range(1,steps))
|
||||
|
|
|
|||
|
|
@ -1,47 +1,47 @@
|
|||
/*REXX program does numerical integration using five different algorithms.*/
|
||||
numeric digits 20 /*use twenty decimal digits precision. */
|
||||
/*REXX pgm performs numerical integration using 5 different algorithms and show results.*/
|
||||
numeric digits 20 /*use twenty decimal digits precision. */
|
||||
|
||||
do test=1 for 4 /*perform the 4 different test suites. */
|
||||
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==3 then do; L=0; H=5000; i=5000000; end
|
||||
if test==4 then do; L=0; H=6000; i=5000000; end
|
||||
do test=1 for 4 /*perform the 4 different test suites. */
|
||||
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==3 then do; L=0; H=5000; i=5000000; end
|
||||
if test==4 then do; L=0; H=6000; i=5000000; end
|
||||
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 ' left rectangular('L", "H', 'i") ──► " left_rect(L, H, i)
|
||||
say ' midpoint rectangular('L", "H', 'i") ──► " midpoint_rect(L, H, i)
|
||||
say ' right rectangular('L", "H', 'i") ──► " right_rect(L, H, i)
|
||||
say ' Simpson('L", "H', 'i") ──► " Simpson(L, H, i)
|
||||
say ' trapezium('L", "H', 'i") ──► " trapezium(L, H, i)
|
||||
end /*test*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
f: if test==1 then return arg(1)**3
|
||||
if test==2 then return 1/arg(1)
|
||||
return arg(1)
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
f: if test==1 then return arg(1)**3 /*choose the cube function. */
|
||||
if test==2 then return 1/arg(1) /* " " reciprocal " */
|
||||
return arg(1) /* " " "as-is" " */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
left_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
$=0
|
||||
do x=a by h for n; $=$+f(x); end /*x*/
|
||||
return $*h/1 /*return the number with no trailing 0s*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
$=0
|
||||
do x=a by h for n; $=$+f(x); end /*x*/
|
||||
return $*h/1
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
midpoint_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
$=0
|
||||
do x=a+h/2 by h for n; $=$+f(x); end /*x*/
|
||||
return $*h/1 /*return the number with no trailing 0s*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
$=0
|
||||
do x=a+h/2 by h for n; $=$+f(x); end /*x*/
|
||||
return $*h/1
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
right_rect: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
$=0
|
||||
do x=a+h by h for n; $=$+f(x); end /*x*/
|
||||
return $*h/1 /*return the number with no trailing 0s*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
$=0
|
||||
do x=a+h by h for n; $=$+f(x); end /*x*/
|
||||
return $*h/1
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
Simpson: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
$=f(a+h/2)
|
||||
@=0; do x=1 for n-1; $=$+f(a+h*x+h*.5); @=@+f(a+x*h); end /*x*/
|
||||
$=f(a+h/2)
|
||||
@=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 the number with no trailing 0s*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
trapezium: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
$=0
|
||||
do x=a by h for n; $=$+(f(x)+f(x+h)); end /*x*/
|
||||
return $*h/2 /*return the number with no trailing 0s*/
|
||||
return h*(f(a) + f(b) + 4*$ + 2*@) / 6
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
trapezium: procedure expose test; parse arg a,b,n; h=(b-a)/n
|
||||
$=0
|
||||
do x=a by h for n; $=$+(f(x)+f(x+h)); end /*x*/
|
||||
return $*h/2
|
||||
|
|
|
|||
56
Task/Numerical-integration/VBA/numerical-integration.vba
Normal file
56
Task/Numerical-integration/VBA/numerical-integration.vba
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Option Explicit
|
||||
Option Base 1
|
||||
|
||||
Function Quad(ByVal f As String, ByVal a As Double, _
|
||||
ByVal b As Double, ByVal n As Long, _
|
||||
ByVal u As Variant, ByVal v As Variant) As Double
|
||||
Dim m As Long, h As Double, x As Double, s As Double, i As Long, j As Long
|
||||
m = UBound(u)
|
||||
h = (b - a) / n
|
||||
s = 0#
|
||||
For i = 1 To n
|
||||
x = a + (i - 1) * h
|
||||
For j = 1 To m
|
||||
s = s + v(j) * Application.Run(f, x + h * u(j))
|
||||
Next
|
||||
Next
|
||||
Quad = s * h
|
||||
End Function
|
||||
|
||||
Function f1fun(x As Double) As Double
|
||||
f1fun = x ^ 3
|
||||
End Function
|
||||
|
||||
Function f2fun(x As Double) As Double
|
||||
f2fun = 1 / x
|
||||
End Function
|
||||
|
||||
Function f3fun(x As Double) As Double
|
||||
f3fun = x
|
||||
End Function
|
||||
|
||||
Sub Test()
|
||||
Dim fun, f, coef, c
|
||||
Dim i As Long, j As Long, s As Double
|
||||
|
||||
fun = Array(Array("f1fun", 0, 1, 100, 1 / 4), _
|
||||
Array("f2fun", 1, 100, 1000, Log(100)), _
|
||||
Array("f3fun", 0, 5000, 50000, 5000 ^ 2 / 2), _
|
||||
Array("f3fun", 0, 6000, 60000, 6000 ^ 2 / 2))
|
||||
|
||||
coef = Array(Array("Left rect. ", Array(0, 1), Array(1, 0)), _
|
||||
Array("Right rect. ", Array(0, 1), Array(0, 1)), _
|
||||
Array("Midpoint ", Array(0.5), Array(1)), _
|
||||
Array("Trapez. ", Array(0, 1), Array(0.5, 0.5)), _
|
||||
Array("Simpson ", Array(0, 0.5, 1), Array(1 / 6, 4 / 6, 1 / 6)))
|
||||
|
||||
For i = 1 To UBound(fun)
|
||||
f = fun(i)
|
||||
Debug.Print f(1)
|
||||
For j = 1 To UBound(coef)
|
||||
c = coef(j)
|
||||
s = Quad(f(1), f(2), f(3), f(4), c(2), c(3))
|
||||
Debug.Print " " + c(1) + ": ", s, (s - f(5)) / f(5)
|
||||
Next j
|
||||
Next i
|
||||
End Sub
|
||||
Loading…
Add table
Add a link
Reference in a new issue