40 lines
1.4 KiB
Text
40 lines
1.4 KiB
Text
F left_rect((Float -> Float) f, Float x, Float h) -> Float
|
||
R f(x)
|
||
|
||
F mid_rect((Float -> Float) f, Float x, Float h) -> Float
|
||
R f(x + h / 2)
|
||
|
||
F right_rect((Float -> Float) f, Float x, Float h) -> Float
|
||
R f(x + h)
|
||
|
||
F trapezium((Float -> Float) f, Float x, Float h) -> Float
|
||
R (f(x) + f(x + h)) / 2.0
|
||
|
||
F simpson((Float -> Float) f, Float x, Float h) -> Float
|
||
R (f(x) + 4 * f(x + h / 2) + f(x + h)) / 6.0
|
||
|
||
F cube(Float x) -> Float
|
||
R x * x * x
|
||
|
||
F reciprocal(Float x) -> Float
|
||
R 1 / x
|
||
|
||
F identity(Float x) -> Float
|
||
R x
|
||
|
||
F integrate(f, a, b, steps, meth)
|
||
V h = (b - a) / steps
|
||
V ival = h * sum((0 .< steps).map(i -> @meth(@f, @a + i * @h, @h)))
|
||
R ival
|
||
|
||
L(a, b, steps, func, func_name) [(0.0, 1.0, 100, cube, ‘cube’),
|
||
(1.0, 100.0, 1000, reciprocal, ‘reciprocal’),
|
||
(0.0, 5000.0, 5'000'000, identity, ‘identity’),
|
||
(0.0, 6000.0, 6'000'000, identity, ‘identity’)]
|
||
L(rule, rule_name) [(left_rect, ‘left_rect’),
|
||
(mid_rect, ‘mid_rect’),
|
||
(right_rect, ‘right_rect’),
|
||
(trapezium, ‘trapezium’),
|
||
(simpson, ‘simpson’)]
|
||
print("#. integrated using #.\n from #. to #. (#. steps) = #.".format(
|
||
func_name, rule_name, a, b, steps, integrate(func, a, b, steps, rule)))
|