Numerical integration: Difference between revisions

Added 11l
m (→‎{{header|Phix}}: added syntax colouring the hard way)
(Added 11l)
Line 44:
 
<br/>
 
=={{header|11l}}==
{{trans|Nim}}
 
<lang 11l>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)))</lang>
 
{{out}}
<pre>
cube integrated using left_rect
from 0 to 1 (100 steps) = 0.245025
cube integrated using mid_rect
from 0 to 1 (100 steps) = 0.2499875
cube integrated using right_rect
from 0 to 1 (100 steps) = 0.255025
cube integrated using trapezium
from 0 to 1 (100 steps) = 0.250025
cube integrated using simpson
from 0 to 1 (100 steps) = 0.25
reciprocal integrated using left_rect
from 1 to 100 (1000 steps) = 4.654991058
reciprocal integrated using mid_rect
from 1 to 100 (1000 steps) = 4.604762549
reciprocal integrated using right_rect
from 1 to 100 (1000 steps) = 4.556981058
reciprocal integrated using trapezium
from 1 to 100 (1000 steps) = 4.605986058
reciprocal integrated using simpson
from 1 to 100 (1000 steps) = 4.605170385
identity integrated using left_rect
from 0 to 5000 (5000000 steps) = 12499997.5
identity integrated using mid_rect
from 0 to 5000 (5000000 steps) = 12500000
identity integrated using right_rect
from 0 to 5000 (5000000 steps) = 12500002.5
identity integrated using trapezium
from 0 to 5000 (5000000 steps) = 12500000
identity integrated using simpson
from 0 to 5000 (5000000 steps) = 12500000
identity integrated using left_rect
from 0 to 6000 (6000000 steps) = 17999997.000000003
identity integrated using mid_rect
from 0 to 6000 (6000000 steps) = 17999999.999999992
identity integrated using right_rect
from 0 to 6000 (6000000 steps) = 18000003.000000003
identity integrated using trapezium
from 0 to 6000 (6000000 steps) = 17999999.999999992
identity integrated using simpson
from 0 to 6000 (6000000 steps) = 17999999.999999992
</pre>
 
=={{header|ActionScript}}==
1,480

edits