How to calculate special functions with Calcpad

There are many special mathematical functions that are not defined by arithmetic expressions, but as a solution of some definite integral. Such are Gamma and related functions, Error function, Fresnel integrals, elliptic integrals, etc. Unlike some advanced mathematical packages as Octave, Maxima or Scilab, they are not implemented internally in Calcpad. However, for real numbers, you can easily define them in any worksheet by using the numerical integration procedures. Read this post further to learn how.

Gamma function

Gamma function is an extension of the factorial to the real and complex domains. For integer arguments it is defined by:

Γ(n) = (n – 1)!

For real and complex numbers it is usually defined by the Euler’s integral form:

However, this form is not convenient for calculations because of the infinite upper limit. Fortunately, the Gamma function can be also expressed as a definite integral with finite limits as follows:

It seems that this is what we need. For real z > 0, we can apply the embedded Tanh-Sinh adaptive integration to solve this by using the $Integral command. But if we try to do that, we will stumble upon another problem – the formula gets unstable for z < 1. We can solve this easily by using the identity for real z > 0:

Finally, we arrive at the following expression, that we can use for real positive values x:

It can be defined in Calcpad by the code bellow:

Γ(x) = $Integral{ln(1/t)^x @ t = 0 : 1}/x

If we plot it for x = 0.05 to 5, we will get the following graph:

To verify how it works, we tested the function for the calculation of several known values. The results are presented in the table below:

nxΓ(x) Expected Error
10.51.7724538509055162.50550506363359×10-16
21110
31.50.8862269254527581.25275253181679×10-16
42112.22044604925031×10-16
52.51.3293403881791388.3516835454453×10-16
632.00000000000000221.11022302462516×10-15
73.53.3233509704478461.069015493817×10-15
846.00000000000000661.03620815631681×10-15

The results are obtained for a target precision of 10-14, which is the default precision in Calcpad for numerical methods. The resulting relative error is equal to or less than 10-15.

Error function

Error function is defined by a definite integral of the Gaussian function as follows:

For real, positive values of z, we can use directly the above formula. It will not work for negative values in Calcpad because Calcpad cannot integrate backwards. In other terms, the upper bound of the integral should be greater than the lower bound.

However, we can use the identity: erf(-z) = –erf(z) to fix this, and it eventually leads to the following equation:

It can be defined in Calcpad by the code bellow:

erf(x) = 2*sign(x)/sqrt(π)*$Integral{e^-t^2 @ t = 0 : abs(x)}

If we plot it for x = -5 to 5, we will get the following graph:

Again, we will check how it works by calculating the function for several points and estimating the relative error.

nxerf(x) Expected Error
10000
20.50.5204998778130470.5204998778130462.1329938237256×10-16
310.8427007929497150.8427007929497152.6349162927426×10-16
41.50.9661051464753110.9661051464753110
520.9953222650189540.9953222650189537.80808532624166×10-16
62.50.9995930479825560.9995930479825557.77472511244569×10-16
730.9999779095030020.9999779095030017.77173285381738×10-16

For the target precision of 10-14 the obtained relative error is close to the machine precision. Other functions like Fresnel integrals, Si, Ci, Shi, Chi, etc. can be defined in a similar way.

Published by Calcpad

Hi, my name is Ned. I am a structural engineer with over 20 years of experience in the design of nuclear and industrial facilities, factories, residential and public buildings. I am a fan of engineering, mathematics and computer programming. I spend a lot of time for developing of useful tools that help structural design.

Leave a comment