Newton Verfahren


SUBMITTED BY: Guest

DATE: Jan. 11, 2017, 10:20 p.m.

FORMAT: C

SIZE: 3.1 kB

HITS: 778

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. typedef double (*func_t)(double);
  5. double function1(double x)
  6. {
  7. return 2*sin(x)-0.5;
  8. }
  9. double function2(double x)
  10. {
  11. return 2*cos(x);
  12. }
  13. typedef struct values_s
  14. {
  15. double *arg;
  16. double * funcval ;
  17. long size ;
  18. } values_t ;
  19. typedef enum boolean_e
  20. {
  21. FALSE = 0,
  22. TRUE = 1
  23. } boolean_t ;
  24. values_t * values_allocate (long size )
  25. {
  26. values_t *elem;
  27. if(!(elem=calloc(1,sizeof(values_t))))
  28. {
  29. fprintf(stderr,"Fehler bei Speicherallozierung!\n");
  30. return 0;
  31. }
  32. if(!(elem->arg=calloc(size,sizeof(double))))
  33. {
  34. fprintf(stderr,"Fehler bei Speicherallozierung!\n");
  35. free(elem);
  36. return 0;
  37. }
  38. if(!(elem->funcval=calloc(size,sizeof(double))))
  39. {
  40. fprintf(stderr,"Fehler bei Speicherallozierung!\n");
  41. free(elem->arg);
  42. free(elem);
  43. return 0;
  44. }
  45. elem->size=size;
  46. return elem;
  47. }
  48. boolean_t values_insert ( values_t *val , double arg , double value , long index )
  49. {
  50. if(!val||index<0||index>=val->size)
  51. return FALSE;
  52. val->arg[index]=arg;
  53. val->funcval[index]=value;
  54. return TRUE;
  55. }
  56. boolean_t values_free ( values_t *val)
  57. {
  58. if(!val)
  59. return FALSE;
  60. free(val->arg);
  61. val->arg=0;
  62. free(val->funcval);
  63. val->funcval=0;
  64. free(val);
  65. //val=0;
  66. return TRUE;
  67. }
  68. boolean_t values_print ( values_t *val)
  69. {
  70. if(!val)
  71. return FALSE;
  72. long i;
  73. for(i=0;i<val->size;i++)
  74. printf("%ld: %10g %10g\n",i,val->arg[i],val->funcval[i]);
  75. return TRUE;
  76. }
  77. double newton_step (double x0 , func_t func , func_t derivate )
  78. {
  79. if(!derivate(x0))
  80. {
  81. fprintf(stderr,"Division durch 0!\n");
  82. return x0;
  83. }
  84. return x0-func(x0)/derivate(x0);
  85. }
  86. boolean_t newton_iter(values_t *val, double x0, double genauigkeit, double schritte, func_t func, func_t derivate)
  87. {
  88. if(!val)
  89. return FALSE;
  90. long i;
  91. for(i=0;i<val->size&&i<schritte;i++)
  92. {
  93. values_insert(val,x0,func(x0),i);
  94. if(fabs(func(x0))<genauigkeit)//absoluter Wert von func(x0)
  95. break;
  96. x0=newton_step(x0,func,derivate);
  97. }
  98. return TRUE;
  99. }
  100. boolean_t values_store(values_t *val,char *file)
  101. {
  102. if(!val)
  103. return FALSE;
  104. FILE *f;
  105. if(!(f=fopen(file,"w")))
  106. return FALSE;
  107. long i;
  108. for(i=0;i<val->size;i++)
  109. fprintf(f,"%ld: %10g %10g\n",i,val->arg[i],val->funcval[i]);
  110. fclose(f);
  111. return TRUE;
  112. }
  113. int main()
  114. {
  115. printf("Newton-Verfahren!\n");
  116. values_t *elem;
  117. double x0=3;
  118. double genauigkeit = 0.0001;
  119. elem=values_allocate(5);
  120. newton_iter(elem,x0,genauigkeit,elem->size,function1,function2);
  121. values_print(elem);
  122. if(values_store(elem,"test.txt")!=TRUE)
  123. fprintf(stderr,"Fehler beim Speichern!\n");
  124. if(values_free(elem)!=TRUE)
  125. {
  126. fprintf(stderr,"Fehler bei Speicherfreigabe!\n");
  127. exit(-1);
  128. }
  129. elem=0;
  130. return 0;
  131. }

comments powered by Disqus