%determines how many nodes are needed to interpolate f(x) on [a,b] w/ guarantee 
%that error < tol, using our error bounds for uniformly spaced/Chebyshev nodes

f = @(x) cos(x);    

a = 0;
b = 1;            %interpolation interval is [a,b]
tol = 10^(-9);    %tol = maximum error tolerance
N = 20;           %maximum number of nodes considered
M = ones(1,N);    %M(n) = upper bound for |f^(n)(x)| on [a,b]

for n=1:N
    error_unif = M(n)*((b-a)/n)^(n+1)/(4*(n+1));
    if error_unif < tol
        disp('number of uniformly spaced nodes needed is...')
        n
        break;
    end
end

for n=1:N
    error_cheb = M(n)*((b-a)/2)^(n+1)/(factorial(n+1)*2^n);
    if error_cheb < tol
        disp('number of Chebyshev nodes needed is...')
        n
        break;
    end
end
