Пример #1
0
def integrate(func: Function, interval: Interval,
              partition_count: int) -> float:
    left, right = interval
    step = (right - left) / partition_count
    x = left + step
    result = 0
    for _ in range(partition_count - 1):
        result += calculate_at_point(func, x)
        x += step
    result = (result * 2 + calculate_at_point(func, left) +
              calculate_at_point(func, right)) * step / 2
    return result
Пример #2
0
def integrate(func: Function, interval: Interval,
              partition_count: int) -> float:
    left, right = interval
    step = (right - left) / partition_count
    result = calculate_at_point(func, left) + calculate_at_point(func, right)
    odd_part = even_part = 0

    x = left + step

    for i in range(1, partition_count):
        if i % 2 == 0:
            even_part += calculate_at_point(func, x)
        else:
            odd_part += calculate_at_point(func, x)
        x += step

    result = (result + 4 * odd_part + 2 * even_part) * step / 3
    return result