Numerical integration: Difference between revisions

Content added Content deleted
(categories added)
(→‎{{header|R}}: Somewhat more idiomatic R)
Line 2,648:
 
=={{header|R}}==
{{needs-review|R|These examples may not be idiomatic}}
{{works with|R|2.11.0}}
<lang R>integrate_simpsonsintegrate.rect <- function(f, a, b, n, k=0) {
#k = 0 for left, 1 for right, 0.5 for midpoint
h = (b-a)/n
h <- sum1 = f(b-a + h)/2)n
x <- sum2seq(a, =b, 0len=n+1)
for sum(i in 1:f(nx[-1]-h*(1-k))) {*h
sum1 = sum1 + f(a + h * i + h/2)
sum2 = sum2 + f(a + h * i)
}
(h / 6) * (f(a) + f(b) + (4*sum1) + (2*sum2))
}
 
integrate_rectintegrate.trapezoid <- function(f, a, b, n,k=0) {
h =<- (b-a)/n
x <- xseq(a, =b, 0len=n+1)
fx <- for f(i in 1:nx) {
x = x + fsum(afx[-1] + fx[-length((i-kx)*h])) * h /2
}
x
}
 
integrate_trapezintegrate.simpsons <- function(f, a, b, n) {
h =<- (b-a)/n
x <- seq(a, b, len=n+1)
x=0
fx <- for f(k in 1:(n-1x)) {
x = sum(fx[-length(x)] + 4*f(ax[-1]-h/2) + kfx[-1]) * h)/6
}
(h/2) * (f(a) + f(b)) + (h*x)
}
 
Line 2,684 ⟶ 2,675:
f4 <- (function(x) {x})
 
integrate_simpsonsintegrate.simpsons(f1,0,1,100) #0.25
integrate_simpsonsintegrate.simpsons(f2,1,100,1000) # 4.60517
integrate_simpsonsintegrate.simpsons(f3,0,5000,5000000) # 12500000
integrate_simpsonsintegrate.simpsons(f4,0,6000,6000000) # 1.8e+07
 
integrate_rectintegrate.rect(f1,0,1,100,10) #TopLeft 0.245025
integrate_rectintegrate.rect(f1,0,1,100,0.5) #Mid 0.2499875
integrate_rectintegrate.rect(f1,0,1,100,01) #TopRight 0.255025
 
integrate_trapezintegrate.trapezoid(f1,0,1,100) # 0.250025</lang>
 
=={{header|REXX}}==