28 lines
928 B
Plaintext
28 lines
928 B
Plaintext
right_rect(e, x, a, b, n) := block([h: (b - a) / n, s: 0],
|
|
for i from 1 thru n do s: s + subst(x = a + i * h, e),
|
|
s * h)$
|
|
|
|
left_rect(e, x, a, b, n) := block([h: (b - a) / n, s: 0],
|
|
for i from 1 thru n do s: s + subst(x = a + (i - 1) * h, e),
|
|
s * h)$
|
|
|
|
mid_rect(e, x, a, b, n) := block([h: (b - a) / n, s: 0],
|
|
for i from 1 thru n do s: s + subst(x = a + (i - 1/2) * h, e),
|
|
s * h)$
|
|
|
|
trapezium(e, x, a, b, n) := block([h: (b - a) / n, s: 0],
|
|
for i from 1 thru n - 1 do s: s + subst(x = a + i * h, e),
|
|
((subst(x = a, e) + subst(x = b, e)) / 2 + s) * h)$
|
|
|
|
simpson(e, x, a, b, n) := block([h: (b - a) / n, s: 0],
|
|
for i from 1 thru n do
|
|
s: s + subst(x = a + i * h, e) + 2 * subst(x = a + (i - 1/2) * h, e),
|
|
(subst(x = a, e) - subst(x = b, e) + 2 * s) * h / 6)$
|
|
|
|
/* some tests */
|
|
|
|
simpson(log(x), x, 1, 2, 20), bfloat;
|
|
2 * log(2) - 1 - %, bfloat;
|
|
|
|
trapezium(1/x, x, 1, 100, 10000) - log(100), bfloat;
|