-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5.py
More file actions
91 lines (69 loc) · 2.68 KB
/
Q5.py
File metadata and controls
91 lines (69 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
111901030
Mayank Singla
Coding Assignment 4 - Q5
"""
# %%
from math import exp
from scipy import integrate
from numpy import linspace
import matplotlib.pyplot as plt
def inp_func(x):
"""
Returns the value of the function 2 * x * e^(x²) at x
"""
return 2 * x * exp(x * x)
def inp_func_integral(x):
"""
Returns the value of the integral of the function 2 * x * e^(x²) at x
Integral is: e^(x²)
"""
return exp(x * x)
def visualize(func, func_integral, func_str):
"""
Visualize as a function of u, area under the curve of input function in the interval [0, u] computed using various integration functions available in Python’s scipy.integrate module
Also compute the exact area
"""
# Minimum and maximum value of u taken
u_min = 0
u_max = 0.2
# Number of points generated
numPoints = 1000
upts = linspace(u_min, u_max, numPoints) # u-points
upts = list(filter(lambda x: x != 0, upts)) # Removing 0 from the list
actual_area = [] # Actual area in each interval
trapezoidal = [] # Use trapezoidal rule to compute area
quadrature = [] # Use general purpose integration rule to compute area
simpson = [] # Use Simpson's rule to compute area from samples.
romberg = [] # Use Romberg Integration to compute area
for u in upts:
xpts = linspace(u_min, u, numPoints) # x-points
ypts = [func(x) for x in xpts] # y-points
actual_area.append(func_integral(u) - func_integral(u_min)) # Exact area
trapezoidal.append(integrate.trapezoid(ypts, xpts)) # Trapezoidal rule
quadrature.append(integrate.quad(func, u_min, u)[0]) # General purpose rule
simpson.append(integrate.simpson(ypts, xpts)) # Simpson's rule
romberg.append(integrate.romberg(func, u_min, u)) # Romberg Integration
# Plotting the curve
# Giving labels and title to the curve
plt.title(
f"Visualizing various integration functions in scipy.integrate module\nfor the function {func_str}"
)
plt.xlabel("u")
plt.ylabel("area")
# Plotting the actual area
plt.plot(upts, actual_area, c="r", label="Actaul Area")
# Plotting the trapezoidal area
plt.plot(upts, trapezoidal, c="b", label="Trapezoidal")
# Plotting the General purpose integration
plt.plot(upts, quadrature, c="g", label="General purpose")
# Plotting the simpson area
plt.plot(upts, simpson, c="y", label="Simpson")
# Plotting the Romberg area
plt.plot(upts, romberg, c="brown", label="Romberg")
# Displaying the curve
plt.grid()
plt.legend(loc="upper left")
plt.show()
if __name__ == "__main__":
visualize(inp_func, inp_func_integral, "2xe^(x²)")