// derivative.C

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

// Numerische Näherung der Ableitung von sin(x) bei x=1.

int main(int argc, char **argv)
{
  int j;

  // *****

  printf("h            rel_err_asym   rel_err_sym\n");

  for(j = 1; j <= 15; j++)
    {
      double h = pow(10.0, -(double)j);

      double df_exact = cos(1.0);

      double df_asym = (sin(1.0+h) - sin(1.0)) / h;
      double df_sym = (sin(1.0+h) - sin(1.0-h)) / (2.0 * h);

      double rel_err_asym = fabs((df_exact - df_asym) / df_exact);
      double rel_err_sym = fabs((df_exact - df_sym) / df_exact);

      printf("%.1e      %.3e      %.3e\n", h, rel_err_asym, rel_err_sym);
    }

  // *****

  return EXIT_SUCCESS;
}



/*

h            rel_err_asym   rel_err_sym
1.0e-01      7.947e-02      1.666e-03
1.0e-02      7.804e-03      1.667e-05
1.0e-03      7.789e-04      1.667e-07
1.0e-04      7.787e-05      1.667e-09
1.0e-05      7.787e-06      1.757e-11
1.0e-06      7.788e-07      2.186e-12
1.0e-07      7.725e-08      5.214e-11
1.0e-08      1.353e-08      3.493e-09
1.0e-09      8.530e-08      3.323e-08
1.0e-10      1.155e-07      2.897e-08
1.0e-11      4.155e-07      1.205e-06
1.0e-12      9.217e-05      2.730e-05
1.0e-13      7.664e-04      5.401e-04
1.0e-14      4.734e-04      2.868e-03
1.0e-15      1.135e-01      1.166e-02

*/
