Newton


SUBMITTED BY: bitcoinsachen

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

FORMAT: C

SIZE: 3.5 kB

HITS: 620

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. typedef double (*func_t) (double);
  5. typedef struct values_s{
  6. double *arg;
  7. double *funcval;
  8. long size;
  9. } values_t;
  10. typedef enum boolean_e{
  11. FALSE = 0,
  12. TRUE = 1
  13. } boolean_t;
  14. double func1 (double x)
  15. {
  16. return 2*sin(x)-0.5;
  17. }
  18. double func2 (double x)
  19. {
  20. return 2*cos(x);
  21. }
  22. values_t *values_allocate(long size)
  23. {
  24. values_t *val=calloc(1,sizeof(values_t));
  25. if(!val)
  26. {
  27. fprintf(stderr,"Allocate Error!");
  28. return 0;
  29. }
  30. val->arg=calloc(size,sizeof(double));
  31. val->funcval=calloc(size,sizeof(double));
  32. if(!val->arg ||!val->funcval)
  33. {
  34. fprintf(stderr,"Allocate Error!");
  35. free(val);
  36. return 0;
  37. }
  38. val->size = size;
  39. return val;
  40. }
  41. boolean_t values_insert(values_t *val,double arg,double value,long index)
  42. {
  43. boolean_t status=FALSE;
  44. if(!val)
  45. {
  46. fprintf(stderr,"Nullpointer!");
  47. return status;
  48. }
  49. if(index<0 || index >= val->size)
  50. {
  51. fprintf(stderr,"Index out of range!");
  52. return status;
  53. }
  54. val->arg[index] = arg;
  55. val->funcval[index] = value;
  56. return status=TRUE;
  57. }
  58. boolean_t values_free(values_t *val)
  59. {
  60. boolean_t status=FALSE;
  61. if(!val)
  62. {
  63. fprintf(stderr,"Nullpointer!");
  64. return status;
  65. }
  66. free(val->arg);
  67. free(val->funcval);
  68. free(val);
  69. return status=TRUE;
  70. }
  71. boolean_t values_print(values_t *val)
  72. {
  73. boolean_t status=FALSE;
  74. long i;
  75. if(!val)
  76. {
  77. fprintf(stderr, "Nullpointer.\n");
  78. return status;
  79. }
  80. for(i=0;i<val->size;i++)
  81. printf("%ld. %5g, %5g\n", i, val->arg[i], val->funcval[i]);
  82. return status=TRUE;
  83. }
  84. double newton_step(double x0, func_t func, func_t derivate)
  85. {
  86. if(derivate(x0)==0)
  87. {
  88. fprintf(stderr, "Error: Division by Zero.\n");
  89. return 0;
  90. }
  91. return x0-func(x0)/derivate(x0);
  92. }
  93. void values_store(values_t *val)
  94. {
  95. FILE *fp=fopen("C:\\users\\Patrick\\Dropbox\\3tes_Semester\\prog2\\Übung4\\newton.txt","w");
  96. long i;
  97. if(fp)
  98. {
  99. for(i=0;i<val->size;i++)
  100. {
  101. fprintf(fp,"%lf\t%lf\n",val->arg[i],val->funcval[i]);
  102. }
  103. }
  104. else fprintf(stderr,"Fehler!");
  105. fclose(fp);
  106. }
  107. void values_plot()
  108. {
  109. FILE *gnuplot = popen("C:\\Users\\Patrick\\Dropbox\\3tes_Semester\\prog2\\Übung4\\newton.txt -persist","w");
  110. if (gnuplot)
  111. {
  112. fprintf(gnuplot, "set title 'P2-UE4: Newton'\n");
  113. fprintf(gnuplot, "set style data lines\n");
  114. fprintf(gnuplot, "cd 'C:\\Programme\\gnuplot\\bin'\n");
  115. fprintf(gnuplot, "f(x)=2*sin(x)-0.5\n");
  116. fprintf(gnuplot, "plot [2.8:3.1] \"p2ue4_newton.txt\" using 1:2 with steps, f(x)\n");
  117. }
  118. pclose(gnuplot);
  119. }
  120. void newton_iter(double x0, long steps, double precision, func_t func, func_t derivate)
  121. {
  122. long i=0;
  123. double xn=0;
  124. values_t *val=values_allocate(steps);
  125. values_insert(val,x0,func(x0),0);
  126. for(i=0;i<steps && (fabs(val->funcval[i])>precision);i++)
  127. {
  128. xn=newton_step(val->arg[i],func,derivate);
  129. values_insert(val,xn,func(xn),i+1);
  130. }
  131. values_print(val);
  132. values_store(val);
  133. values_plot();
  134. values_free(val);
  135. }
  136. int main()
  137. {
  138. newton_iter(3,10,0.0001,func1,func2);
  139. return 0;
  140. }

comments powered by Disqus