Anonymous user
Numerical integration: Difference between revisions
Updated to work with Nim 1.4: replaced ".. <" with "..<", added a space before "x". Updated result.
m (→{{header|Wren}}: Change of parameter name for function arguments.) |
(Updated to work with Nim 1.4: replaced ".. <" with "..<", added a space before "x". Updated result.) |
||
Line 3,475:
proc cube(x: float): float =
x * x * x
proc reciprocal(x: float): float =
Line 3,485:
proc integrate(f: Function; a, b: float; steps: int; meth: Rule): float =
let h = (b-a) / float(steps)
for i in 0 ..
result += meth(f, a+float(i)*h, h)
result = h * result
Line 3,503:
Output:
<pre>cube integrated using leftRect
from 0 to 1 (100 steps) =
cube integrated using midRect
from 0 to 1 (100 steps) =
cube integrated using rightRect
from 0 to 1 (100 steps) =
cube integrated using trapezium
from 0 to 1 (100 steps) =
cube integrated using simpson
from 0 to 1 (100 steps) =
reciprocal integrated using leftRect
from 1 to 100 (1000 steps) = 4.
reciprocal integrated using midRect
from 1 to 100 (1000 steps) = 4.
reciprocal integrated using rightRect
from 1 to 100 (1000 steps) = 4.
reciprocal integrated using trapezium
from 1 to 100 (1000 steps) = 4.
reciprocal integrated using simpson
from 1 to 100 (1000 steps) = 4.
identity integrated using leftRect
from 0 to 5000 (5000000 steps) =
identity integrated using midRect
from 0 to 5000 (5000000 steps) =
identity integrated using rightRect
from 0 to 5000 (5000000 steps) =
identity integrated using trapezium
from 0 to 5000 (5000000 steps) =
identity integrated using simpson
from 0 to 5000 (5000000 steps) =
identity integrated using leftRect
from 0 to 6000 (6000000 steps) =
identity integrated using midRect
from 0 to 6000 (6000000 steps) =
identity integrated using rightRect
from 0 to 6000 (6000000 steps) =
identity integrated using trapezium
from 0 to 6000 (6000000 steps) =
identity integrated using simpson
from 0 to 6000 (6000000 steps) =
=={{header|OCaml}}==
|