RosettaCodeData/Task/Numerical-integration/00-TASK.txt

45 lines
2.3 KiB
Plaintext

Write functions to calculate the definite integral of a function <big><big> {{math|1=''ƒ(x)''}} </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]]
:** composite
Your functions should take in the upper and lower bounds ({{math|''a''}} and {{math|''b''}}), and the number of approximations to make in that range ({{math|''n''}}).
Assume that your example already has a function that gives values for <big> {{math|1=''ƒ(x)''}} </big>.
Simpson's method is defined by the following pseudo-code:
{| class="mw-collapsible mw-collapsed"
|+ Pseudocode: Simpson's method, composite
|-
|
'''procedure''' quad_simpson_composite(f, a, b, n)
h := (b - a) / n
sum1 := f(a + h/2)
sum2 := 0
loop on i from 1 to (n - 1)
sum1 := sum1 + f(a + h * i + h/2)
sum2 := sum2 + f(a + h * i)
&nbsp;
''answer'' := (h / 6) * (f(a) + f(b) + 4*sum1 + 2*sum2)
|}
Demonstrate your function by showing the results for:
* &nbsp; {{math|1=ƒ(x) = x<sup>3</sup>}}, &nbsp; &nbsp; &nbsp; where &nbsp; '''x''' &nbsp; is &nbsp; &nbsp; [0,1], &nbsp; &nbsp; &nbsp; with &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 100 approximations. &nbsp; The exact result is &nbsp; &nbsp; 0.25 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (or 1/4)
* &nbsp; {{math|1=ƒ(x) = 1/x}}, &nbsp; &nbsp; where &nbsp; '''x''' &nbsp; is &nbsp; [1,100], &nbsp; &nbsp; with &nbsp; &nbsp; &nbsp; &nbsp;1,000 approximations. &nbsp; The exact result is &nbsp; &nbsp; 4.605170<sup>+</sup> &nbsp; &nbsp; (natural log of 100)
* &nbsp; {{math|1=ƒ(x) = x}}, &nbsp; &nbsp; &nbsp; &nbsp; where &nbsp; '''x''' &nbsp; is &nbsp; [0,5000], &nbsp; with 5,000,000 approximations. &nbsp; The exact result is &nbsp; 12,500,000
* &nbsp; {{math|1=ƒ(x) = x}}, &nbsp; &nbsp; &nbsp; &nbsp; where &nbsp; '''x''' &nbsp; is &nbsp; [0,6000], &nbsp; with 6,000,000 approximations. &nbsp; The exact result is &nbsp; 18,000,000
<br/>
;See also:
* &nbsp; [[Active object]] for integrating a function of real time.
* &nbsp; [[Special:PrefixIndex/Numerical integration]] for other integration methods.
<br/>