#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 10 13:52:35 2024

@author: sam
"""

def f(x):
    return x**3 + 1

def qtrapn(f,a,b,N):
    h = (b-a)/N #defines width of interval
    I = f(a) + f(b) #creates variable I and calculates end points seperately
    for i in range(1,int(N)): #iterates through 1 from the beginning and 1 from the end, to exclude the end points already calculated
        xi = a + i*h #defines the ith point
        I += 2*f(xi) #adds 2 times the value of the function at the ith point, to the variable I
    I *= h/2 #multiplies I by h/2
    return I

result = qtrapn(f,0,2,4) #calls function qtrapn with function f, bounds [0,2], and 4 intervals
print(result)