/*************************************************** * Program to demonstrate the Chi-Square() Function * * ------------------------------------------------ * * Reference: BASIC Scientific Subroutines, Vol. II * * By F.R. Ruckdeschel, BYTE/McGRAWW-HILL, 1981 [1].* * * * C++ Version by J.-P. Moreau, Paris. * * (www.jpmoreau.fr) * * ------------------------------------------------ * * SAMPLE RUN: * * How many degrees of freedom: 100 * * What is the range (X1,X2): * * X1: 50 * * X2: 150 * * What is the table step size: 5 * * * * X Chi-Square PDF * * ------------------------ * * 50 0.00000 * * 55 0.00003 * * 60 0.00018 * * 65 0.00076 * * 70 0.00237 * * 75 0.00571 * * 80 0.01107 * * 85 0.01772 * * 90 0.02393 * * 95 0.02779 * * 100 0.02816 * * 105 0.02525 * * 110 0.02025 * * 115 0.01468 * * 120 0.00970 * * 125 0.00588 * * 130 0.00330 * * 135 0.00172 * * 140 0.00084 * * 145 0.00038 * * 150 0.00017 * * * ***************************************************/ #include #include double m1,x,x1,x2,x3,y; int m; /************************************************** * Series approximation subroutine LN(X!) * * Accuracy better then 6 places for x>=3 * * Accuracy better than 12 places for x>10 * * Advantage is that very large values of the * * argument can be used without fear of over flow. * * Reference: CRC Math Tables. * * x is the input, y is the output. * * ************************************************/ void LN_FactX() { double x1; x1 = 1.0 / (x * x); y = (x + 0.5) * log(x) - x * (1 - x1 / 12 + x1 * x1 / 360 - x1 * x1 * x1 / 1260 + x1 * x1 * x1 * x1 / 1680); y += 0.918938533205; } /***************************************************** * Chi-square function subroutine. This program takes * * a given degree of freedom, m and value, x, and * * calculates the chi-square density distribution * * function value, y. * * Reference: Texas Instruments SR-51 owners Manual, * * 1974. * * Subroutine used: LN(X!). * *****************************************************/ void Chi_Square() { double c; //Save X m1 = x; //Perform calculation x = (double) (m / 2 - 1); //Call LN(X!) subroutine LN_FactX(); x = m1; c = -x / 2 + (m / 2 - 1) * log(x) - (m / 2) * log(2) - y; y = exp(c); } void main() { printf("\n How many degrees of freedom: "); scanf("%d",&m); printf(" What is the range (X1,X2):\n"); printf(" X1: "); scanf("%lf",&x1); printf(" X2: "); scanf("%lf",&x2); printf(" What is the table step size: "); scanf("%lf",&x3); printf("\n X Chi-Square PDF "); printf("\n -------------------------\n"); x=x1; while (x <= x2) { Chi_Square(); printf(" %3.0f %7.5f\n",x,y); x += x3; } } // End of file chi-sqr.cpp