Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#includ #include #include / / TODO / / Implement the below function / / Simulate one particle moving n steps in random directions / /
#includ
#include
#include
TODO
Implement the below function
Simulate one particle moving n steps in random directions
Use a random number generator to decide which way to go at every step
When the particle stops at a final location, use the memory pointed to by grid to
record the number of particles that stop at this final location
Feel free to declare, implement and use other functions when needed
void oneparticleint grid int n
TODO
Implement the following function
This function returns the fraction of particles that lie within the distance
rn from the origin including particles exactly rn away
The distance used here is Euclidean distance
Note: you will not have access to math.h when submitting on Mimir
double densityint grid int n double r
use this function to print results
void printresultint grid int n
printfradius density
;
forint k ; k ; k
printflf lf
k densitygrid nk;
TODO
Finish the following function
See the assignment decription on Piazza for more details
void diffusionint n int m
fill in a few line of code below
forint i ; im; i oneparticlegrid n;
printresultgrid n;
fill in some code below
int mainint argc, char argv
ifargc
printfUsage: s n m
argv;
return ;
int n atoiargv;
int m atoiargv;
assertn && n ;
assertm && m ;
srand;
diffusionn m;
return ;
In this exercise, we use a D random walk to simulate a diffusion process. Imagine a particle starting at
the origin that has equal probabilities to go in possible directions left, right, backward, forward,
down, and up For example, when the particle is at x y z with equal probability its next location is
at x y zx y zx y zx y zx y z or x y z
The particle will conduct the random walk for n steps. We are interested in the distribution of the final
locations of particles after each takes n steps. Specifically, we would like to know the distribution of the
distance between the final location and the origin. In order to obtain this distribution, we simulate m such
particles, and check the proportion of the particles that lies within rn distance from the origin, where r is a
real number between and Note all the particles will be within a sphere with radius n since particles only
move n steps and the furthest they can go is a distance n from the origin. In our simulation, we will calculate
the proportion of particles that are within rn from the origin for r
Below is the main function for this program. Note how we use command line arguments.
int mainint argc, char argv
ifargc
printfUsage: s n m
argv;
return ;
int n atoiargv;
int m atoiargv;
srand;
diffusionn m;
return ;
We need to implement the function
void diffusionint n int m
In this function, we need to dynamically allocate memory to represent a D grid where x y z coordinates
range from n to n For all the possible x y z coordinates within the range, we save the number of particles
that end up at the location x y z During the simulation, if the final location of a particle is x y z the
corresponding value will be incremented by
Generate a random number using rand If this number is or you should move left or right
on the xaxis, respectively. If it is or you should move up or down on the yaxis, respectively. If it is
or move up or down on the zaxis, respectively.
In the implementation of main, we use the below function to print out results.
void printresultint grid int n
printfradius density
;
forint k ; k ; k
printflf lf
k densitygrid nk;
The function
double densityint grid int n double r
returns the proportion of particles in the grid that are within the sphere of rn radius from the origin. Note
that to implement the above function, we need to calculate the Euclidean distance between x y z and the
origin The formula is p
x
y
z
Think about how we can avoid using the square root in our
code.
Below are outputs from a few sample runs. We can use these outputs to check our code.
$ diffusion
radius density
$ diffusion
radius density
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started