Numerical integration: Difference between revisions
m
make code foldable
Artoria2e5 (talk | contribs) (→top: minor: format) |
Artoria2e5 (talk | contribs) m (make code foldable) |
||
Line 7:
:** 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''}}).
Line 15:
Simpson's method is defined by the following pseudo-code:
{| class="mw-collapsible mw-collapsed"
|+ 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)
Demonstrate your function by showing the results for:
|