function int = composite_trap(f, a, b, n)
    % Input:
    % f - function handle or string for the integrand
    % a - lower limit of integration
    % b - upper limit of integration
    % n - number of subintervals
    % Output:
    % int - approximate value of the integral

    % Calculate step size
    dx = (b - a) / n;

    % Initialize the sum with the first and last terms
    sum_f = feval(f, a) + feval(f, b);

    % Sum the interior points
    for i = 1:n-1
        x_i = a + i * dx; % Calculate the current x value
        sum_f = sum_f + 2 * feval(f, x_i); % Add twice the interior function values
    end

    % Apply the trapezoidal rule formula
    int = (dx / 2) * sum_f;
end