Consider a function lets say $f(x) = xe^{-x^2} $. How can we determine the area under the curve from say 1 to 10
In mathematical terms this is just $ \int_1^{100} x e^{-x^2} \,dx $. We simply can do this by U-substitution and solve.
For us that would be $ \frac{-e^{-x^2}}{2} |_1^{10} $ which is approx 0.18393972
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format='retina'
X = np.arange(0,10,0.1)
plt.plot(X,X)
plt.show()
def func(x):
return np.exp(-x**2)*x
X = np.arange(-2,10,0.1)
plt.plot(X,func(X))
plt.show()
X = np.arange(-2,10,0.1)
Y = np.arange(1,10,0.1)
plt.plot(X,func(X))
plt.fill_between(Y,func(Y),color='r')
plt.xlim(-2,4)
plt.show()
X = np.arange(-2,10,0.1)
a=2
b=3
Y=np.arange(a,b,0.1)
plt.plot(X,X)
plt.fill_between(Y,Y,color='r')
plt.plot([a,b],[a,0],c='black')
plt.show()
$$ \text{Area} = \frac{a}{2}\Delta x+\frac{b}{2}\Delta x $$
Or another way to say this with functions
$$ \text{Area} = \frac{f(x_n)+f(x_{n+1})}{2}\Delta x $$
def Trapezoid_int(a,b,f,n):
Deltax = (b-a)/n
Area = 0
for c in range(0,n):
Area += (f(a+c*Deltax)+f(a+(c+1)*Deltax))/2*Deltax
return Area
Trapezoid_int(1,10,func,3)
X = np.arange(-2,10,0.1)
Y = np.arange(1,10,0.1)
plt.plot(X,func(X))
plt.fill_between(Y,func(Y),color='r')
plt.xlim(-2,4)
plt.show()
def Trap_top_line(x1,x2,f,x):
y1 = f(x1)
y2 = f(x2)
return (y2-y1)/(x2-x1)*(x-x1)+y1
a=1
b=10
n=10
Deltax = (b-a)/n
X = np.arange(a-3,b+1,0.1)
plt.plot(X,func(X))
for c in range(0,n):
Y = np.arange(a+c*Deltax,a+(c+1)*Deltax,0.01)
plt.fill_between(Y,Trap_top_line(a+c*Deltax,a+(c+1)*Deltax,func,Y),color='r')
plt.vlines(a+c*Deltax,0,func(a+c*Deltax),alpha=0.3)
plt.show()
Trapezoid_int(1,10,func,10)
Lets look at $ \sin (x) $
def func2(x):
return np.sin(x)**2
Trapezoid_int(-np.pi,np.pi,func2,20)
a=-np.pi
b=np.pi
n=100
Deltax = (b-a)/n
X = np.arange(a-1,b+1,0.1)
plt.plot(X,func2(X))
for c in range(0,n):
Y = np.arange(a+c*Deltax,a+(c+1)*Deltax,0.01)
plt.fill_between(Y,Trap_top_line(a+c*Deltax,a+(c+1)*Deltax,func2,Y),color='r')
plt.vlines(a+c*Deltax,0,func2(a+c*Deltax),alpha=0.3)
plt.show()
Trapezoid_int(-np.pi,np.pi,func2,3)
for c in range(2,50):
plt.plot(c,Trapezoid_int(1,10,func,c),'.',c='b')
plt.ylim(0.15,0.3)
Trapezoid_int(1,10,func,7)
def Midpoint_int(a,b,f,n):
Deltax = (b-a)/n
Area = 0
for c in range(0,n):
Area += (f(a+(c+0.5)*Deltax)*Deltax)
return Area
Midpoint_int(1,10,func,7)
def Box_line(x1,x2,f,x):
y1 = f((x1+x2)/2)
return y1
a=1
b=10
n=100
Deltax = (b-a)/n
X = np.arange(a-3,b+1,0.1)
plt.plot(X,func(X))
for c in range(0,n):
Y = np.arange(a+c*Deltax,a+(c+1)*Deltax,0.01)
plt.fill_between(Y,Box_line(a+c*Deltax,a+(c+1)*Deltax,func,Y),color='r')
plt.vlines(a+c*Deltax,0,func(a+c*Deltax),alpha=0.3)
plt.show()
Midpoint_int(1,10,func,1000)
Trapezoid_int(1,10,func,1000)
for c in range(2,50):
plt.plot(c,Trapezoid_int(1,10,func,c),'.',c='b')
plt.plot(c,Midpoint_int(1,10,func,c),'.',c='r')
plt.ylim(0.15,0.3)
for c in range(2,50):
plt.plot(c,Trapezoid_int(1,10,func2,c),'.',c='b')
plt.plot(c,Midpoint_int(1,10,func2,c),'.',c='r')