Hey. I need to make a program for calculating a definite integral sin (x) / x with integration limits from 1 to 10. It is necessary to solve the method of rectangles (left) with an accuracy of 0.001.
The partitioning step is calculated as follows: h = (b - a) / n where a and b = 1 and 10, n = the number of partitions (first 2, then 4, 8, 16 ...). The code will need to be explained to Sishnik, who has never seen a python in his eyes, so it is advisable to do as much as possible in a "sish" one. Here I wrote an approximate algorithm on Python 3, but it gives an unpredictable result. When the online calculator displays 0.71. thank
from math import sin import numpy as np def func(x): return sin(x) / x n = 2 # Текущая точность a = 1 b = 10 Si = [] print("Интегрируемая функция: f(x) = sin(x) / x") print("Точность: 0.001") def work(n): xi = [] # массив с точками разбиений print("Текущее число разбиений", n) h = (b - a)/n # Шаг print("Текущий шаг: ", h) for x in np.arange(a, b, h): # заносим в массив xi текущие точки для разбиения xi.append(func(x)) print("Значения выбранных точек: ", xi) sum = 0 for i in xi: sum += h * func(i) tmp_otvet = h * sum # вычисление по формуле левых прямоугольниках print("Текущий результат: ", tmp_otvet) if n == 2: # Если запустили в первый раз, то точность не высчитываем Si.append(tmp_otvet) # В список Si скапливаем результаты вычислений work(4) # запускаем рекурсию else: if abs(Si[-1] - tmp_otvet) < 0.001: # если необходимая точность достигнута, то выводим ответ otvet(tmp_otvet, n) else: Si.append(tmp_otvet) # Иначе запускаем рекурсию с увеличенным вдвое числом разбиений work(n * 2) def otvet(S, n): print("___________") print("Результат: ", S) print("Число разбиений: ", n) exit(0) work(2) 
