This is a notebook of integration, alias, note for Numerical Recipe.


In [33]:
import numpy as np
from scipy import integrate

Equally spaced abscissas

Example: f(x)=(x-1)^2 from 0 to 2.

Closed Newton-Cotes Formulas


In [34]:
def ftest(x):
    return (x-1)**2

In [35]:
ftest(0)


Out[35]:
1

In [36]:
ftest(2)


Out[36]:
1

In [37]:
integrate.romberg(ftest,0,2)


Out[37]:
0.66666666666666663

Trapezoidal Rule


In [38]:
2*(ftest(0)+ftest(2))/2


Out[38]:
2

In [39]:
np.trapz([ftest(0),ftest(2)],x=[0,2])


Out[39]:
2.0

Simpson's Rule


In [40]:
1*(ftest(0)/3+4*ftest(1)/3+ftest(2)/3)


Out[40]:
0

Simpson's 3/8 Rule


In [41]:
2/3*(3*ftest(0)/8+9*ftest(2/3)/8+9*ftest(4/3)/8+3*ftest(2)/8)


Out[41]:
0

Extended Trapezoidal Rule


In [42]:
intExtTrap=1/2*(ftest(0)+ftest(2))

In [43]:
for seg in range(1,199):
    intExtTrap = intExtTrap+ftest(0.01*seg)

In [44]:
intExtTrap= intExtTrap*0.01
print(intExtTrap)


0.646899

In [45]:
delExtTrap3=0.01
intExtTrap3=5/12*ftest(0)+13/12*ftest(delExtTrap3)+13/12*ftest(2-delExtTrap3)+5/12*ftest(2)

In [46]:
for seg in range(1,197):
    intExtTrap3 = intExtTrap3 + ftest(delExtTrap3+seg*delExtTrap3)

In [47]:
intExtTrap3=(intExtTrap3)*delExtTrap3
print(intExtTrap3)


0.647096

Gaussian Quadratures

Integration of ODE

  • Runge-Kutta
  • Richardson extrapolation
  • predictor-corrector

Runge-Kutta


In [ ]: