/* gcc melsimp.c -o xmel -lm to compile */ /* xmel > resu to execute */ /* gnuplot plotresu to plot results using gnuplot */ /* plotresu is simply: */ /* plot 'resu' using 1:2 title "opinions " with points */ /* pause -1 "Hit return to continue" */ /* libraries --------------------------------------------------------*/ #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> /* constants : partie calcul ----------------------------------------*/ #define MAXINT 2147483647 /* for random sampling */ #define L 200 /* number of agents */ float a[L],mu,d,cardis,av; /* opinions, speed of updating, threshold, squared distances */ int i, ix, iy, k; /* loop indices */ int nbiter,t; /* iterations number, random number seed */ int r0 (int s); /* random number routines */ float r01(); /*-------------------------------------------------------------------*/ float r01() { float j; j=rand(); return (j / (float)MAXINT); } /*-------------------------------------------------------------------*/ int r0 (int s) { return (random () % s); } /*------------------------------------------------------------------*/ main () { mu=0.3; /* change parameters in these four lines */ d=0.2; t=54783; nbiter=10000; srandom (t); for (i=0;i<l;i++) a[i]=r01(); for (k = 0; k < nbiter; k++) { ix = (int)(r01()*L); iy = (int)(r01()*L); cardis=(a[ix]-a[iy])*(a[ix]-a[iy]); if (cardis<d*d) {av=a[ix]; a[ix]=a[ix]+mu*(a[iy]-a[ix]); a[iy]=a[iy]+mu*(av-a[iy]); printf ("%d %f %f \n", k, a[ix],a[iy]); } } }