Untitled
unknown
python
a year ago
1.1 kB
5
Indexable
Never
import math def f(x): return math.exp(x) * math.cos(x) def simpson_integration(f, a, b, n): h = (b - a) / n integral = 0 for i in range(n): x0 = a + i * h x1 = a + (i + 0.5) * h x2 = a + (i + 1) * h integral += h / 6 * (f(x0) + 4 * f(x1) + f(x2)) return integral def runge_estimation(f, a, b, n, old_integral, epsilon): new_integral = simpson_integration(f, a, b, n) error = abs(new_integral - old_integral) / 15 if error < epsilon: return new_integral return runge_estimation(f, a, b, 2 * n, new_integral, epsilon) a = 0 b = 1 epsilon = 0.5 * (10**(-6)) n = 1000 # количество разбиений exact_integral = math.exp(b) * (math.sin(b) + math.cos(b)) - math.exp(a) * (math.sin(a) + math.cos(a)) integral = simpson_integration(f, a, b, n) integral = runge_estimation(f, a, b, n, integral, epsilon) print("Численное значение интеграла:", integral) print("Точное значение интеграла:", exact_integral)