In mathematics, Boole's rule, named after George Boole, is a method of numerical integration.
It approximates an integral: by using the values of f at five equally spaced points: [1]
It is expressed thus in Abramowitz and Stegun: [2] where the error term is for some number between and where 945 = 1 × 3 × 5 × 7 × 9.
It is often known as Bode's rule, due to a typographical error that propagated from Abramowitz and Stegun. [3]
The following constitutes a very simple implementation of the method in Common Lisp which ignores the error term:
Example implementation in Common Lisp |
(defunintegrate-booles-rule(fx1x5)"Calculates the Boole's rule numerical integral of the function F in the closed interval extending from inclusive X1 to inclusive X5 without error term inclusion."(declare(type(function(real)real)f))(declare(typerealx1x5))(let((h(/(-x5x1)4)))(declare(typerealh))(let*((x2(+x1h))(x3(+x2h))(x4(+x3h)))(declare(typerealx2x3x4))(*(/(*2h)45)(+(*7(funcallfx1))(*32(funcallfx2))(*12(funcallfx3))(*32(funcallfx4))(*7(funcallfx5))))))) |
In cases where the integration is permitted to extend over equidistant sections of the interval , the composite Boole's rule might be applied. Given divisions, where mod , the integrated value amounts to: [4]
where the error term is similar to above. The following Common Lisp code implements the aforementioned formula:
Example implementation in Common Lisp |
(defunintegrate-composite-booles-rule(fabn)"Calculates the composite Boole's rule numerical integral of the function F in the closed interval extending from inclusive A to inclusive B across N subintervals."(declare(type(function(real)real)f))(declare(typerealab))(declare(type(integer1*)n))(let((h(/(-ba)n)))(declare(typerealh))(flet((f[i](i)(declare(type(integer0*)i))(let((xi(+a(*ih))))(declare(typerealxi))(thereal(funcallfxi)))))(*(/(*2h)45)(+(*7(+(f[i]0)(f[i]n)))(*32(loopforifrom1to(-n1)by2sum(f[i]i)))(*12(loopforifrom2to(-n2)by4sum(f[i]i)))(*14(loopforifrom4to(-n4)by4sum(f[i]i)))))))) |
Example implementation in R |
booleQuad<-function(fx,dx){# Calculates the composite Boole's rule numerical # integral for a function with a vector of precomputed# values fx evaluated at the points in vector dx.n<-length(dx)h<-diff(dx)stopifnot(exprs={length(fx)==nn>8Lh[1L]>=0n>=2Ln%%4L==1LisTRUE(all.equal(h,rep(h[1L],length(h))))})nm2<-n-2Lcf<-double(nm2)cf[seq.int(1,nm2,2L)]<-32cf[seq.int(2,nm2,4L)]<-12cf[seq.int(4,nm2,4L)]<-14cf<-c(7,cf,7)sum(cf*fx)*2*h[1L]/45} |