% Written by Min-Hua Huang
% June 9, 2011
% Testing the cumulative distribution functions of the Truncated Normal distribution with the "h" factorization method.
% Section 4.8 in "Indefinite Integration of the Gamma Integral".
clear; clc; format long; pre=300; digits(700);  n=10^5;
fprintf('Testing the distribution functions of the Truncated Normal distribution:\n\n');
% ************************************************************************
% Truncated Normal distribution
x=1.5; mu=1; sigma=2; a=-2; b=2; 
answer1=(normcdf(x,mu,sigma)-normcdf(a,mu,sigma))/(normcdf(b,mu,sigma)-normcdf(a,mu,sigma));
check1_1=(x-mu)/2*exp(-(x-mu)^2/(2*sigma^2))*h(-1/2,-(x-mu)^2/(2*sigma^2),n);
check1_2=(b-mu)/2*exp(-(b-mu)^2/(2*sigma^2))*h(-1/2,-(b-mu)^2/(2*sigma^2),n);
check1_3=(a-mu)/2*exp(-(a-mu)^2/(2*sigma^2))*h(-1/2,-(a-mu)^2/(2*sigma^2),n);
check1=(check1_1-check1_3)/(check1_2-check1_3);
fprintf('Truncated Normal=%8.5f, huang method=%8.5f.\n\n',answer1,check1);
