function y = closed1dK(f,a,b,n)

% f is the function in question
% n is the number of partitions
% m is the degree of precision for the Romberg, it is set in line 20
% a and b are our limits
% Rom1 will return a finite result if the singularity is near a, Rom2 will
% return a result if the singularity is near b.
warning off all;

m = 3;
for k = 1:n
    
    ank(1,k) = (b-a)*2*k/(n^2+n);
    right1(1,k) = a + sum(ank(1,1:k));
    right1(2,2*k) = a + sum(ank(1,1:k));
    right1(3,4*k) = a + sum(ank(1,1:k));
    

end
right1(2,1) = a + ank(1,1)/2;
right1(3,1) = a + ank(1,1)/4;


for k = 1:n-1
    right1(2,2*k+1) = (right1(1,k)+right1(1,k+1))/2;
end

for k = 1:2*n-1
    right1(3,2*k) = right1(2,k);
    right1(3,2*k+1) = 1/2*(right1(2,k)+right1(2,k+1));
end


    

ank(2,1) = ank(1,1)/2;
ank(3,1) = ank(1,1)/4;

for k = 2:2*n
    ank(2,k) = right1(2,k) - right1(2,k-1);
end

for k = 2:4*n
    ank(3,k) = right1(3,k) - right1(3,k-1);
end
for i = 1:3
   
    for k = 2:4*n
    
            left1(i,k) = right1(i,k-1);
    end
    
end

Rom1 = 0;Rom2 = 0; Rom3 = 0;

Rom1 = ank(1,1)*feval(f,right1(1,1))/2;


if m <= 2
        for k = 2:n
        Rom1 = Rom1 + ank(1,k)*(feval(f,right1(1,k)) + feval(f,left1(1,k)))/2;
        end

        Rom1
    return
   
end



S1(1,1) = ank(1,1)*feval(f,right1(1,1))/2; 
S1(1,2) = ank(2,1)*feval(f,right1(2,1))/2; 
S1(1,3) = ank(3,1)*feval(f,right1(3,1))/2; 


if m >= 2
    for i = 1:m
        for k = 2:2^(i-1)*n

            S1(1,i) = S1(1,i) + ank(i,k)*(feval(f,right1(i,k)) + feval(f,left1(i,k)))/2;
        end
    end
        
    
        Rom1 = (4*S1(1,2) - S1(1,1))/3;
        T1 = Rom1;
        T2 = (4*S1(1,3) - S1(1,2))/3;
        Rom2 = (16*T2 - T1)/15;

end
Rom2
