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')
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format='retina'
def func(x):
return x**2 +2*x-1
func(2)
X = np.arange(-2,2,0.01)
plt.plot(X,func(X))
plt.show()
F = lambda c: c**2 +2*c -1
plt.plot(X,F(X))
XX = np.linspace(-2,2,5000)
plt.plot(XX,F(XX))
import Pythonic_files as PF
PF.func(2)
PF.Generate_Trap_int(-2,4,F,50)
PF.Generate_MidP_int(-2,4,F,50)
This is a bit weird,
answer = 1+2
expected = 3
answer == expected
answer = 0.1 + 0.2
expected = 0.3
answer == expected
answer
%%time
PF.Midpoint_int(-2,2,F,500000)
%%time
PF.Trapezoid_int(-2,2,F,500000)
def mid_point_Vectorization(a,b,f,n):
Deltax= (b-a)/n
X = np.arange(a+Deltax/2,b+Deltax/2,Deltax)
return Deltax*sum(f(X))
%%time
mid_point_Vectorization(-2,2,F,500000)
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format='retina'
fig = plt.figure() # this generates something that you will add to, its like plt.show()
ax = plt.axes(projection ="3d") # makes sure that its a 3-d
ax.view_init(50, 35)
fig = plt.figure()
ax = plt.axes(projection ="3d")
X=np.linspace(-15,15,200)
Y=np.linspace(-15,15,200)
X,Y = np.meshgrid(X,Y)
R = np.sqrt(X**2 + Y**2)
Z=np.sin(R)*np.exp(-R/10)
ax.plot_surface(X,Y,Z,linewidth=0, antialiased=False)
ax.view_init(70, 35)
fig = plt.figure()
ax = plt.axes(projection ="3d")
ax.view_init(70, 35)
ax.contour3D(X,Y,Z,50,cmap='binary')
ax.set_xlabel('This is x')
fig = plt.figure()
ax = plt.axes(projection ="3d")
ax.view_init(70, 35)
ax.contour3D(X,Y,Z,50,cmap='inferno')
fig = plt.figure()
ax = plt.axes(projection ="3d")
zline = np.linspace(0,45,1000)
xline = np.sin(zline)*np.exp(zline/20)
yline= np.cos(zline)*np.exp(zline/20)
ax.plot3D(xline,yline,zline)
zpoints = 45*np.random.random(300)
xpoints = np.sin(zpoints)*np.exp(zpoints/20) + 1*np.random.random()
ypoints = np.cos(zpoints)*np.exp(zpoints/20) + 1*np.random.random()
ax.view_init(30, 35)
ax.scatter3D(xpoints,ypoints,zpoints,c=(max(zpoints)-zpoints),cmap='inferno')
np.random.random(5)
def func3D(x,y):
return np.exp(-(x**2+y**2)/100)*(np.cos(x**2/16+y**2/4))
fig = plt.figure()
ax = plt.axes(projection ="3d")
X = np.linspace(-10,10,1000)
Y = np.linspace(-10,10,1000)
X,Y = np.meshgrid(X,Y)
Z = func3D(X,Y)
ax.contour3D(X,Y,Z,500,cmap='inferno')
ax.view_init(60, 55)
fig = plt.figure()
ax = plt.axes(projection ="3d")
X = np.linspace(-10,10,100)
Y = np.linspace(-10,10,100)
X,Y = np.meshgrid(X,Y)
Z = func3D(X,Y)
ax.contour3D(X,Y,Z,500,cmap='plasma')
ax.view_init(60, 55)
fig = plt.figure()
ax = plt.axes(projection ="3d")
ax.view_init(60, 55)
X = np.linspace(-10,10,100)
Y = np.linspace(-10,10,100)
X,Y = np.meshgrid(X,Y)
Z = func3D(X,Y)
ax.plot_surface(X,Y,Z,cmap='viridis',edgecolor='none',rstride=1, cstride=1)
fig = plt.figure()
ax = plt.axes(projection ="3d")
ax.view_init(60, 55)
X = np.linspace(-10,10,100)
Y = np.linspace(-10,10,100)
X,Y = np.meshgrid(X,Y)
Z = func3D(X,Y)
ax.plot_wireframe(X,Y,Z,color='black',alpha=0.1)
ax.set_title('wireframe')
fig = plt.figure()
ax = plt.axes(projection ="3d")
ax.view_init(60, 55)
X = np.linspace(-10,10,100)
Y = np.linspace(-10,10,100)
X,Y = np.meshgrid(X,Y)
Z = func3D(X,Y)
ax.scatter(X,Y,Z,alpha=0.1)
fig = plt.figure()
ax = plt.axes(projection ="3d")
ax.view_init(60, 55)
X = np.linspace(-10,10,100)
Y = np.linspace(-10,10,100)
X,Y = np.meshgrid(X,Y)
X=np.ravel(X)
Y=np.ravel(Y)
Z = func3D(X,Y)
ax.plot_trisurf(X,Y,Z)
X = np.linspace(-2,2,5)
Y = np.linspace(-2,2,5)
Y
X,Y = np.meshgrid(X,Y)
Y
X=np.ravel(X)
X
u = np.linspace(0, 2.0 * np.pi, endpoint=True, num=500)
v = np.linspace(-0.5, 0.5, endpoint=True, num=100)
u, v = np.meshgrid(u, v)
u, v = u.flatten(), v.flatten()
x = (1 + 0.5 * v * np.cos(u / 2.0)) * np.cos(u)
y = (1 + 0.5 * v * np.cos(u / 2.0)) * np.sin(u)
z = 0.5 * v * np.sin(u / 2.0)
ax = plt.axes(projection='3d')
fig = plt.figure()
ax.scatter(x, y, z, c=z, cmap='viridis', linewidth=0.1)
ax.view_init(60, 55)