forked from bellshade/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpson.py
126 lines (107 loc) · 3.06 KB
/
simpson.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# type: ignore[arg-type]
from typing import Callable
import numpy as np
def simpson_13(
func: Callable[..., float], a: float, b: float, eps: float = 0.0001, *args, **kwargs
) -> float:
"""
Metode simpson 1/3 menggunakan
kurva polinomial ordo 2 untuk melakukan
approximasi terhadap fungsi yang akan
diintegrasi
Parameter:
f = fungsi input
a = batas bawah integrasi
b = batas atas integrasi
eps = error relatif maksimal
>>> simpson_13(lambda x: x**2, 0, 2)
2.6666666666666665
>>> simpson_13(lambda x: x**5, 0, 1)
0.16666679687500002
"""
try:
# Iterasi pertama
n = 10
h = (b - a) / n
fx = [func(a + i * h, *args, **kwargs) for i in range(n + 1)]
L0 = h / 3 * (fx[0] + 4 * sum(fx[1:-1:2]) + 2 * sum(fx[2:-2:2]) + fx[-1])
# Optimasi
err = 1
while err > eps:
n *= 2
h = (b - a) / n
fx = [func(a + i * h, *args, **kwargs) for i in range(n + 1)]
L1 = h / 3 * (fx[0] + 4 * sum(fx[1:-1:2]) + 2 * sum(fx[2:-2:2]) + fx[-1])
err = np.abs(L1 - L0) / np.abs(L1)
L0 = L1
except Exception:
raise RuntimeError("Integrasi gagal, pastikan fungsi anda benar!")
return L1
def simpson_38(
func: Callable[..., float], a: float, b: float, eps: float = 0.0001, *args, **kwargs
) -> float:
"""
Metode simpson 3/8 menggunakan
kurva polinomial ordo 3 untuk melakukan
approximasi terhadap fungsi yang akan
diintegrasi
Parameter:
f = fungsi input
a = batas bawah integrasi
b = batas atas integrasi
eps = error relatif maksimal
>>> simpson_38(lambda x: x**2, 0, 2)
2.666666666666667
>>> simpson_38(lambda x: x**5, 0, 1)
0.16666667809785096
"""
try:
# Iterasi pertama
n = 10
h = (b - a) / n
fx = [func(a + i * h, *args, **kwargs) for i in range(n + 1)]
L0 = (
3
/ 8
* h
* (
fx[0]
+ 3 * sum(fx[1:-2:3])
+ 3 * sum(fx[2:-1:3])
+ 2 * sum(fx[3:-3:3])
+ fx[-1]
)
)
# Optimasi
err = 1
while err > eps:
n *= 3
h = (b - a) / n
fx = [func(a + i * h, *args, **kwargs) for i in range(n + 1)]
L1 = (
3
/ 8
* h
* (
fx[0]
+ 3 * sum(fx[1:-2:3])
+ 3 * sum(fx[2:-1:3])
+ 2 * sum(fx[3:-3:3])
+ fx[-1]
)
)
err = np.abs(L1 - L0) / np.abs(L1)
L0 = L1
except Exception:
raise RuntimeError("Integrasi gagal, pastikan fungsi anda benar!")
return L1
if __name__ == "__main__":
def f(x: float) -> float:
"""
Test Function
"""
return x ** 4
print(simpson_13(f, 0, 1))
print(simpson_38(f, 0, 1))
import doctest
doctest.testmod()