Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize numerical integration #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions profiling/numerical_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@

def numerical_integration(f, a, b, n):
s = []
dx = (b - a) / n
for i in range(n):
dx = (b - a) / n
x = a + (i + 0.5) * dx
y = f(x)
s = s + [y * dx]
s.append(y * dx)
return s


def measure_integration_errors(f, F, n_values, a, b):
# Calculate all integrals
F_a_list = []
F_n_list = []
F_analytical = F(b) - F(a)
for n in n_values:
F_analytical = F(b) - F(a)
F_numerical_boxes = numerical_integration(f, a, b, n)
F_a_list.append(F_analytical)
F_n_list.append(F_numerical_boxes)

# Calculate and sum error
errors = []
for F_analytical, F_numerical_boxes in zip(F_a_list, F_n_list):
for F_numerical_boxes in F_n_list:
F_numerical = sum(F_numerical_boxes)
error = abs(F_analytical - F_numerical)
errors = errors + [error]
Expand Down