Généraliser un programme (méthode de Monte-Carlo)

valentin12 - 14 janv. 2013 à 17:12
cptpingu Messages postés 3837 Date d'inscription dimanche 12 décembre 2004 Statut Modérateur Dernière intervention 28 mars 2023 - 14 janv. 2013 à 18:18
bonjour à tous voici un programme permettant de vérifier des points choisis au hasard
dans le volume d'une intégration V.

ma question est de savoir si c'est possible de généraliser ce programme à D dimension?
comment le faire merci
#define M1 259200
#define IA1 7141
#define IC1 54773
#define RM1 (1.0/M1)
#define M2 134456
#define IA2 8121
#define IC2 28411
#define RM2 (1.0/M2)
#define M3 243000
#define IA3 4561
#define IC3 51349

double ran1(idum)
int *idum;
{
static long ix1,ix2,ix3;
static double r[98];
double temp;
static int iff=0;
int j;

if (*idum < 0 || iff == 0) {
iff=1;
ix1=(IC1-(*idum)) % M1;
ix1=(IA1*ix1+IC1) % M1;
ix2=ix1 % M2;
ix1=(IA1*ix1+IC1) % M1;
ix3=ix1 % M3;
for (j=1;j<=97;j++) {
ix1=(IA1*ix1+IC1) % M1;
ix2=(IA2*ix2+IC2) % M2;
r[j]=(ix1+ix2*RM2)*RM1;
}
*idum=1;
}
ix1=(IA1*ix1+IC1) % M1;
ix2=(IA2*ix2+IC2) % M2;
ix3=(IA3*ix3+IC3) % M3;
j=1 + ((97*ix3)/M3);
if (j > 97 || j < 1) printf("RAN1: This cannot happen.");
temp=r[j];
r[j]=(ix1+ix2*RM2)*RM1;
return temp;
}

#undef M1
#undef IA1
#undef IC1
#undef RM1
#undef M2
#undef IA2
#undef IC2
#undef RM2
#undef M3
#undef IA3
#undef IC3

float f (float x)

{
return(exp(-x)) ;
}
main ()
{ int i ; float I ; int idum = 1; int n=1000;
I=0.0;
for (i = 0; i<n ; i++) I+= f(ran1(&idum));
printf("afficher le resultat\n");
printf("%f", I/n);
for(;;);
}

1 réponse

cptpingu Messages postés 3837 Date d'inscription dimanche 12 décembre 2004 Statut Modérateur Dernière intervention 28 mars 2023 123
14 janv. 2013 à 18:18
Bonjour.

Merci d'utiliser des balises de code quand tu en postes (3ème icône en partant de la droite).

________________________________________________________________________
Historique de mes créations, et quelques articles:
[ http://0217021.free.fr/portfolio http://0217021.free.fr/portfolio]
Merci d'utiliser Réponse acceptée si un post répond à votre question
0
Rejoignez-nous