Numerical Integration

Subhendu Ghosh, Swarnadeep Datta, Debanjan Bhattacharjee

Numerical Integration

The main aim of numerical integration methods is to approximate the values of definite integrals which are difficult to compute mathematically
Consider a given definite integral \[I(f)\;\;\;=\;\; \int_{a}^{b}f( x) \,dx \] We shall approximate this integral by \[I_{n} \;\;\;\;\;=\;\;\;\;\sum_{i=i_{0}}^n w_if(x_i)\] where \(i_{0} = 0\) or \(i_{0} = 1\) is always the case.

In other words

The problem of integration may be stated as follows:-
Given a set of points \((x_0,y_o), (x_1,y_1), (x_2,y_2),...,(x_n,y_n)\) of a function f(x)=y. The problem is to find the value of the definite integral \(\int_{a}^{b}f( x) \,dx\). In numerical integration, the integrand f(x) is first replaced by a suitable interpolation polynomial \(\phi(x)\) ,and then the approximate value of the definite integral is evaluated by \[\int_{a}^{b}f(x) dx \;\; \overset{\sim}{=}\;\;\int_{a}^{b}\phi(x) dx \] This gives us the ‘quadrature formula’ for numerical integration.In the case of equidistant tabular points, either the Newton’s forward formula or Newton’s backward formula are used. Otherwise, we can use Lagrange’s formula for the interpolating polynomial.

General Quadrature Formula

Let y = f(x) be a function defined on the interval [a,b] and y = f(x) be known corresponding to the (n+1) equidistant values or arguments \(x_{i}=x_{0}+ih ,i =0,1,2,...,n\) , where a =\(x_{0}\;,b=x_{n}=x_{0}+nh\) and h being the interval of the differencing. That is , y = f(x) is known for (n+1) data points: \((x_{0},y_{0})\;,(x_{1},y_{1})\;,...(x_{n},y_{n})\;,\) where a = \(x_0\; b=x_n\) , \(x_i-x_{i-1}\) = h, i=1,2,…,n and h=\(\dfrac{b-a}{n}\) , called the length of each interval.

Contd,..

A general quadrature formula is obtained by replacing the integarand f(x) by Newton’s forward difference interpolating polynomial \(\phi(x)\) such that \(y_i= f(x_i)=\phi(x_i)\) , for each \(i=0,1,2,…,n\), where \[\phi(x)=\phi(x_0 +uh)= y_0+ u\Delta y_0 +\dfrac{u(u-1)}{2!}\Delta^2y_0 +\dfrac{u(u-1)(u-2)}{3!}\Delta^3y_0+...+\dfrac{u(u-1)...(u-n+1)}{n!}\Delta^ny_0\]with u = \(\dfrac{x-x_0}{h}\)

Contd,..

Now, \(I = \int_{a}^{b}f(x) dx = \int_{x_0}^{x_n}f(x) dx\overset{\sim}{=}\; \int_{x_0}^{x_n}\phi(x) dx\; =\int_{x_0}^{x_0+nh}f(x) dx\) \(= h\int_{0}^{n}\phi(x_0+uh) du\;\;[since\;,\; x =x_0+uh\; and\;dx=h\;du]\) \(=h\int_{0}^{n}[y_0+ u\Delta y_0 +\dfrac{u(u-1)}{2!}\Delta^2y_0 +\dfrac{u(u-1)(u-2)}{3!}\Delta^3y_0+...]\; du\) \(=h\;[uy_0+ \dfrac{u^2}{2}\Delta y_0 +(\dfrac{u^3}{3}-\dfrac{u^2}{2})\dfrac{\Delta^2y_0}{2!} +(\dfrac{u^4}{4}-u^3+u^2)\dfrac{\Delta^3y_0}{3!}\;+\;...]_0^n\) \(=\; h[ny_0+ \dfrac{n^2}{2}\Delta y_0 +(\dfrac{n^3}{3}-\dfrac{n^2}{2})\dfrac{\Delta^2y_0}{2!} +(\dfrac{n^4}{4}-n^3+n^2)\dfrac{\Delta^3y_0}{3!}\;+\;...]\) This formula is called General Gauss Legendre quadrature formula for equidistant ordinates. From the above general quadature formula by replacing n = 1,2,3,…, etc .

Contd,..

For n=1 i.e., when the linear interpolating polynomial is used the we have \[I = \int_{a}^{b}f(x) dx\; \overset{\sim}{=}\; \int_{x_0}^{x_n}\phi(x)dx=h\left[y_0+\dfrac{1}{2}\Delta y_0\right]\;=\dfrac{h}{2}[y_0+y_1]\] Similarly, using interpolating formula of degree \(2\)(i.e., \(n=2\)), we obtain \[I = \int_{a}^{b}f(x) dx\; \overset{\sim}{=}\; \int_{x_0}^{x_n}\phi(x)dx\; =\; h\left[2y_0+2\Delta y_0+\left(\dfrac{8}{3}-\dfrac{4}{2}\right)\Delta^2y_0\right]\] \[=2h\left[y_0+(y_1-y_0)+\dfrac{1}{3}\times\dfrac{y_2-2y_1+y_0}{2}\right]\] \[=\dfrac{h}{3}\left[y_0+4y_1+y_2\right]\]

Contd,..

In the above we have replaced the integrand by an interpolating polynomial over the whole interval \([a,b]\) and then integrated it term by term . However this process is not very useful. More useful Numerical Integration formulae are obtained by dividing the interval [a,b] in n sub-intervals \([x\_{k-1},x_k]\),where \(x_k=x_0+kh\) for k=1,2,3,…,n with \(a=x_0,\;b=x_n=x_0+nh\; and \;h=\dfrac{b-a}{n}\)

Methods Of Numerical Integration

The methods of numerical integration which we shall take into consideration are
- Trapezoid Rule
- Simpson’s 1/3rd Rule
- The mid-point Rule
- Simpson’s 3/8th Rule
- Boole’s Rule
- Extrapolation methods

Packages Used

Code
library(pracma)
library(kableExtra)

Trapezoid Rule

In trapeziod rule we approximate the integral by:-

\[T_{n} \;=\;\;\dfrac{h}{2}\left(f(x_{0})+2f(x_{1})+2f(x_{2})+\ldots+2f(x_{n-1})+f(x_{n})\right)\] The Error is given by :- \[T_{n} - I(f)\;\;\;=-\dfrac{b-a}{12}h^{2}f^{2}(\epsilon_{h})\] Where \(\epsilon_{h}\) is some value in the interval [a,b]

Geometric Interpretation of Trapezoidal rule

In trapezoidal rule the integrand y=f(x) is replaced by n straight lines joining the pairs of points: \((x_0,y_0),(x_1,y_1),...,(x_n,y_n)\) then the area bounded by the curve, between the ordinates \(x=x_0\) and \(x=x_n\) and the x axis is approximated by the sum of area of n trapeziums obtained.  The area of the integration \(\int_{a}^bf(x)dx\) is approximated by dividing the total into n trapezoids rather than rectangles. The area of the trapezium ABCD with a height of h and the bases of length \(y_0\) and \(y_1\) is given by,

Contd,..

Area of the trapezium ABCD = \(\dfrac{h}{2}(y_0 +y_1)\) alternatively, we assume the linear equation \[y=f(x)\overset{\sim}{=}\phi(x)=c_0 + c_1x\] Now, \(y_0 = \phi(x_0) = c_0 +c_1x_0\) and \(y_1 = \phi(x_1) = c_0 +c_1x_1\)
Then the area bounded by the line segment AB, the straight lines \(x=x_0, x=x_1\) and the x axis is approximated by the definite integral: \[\int_{x_0}^{x_1}(c_0 + c_1x)dx = \left[c_ox + c_1\dfrac{x^2}2\right]^{x_1}_{x_0}\] \[=c_0(x_1-x_0)+\dfrac{c_1}2(x_1^2\;-\;x_0^2)\] \[=c_0h\;+\;\dfrac{c_1}2(x_1-x_0)(x_1+x_0)\] \[=\;\dfrac{h}2[2c_0\;+c_1(x_0+x_1)]\] \[=\dfrac{h}2[(c_0+c_1x_0)+(c_0+c_1x_1)]\] \[=\dfrac{h}2(y_0+y_1)\]

Contd,..

Therefore, \(\int_{x_0}^{x_1}\phi(x)dx\;=\;\int_{x_0}^{x_1}(c_0+c_1x)dx\;=\;\dfrac{h}2(y_0 +y_1)\)
Thus we have \[\int_a^bf(x)dx\overset{\sim}=\int_{x_0}^{x_n}\phi(x)dx\] \[= \int_{x_0}^{x_1}\phi(x)dx\;\int_{x_1}^{x_2}\phi(x)dx\; +....+\int_{x_{n-1}}^{x_n}\phi(x)dx\] \[= \dfrac{h}2[(y_0 +y_1)+(y_1+y_2)+...+(y_{n-1}+y_n)]\] \[=\dfrac{h}2[(y_0+y_n)+2(y_1+...+y_{n-1})]\]

\[f(x) \hspace{0.5cm}=\hspace{0.5cm}log(x)tan^{-1}(x)\hspace{0.5cm} on\hspace{.5cm} [1,2]\]

Code
f <- function(x){
  log(x)*atan(x)
}
g<-NULL
h<-NULL
for(i in 1:10)
  {
  if(i>1 ){
    g[i]=f(1+(i-1)/10)
  }
  else{
    g[i]=0
  }
}
T <- (f(1)+2*sum(g)+f(2))/20
T
[1] 0.3930818

Simpson’s 1/3rd Rule

In Simpson’s 1/3rd rule we approximate the integral by:-

\[S_{n}\;=\;\dfrac{h}{3}(f(x_{0})+4f(x_{1})+2f(x_{2})+4f(x_{3})+....+2f(x_{n-2})+4f(x_{n-1})+f(x_{n}))\] We can only use Simpson’s 1/3 rule when the n is even The Error is given by :- \[S_{n} - I(f)\;\;\;=-\dfrac{b-a}{90}h^{4}f^{4}(\epsilon_{h})\] Where \(\epsilon_{h}\) is some value in the interval [a,b]

Geometric Interpretation of Simpson’s 1/3rd rule

In Simpson’s 1/3rd rule, the integrand y=f(x) is replaced by n/2 arcs of second degree polynomials or parabolas passing through the points:\([(x_0,y_0),(x_1,y_1)(x_2,y_2)];[(x_2,y_2),(x_3,y_3)(x_4,y_4)];...;[(x_{n-2},y_{n-2}),(x_{n-1},y_{n-1})(x_n,y_n)]\)
The area bounded by the curve y = f(x) between the ordinates \(x=x_0\) and \(x=x_n\) and the x axis is approximated by the sum of areas of the n/2 parabolas obtained.
Let us assume that the equation of the parabola be \(y\;=\;f(x)\;\overset{\sim}=\;\phi(x)\;=\;c_0+c_1x+c_2x^2\)

Contd,..

Now, \(y_0 = \phi(x_0) = c_0+c_1x_0+c_2x_0^2\)
\(y_1 = \phi(x_1) = c_0+c_1x_1+c_2x_1^2\)
\(y_2 = \phi(x_2) = c_0+c_1x_2+c_2x_2^2\) \[\dfrac{h}3(y_0+4y_1+y_2)\] \[=\dfrac{h}3[(c_0+c_1x_0+c_2x_0^2)+4(c_0+c_1x_1+c_2x_1^2)+(c_0+c_1x_2+c_2x_2^2)]\] \[=\dfrac{h}3(6c_0\;+\;c_1(x_0 +4x_1+x_2)\;+\;c_2(x_0^2 +4x_1^2+x_2^2))\] \[=c_02h\;+\;c_1\dfrac{h}3(6x_0+6h)\;+\;c_2\dfrac{h}3(x_0^2+4(x_0 +h)^2+(x_0+2h)^2)\] \[=c_02h+c_12h(x_0+h)+c_2\dfrac{2h}3(3x_0^2+6x_0h+4h^2)\]

Contd,..

Then the area bounded by the parabola ABC, the straight lines \(x=x_0,x=x_2\) and the x axis is approximated by the definite integral: \[\int_{x_0}^{x_2}(c_0 + c_1x + c_2x^2)dx\] \[=\left[c_0x + c_1\dfrac{x^2}2 + c_2\dfrac{x^3}3\right]_{x_0}^{x_2}\] \[=c_0(x_2-x_0) + \dfrac{c_1}2(x_2^2-x_0^2) + \dfrac{c_2}3(x_2^3-x_0^3)\] \[=c_02h+c_12h(x_0+h)+c_2\dfrac{2h}3(3x_0^2 + 6x_0h + 4h^2)\]

Contd,..

Therefore,\(\int_{x_0}^{x_2}\phi(x)dx=\int_{x_0}^{x_2}(c_0+c_1x+c_2x^2)dx=\dfrac{h}3(y_0+4y_1+y_2)\) Thus,\(\int_{a}^{b}f(x)dx\overset{\sim}=\int_{x_0}^{x_0+nh}\phi(x)dx\) \[=\dfrac{h}3(y_0+4y_1+y_2)+\dfrac{h}3(y_2+4y_3+y_4)+...+\dfrac{h}3(y_{n-2}+4y_{n-1}+y_n)\] \[=\dfrac{h}3[(y_0+y_n)+4(y_1+y_3+...+y_{n-1})+2(y_2+y_4+...+y_{n-2})]\]

\[f(x) \hspace{.5cm}=\hspace{.5cm}log(x)tan^{-1}(x)\hspace{.5cm} on\hspace{.5cm} [1,2]\]

Code
f <- function(x){
  log(x)*atan(x)
}
g<-NULL
h<-NULL
for(i in 1:10)
  {
  if(i>1 && i%%2 == 0){
    g[i]=f(1+(i-1)/10)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%2 == 1){
    h[i]=f(1+(i-1)/10)
  }
  else{
    h[i]=0
  }
}
S <- (f(1)+4*sum(g)+2*sum(h)+f(2))/30
S
[1] 0.3931604

Adaptive Simpson’s Method

Adaptive Simpson’s method uses an estimate of the error we get from calculating a definite integral using Simpson’s rule. If the error exceeds a user-specified tolerance, the algorithm calls for subdividing the interval of integration in two and applying adaptive Simpson’s method to each sub interval in a recursive manner. The technique is usually much more efficient than composite Simpson’s rule since it uses fewer function evaluations in places where the function is well-approximated by a cubic function.

\[f(x) \hspace{.5cm}=\hspace{.5cm} log(x)tan^{-1}(x)\hspace{,5cm} on \hspace{.5cm}[1,2]\]

Code
library(pracma)
f<- function(x) log(x)*atan(x)
simpadpt(f,1,2)
[1] 0.3931597

Mid-point Rule

Midpoint Rule

An interesting and a very simple quadrature rule is the midpoint rule , based on exactly integrating a linear Taylor approximation to the integrand. This results in a rule that is actually more accurate than the trapezoidal rule. We proceed in much the same manner as with the trapezoidal and simpson’s rules; the approximation is: \[M_n(f)=h\sum_{i=1}^{n}f(a+ (i-\dfrac{1}{2})h)\]

\[f(x)\hspace{.5cm} = \hspace{.5cm}log(x)tan^{-1}(x)\hspace{.5cm} on\hspace{.5cm} [1,2]\]

Code
f <- function(x){
  log(x)*atan(x)
}
g<-NULL
for (i in 1:10){
  g[i]<-f(1+(2*i-1)/20)
}
M<-(1/10)*sum(g)
M
[1] 0.3931986

Simpson’s 3/8th rule

Simpson’s 3/8 rule

Simpson’s 3/8 rule, often known as Simpson’s second rule is another method of numerical integration proposed by Thomas Simpson. It is based on a cubic interpolation rather than a quadratic interpolation. The approximation formula is: \[S^{3/8}_n = \dfrac{3}8h(f(x_0)+3f(x_1)+....+3f(x_{n-1})+f(x_n))\] Note that we can only use this formula when n is a multiple of 3
The Error is given by :- \[S^{3/8}_{n} - I(f)\;\;\;=-\dfrac{3(b-a)}{80}h^{4}f^{4}(\epsilon_{h})\] Where \(\epsilon_{h}\) is some value in the interval [a,b]

\[f(x) \hspace{.5cm}= \hspace{0.5cm}log(x)tan^{-1}(x)\hspace{.5cm} on\hspace{.5cm}[1,2]\]

Code
f <- function(x){
  log(x)*atan(x)
}
g<-NULL
h<-NULL
for (i in 1:12)
  {
  if(i>1 && i%%3 == 0){
    g[i]=f(1+(i-1)/12)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%3 != 0){
    h[i]=f(1+(i-1)/12)
  }
  else{
    h[i]=0
  }
}
S_2<- (3/8)*(1/12)*(f(1)+3*sum(h)+2*sum(g)+f(2))
S_2
[1] 0.3770855

Boole’s Rule

Boole’s Rule

Here we approximate the integral by:-
\[B_n = \dfrac{2h}{45}(7(f(x_0)+f(x_n))\;\; + \;\;32\sum_{i\epsilon(1,3,...,n-1)}f(x_i) +12\sum_{i\epsilon(2,6,...,n-2)}f(x_i)\;\;+14\sum_{i\epsilon(4,8,..,n)}f(x_i))\] Note that we can use this formula when n is a multiple of 4
The error of this approximation is given by, The Error is given by :- \[B_{n} - I(f)\;\;\;=-\dfrac{8(b-a)}{945}h^{6}f^{6}(\epsilon_{h})\] Where \(\epsilon_{h}\) is some value in the interval [a,b]

\[f(x) \hspace{.5cm}= \hspace{.5cm}log(x)tan^{-1}(x)\hspace{.5cm} on\hspace{.5cm} [1,2]\]

Code
f <- function(x){
  log(x)*atan(x)
}
g<-NULL
h<-NULL
k<-NULL
for (i in 1:12)
  {
  if(i>1 && i%%4 == 0){
    g[i]=f(1+(i-1)/12)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%2==0 && i%%4 != 0){
    h[i]=f(1+(i-1)/12)
  }
  else{
    h[i]=0
  }
  if(i>1 && i%%2!=0){
    k[i]=f(1+(i-1)/12)
  }
  else{
    k[i]=0
  }
}
B<-(2/45)*(1/12)*(7*(f(1)+f(2))+32*sum(k)+12*sum(h)+14*sum(g))
B
[1] 0.3688798

Extrapolation Methods

Extrapolation Methods

One of the most important ideas in computational mathematics is that we can take the information from a few approximations and use that to both estimate the error in the approximation and generate a significantly improved approximation.
Given and integral \(I(f)\), consider a generic quadrature rule that we will denote by \(I_n(F)\) Assume an error relationship of the form \[I(f) - I_n(f) \overset{\sim}=\;\;Cn^{-p}\] Note that this relation holds true for all of our discussed approximating rules

Contd,..

We want to use this approximate equality to do three different things:
1. Estimate the value of p.
2. Construct a computable estimate of the error \(I(f) - I_n(f)\)
3. Consrtuct an improved approximation of \(I(f)\)
We can in fact do all three without a lot of work, and it leads us to a very efficient scheme for approximating integration.

Estimating p

Consider the following , \[I(f)-I_n(f)\overset{\sim}=\;\;Cn^{-p}\] \[I(f)-I_{2n}(f)\overset{\sim}=\;\;C(2n)^{-p}\] \[I(f)-I_{4n}(f)\overset{\sim}=\;\;C(4n)^{-p}\] Now, consider the ratio, \[r_{4n}\;\;=\;\;\dfrac{I_n-I_{2n}}{I_{2n}-I_{4n}}\] Where we have dropped the explicit argument f from the integration rule for notational simplicity. We have \[r_{4n}\;\;\;\;=\;\;\dfrac{(I_n-I)+(I-I_{2n})}{(I_{2n}-I)+(I-I_{4n})}\] \[\overset{\sim}=\dfrac{-Cn^{-p}+C(2n)^{-p}}{-C(2n)^{-p}+C(4n)^{-p}}\] \[\overset{\sim}=\dfrac{-n^{-p}+(2n)^{-p}}{-(2n)^{-p}+(4n)^{-p}}\] \[=\dfrac{2^{-p}-1}{4^{-p}-2^{-p}}\] \[=2^p\] Thus, the computable ratio \(r_{4n}\) is approximately equal to \(2^p\). Therefore we can estimate the value of p by solving to get \[p\;\;\overset{\sim}{=}\;\;\dfrac{log\;r_{4n}}{log\;2}\]

Error estimation and an improved approximation

We have already seen that our assumptions imply that \[I\;\;-\;\;I_{2n}\;\;\overset{\sim}=\;C(2n)^{-p}\;\;=2^{-p}(Cn^{-p})\;\;\overset{\sim}=\;\;2^{-p}(I-I_n)\] and we can solve the approximate equality to get \[I\;\;\overset{\sim}=\;\;\dfrac{2^pI_{2n}-I_n}{2^p-1}\] Thus we define, \[R_{2n}\;\;=\;\;\dfrac{2^pI_{2n}-I_n}{2^p-1}\] which we call RICHARDSON’s extrapolated value.

\[f(x)\hspace{.5cm} =\hspace{.5cm} log(x)tan^{-1}(x)\hspace{.5cm} on\hspace{.5cm} [1,2]\]

Code
f <- function(x){
  log(x)*atan(x)
}
g<-NULL
h<-NULL
for(i in 1:10)
  {
  if(i>1 ){
    g[i]=f(1+(i-1)/10)
  }
  else{
    g[i]=0
  }
}
T1 <- (f(1)+2*sum(g)+f(2))/20
g<-NULL
h<-NULL
for(i in 1:20)
  {
  if(i>1 ){
    g[i]=f(1+(i-1)/20)
  }
  else{
    g[i]=0
  }
}
T2 <- (f(1)+2*sum(g)+f(2))/40
g<-NULL
h<-NULL
for(i in 1:40)
  {
  if(i>1 ){
    g[i]=f(1+(i-1)/40)
  }
  else{
    g[i]=0
  }
}
T4 <- (f(1)+2*sum(g)+f(2))/80
r=(T1-T2)/(T2-T4)
p=log(r)/log(2)
R<-(2^p*T2-T1)/(2^p-1)
R
[1] 0.3931596

Integrate Function of R

\[f(x)\hspace{.5cm} =\hspace{.5cm} log(x)tan^{-1}(x)\hspace{.5cm} on\hspace{.5cm} [1,2]\]

Code
f <- function(x){
  log(x)*atan(x)
}

integrate(f,lower = 1,upper = 2,subdivisions = 10)$value
[1] 0.3931596

Calculating the value of \(\pi\) through several methods

We know that the area of the unit circle is \(\pi\),
The equation of the unit circle is \[x^2\;\;+\;\;y^2\;\;=\;\;1\] \[<=>\;\;y\;\;=\;\;\sqrt{1-x^2}\] So we shall integrate this function through our integration methods and shall obtain the area of the unit semi circle. Multiplying it by two we will get area of the unit circle that us \(\pi\)

Finding Area of The Unit Circle to Calculate Pi

[1] "3.14159265359"
Estimated Value of pi
Names Val
Trapezoidal 3.14142050241899
Simpson's 1/3rd 3.14152541056175
Mid-point 3.14164307031918
Simpson's 3/8th 3.13996036426816
Boole's 3.1366063959031
Extrapolating 3.14159265468571
Integrate function 3.14159307692069

Comparing the estimated time

[1] "Trapezoid Rule"
   user  system elapsed 
   0.02    0.00    0.01 
[1] "Simpson's 1/3rd"
   user  system elapsed 
      0       0       0 
[1] "Mid point rule"
   user  system elapsed 
      0       0       0 
[1] "Simpson 3/8th rule"
   user  system elapsed 
   0.00    0.00    0.01 
[1] "Boole's Rule"
   user  system elapsed 
   0.00    0.00    0.02 
[1] "Extrapolation Rule"
   user  system elapsed 
   0.02    0.00    0.03 
[1] "Integral function"
   user  system elapsed 
      0       0       0 

Calculating the value of the integral of \(\dfrac{sin(x)}{(1+x)}\) over [0,1]

Estimated Value
Names Val
Trapezoidal 0.284226380949649
Simpson's 1/3rd 0.284226985510906
Mid-point 0.284227287793651
Simpson's 3/8th 0.283933901923158
Boole's 0.283784102256449
Extrapolating 0.284226985512435
Integrate function 0.284226985512411

Computational time

[1] "Trapezoid Rule"
   user  system elapsed 
   0.02    0.00    0.02 
[1] "Simpson's 1/3rd"
   user  system elapsed 
   0.01    0.00    0.03 
[1] "Mid point rule"
   user  system elapsed 
   0.00    0.00    0.03 
[1] "Simpson 3/8th rule"
   user  system elapsed 
   0.02    0.00    0.01 
[1] "Boole's Rule"
   user  system elapsed 
   0.00    0.00    0.03 
[1] "Extrapolation Rule"
   user  system elapsed 
   0.02    0.00    0.02 
[1] "Integral function"
   user  system elapsed 
      0       0       0 

\[\dfrac{sin(x)}{(1+x)}\]

Code
curve(sin(x)/(1+x),0,1)

Finding the Arc length

If \(f'(x)\) is continuous on [a,b], then the arc length of the curve y=f(x) on the interval [a,b] is given by \[\int_{a}^{b}\sqrt{1+(f'(x))^2}dx\]

\[\dfrac{sin(x)}{(1+x)}\]

Code
f<-function(x){
  sqrt(1+(((1+x)*cos(x)-sin(x))/(1+x)^2)^2)
}
g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 ){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
}
T <- (f(0)+2*sum(g)+f(1))/720

g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 && i%%2 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%2 == 1){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
}
S <- (f(0)+4*sum(g)+2*sum(h)+f(1))/1080

g<-NULL
for (i in 1:360){
  g[i]<-f((2*i-1)/720)
}
M<-(1/360)*sum(g)

g<-NULL
h<-NULL
for (i in 1:360)
  {
  if(i>1 && i%%3 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%3 != 0){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
}
S_2<- (3/8)*(1/360)*(f(0)+3*sum(h)+2*sum(g)+f(1))

g<-NULL
h<-NULL
k<-NULL
for (i in 1:360)
  {
  if(i>1 && i%%4 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%2==0 && i%%4 != 0){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
  if(i>1 && i%%2!=0){
    k[i]=f((i-1)/360)
  }
  else{
    k[i]=0
  }
}
B<-(2/45)*(1/360)*(7*(f(0)+f(1))+32*sum(k)+12*sum(h)+14*sum(g))

g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 ){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
}
T1 <- (f(0)+2*sum(g)+f(1))/720
g<-NULL
h<-NULL
for(i in 1:720)
  {
  if(i>1 ){
    g[i]=f((i-1)/720)
  }
  else{
    g[i]=0
  }
}
T2 <- (f(0)+2*sum(g)+f(1))/1440
g<-NULL
h<-NULL
for(i in 1:1440)
  {
  if(i>1 ){
    g[i]=f((i-1)/1440)
  }
  else{
    g[i]=0
  }
}
T4 <- (f(0)+2*sum(g)+f(1))/2880
r=(T1-T2)/(T2-T4)
p=log(r)/log(2)
R<-(2^p*T2-T1)/(2^p-1)

I<-integrate(f,lower = 0,upper = 1,subdivisions = 360)$value

Val<-as.numeric(c(T,S,M,S_2,B,R,I))
Names<-c("Trapezoidal","Simpson's 1/3rd","Mid-point","Simpson's 3/8th","Boole's","Extrapolating","Integrate function")

out<-cbind(Names,Val)

kbl(out,caption = 'Estimated Value Arc Length') %>%
kable_styling(bootstrap_options = c("striped", "hover","condensed", "responsive"))
Estimated Value Arc Length
Names Val
Trapezoidal 1.11008696334574
Simpson's 1/3rd 1.1100860724475
Mid-point 1.11008562698914
Simpson's 3/8th 1.10890067599021
Boole's 1.10737819107872
Extrapolating 1.11008607244083
Integrate function 1.11008607244093

Surface Area of Rotation

If \(f'(x)\) is continuous on [a,b], then the suface area of a solid of revolution obtained by rotating the curve y = f(x) :

  1. Around the y-axis on the interval [a, b] is given by (provided that \(x\ge0\)) \[\int_a^b2{\pi}x\sqrt{1+(f'(x))^2}dx\]
  2. Around the x-axis on the interval [a, b] is given by (provided that \(y\ge0\)) \[\int_a^b2{\pi}f(x)\sqrt{1+(f'(x))^2}dx\]

\[\dfrac{sin(x)}{(1+x)}\]

Code
f<-function(x){
 2*pi*(sin(x)/(1+x))*sqrt(1+(((1+x)*cos(x)-sin(x))/(1+x)^2)^2)
}
g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 ){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
}
T <- (f(0)+2*sum(g)+f(1))/720

g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 && i%%2 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%2 == 1){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
}
S <- (f(0)+4*sum(g)+2*sum(h)+f(1))/1080

g<-NULL
for (i in 1:360){
  g[i]<-f((2*i-1)/720)
}
M<-(1/360)*sum(g)

g<-NULL
h<-NULL
for (i in 1:360)
  {
  if(i>1 && i%%3 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%3 != 0){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
}
S_2<- (3/8)*(1/360)*(f(0)+3*sum(h)+2*sum(g)+f(1))

g<-NULL
h<-NULL
k<-NULL
for (i in 1:360)
  {
  if(i>1 && i%%4 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%2==0 && i%%4 != 0){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
  if(i>1 && i%%2!=0){
    k[i]=f((i-1)/360)
  }
  else{
    k[i]=0
  }
}
B<-(2/45)*(1/360)*(7*(f(0)+f(1))+32*sum(k)+12*sum(h)+14*sum(g))

g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 ){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
}
T1 <- (f(0)+2*sum(g)+f(1))/720
g<-NULL
h<-NULL
for(i in 1:720)
  {
  if(i>1 ){
    g[i]=f((i-1)/720)
  }
  else{
    g[i]=0
  }
}
T2 <- (f(0)+2*sum(g)+f(1))/1440
g<-NULL
h<-NULL
for(i in 1:1440)
  {
  if(i>1 ){
    g[i]=f((i-1)/1440)
  }
  else{
    g[i]=0
  }
}
T4 <- (f(0)+2*sum(g)+f(1))/2880
r=(T1-T2)/(T2-T4)
p=log(r)/log(2)
R<-(2^p*T2-T1)/(2^p-1)

I<-integrate(f,lower = 0,upper = 1,subdivisions = 360)$value

Val<-as.numeric(c(T,S,M,S_2,B,R,I))
Names<-c("Trapezoidal","Simpson's 1/3rd","Mid-point","Simpson's 3/8th","Boole's","Extrapolating","Integrate function")

out<-cbind(Names,Val)

kbl(out,caption = 'Estimated Surface Area of Rotation') %>%
kable_styling(bootstrap_options = c("striped", "hover","condensed", "responsive"))
Estimated Surface Area of Rotation
Names Val
Trapezoidal 1.89914753720949
Simpson's 1/3rd 1.89915305749597
Mid-point 1.89915581772731
Simpson's 3/8th 1.897305709304
Boole's 1.89636147877455
Extrapolating 1.89915305755959
Integrate function 1.89915305755862

Volume of Rotation

If y is given as a function of x, the volume of a solid obtained by rotating the portion of the curve between x = a and x = b about the x-axis is given by :

\[\int_a^b{\pi}\;(f(x))^2dx\]

\[\dfrac{sin(x)}{(1+x)}\]

Code
f<-function(x){
 pi*(sin(x)/(1+x))^2
}
g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 ){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
}
T <- (f(0)+2*sum(g)+f(1))/720

g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 && i%%2 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%2 == 1){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
}
S <- (f(0)+4*sum(g)+2*sum(h)+f(1))/1080

g<-NULL
for (i in 1:360){
  g[i]<-f((2*i-1)/720)
}
M<-(1/360)*sum(g)

g<-NULL
h<-NULL
for (i in 1:360)
  {
  if(i>1 && i%%3 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%3 != 0){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
}
S_2<- (3/8)*(1/360)*(f(0)+3*sum(h)+2*sum(g)+f(1))

g<-NULL
h<-NULL
k<-NULL
for (i in 1:360)
  {
  if(i>1 && i%%4 == 0){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
if(i>1 && i%%2==0 && i%%4 != 0){
    h[i]=f((i-1)/360)
  }
  else{
    h[i]=0
  }
  if(i>1 && i%%2!=0){
    k[i]=f((i-1)/360)
  }
  else{
    k[i]=0
  }
}
B<-(2/45)*(1/360)*(7*(f(0)+f(1))+32*sum(k)+12*sum(h)+14*sum(g))

g<-NULL
h<-NULL
for(i in 1:360)
  {
  if(i>1 ){
    g[i]=f((i-1)/360)
  }
  else{
    g[i]=0
  }
}
T1 <- (f(0)+2*sum(g)+f(1))/720
g<-NULL
h<-NULL
for(i in 1:720)
  {
  if(i>1 ){
    g[i]=f((i-1)/720)
  }
  else{
    g[i]=0
  }
}
T2 <- (f(0)+2*sum(g)+f(1))/1440
g<-NULL
h<-NULL
for(i in 1:1440)
  {
  if(i>1 ){
    g[i]=f((i-1)/1440)
  }
  else{
    g[i]=0
  }
}
T4 <- (f(0)+2*sum(g)+f(1))/2880
r=(T1-T2)/(T2-T4)
p=log(r)/log(2)
R<-(2^p*T2-T1)/(2^p-1)

I<-integrate(f,lower = 0,upper = 1,subdivisions = 360)$value

Val<-as.numeric(c(T,S,M,S_2,B,R,I))
Names<-c("Trapezoidal","Simpson's 1/3rd","Mid-point","Simpson's 3/8th","Boole's","Extrapolating","Integrate function")

out<-cbind(Names,Val)

kbl(out,caption = 'Estimated Volume of Rotation') %>%
kable_styling(bootstrap_options = c("striped", "hover","condensed", "responsive"))
Estimated Volume of Rotation
Names Val
Trapezoidal 0.299047748837929
Simpson's 1/3rd 0.299047647232792
Mid-point 0.299047596412387
Simpson's 3/8th 0.298661602129454
Boole's 0.29846429707443
Extrapolating 0.29904764721991
Integrate function 0.299047647220108

Some Further topics in Numerical Integration

Some Topics which we shall not discuss due to the limitations of time but shall only state are
- Romberg Integration
- Quadrature with Non-smooth Integrands
- Adaptive Integration
- Peano Estimates for Trapezoid rule

References

  • “An Introduction to Numerical Methods and Analysis” by James F. Epperson
  • “Methods of Numerical Integration” by Davis,P., and Rabinowitz,P.
  • Google
  • Wikiepedia

Thank You