-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumerical_integration.py
45 lines (39 loc) · 1.12 KB
/
numerical_integration.py
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
'''
Numerical Integration using Trapezoidal Approximation
'''
import numpy as np
def fn(x):
return x
# Basic implementation, No optimizations
def integrate1(fn, xmin, xmax, N):
dx = (xmax - xmin) / N
eps = 0.01 * dx
area = 0
# area_trapezoid = base * (h1 + h2) / 2
for x in np.arange(xmin, xmax - eps, dx):
area += dx * (fn(x) + fn(x + dx)) / 2
return area
# Optimization #1: Factor out constants multiplication
def integrate2(fn, xmin, xmax, N):
dx = (xmax - xmin) / N
eps = 0.01 * dx
area = 0
# area_trapezoid = base * (h1 + h2) / 2
for x in np.arange(xmin, xmax - eps, dx):
area += (fn(x) + fn(x + dx))
area = area * dx / 2
return area
# Optimization #2: Remove double calls to Fn(x)
def integrate3(fn, xmin, xmax, N):
dx = (xmax - xmin) / N
eps = 0.01 * dx
area = 0
# area_trapezoid = base * (h1 + h2) / 2
for x in np.arange(xmin, xmax + eps, dx):
area += fn(x)
area = area * 2 - fn(xmin) - fn(xmax)
area = area * dx / 2
return area
print(integrate1(fn, 1, 4, 3))
print(integrate2(fn, 1, 4, 3))
print(integrate3(fn, 1, 4, 3))