Advertisement
orneto

simpson

Jun 24th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. double funcao(double x){
  6.     double e = 2.71;
  7.     //entre com a funcao
  8.  
  9.     return (pow(x,2) * exp(x));
  10. }
  11.  
  12. double derivadaQuarta(double x){
  13.     double e = 2.71;
  14.     //entre com a derivada quarta da funcao
  15.  
  16.     return (exp(x) * (pow(x,2) + 8*x + 12));
  17. }
  18.  
  19. double maior(double x[], int m){
  20.     int i;
  21.     double aux, maior = 0;
  22.  
  23.     for(i=0; i<=m; i++){
  24.         aux = fabs(derivadaQuarta(x[i]));
  25.  
  26.         if(aux > maior){
  27.             maior = aux;
  28.         }
  29.     }
  30.  
  31.     return maior;
  32. }
  33.  
  34. int main()
  35. {
  36.     double a, b, c, d , h, x[100], y[100], it=0, max, error;
  37.     int subintervalos, i=0, m;
  38.  
  39.     printf("Digite o valor de a e b, respectivamente: ");
  40.     scanf("%lf %lf",&a,&b);
  41.     printf("Digite a quantidade de subintervalos: ");
  42.     scanf(" %d",&subintervalos);
  43.  
  44.     //alterar essa parte caso ele passe o valor de 2n direto (2 * subintervalos)
  45.     //a fórmula ficará => h = (b - a)/subintervalos
  46.     //e trocar o 'm' por 'subintervalos' na função maior e nos dois for abaixo
  47.  
  48.     h = (b - a)/(2 * subintervalos);
  49.  
  50.     m = 2 * subintervalos;
  51.  
  52.     printf("\nIntervalos que serao usados: \n");
  53.  
  54.     d = a;
  55.     while(d < b){
  56.         c = d + h;
  57.         printf("%lf ",d);
  58.         printf("%lf ",c);
  59.         printf("\n");
  60.  
  61.         x[i] = d;
  62.         y[i] = funcao(d);
  63.  
  64.         d += h;
  65.         i++;
  66.     }
  67.  
  68.     x[i] = c;
  69.     y[i] = funcao(c);
  70.  
  71.     //imprime os valores de x e y
  72.     printf("\nTabela de valores X e Y: \n");
  73.     printf(" ------------------------------");
  74.     printf("\n|   x          |           y   |\n");
  75.     printf("-------------------------------\n");
  76.     for(i=0; i<=m; i++){
  77.         printf("|%.3lf         |       %.6lf|\n",x[i],y[i]);
  78.     }
  79.     printf(" ------------------------------\n");
  80.     printf("\n");
  81.  
  82.     //calcula o valor da área
  83.     for(i=0; i<=m; i++){
  84.         if(i == 0){
  85.             it += y[i];
  86.         }
  87.  
  88.         if(i == m){
  89.             it += y[i];
  90.             it = it * h/3;
  91.         }
  92.  
  93.         if(i > 0 && i < m){
  94.             if(i % 2 == 0){
  95.                 it += 2 * y[i];
  96.             }else{
  97.                 it += 4 * y[i];
  98.             }
  99.         }
  100.  
  101.     }
  102.  
  103.     printf("It = %lf\n\n",it);
  104.  
  105.     //recebe o maior valor do intervalo na derivada a quarta da função
  106.     max = maior(x,m);
  107.  
  108.  
  109.     printf("f(iv) (x) em %lf eh: %.5lf\n",a,derivadaQuarta(a));
  110.     printf("f(iv) (x) em %lf eh: %.5lf\n",b,derivadaQuarta(b));
  111.     printf("\n");
  112.  
  113.     error = (pow(h,4)/180) * (b - a) * max;
  114.     printf("Erro = %.8lf \n",error);
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement