1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <stdlib.h>
#include <stdio.h>
#define DEUX_PI ( 2.0 * 3.141592653589793238462643383279502884197169399375 ) // PI x 2
#include <math.h>
#include <conio.h>
//function on uniform random variable
double uniform_random_variable (double a, double b)
{
return ( rand()/(double)RAND_MAX ) * (b-a) + a;
}
//function on normal random variable
/**
* Retourne un nombre pseudo-aléatoire selon une loi normale(Distribution gaussienne) de paramètres mu et sigma
* @param mu moyenne (espérance mathématique) de la distribution
* @param sigma écart-type de la distribution
*/
double normal_random_variable(double mu, double sigma)
{
// On récupère deux nombres pseudo-aléatoires indépendants selon une loi uniforme sur l'intervalle [0;1]
double rand_a =uniform_random_variable (0,1);
double rand_b =uniform_random_variable (0,1);
double randNumNorm = sqrt(-2.0*log(rand_a))*cos(DEUX_PI*rand_b);
return (mu + sigma * randNumNorm);
}
main ()
{
int bound_x=1000;
int bound_y=1000;
int tm_sim=200;//s
int pas_tm=1;//s
double alpha=0.75;//[0..1]
double mean_velocity=10;//m/s
double mean_direction=DEUX_PI/4;//radian
double normal_velocity;
double normal_direction;
int nbr_stp=(int)ceil(tm_sim/pas_tm);//printf("%lf ",ceil(tm_sim/pas_tm));
int t=1;
double vct_velocity[tm_sim+1];
double vct_direction[tm_sim+1];
/*int*/ double vct_x[tm_sim+1];
/*int*/double vct_y[tm_sim+1];
//mean_velocity=uniform_random_variable (0,10);printf("%lf ",mean_velocity);//average speed[0..1]
//mean_direction=uniform_random_variable (0,DEUX_PI);//average direction[0..2pi]
vct_velocity[0]=mean_velocity;
vct_direction[0]=mean_direction;printf("%lf ",mean_direction);
vct_x[0]=uniform_random_variable(0,bound_x); printf("%lf ",vct_x[0]);
vct_y[0]=uniform_random_variable(0,bound_y); printf("%lf ",vct_y[0]);
while(t<=nbr_stp)
{ normal_velocity=normal_random_variable(0,1);printf("%lf ",normal_velocity);//mean=0,std dev=1
normal_direction=normal_random_variable(0,1);printf("%lf ",normal_direction);//mean=0,std dev=1
// Calculate the NEW velocity, direction using the Gauss-Markov formula
vct_velocity[t]=alpha*vct_velocity[t-1]+(1-alpha)*mean_velocity+sqrt(1-(alpha*alpha))*normal_velocity;printf("%lf ",vct_velocity[t]);
vct_direction[t]=alpha*vct_direction[t-1]+(1-alpha)*mean_direction+sqrt(1-(alpha*alpha))*normal_direction;printf("%lf ",vct_direction[t]);
// Calculate the NEW position using the previous position
vct_x[t]=/*(int)round(*/vct_x[t-1]+(vct_velocity[t]*cos(vct_direction[t]))/*)*/; printf("x[%d]=%lf",t,vct_x[t]);
vct_y[t]=/*(int)round(*/vct_y[t-1]+(vct_velocity[t]*sin(vct_direction[t]))/*)*/;printf("y[%d]=%lf \n",t,vct_y[t]);
// if the position is out of range so you have to change the mean direction
/* if(vct_x[t]<10 && vct_y[t]<10) {mean_direction=DEUX_PI/8;}
else if(vct_x[t]<10){mean_direction=0;}
else if(vct_y[t]<10){mean_direction=DEUX_PI/4;}
if(vct_x[t]<10 && vct_y[t]>(bound_y-10)) {mean_direction=(21/24)*DEUX_PI;}//315
else if(vct_Y[t]>(bound_y-10)){mean_direction=(3/4)*DEUX_PI;} //270
if(vct_y[t]>(bound_y-10)&&vct_x[t]>(bound_x-10)){mean_direction=(5/8)*DEUX_PI;}//225
else if(vct_x[t]>(bound_x-10)){mean_direction=DEUX_PI/2;}
if(vct_y[t]<10)&&vct_x[t]>(bound_x-10)){mean_direction=(3/8)*DEUX_PI;}//135*/
/* */if(vct_x[t]<10 && vct_y[t]<10) {mean_direction=DEUX_PI/8;}
else if(vct_x[t]<10 && vct_y[t]>(bound_y-10)) {mean_direction=(21/24)*DEUX_PI;}//315
else if(vct_y[t]>(bound_y-10)&& vct_x[t]>(bound_x-10)){mean_direction=(5/8)*DEUX_PI;}//225
else if(vct_y[t]<10 && vct_x[t]>(bound_x-10)){mean_direction=(3/8)*DEUX_PI;}//135
else if(vct_x[t]<10){mean_direction=0;}
else if(vct_y[t]<10){mean_direction=DEUX_PI/4;}
else if(vct_y[t]>(bound_y-10)){mean_direction=(3/4)*DEUX_PI;} //270
else if(vct_x[t]>(bound_x-10)){mean_direction=DEUX_PI/2;}
// if(vct_x[t]<10 || vct_x[t]>(bound_x-10)){mean_direction=(DEUX_PI/2)-mean_direction;}//vct_direction[t]=vct_direction[t]+(DEUX_PI/2);
// vct_x[t]=/*(int)round(*/vct_x[t-1]+(vct_velocity[t]*cos(vct_direction[t]));}
// if(vct_y[t]<10 || vct_y[t]>(bound_x-10)){mean_direction=-mean_direction;}//vct_direction[t]=vct_direction[t]+(DEUX_PI/2)
// vct_y[t]=/*(int)round(*/vct_[t-1]+(vct_velocity[t]*cos(vct_direction[t]));}
printf("%lf",mean_direction);
t++;
}
FILE * pFile;
pFile = fopen ("myfile.txt","w");
for (int i=0;i<=(int)round(tm_sim/pas_tm);i++){
fprintf(pFile,"%lf\t%lf\n",vct_x[i],vct_y[i]);//,ix[%d]=y[%d]=i,
}
fclose (pFile);
getch();
return 0;
} |
Partager