←to practical programming

Exercise "plots"

Tasks

  1. Plot the error-function together with several of its tabulated values (as a test: the curve should pass through the tabulated points).

    You can use the following approximation,

    static double erf(double x){
    /// single precision error function (Abramowitz and Stegun, from Wikipedia)
    if(x<0) return -erf(-x);
    double[] a={0.254829592,-0.284496736,1.421413741,-1.453152027,1.061405429};
    double t=1/(1+0.3275911*x);
    double sum=t*(a[0]+t*(a[1]+t*(a[2]+t*(a[3]+t*a[4]))));/* the right thing */
    return 1-sum*Exp(-x*x);
    } 
  2. Plot the gamma-function together with several of its tabulated values (factorials) as a test. You can use the following [Stirling approximation],

    static double gamma(double x){
    ///single precision gamma function (Gergo Nemes, from Wikipedia)
    if(x<0)return PI/sin(PI*x)/gamma(1-x);
    if(x<9)return gamma(x+1)/x;
    double lngamma=x*Log(x+1/(12*x-1/x/10))-x+Log(2*PI/x)/2;
    return Exp(lngamma);
    }
    	
    Try to reproduce the plot from the Wikipedia article.

  3. The gamma-function overflows easily, so the logarithm of the gamma function is often a more useful function,
    static double lngamma(double x){
    if(x<=0) throw new ArgumentException("lngamma: x<=0");
    if(x<9) return lngamma(x+1)-Log(x);
    return x*Log(x+1/(12*x-1/x/10))-x+Log(2*PI/x)/2;
    }
    	
    Using lngamma make a similar plot as above for the logarithm of the gamma-function.
  4. (Extra) Implement the complex function complex G(complex z) that calculates Γ(z) for complex arguments: use the (suitable modified) formula from wikipedia above, it works for complex arguments as well, I believe. Try to reproduce the 3d plot of the absolute value of the Γ-function in the complex plane from Wikipedia article.