-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
56 lines (42 loc) · 1.13 KB
/
algorithms.py
File metadata and controls
56 lines (42 loc) · 1.13 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
from multiprocessing.connection import wait
from tracemalloc import stop
import numpy as np
import sys
import time
def trapezoid_rule():
a = 0
b = np.pi
n = 2
N = 11
while(n <= N):
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
f = np.sin(x)
I_trap = (h/2)*(f[0] + 2 * sum(f[1:n-1])
+ f[n-1])
err_trap = 2 - I_trap
print(chr(27) + "[2J")
print("Trapezoid rule estimation with " +
str(n) + " points: " + str(I_trap))
print("Error: " + str(err_trap))
n += 1
time.sleep(0.25)
def simpsons_rule():
a = 0
b = np.pi
n = 2
N = 11
while(n <= N):
h = (b - a) / (n - 1)
x = np.linspace(a, b, n)
f = np.sin(x)
I_simp = (h/3) * (f[0] + 2*sum(f[:n-2:2])
+ 4*sum(f[1:n-1:2]) + f[n-1])
err_simp = 2 - I_simp
print(chr(27) + "[2J")
print("Simpson's rule estimation with " +
str(n) + " points: " + str(I_simp))
print("Error: " + str(err_simp))
n += 1
time.sleep(0.25)
simpsons_rule()