Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <math.h>
  5.  
  6.  
  7. double  diffaitken( double (*fct)(double), double x, double h0, double tau) {
  8.  
  9.   assert (tau > 0);
  10.  
  11.   double phi = 0;
  12.   double phip = 0;
  13.   double hp = 0;
  14.   double help = 1;
  15.   double h = 0;
  16.   int n= 0;
  17.   int j = 0;
  18.   double erg = 0;
  19.  
  20.   double *diff = malloc(1*sizeof(double));
  21.   double *aitk = malloc (1*sizeof(double));
  22.  
  23.   for (n=0;1 ; n++) {              
  24.  
  25.    
  26.     for (j = 1;j < n+4 ;j++) {     // ACHTUNG MIT 1 ANGEFANGEN
  27.    
  28.     phip = phi;
  29.  
  30.     hp = h;
  31.  
  32.     if (j == 1) {
  33.  
  34.       h = h0;
  35.  
  36.     }
  37.  
  38.     else {
  39.  
  40.       help = help *2;
  41.  
  42.       h = 1/(2*help*h0);  
  43.   }
  44.     phi = (fct(x+h)-fct(x))/h;
  45.  
  46.     diff = realloc (diff,j*sizeof(double));
  47.  
  48.     diff[j-1]=phi;                  
  49.  
  50.  
  51.     }
  52.  
  53.  
  54.     aitk = realloc (aitk,(n+1)*sizeof(double));
  55.  
  56.     aitk[n] = diff[n] - ((diff[n+1]-diff[n])*(diff[n+1]-diff[n]))/(diff[n+2]-2*diff[n+1]+diff[n]);
  57.  
  58.    
  59.     if (fabs(aitk[n]) <= tau) {          
  60.  
  61.     if (fabs(aitk[n-1]-aitk[n]) <= tau) {
  62.  
  63.       //printf("%f\n",aitk[n]);
  64.      
  65.       erg= aitk[n];
  66.  
  67.       return erg;
  68.  
  69.     }
  70.  
  71.       }
  72.       else {
  73.  
  74.     if (fabs(aitk[n-1]-aitk[n]) <= tau * aitk[n]) {
  75.  
  76.       // printf("%f\n",aitk[n]);
  77.       erg = aitk[n];
  78.       return erg;
  79.     }
  80.       }
  81.    
  82.  
  83.     printf("h_n= %f\n",h);
  84.     printf("|y_n-y_n-1| = %f\n",fabs(aitk[n+1]-aitk[n]));
  85.     printf("y_n = %f\n",aitk[n+1]);
  86.     printf("\n");
  87.                  
  88. }
  89.  
  90. }
  91.  
  92.  
  93. double fct( double x) {
  94.  
  95.   return x*x*x;
  96. }
  97.  
  98.  
  99. int main () {
  100.  
  101.   double x = 0;
  102.   double h= 0;
  103.   double tau= 0;
  104.   double z = 0;  
  105.  
  106.  
  107.   printf("Geben Sie einen Auswertungspunkt x ein:");
  108.   scanf("%lf",&x);
  109.  
  110.   printf("Geben Sie eine Schrittweite h ein:");
  111.   scanf("%lf",&h);
  112.  
  113.   printf("Geben Sie ein tau ein:");
  114.   scanf("%lf",&tau);
  115.  
  116.   fct(x) ;
  117.  
  118.  z= diffaitken(fct,x,h,tau);
  119.  
  120.  printf("Das Ergebnis ist: %f",z);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement