% Chi-Square Distribution Analysis
% A.A. Trani (March 20, 1999)

% Function calls: 
% chis.m - second moment of the Chi-square dist.
% chif.m - first moment of the Chi-square dist.


global NU

% Define the parameter of the distribution (NU)

NU = 11;

% Define upper and lower bounds to get a nice plot

npoints = 100;
parameter = 4;
means = NU	;						% expected value of the distribution
sigma = sqrt(2*NU);					% standard deviation of distribution
low = 0;
high = means + parameter* sigma;
interval = (high - low) / npoints;

% Define random variable x

x=low:interval:high;


% Define the function of the random variable x (PDF function)

fx = 0.5^(NU/2) * x.^(NU/2-1) .* exp(-0.5 .* x) / gamma(NU/2);

% Plot the random variable x versus the PDF function

plot(x,fx)
xlabel('Random variable - x')
ylabel('f(x)')
grid

% Compute area under the curve f(x) from -infinity to infinity
% Change the values of the last two arguments if you 
% want another range of values

Pn=quad8('chi',0,100);

  disp(' ')
  disp([blanks(5),'Area under the Chi-Square Distribution : '])
  disp(' ')
  disp([blanks(5),'     P(x>0)=',num2str(Pn)])
  disp(' ')
  
% Compute the first and second moments of the distribution
%
% The first moment is the mean of the distribution
% the second moment is the variance

mean_chi = quad8('chif',0,50);
var_chi = quad8('chis',0,50);

  disp(' ')
  disp([blanks(5),'First moment of the Chi-Square PDF : '])
  disp(' ')
  disp([blanks(5), 'P(x>0) = ' ,num2str(mean_chi)])
  disp(' ')
  disp(' ')
  disp([blanks(5), ' Second moment of the Chi-Square PDF : '])
  disp(' ')
  disp([blanks(5), 'P(x>0) = ' , num2str(var_chi)])
  disp(' ')
  



