Untitled
unknown
python
2 years ago
876 B
11
Indexable
import math
def f(x):
return math.exp(x) * math.cos(x)
def simpson_integration(a, b, epsilon):
dx = b - a
olds = 0
oldt = 0.5 * (f(a) + f(b)) * dx
i = 1
while True:
t = 0
spacing = dx / i
x = a + 0.5 * spacing
for q in range(i):
t += f(x)
x += spacing
t = 0.5 * (olds + t * spacing)
s = (4 * t - oldt) / 3
if abs(s - olds) <= epsilon:
return s
olds = s
oldt = t
i *= 2
a = 0
b = 1
epsilon = 0.5 * (10**(-6))
exact_integral = (math.cos(1) * math.e + math.sin(1) * math.e - 1) / 2
integral = simpson_integration(a, b, epsilon)
print("Численное значение интеграла:", integral)
print("Точное значение интеграла:", exact_integral)Editor is loading...
Leave a Comment