% Q1-1

close all
clear
%% Analytical solutions a=1, b=2
format compact
format long
Io = [  2-sqrt(2);
        2^(3/2)-2;
        (2^(5/2))/3 - 2/3;
        (2^(7/2)-2)/5
    ]

ksiVec = [-3/2 -1/2 1/2 3/2];
j=1;
for ksi = ksiVec
    f=@(x) x.^ksi;
    a=1; b=2; % Bounds

    n = [ 10 20 40 80];  % Number of intervals
    h = (b-a)./n;       % Interval length.

    i = 1;
    for n = n
        x = linspace(a,b,n+1);
        Imidp(i) = sum(f((x(1:n)+x(2:n+1))./2).*h(i));
        err(i) = abs(Imidp(i) - Io(j));
        i = i+1;
    end

    loglog(h, err, '*-')
    hold on
    title('I_{midp} vs h')
    grid on

    legend('$\xi=-3/2$','$\xi=-1/2$','$\xi=1/2$','$\xi=3/2$','interpreter','latex')

    ylabel('err(h)')
    xlabel('h')
    
    j=j+1;
end

