-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegrate.py
More file actions
45 lines (33 loc) · 1.05 KB
/
integrate.py
File metadata and controls
45 lines (33 loc) · 1.05 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
import numpy as np
import matplotlib.pyplot as plt
class Integrate:
""" Base class for different numerical integration methods
Parameters
----------
f : function
A single variable function f(x)
a , b : numbers
Interval of integration [a,b]
defult to [-1,1]
N : integer
Number of subintervals of [a,b]
"""
def __init__(self, f):
self.a = -1
self.b = 1
self.f = f
self.N = 100
def plot_function(self):
# x and y values for the trapezoid rule
x = np.linspace(self.a, self.b, self.N+1)
y = self.f(x)
# X and Y values for plotting y=f(x)
X = np.linspace(self.a, self.b, 100)
Y = self.f(X)
plt.plot(X,Y, c='b')
for i in range(self.N):
xs = [x[i],x[i],x[i+1],x[i+1]]
ys = [0,y[i], y[i+1],0]
plt.fill(xs,ys,'b',edgecolor='b',alpha=0.1)
plt.title('Trapezoid Rule, N = {}'.format(self.N))
plt.show()