Differ


SUBMITTED BY: bitcoinsachen

DATE: Jan. 19, 2017, 9:10 p.m.

FORMAT: C

SIZE: 3.6 kB

HITS: 220

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. typedef double(*func_t)(double);
  5. double polynom (double);
  6. double cosine (double);
  7. double exp (double);
  8. double squareroot (double);
  9. typedef struct values_s{
  10. double *x;
  11. double *funcval;
  12. double *derivate;
  13. long size;
  14. } values_t;
  15. typedef enum boolean_e{
  16. FALSE = 0,
  17. TRUE = 1
  18. } boolean_t;
  19. values_t *values_allocate(long size)
  20. {
  21. values_t *val=calloc(1,sizeof(values_t));
  22. if(!val) return 0;
  23. val->x = calloc(size,sizeof(double));
  24. val->funcval = calloc(size,sizeof(double));
  25. val->derivate = calloc(size,sizeof(double));
  26. if(!val->x || !val->funcval || !val->derivate)
  27. {
  28. free(val);
  29. return 0;
  30. }
  31. val->size = size;
  32. return val;
  33. }
  34. boolean_t values_insert(values_t *val,double x1, double x2, double x3,long index)
  35. {
  36. boolean_t status=FALSE;
  37. if(!val) return status;
  38. if(index<0 || index>=val->size) return status;
  39. val->x[index] = x1;
  40. val->funcval[index] = x2;
  41. val->derivate[index] = x3;
  42. return status=TRUE;
  43. }
  44. boolean_t values_free(values_t *val)
  45. {
  46. boolean_t status=FALSE;
  47. if(!val) return status;
  48. free(val->x);
  49. free(val->funcval);
  50. free(val->derivate);
  51. free(val);
  52. return status=TRUE;
  53. }
  54. boolean_t values_print(values_t *val)
  55. {
  56. boolean_t status=FALSE;
  57. long i;
  58. if(!val) return status;
  59. for(i=0;i<val->size;i++)
  60. {
  61. printf("%ld.:\t%lf\t%lf\t%lf\n",i+1,val->x[i],val->funcval[i],val->derivate[i]);
  62. }
  63. return status=TRUE;
  64. }
  65. boolean_t differentiate(values_t *val, func_t func)
  66. {
  67. boolean_t status=FALSE;
  68. long i;
  69. if(!val) return status;
  70. for(i=0;i<val->size;i++)
  71. val->funcval[i]=func(val->x[i]);
  72. for(i=0;i<val->size;i++)
  73. {
  74. if(((val->x[i+1]-val->x[i-1])) == 0) //Nenner = 0 !
  75. {
  76. fprintf(stderr,"Error: Division by Zero!\n");
  77. val->derivate[i]=0;
  78. continue;
  79. }
  80. val->derivate[i]=(val->funcval[i+1]-val->funcval[i-1])/((val->x[i+1]-val->x[i-1]));
  81. }
  82. return status=TRUE;
  83. }
  84. boolean_t values_save(values_t *val)
  85. {
  86. boolean_t status=FALSE;
  87. FILE *fp;
  88. long i;
  89. if((fp = fopen("C:\\Programme\\gnuplot\\bin\\p2ue4_diff.txt","w")))
  90. {
  91. if(val)
  92. {
  93. for(i=0;i<val->size;i++)
  94. fprintf(fp,"%lf\t%lf\t%lf\n",val->x[i],val->funcval[i],val->derivate[i]);
  95. }
  96. else return status;
  97. }
  98. else return status;
  99. fclose(fp);
  100. return status=TRUE;
  101. }
  102. void values_plot()
  103. {
  104. FILE *gnuplot = popen("C:\\Programme\\gnuplot\\bin\\gnuplot -persist","w");
  105. if (gnuplot)
  106. {
  107. fprintf(gnuplot, "set title 'P2-UE4: Diff'\n");
  108. fprintf(gnuplot, "set style data lines\n");
  109. fprintf(gnuplot, "set xlabel 'x'\n");
  110. fprintf(gnuplot, "set ylabel 'f(x)'\n");
  111. fprintf(gnuplot, "set yrange [-1.1:1.1]\n");
  112. fprintf(gnuplot, "cd 'C:\\Programme\\gnuplot\\bin'\n");
  113. fprintf(gnuplot, "plot \"p2ue4_diff.txt\" using 1:2, \"p2ue4_diff.txt\" using 1:3\n");
  114. }
  115. pclose(gnuplot);
  116. }
  117. int main()
  118. {
  119. long i;
  120. values_t *tmp;
  121. tmp=values_allocate(500);
  122. double x=2*M_PI/500;
  123. for(i=0;i<500;i++)
  124. values_insert(tmp,(x*i),0,0,i);
  125. differentiate(tmp,cosine);
  126. values_print(tmp);
  127. values_save(tmp);
  128. values_plot();
  129. values_free(tmp);
  130. return 0;
  131. }

comments powered by Disqus