Question: Exercise 1 . ( 5 0 points ) Diffusion Homework # 3 In this exercise, we use a 3 D random walk to simulate a
Exercise points Diffusion
Homework #
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 xyz with equal probability its next location is
at x yzx yzxy zxy zxyz or xyz
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 xyz coordinates
range from n to n For all the possible xyz coordinates within the range, we save the number of particles
that end up at the location xyz During the simulation, if the final location of a particle is xyz 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 xyz and the
origin The formula is xyz 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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
