←to practical programming

Exercise "gnuplot"

Scientific plots with gnuplot.
    1. Install gnuplot in your box.
      • Ubuntu:
        sudo apt --yes install gnuplot 
      • MacOS with homebrew:
        brew install gnuplot [--with-qt|--with-x11] [--with-cairo] 
    2. Plot the error-function together with several of its tabulated values (as a test). Try to reproduce the plot from the Wikipedia article. Use svg terminal. The error-function can be approximated like this,
      public 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);
      } 
    3. Plot the gamma-function together with several of its tabulated values (factorials) (as a test). Try to reproduce the plot from the Wikipedia article. Use svg terminal. The gamma-function can be approximated like this,
      public 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);
      }
  1. Implement and plot the function double lngamma(double x) that calculates ln(Γ(x)) for large x without overflows.
  2. Implement the complex function complex gamma(complex z) that calculates Γ(z) for complex arguments. Plot 3d plot of the absolute value of the Γ-function in the complex plane as in the Wikipedia article.