📄 gauss.c
字号:
/******************************************************************************* File Name: gauss.c* Purpose: gauss-markov mobility model.* Author: Jeff Boleng* Date Created:** Edited 12 July 2006 by Jon Petty: * Changed seed code to rand((int)time(NULL) + (int)getpid())** Copyright (C) 2004 Toilers Research Group -- Colorado School of Mines** Please see COPYRIGHT.TXT and LICENSE.TXT for copyright and license* details.******************************************************************************/ #include <stdlib.h>#include <stdio.h>#include <time.h>#include <math.h>#include <unistd.h>#define true 1#define false 0#define min(a,b) (((a) < (b)) ? (a) : (b))#define max(a,b) (((a) > (b)) ? (a) : (b))int main(int argc, char *argv[]){ double pi = 2*asin(1.0); double alpha = 1.0; double meanSpeed = 10.0; double meanDirection = pi/2.0; double maxX=1000.0, maxY=1000.0; double endTime=1000.0; double interval = 1.0; double speed = meanSpeed; double direction = meanDirection; double xLoc = 0.0; double yLoc = 0.0; double curtime = 0.0; double randGauss() { double u = ((double)rand()/(double)RAND_MAX)*1.0; double v = ((double)rand()/(double)RAND_MAX)*1.0; double x; x = sqrt(-2*log(v))*cos(2*pi*u); return x; } double newSpeed(double oldV) { double v = 0.0; /* double r = ((double)rand()/(double)RAND_MAX)*1.0; */ double r = randGauss(); v = alpha*oldV + (1-alpha)*meanSpeed + sqrt(1-alpha*alpha) * r; return v; } double newDirection(double oldD, double x, double y) { double d = 0.0; double r = 0.0; double localDirection = meanDirection; if ((x<=100) && (y<=100)) { localDirection = pi/4.0; } else if ((x<=100) && (y>=900)) { localDirection = 2.0*pi - pi/4.0; } else if ((x>=900) && (y>=900)) { localDirection = pi + pi/4.0; } else if ((x>=900) && (y<=100)) { localDirection = pi - pi/4.0; } else if (x<=100) { localDirection = 2.0*pi; } else if (x>=900) { localDirection = pi; } else if (y<=100) { localDirection = pi/2.0; } else if (y>=900) { localDirection = pi + pi/2.0; } /* r = ((double)rand()/(double)RAND_MAX)*2.0*meanDirection; */ r = randGauss(); d = alpha*oldD + (1-alpha)*localDirection + sqrt(1-alpha*alpha) * r; meanDirection = localDirection; return d; } if (argc == 2) { alpha = atof(argv[1]); fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); fprintf(stdout, "#\tmaxX = %9.2f\n", maxX); fprintf(stdout, "#\tmaxY = %9.2f\n", maxY); fprintf(stdout, "#\tendTime = %9.2f\n", endTime); fprintf(stdout, "#\tmeanSpeed = %9.2f\n", meanSpeed); fprintf(stdout, "#\talpha = %9.2f\n", alpha); fprintf(stdout, "#\tinterval = %9.2f\n", interval); fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"); } else { fprintf(stderr, "Usage: gauss <alpha>\n"); return -1; } /* */ srand((int)time(NULL) + (int)getpid()); speed = meanSpeed; direction = meanDirection; xLoc = ((double)rand()/(double)RAND_MAX)*maxX; yLoc = ((double)rand()/(double)RAND_MAX)*maxY; xLoc = 500.0; yLoc = 500.0; fprintf(stdout, "set terminal postscript epsf solid\n"); fprintf(stdout, "set output \"gauss%.2f.eps\"\n", alpha); fprintf(stdout, "set xrange[0:%f]\n", maxX); fprintf(stdout, "set yrange[0:%f]\n\n", maxY); fprintf(stdout, "set multiplot\n"); fprintf(stdout, "plot \'-\' notitle with linespoints\n"); fprintf(stdout, "%f %f\ne\n", xLoc, yLoc); fprintf(stdout, "plot \'-\' notitle with lines\n"); fprintf(stdout, "%f %f\n", xLoc, yLoc); while (curtime <= endTime) { xLoc += speed*cos(direction); yLoc += speed*sin(direction); fprintf(stdout, "%f %f\n", xLoc, yLoc); speed = newSpeed(speed); direction = newDirection(direction, xLoc, yLoc); curtime += interval; } /* while (curtime <= endTime) */ fprintf(stdout, "e\n\n"); fprintf(stdout, "plot \'-\' notitle with linespoints\n"); fprintf(stdout, "%f %f\ne\n", xLoc, yLoc); fprintf(stdout, "set nomultiplot\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -