Numerical integration: Difference between revisions
m
→{{header|R}}: simpler and more general
(Numerical integration in Yabasic) |
m (→{{header|R}}: simpler and more general) |
||
Line 4,547:
{{works with|R|2.11.0}}
<lang
h <- (b - a) / n▼
s <- s + sum(v * f(
}
s * h
}
c(rect.left = integ(f, a, b, n, 0, 1),
▲ h <- (b-a)/n
rect.mid = integ(f, a, b, n, 0.5, 1),
trapezoidal = integ(f, a, b, n, c(0, 1), c(0.5, 0.5)),
simpson = integ(f, a, b, n, c(0, 0.5, 1), c(1, 4, 1) / 6))
}
test(\(x) x^3, 0, 1, 100)
# rect.left rect.right rect.mid trapezoidal simpson
# 0.2450250 0.2550250 0.2499875 0.2500250 0.2500000
test(\(x) 1 / x, 1, 100, 1000)
# rect.left rect.right rect.mid trapezoidal simpson
# 4.654991 4.556981 4.604763 4.605986 4.605170
test(\(x) x, 0, 5000, 5e6)
# rect.left rect.right rect.mid trapezoidal simpson
# 12499998 12500003 12500000 12500000 12500000
test(\(x) x, 0, 6000, 6e6)
# rect.left rect.right rect.mid trapezoidal simpson
# 1.8e+07 1.8e+07 1.8e+07 1.8e+07 1.8e+07</lang>
=={{header|Racket}}==
|