function chi2dist
a=[.995,.99,.975,.95,.9,.5,.1,.05,.025,.01,.005];
for n=1:10, for m=1:length(a), c(n,m)=x2a(n,a(m)); end, end
disp([' n:a',sprintf(' %7.3f',a)]);
for n=1:10, disp(sprintf('%2d: %s',n,sprintf(' %7.3f',c(n,:)))); end

% x2a(n) : chi-squre distribution critical value -------------------------------
function x=x2a(n,a)

% set range of search
r=[0,10]; while a<x2q(n,r(2)), r(2)=r(2)*2; end

% binary search
while 1
    x=(r(1)+r(2))/2; p=x2q(n,x);
    %disp(sprintf('x=%f,p=%f',x,p))
    if abs(p-a)<a*1E-5, break, elseif p<a, r(2)=x; else r(1)=x; end
end

% chi-square function ----------------------------------------------------------
function p=x2q(n,x), p=1-gammainc(x/2,n/2);
