45 lines
2.3 KiB
Plaintext
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)
|
|
|
|
''answer'' := (h / 6) * (f(a) + f(b) + 4*sum1 + 2*sum2)
|
|
|}
|
|
|
|
|
|
Demonstrate your function by showing the results for:
|
|
* {{math|1=ƒ(x) = x<sup>3</sup>}}, where '''x''' is [0,1], with 100 approximations. The exact result is 0.25 (or 1/4)
|
|
* {{math|1=ƒ(x) = 1/x}}, where '''x''' is [1,100], with 1,000 approximations. The exact result is 4.605170<sup>+</sup> (natural log of 100)
|
|
* {{math|1=ƒ(x) = x}}, where '''x''' is [0,5000], with 5,000,000 approximations. The exact result is 12,500,000
|
|
* {{math|1=ƒ(x) = x}}, 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.
|
|
* [[Special:PrefixIndex/Numerical integration]] for other integration methods.
|
|
|
|
<br/>
|
|
|