Numerical integration: Difference between revisions

m
make code foldable
(→‎top: minor: format)
m (make code foldable)
Line 7:
:** midpoint
:* [[wp:Trapezoidal_rule|trapezium]]
:* [[wp:Simpson%27s_rule|Simpson's]] (composite)
:** 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''}}).
Line 15:
 
Simpson's method is defined by the following pseudo-code:
{| class="mw-collapsible mw-collapsed"
<pre>
|+ Pseudocode: Simpson's method, composite
h := (b - a) / n
|-
sum1 := f(a + h/2)
|
sum2 := 0
'''procedure''' quad_simpson_composite(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)
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)
</pre>
 
Demonstrate your function by showing the results for:
17

edits