Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <math.h>
  5.  
  6.  
  7. double* diff( double (*fct)(double), double x, double h0, double tau, int * n) {
  8.  
  9. assert (tau > 0);
  10. double phi = 0;
  11. double phip= 0;
  12. double hp= 0;
  13. double help = 1;
  14. double h = 0;
  15.  
  16. double * z= malloc (1*sizeof(double));
  17.  
  18. while(1) {
  19. phip = phi;
  20.  
  21. hp = h;
  22.  
  23. if (*n == 1) {
  24. h = h0;
  25. }
  26.  
  27. else {
  28. help = help *2;
  29. h = 1/(2*help*h0);
  30. }
  31.  
  32. phi = (fct(x+h)-fct(x))/h;
  33.  
  34. z = realloc(z,*n *sizeof(double));
  35.  
  36. z[*n-1]= phi;
  37.  
  38. if (fabs(phi) <= tau && fabs(phip-phi)<= tau ) {
  39.  
  40. return z;
  41. }
  42. if ( fabs(phi) > tau && fabs (phip - phi) <= tau * fabs(phip)) {
  43.  
  44. return z;
  45. }
  46.  
  47. *n=*n+1;
  48. }
  49. }
  50.  
  51.  
  52.  
  53. double fct (double x) {
  54. return x*x;
  55. }
  56.  
  57.  
  58. void printVector(double * vector, int dim) {
  59. int j = 0;
  60. for (j= 0; j < dim; j++) {
  61. printf("%f\n",vector[j]);
  62. }
  63. }
  64.  
  65. int main () {
  66.  
  67. double x = 0;
  68. double tau = 0;
  69. int n = 1;
  70. double *folge = NULL;
  71. double h0 = 0;
  72.  
  73. printf("Geben Sie einen Startwert x ein:" );
  74. scanf("%lf",&x);
  75.  
  76.  
  77. printf("Geben Sie ein h0 ein: ");
  78. scanf("%lf",&h0);
  79.  
  80. printf("Geben Sie ein tau ein:");
  81. scanf("%lf",&tau);
  82.  
  83. folge= diff(fct,x,h0,tau,&n);
  84. printVector(folge,n);
  85. free (folge);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement