Advertisement
luizaspan

MC Estimativa Pi (com erros e desvio padrão)

Nov 18th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. #define FRANDOM ((double) random()/RAND_MAX)
  7.  
  8. #define N 1e4 // não poderia inicializar vetor
  9. #define M 10000
  10.  
  11. int main(void)
  12. {
  13.     double x, y, nacertos, pi_soma = 0.0, pi2_soma = 0.0;
  14.  
  15.     for (double n = 0; n<M; n++) {
  16.  
  17.         nacertos = 0.0;
  18.  
  19.         for (int i = 0; i < N; i++)
  20.         {
  21.             x = FRANDOM;
  22.             y = FRANDOM;
  23.  
  24.             double f = x*x + y*y;
  25.  
  26.             if (f<1)
  27.                 nacertos++;
  28.         }
  29.  
  30.         // printf("%lf\n", nacertos/N*4);
  31.  
  32.         pi_soma += nacertos/N*4;
  33.         pi2_soma += (nacertos/N*4)*(nacertos/N*4);
  34.     }
  35.     double pi_medio = pi_soma/M, pi2_medio = pi2_soma/M;
  36.  
  37.     printf("Pi Médio: %lf\nErro calculado: %e\nErro real: %e\n", pi_medio, sqrt(pi2_medio-pi_medio*pi_medio)/sqrt(M), fabs((pi_medio-M_PI)));
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement