Brownian Motion Write a program that will simulate Brownian motion by doing ALL
ID: 3557091 • Letter: B
Question
Brownian Motion
Write a program that will simulate Brownian motion by doing ALL of the following:
1) Ask the user how many trials to run.
2) Run the requested number of trials. For each trial you will simulate the Brownian motion of one particle over 10 microseconds (10 jumps). The particle should jump up to 1 nanometer to the left or right with each jump.
3) Calculate the average absolute value of the distance that the particle has moved.
4) Print the average absolute value of the distance to the screen.
Hints:
1) You will need rand() from <stdlib.h> to generate random numbers to simulate the jumping. Note that rand() fenerates random integers, but for Brownian motion the jumps must be floating point values.
Sample:
How many trials to run?
1000
The average absolute value of the distance travelled was 1.009000
Explanation / Answer
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand (time(NULL));
int i,j;
int numOfTrials;
printf("How many trial you want to run :");
scanf("%d",&numOfTrials);
double averageDistance=0;
double sumOfDistances=0;
for(i=0;i<numOfTrials;i++)
{
double distance=0;
for(j=0;j<10;j++)
{
double randomValue=((double)rand()/(double)RAND_MAX)*2 -1.0;
if(randomValue<0.0)
distance=distance-1.0;
else
distance=distance+1.0;
}
sumOfDistances=sumOfDistances+abs(distance);
}
averageDistance=sumOfDistances/numOfTrials;
printf("The average distance is :%lf ",averageDistance);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.