upload exercises
This commit is contained in:
parent
32ad3f2662
commit
d17339a560
75 changed files with 773 additions and 0 deletions
38
exercises/exerciseB/numerical_integration.py
Executable file
38
exercises/exerciseB/numerical_integration.py
Executable file
|
@ -0,0 +1,38 @@
|
|||
"""Exercise 2b: multiprocessing
|
||||
"""
|
||||
|
||||
def integrate(f, a, b, n):
|
||||
"Perform numerical integration of f in range [a, b], with n steps"
|
||||
s = []
|
||||
for i in range(n):
|
||||
dx = (b - a) / n
|
||||
x = a + (i + 0.5) * dx
|
||||
y = f(x)
|
||||
s = s + [y * dx]
|
||||
return sum(s)
|
||||
|
||||
def f(x):
|
||||
"A polynomial that we'll integrate"
|
||||
return x ** 4 - 3 * x
|
||||
|
||||
def F(x):
|
||||
"The analatic integral of f. (F' = f)"
|
||||
return 1 / 5 * x ** 5 - 3 / 2 * x ** 2
|
||||
|
||||
def compute_error(n):
|
||||
"Calculate the difference between the numerical and analytical integration results"
|
||||
a = -1.0
|
||||
b = +2.0
|
||||
F_analytical = F(b) - F(a)
|
||||
F_numerical = integrate(f, a, b, n)
|
||||
return abs((F_numerical - F_analytical) / F_analytical)
|
||||
|
||||
def main():
|
||||
ns = [10_000, 25_000, 50_000, 75_000]
|
||||
errors = ... # TODO: write a for loop, serial map, and parallel map here
|
||||
|
||||
for n, e in zip(ns, errors):
|
||||
print(f'{n} {e:.8%}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue