Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #define H 0.000002
  5. #define INICIO -1
  6. #define FIM 1
  7.  
  8. double fx(double x)  {
  9.     return (1+ tanh(4*x))/2;
  10. }
  11.  
  12. double calculaH (int k) {
  13.     double h0=H;
  14.     return h0/pow(2,k);
  15. }
  16.  
  17. double calculaX0(double x0, int k) {
  18.  
  19.     double h= calculaH(k);
  20.  
  21.  
  22.     return ((-3.0/2.0)*fx(x0)+2.0*fx(x0+h)-(1.0/2.0)*fx(x0+2*h))*(1/h);
  23. }
  24.  
  25. double calculaX1(double x1, int k) {
  26.      double h= calculaH(k);
  27.     return ( (-1.0/2.0) * fx(x1-h) + (1.0/2.0) * fx(x1+h)) * (1.0 / h);
  28. }
  29.  
  30. double calculaX2(double x2, int k) {
  31.      double h= calculaH(k);
  32.     return ( (1.0/2.0) * fx(x2-2*h) - 2.0 * fx(x2-h) + (3.0/2.0) * fx(x2) ) * (1.0/h);
  33. }
  34.  
  35. int main()
  36. {
  37.     double x0=0, x1=0.1, x2=0.2,d;
  38.     int k =4,i;
  39.     double vetX0[k], vetX1[k],vetX2[k];
  40.  
  41.     for (i=0;i<=k;i++){
  42.  
  43.         vetX0[i]=calculaX0(x0,i);
  44.         vetX1[i]=calculaX1(x1,i);
  45.         vetX2[i]=calculaX2(x2,i);
  46.  
  47.         printf("\n\nk=%d : h=%.6lf\n",i,calculaH(i));
  48.         printf("f'(x0)=%.20lf", vetX0[i]);
  49.         printf("\nf'(x1)=%.20lf", vetX1[i]);
  50.        printf("\nf'(x2)=%.20lf", vetX2[i]);
  51.     }
  52.  
  53.     //calculando o erro relativo
  54.  
  55.     printf("\n\nErro relativo\n");
  56.    /* for(i=1;i<k;i++){
  57.         printf("\n\nK= %d\n",i);
  58.        printf("dx0= %.20lf",(fabs(vetX0[i]-vetX0[i-1]))/fabs(vetX0[i]));
  59.  
  60.  
  61.        // printf("\ndx1= %.6lf",(fabs(vetX1[i+1]-vetX1[i]))/fabs(vetX1[i]));
  62.       //  printf("\ndx2= %.6lf",(fabs(vetX2[i+1]-vetX2[i]))/fabs(vetX2[i]));
  63.     }*/
  64.     for(i=1;i<k;i++){
  65.         printf("\n\nPara k = %d\n",i);
  66.         printf("Er f'(x0)=%.6e",( fabs(vetX0[i]-vetX0[i-1]) ) / fabs(vetX0[i]) );
  67.        printf("\nEr f'(x1)=%.6e",(fabs(vetX1[i]-vetX1[i-1]))/fabs(vetX1[i]));
  68.        printf("\nEr f'(x2)=%.6e",(fabs(vetX2[i]-vetX2[i-1]))/fabs(vetX2[i]));
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement