home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Applications / Calculators / NumberCrunch 1.41 / Number Crunch II Demo / Library Routines / Text Examples / Special Functions < prev   
Encoding:
Text File  |  1992-09-03  |  6.6 KB  |  211 lines  |  [TEXT/NCII]

  1. ################
  2. # Special Functions
  3. #   (Also see the file Fitting Functions to Arrays, which
  4. #     has definitions for Legendre Polynomials.)
  5. # function Gamma(x) # Returns  integral 0->∞ { exp(-t) t^(x-1) dt .
  6. # function IncompleteGamma(a,x)  # Returns P(a,x)= 1/gamma(x)  integral 0->x { exp(-t) t^(a-1) dt }
  7. # function ErrorFunction(x)  # Return erf(x).
  8. # function ChiSqProbability(ChiSq,Nu)  # Returns prob. of ChiSqaured fit.
  9. # function Bessel(L,x) # Returns the spherical Bessel functions j_l(x), l=1,2,3,…
  10. #  This text file explains and gives examples of
  11. #  some of the routines in the file All Library Routines, which
  12. #  should be Opened before trying any of these examples.
  13. #################
  14.  
  15. # Here are the xCOD's that are used.
  16.  
  17. xGAMMA
  18.   # xCOD xGAMMA(N,x,Answer:num), finds Answer[1…N]=Gamma(x[1…N])=integral 0->∞ of t^(x-1) e^(-t) dt, with Gamma(J)=(J-1)! for integer J ; an external program.
  19.  
  20. xINCOMPLETE_GAMMA
  21.   # xCOD xINCOMPLETE_GAMMA(N,a,X,Answer,Err:num), calculates Answer[1…N]=1/gamma(a) integral 0 to x[1…N] {exp(-t) t^(a-1)}. Err: 0=> success, -1=> hit iteration limit, -2=> x≤0.; an external program.
  22.  
  23. xBESSEL
  24.   # xCOD xBESSEL(L,N,X,Answer,err:num); computes a spherical Bessel function Answer[1…N]=j_L(X[1…N]). Err: 0=> success, -1=> L < 0.; an external program.
  25.  
  26.  
  27. ####
  28. # Interface routines:
  29. #
  30.  
  31. # The gamma function is Gamma(x) = integral 0->∞ { exp(-t) t^(x-1) dt},
  32. # which is Gamma(n) = (n-1)! for integer n.
  33. #
  34. function Gamma(x) # Returns  integral 0->∞ { exp(-t) t^(x-1) dt .
  35. .   var n,y
  36. . #   Input:     x = number or array
  37. . #   Output:   Gamma(x) = number or array = G(x)
  38. . #                     = integral 0->∞ { exp(-t) t^(x-1) dt
  39. .   begin
  40. .     n = size(x)
  41. .     y = x # save space for answer
  42. .     xGAMMA(n,x,y)
  43. .     Gamma = y
  44. .   end
  45. .   
  46.  
  47. # This incomplete gamma function (sometimes called P(a,x)) is
  48. #    1/gamma(x)  integral 0->x { exp(-t) t^(a-1) dt }
  49. # and has IncompleteGamma(a,0) = 0 and 
  50. # IncompleteGamma(1,∞)=1. 
  51. #   The error function and the goodness of a Chi Squared 
  52. # fit may be calculated in terms of this.
  53. #
  54. function IncompleteGamma(a,x)  # Returns P(a,x)= 1/gamma(x)  integral 0->x { exp(-t) t^(a-1) dt }
  55. .   var n,y, err
  56. . #     Input:     a = number > 0
  57. . #                   x = number or array
  58. . #     Output:
  59. . #                   IncompleteGamma = P(a,x) = number or array
  60. . #                     = 1/gamma(x)  integral 0->x { exp(-t) t^(a-1) dt }
  61. .   begin
  62. .     n=size(x)
  63. .     y=x  # save space for answer
  64. .     xINCOMPLETE_GAMMA(n,a,x,y,err)
  65. .     if err=0 then
  66. .       IncompleteGamma = y
  67. .     else if -2 then
  68. .       IncompleteGamma = " ERROR in xINCOMPLETE_GAMMA, x<0 "
  69. .     else
  70. .       IncompleteGamma = " ERROR in xINCOMPLETE_GAMMA, did not converge"
  71. .   end
  72. .  
  73.  
  74. # the Error Function is erf(x) = 2/sqrt(π) integral 0->x { exp(-t^2) dt }
  75. # which is the integrated probality of a Gaussian normal distribution 
  76. # with sigma=1/sqrt(2) from -x to x.
  77. #
  78. #
  79. function ErrorFunction(x)  # Return erf(x).
  80. .   #    Input:  x = number or array
  81. .   #    Output  ErrorFunction = erf(x) = number or array
  82. .   #                   = 2/sqrt(π) integral 0->x { exp(-t^2) dt }
  83. .   begin
  84. .     if x<0 then
  85. .       ErrorFunction = -IncompleteGamma(0.5, x^2)
  86. .     else
  87. .       ErrorFunction = IncompleteGamma(0.5, x^2)
  88. .     end
  89. .   end
  90. .   
  91.  
  92. #
  93. # This returns the probability that a certain model with
  94. # Nu degress of freedom and a given ChiSq = c2 could happen
  95. # by chance. 
  96. #
  97. function ChiSqProbability(ChiSq,Nu)  # Returns prob. of ChiSqaured fit.
  98. .  # Input: ChiSq = sum [(yData[1…N]-yModel(xData))/sigma]^2
  99. .  #          Nu = N data points - J model params = degrees of freedom
  100. .  # Output: ChiSqProbability = prob of a fit this bad being due
  101. .  #              to Gaussian normal errors with stnd. dev. Sigma[1…N].
  102. .  #     (ChiSqProbabilitity << 1 means the fit is bad.)
  103. .  begin
  104. .      ChiSqProbability = 1 - IncompleteGamma(Nu/2, ChiSq/2);
  105. .   end
  106. .   
  107.  
  108.  
  109.  
  110. function Bessel(L,x) # Returns the spherical Bessel functions j_l(x), l=1,2,3,…
  111. .   var n,y, err
  112. . #   Input:    
  113. . #                     L = positive integer
  114. . #                     x = number or array
  115. . #   Output:
  116. . #                     Bessel = j_l(x) = number or array
  117. .   begin
  118. .     n = size(x)
  119. .     y = x # save space for answer
  120. .     xBESSEL(L,n,x,y,err)
  121. .     if err<>0 then
  122. .       Bessel = "ERROR: L must be >0 in Bessel."
  123. .     else
  124. .       Bessel = y
  125. .   end
  126. .   
  127.  
  128.  
  129. #######
  130. # Examples
  131. ########
  132.  
  133. # A Bessel function:
  134. # For a single value,
  135.   Bessel(2, 3.2)
  136.   0.30536678    #  this is j_2(3.2)
  137. #
  138. #     The easiest way to see the whole function is to just
  139. #     plot   [x, Bessel(L,x)]   for L=1,2,3, etc.
  140. # Or, for a short little table,
  141. x = [0, 0.5, 1, 1.5, 2];   j_1 = Bessel(1,x);
  142. #  table(x,j_1)       # evaluate the "table" command to get:
  143. # x                  j_1
  144. # - - - - - - - - - - - - - - - 
  145.    0                  0
  146.    0.5               0.16254
  147.    1                  0.30117
  148.    1.5               0.39617
  149.    2                  0.4354
  150.  
  151.  
  152. # The Gamma function:
  153. Gamma(5) 
  154.   24 
  155. # This is the same as 4!=4*3*2*1
  156. 4!
  157.   24 
  158. #  whilel
  159. Gamma(0.5)
  160.   1.77245385 
  161. # is the same as sqrt(π)
  162. sqrt(π)
  163.   1.77245385 
  164. # Again, you can just plot
  165. #      [x, Gamma(x)] 
  166. #  to see it.  Note that it blows up at x=0, -1, -2, ….
  167.  
  168.  
  169. # The error function is usually used to describe the probability that
  170. # a normal variable is within some number of standard deviations
  171. # from the mean.  This is often used in scientific data to describe
  172. # how accurate a result is : "one sigma" means a result which, 
  173. # on the average, woule happen as often as a normal variable
  174. # would fall within 1 standard deviation of the mean.
  175. #
  176. # The sigma for the error function is 1/sqrt(2):
  177. sigma = 1/sqrt(2)
  178.   sigma = 0.70710678 
  179. #
  180. #  Then
  181. ErrorFunction(1 sigma)
  182.   0.68268948                   # 68% (~2/3) chance of being ± 1 sigma from mean.
  183. ErrorFunction(2 sigma)
  184.   0.95449972                   # 95% chance of being with ± 2 sigma
  185. ErrorFunction(3 sigma)
  186.   0.9973002                     # 99.7% chance of being with ± 3 sigma
  187.  
  188.  
  189.  
  190. # Usually the agreement between a model G(x) with Nu degrees of freedom 
  191. # and a data set xData, yData[1…n] with standard deviations is given by 
  192. # ChiSquared = sum over n { [yData - G(xData)]/sigma }^2.
  193. # Then the probability that that the ChiSquared errors could be as large 
  194. # as they are by pure chance, assuming that they are distributed 
  195. # normally with standard deviations sigma[1…n], is given by
  196. #  ChiSqProbability(ChiSq, Nu), given above.
  197. # If , for instance, a certain data model with 3 free parameters is 
  198. # supposed to model a certain data set which has a ChiSq errors of
  199. # 8.3, then the probability that this could have happened by chance is
  200. ChiSqProbability(8.3, 3)
  201.   0.0402019 
  202. # or 4%.  Thus this model is not a good fit to the data.
  203.  
  204.