This is a notebook of integration, alias, note for Numerical Recipe.
In [33]:
import numpy as np
from scipy import integrate
Example: f(x)=(x-1)^2 from 0 to 2.
In [34]:
def ftest(x):
return (x-1)**2
In [35]:
ftest(0)
Out[35]:
In [36]:
ftest(2)
Out[36]:
In [37]:
integrate.romberg(ftest,0,2)
Out[37]:
Trapezoidal Rule
In [38]:
2*(ftest(0)+ftest(2))/2
Out[38]:
In [39]:
np.trapz([ftest(0),ftest(2)],x=[0,2])
Out[39]:
Simpson's Rule
In [40]:
1*(ftest(0)/3+4*ftest(1)/3+ftest(2)/3)
Out[40]:
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]:
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)
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)
In [ ]: