Numerical integration: Difference between revisions

Content added Content deleted
Line 2,597: Line 2,597:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
<lang julia>function simpson(f, a,b, n)

h = (b-a)/n
<lang julia>function simpson(f::Function, a::Number, b::Number, n::Integer)
s = f(a + h/2)
h = (b - a) / n
s = f(a + h / 2)
for i in 1:(n-1)
for i in 1:(n-1)
s += f(a + h*i + h/2) + 0.5 * f(a + h*i)
s += f(a + h * i + h / 2) + f(a + h * i) / 2
end
end
h/6 * (f(a) + f(b) + 4*s)
return h/6 * (f(a) + f(b) + 4*s)
end</lang>
end

rst =
simpson(x -> x ^ 3, 0, 1, 100),
simpson(x -> 1 / x, 1, 100, 1000),
simpson(x -> x, 0, 5000, 5_000_000),
simpson(x -> x, 0, 6000, 6_000_000)

@show rst</lang>

{{out}}
{{out}}
<pre>rst = (0.25000000000000006, 4.605170384957135, 1.25e7, 1.8e7)</pre>
<pre>
julia> simpson(x -> x^3, 0,1, 100),
simpson(x -> 1/x, 1,100, 1000),
simpson(x -> x, 0, 5000, 5000000),
simpson(x -> x, 0, 6000, 6000000)
(0.25000000000000006,4.605170384957135,1.25e7,1.8e7)
</pre>


=={{header|Kotlin}}==
=={{header|Kotlin}}==