Numerical integration: Difference between revisions

Add Swift
m (→‎{{header|Ring}}: Remove vanity tags)
(Add Swift)
Line 4,582:
1 | 17999997 18000003 18000000 18000000 18000000 |
+--------------------------------------------------------+</pre>
 
=={{header|Swift}}==
<lang swift>public enum IntegrationType : CaseIterable {
case rectangularLeft
case rectangularRight
case rectangularMidpoint
case trapezium
case simpson
}
 
public func integrate(
from: Double,
to: Double,
n: Int,
using: IntegrationType = .simpson,
f: (Double) -> Double
) -> Double {
let integrationFunc: (Double, Double, Int, (Double) -> Double) -> Double
 
switch using {
case .rectangularLeft:
integrationFunc = integrateRectL
case .rectangularRight:
integrationFunc = integrateRectR
case .rectangularMidpoint:
integrationFunc = integrateRectMid
case .trapezium:
integrationFunc = integrateTrapezium
case .simpson:
integrationFunc = integrateSimpson
}
 
return integrationFunc(from, to, n, f)
}
 
private func integrateRectL(from: Double, to: Double, n: Int, f: (Double) -> Double) -> Double {
let h = (to - from) / Double(n)
var x = from
var sum = 0.0
 
while x <= to - h {
sum += f(x)
x += h
}
 
return h * sum
}
 
private func integrateRectR(from: Double, to: Double, n: Int, f: (Double) -> Double) -> Double {
let h = (to - from) / Double(n)
var x = from
var sum = 0.0
 
while x <= to - h {
sum += f(x + h)
x += h
}
 
return h * sum
}
 
private func integrateRectMid(from: Double, to: Double, n: Int, f: (Double) -> Double) -> Double {
let h = (to - from) / Double(n)
var x = from
var sum = 0.0
 
while x <= to - h {
sum += f(x + h / 2.0)
x += h
}
 
return h * sum
}
 
private func integrateTrapezium(from: Double, to: Double, n: Int, f: (Double) -> Double) -> Double {
let h = (to - from) / Double(n)
var sum = f(from) + f(to)
 
for i in 1..<n {
sum += 2 * f(from + Double(i) * h)
}
 
return h * sum / 2
}
 
private func integrateSimpson(from: Double, to: Double, n: Int, f: (Double) -> Double) -> Double {
let h = (to - from) / Double(n)
var sum1 = 0.0
var sum2 = 0.0
 
for i in 0..<n {
sum1 += f(from + h * Double(i) + h / 2.0)
}
 
for i in 1..<n {
sum2 += f(from + h * Double(i))
}
 
return h / 6.0 * (f(from) + f(to) + 4.0 * sum1 + 2.0 * sum2)
}
 
let types = IntegrationType.allCases
 
print("f(x) = x^3:", types.map({ integrate(from: 0, to: 1, n: 100, using: $0, f: { pow($0, 3) }) }))
print("f(x) = 1 / x:", types.map({ integrate(from: 1, to: 100, n: 1000, using: $0, f: { 1 / $0 }) }))
print("f(x) = x, 0 -> 5_000:", types.map({ integrate(from: 0, to: 5_000, n: 5_000_000, using: $0, f: { $0 }) }))
print("f(x) = x, 0 -> 6_000:", types.map({ integrate(from: 0, to: 6_000, n: 6_000_000, using: $0, f: { $0 }) }))</lang>
 
{{out}}
<pre>f(x) = x^3: [0.2450250000000004, 0.23532201000000041, 0.2401367512500004, 0.25002500000000005, 0.25000000000000006]
f(x) = 1 / x: [4.55599105751469, 4.654000076443428, 4.603772058385689, 4.60598605751468, 4.605170384957145]
f(x) = x, 0 -> 5_000: [12499997.500728704, 12499992.500729704, 12499995.000729209, 12500000.000000002, 12500000.0]
f(x) = x, 0 -> 6_000: [17999997.001390498, 17999991.0013915, 17999994.001391016, 18000000.000000004, 17999999.999999993]</pre>
 
=={{header|Tcl}}==