Modify the MatLab code below to answer the following problem: Consider placing a uniform…
- User
Modify the MatLab code below to answer the following problem:
Consider placing a uniform electric field in the x-direction by modifying the probabilities to step right, up, left or down. Instead of 1/4 probability to step in each of these directions, parameterize the probabilities as: ph + pv = 1 where ph is the probability to step in the horizontal direction pv is the probability to step in the vertical direction. Note that pv = 1 − ph . Let the probability to step up equal the probability to step down, such that pu = pd = pv/2. Let the probability to step right be increased as the electric field is increased. We will not try to get a functional relationship between field strength and probability. Instead, let us define a parameter called 𝛼 that has a range between [-1,1]. When 𝛼 = −1, the electric field causes the particle to always step left. When A = 1, the electric field causes the particle to always step right. When A = 0, the random walk has equal probability to step left and right given by pL = pR = ph/2. A reasonable parameterized model might look like: ph = 1/2*(1+A^2) pv = 1 - ph pu = pd = pv/2 pL = (ph/2)(1-A) pR = (ph/2)(1+A)
Answer the following questions, for a variety of different A for the electric field in the x-direction.
- Visualize the random walk trajectories
- In the x-direction, calculate the mean squared displacement as a function of number of steps.
- In the y-direction, calculate the mean squared displacement as a function of number of steps.
- For 1,000,000 walks, plot the histogram of locations of the random walker in the x-direction after 100, 1000, 10000, 100000, 1000000 steps.
- For 1,000,000 walks, plot the histogram of locations of the random walker in the y-direction after 100, 1000, 10000, 100000, 1000000 steps.
Using the same model except with the electric field in the y-direction, answer these questions:
- What should the new equations for the step-probabilities be?
- Visualize the random walk trajectories
- In the x-direction, calculate the mean squared displacement as a function of number of steps.
- In the y-direction, calculate the mean squared displacement as a function of number of steps.
- For 1,000,000 walks, plot the histogram of locations of the random walker in the x-direction after 100, 1000, 10000, 100000, 1000000 steps.
- For 1,000,000 walks, plot the histogram of locations of the random walker in the y-direction after 100, 1000, 10000, 100000, 1000000 steps.
%% RANDOM WALKTS, BROWNIAN MOTION, AND DIFFUSION
% ————————— parameters width = 5; % width of pipe in mm a = 0.01; % lattice constant in mm pw = 0; % probability of waiting num_steps = 1000000; % # steps for histograms num_walks = 1000000; % # random walks for histograms
% ————————— calculate lattice size based on width lattice_size = round(width/a);
% ————————— initialize random walker position x = 0; y = lattice_size/2;
% ————————— initialize mean squared displacement variables msd_x = zeros(1,num_steps); msd_y = zeros(1,num_steps);
% ————————— loop over the # of steps for step = 2:num_steps+1 % choice: does walker wait or make a step? if rand<=pw % walker waits continue; else % walker makes a step % calculate the step direction dx = randi([-1,1]); dy = randi([-1,1]); % update walker position x = x+dx; y = y+dy; % reflect at the y-direction boundary if y<1 y = 2-y; elseif y>lattice_size y = 2lattice_size - y - 1; end % calculate mean squared displacement in each direction msd_x(step) = (xa)^2; msd_y(step) = ((y-(lattice_size/2))a)^2; end end
% —————————— 1) visualize the random walk trajectories figure; plot((0:num_steps)a, (msd_ya), ‘b.’) title(‘Random Walk Trajectories’); xlabel(‘x (mm)’); ylabel(‘y (mm)’); xlim([0,num_stepsa]);
% ————————— 2) mean squared displacement in x-direction figure; n_steps = 1:num_steps; msd_x = cumsum(msd_x) ./ (1:num_steps+1); plot(n_steps(1:num_steps-1)a, msd_x(2:num_steps)a^2, ‘b’) title(‘Mean Squared Displacement in the x-Direction’); xlabel(‘Number of Steps’); ylabel(‘Mean Squared Displacement’); xlim([0,num_stepsa]);
% ————————— 3) mean squared displacement in y-direction figure; msd_y = cumsum(msd_y) ./ (1:num_steps+1); plot(n_steps(1:num_steps)a, msd_y(1:num_steps)a^2, ‘b’) title(‘Mean Squared Displacement in the y-Direction’); xlabel(‘Number of Steps’); ylabel(‘Mean Squared Displacement’); xlim([0, num_stepsa]);
% ————————— 4) histogram of locations in x-direction figure; for num_steps = [100,1000,10000,100000,1000000] x_hist = zeros(1,lattice_size+1); for walk = 1:num_walks x=0; for step = 1:num_steps dx = randi([-1,1]); x = x+dx; if x<0 x=0; elseif x>lattice_size x = lattice_size; end end x_hist(x+1) = x_hist(x+1) + 1; end subplot(2,3,find([100,1000,10000,100000,1000000] == num_steps)); bar(0:lattice_size, x_hist / num_walks, ‘b’); title(['Histogram of x-locations after ‘, num2str(num_steps), ’ steps’]); xlabel(‘x’); ylabel(‘Probability’); end
% ————————— 5) histogram of locations in y-direction figure; for num_steps = [100,1000,10000,100000,1000000] y_hist = zeros(1,lattice_size+1); for walk = 1:num_walks x = 0; y = lattice_size/2; for step = 1:num_steps dx = randi([-1,1]); dy = randi([-1,1]); x = x+dx; y = y+dy; if y<1 y = 2-y; elseif y>lattice_size y = 2lattice_size - y - 1; end end y_hist(y+1) = y_hist(y+1) + 1; end subplot(2,3,find([100,1000,10000,100000,1000000] == num_steps)); bar(0:lattice_size, y_hist / num_walks, ‘b’); title(['Histogram of y-locations after ‘, num2str(num_steps), ’ steps’]); xlabel(‘y’); ylabel(‘Probability’); end
% ————————— 6) calculate avg time per step for given diffusion constant D_desired = 1e-7; % desired diffusion constant in cm^2/sec t = D_desired / (a^2); % avg time per step in sec % substitude the values, t = (10^-7) / (.01^2) = 10^-3 % so the average time per step (t) should be 10^-3 seconds
% ————————— 7) calculate diffusion constant in the x-direction figure; diffusion_x = gradient(msd_xa^2) / (2t); plot(n_stepsa, diffusion_x); title(‘Diffusion Constant in the x-Direction’); xlabel(‘Time (mm)’); ylabel(‘Diffusion Constant’); xlim([0, num_steps*a]);
- Assistant
%% RANDOM WALKTS, BROWNIAN MOTION, AND DIFFUSION
% ————————— parameters width = 5; % width of pipe in mm a = 0.01; % lattice constant in mm pw = 0; % probability of waiting num_steps = 1000000; % # steps for histograms num_walks = 1000000; % # random walks for histograms
% ————————— calculate lattice size based on width lattice_size = round(width/a);
% --- Parameterize the step probabilities --- A = 0.5; % Electric field strength parameter ph = 1/2*(1+A^2); pv = 1 - ph; pu = pv/2; pd = pv/2; pL = (ph/2)(1-A); pR = (ph/2)(1+A); % -----------------------------------------
% ————————— initialize random walker position x = 0; y = lattice_size/2;
% ————————— initialize mean squared displacement variables msd_x = zeros(1,num_steps); msd_y = zeros(1,num_steps);
% ————————— loop over the # of steps for step = 2:num_steps+1 % choice: does walker wait or make a step? if rand<=pw % walker waits continue; else % walker makes a step % calculate the step direction dx = zeros(1,2); dy = zeros(1,2); dx(rand<=pL+0.5) = -1; dx(rand<=pR+(pL/2)) = 1; dy(rand<=pu+0.5) = -1; dy(rand<=pd+(pu/2)) = 1; % update walker position x = x+sum(dx); y = y+sum(dy); % reflect at the y-direction boundary if y<1 y = 2-y; elseif y>lattice_size y = 2lattice_size - y - 1; end % calculate mean squared displacement in each direction msd_x(step) = (xa)^2; msd_y(step) = ((y-(lattice_size/2))*a)^2; end end
% —————————— 1) visualize the random walk trajectories figure; plot((0:num_steps)a, (msd_ya), 'b.') title('Random Walk Trajectories'); xlabel('x (mm)'); ylabel('y (mm)'); xlim([0,num_steps*a]);
% ————————— 2) mean squared displacement in x-direction figure; n_steps = 1:num_steps; msd_x = cumsum(msd_x) ./ (1:num_steps+1); plot(n_steps(1:num_steps-1)*a, msd_x(2:num_steps)a^2, 'b') title('Mean Squared Displacement in the x-Direction'); xlabel('Number of Steps'); ylabel('Mean Squared Displacement'); xlim([0,num_stepsa]);
% ————————— 3) mean squared displacement in y-direction figure; msd_y = cumsum(msd_y) ./ (1:num_steps+1); plot(n_steps(1:num_steps)*a, msd_y(1:num_steps)a^2, 'b') title('Mean Squared Displacement in the y-Direction'); xlabel('Number of Steps'); ylabel('Mean Squared Displacement'); xlim([0, num_stepsa]);
% ————————— 4) histogram of locations in x-direction figure; for num_steps = [100,1000,10000,100000,1000000] x_hist = zeros(1,lattice_size+1); for walk = 1:num_walks x=0; for step = 1:num_steps dx = zeros(1,2); dx(rand<=pL+0.5) = -1; dx(rand<=pR+(pL/2)) = 1; x = x+sum(dx); if x<0 x=0; elseif x>lattice_size x = lattice_size; end end x_hist(x+1) = x_hist(x+1) + 1; end subplot(2,3,find([100,1000,10000,100000,1000000] == num_steps)); bar(0:lattice_size, x_hist / num_walks, 'b'); title(['Histogram of x-locations after ', num2str(num_steps), ' steps']); xlabel('x'); ylabel('Probability'); end
% ————————— 5) histogram of locations in y-direction figure; for num_steps = [100,1000,10000,100000,1000000] y_hist = zeros(1,lattice_size+1); for walk = 1:num_walks x = 0; y = lattice_size/2; for step = 1:num_steps dx = zeros(1,2); dy = zeros(1,2); dx(rand<=pL+0.5) = -1; dx(rand<=pR+(pL/2)) = 1; dy(rand<=pu+0.5) = -1; dy(rand<=pd+(pu/2)) = 1; x = x+sum(dx); y = y+sum(dy); if y<1 y = 2-y; elseif y>lattice_size y = 2*lattice_size - y - 1; end end y_hist(y+1) = y_hist(y+1) + 1; end subplot(2,3,find([100,1000,10000,100000,1000000] == num_steps)); bar(0:lattice_size, y_hist / num_walks, 'b'); title(['Histogram of y-locations after ', num2str(num_steps), ' steps']); xlabel('y'); ylabel('Probability'); end
% ————————— NEW CODE FOR ELECTRIC FIELD IN Y-DIRECTION —————————
% --- Parameterize the step probabilities --- A = 0.5; % Electric field strength parameter ph = 1/2*(1+A^2); pv = 1 - ph; pu = pv/2; pd = pv/2; pU = (pv/2)(1-A); pD = (pv/2)(1+A); % -----------------------------------------
% ————————— initialize random walker position x = lattice_size/2; y = 0;
% ————————— initialize mean squared displacement variables msd_x = zeros(1,num_steps); msd_y = zeros(1,num_steps);
% ————————— loop over the # of steps for step = 2:num_steps+1 % choice: does walker wait or make a step? if rand<=pw % walker waits continue; else % walker makes a step % calculate the step direction dx = zeros(1,2); dy = zeros(1,2); dx(rand<=pu+0.5) = -1; dx(rand<=pd+(pu/2)) = 1; dy(rand<=pU+0.5) = -1; dy(rand<=pD+(pU/2)) = 1; % update walker position x = x+sum(dx); y = y+sum(dy); % reflect at the x-direction boundary if x<1 x = 2-x; elseif x>lattice_size x = 2*lattice_size - x - 1; end % calculate mean squared displacement in each direction msd_x(step) = ((x-(lattice_size/2))a)^2; msd_y(step) = (ya)^2; end end
% ————————— 1) visualize the random walk trajectories figure; plot((0:num_steps)a, (msd_xa), 'b.') title('Random Walk Trajectories'); xlabel('x (mm)'); ylabel('y (mm)'); xlim([0,num_steps*a]);
% ————————— 2) mean squared displacement in x-direction figure; n_steps = 1:num_steps; msd_x = cumsum(msd_x) ./ (1:num_steps+1); plot(n_steps(1:num_steps-1)*a, msd_x(2:num_steps)a^2, 'b') title('Mean Squared Displacement in the x-Direction'); xlabel('Number of Steps'); ylabel('Mean Squared Displacement'); xlim([0,num_stepsa]);
% ————————— 3) mean squared displacement in y-direction figure; msd_y = cumsum(msd_y) ./ (1:num_steps+1); plot(n_steps(1:num_steps)*a, msd_y(1:num_steps)a^2, 'b') title('Mean Squared Displacement in the y-Direction'); xlabel('Number of Steps'); ylabel('Mean Squared Displacement'); xlim([0, num_stepsa]);
% ————————— 4) histogram of locations in x-direction figure; for num_steps = [100,1000,10000,100000,1000000] x_hist = zeros(1,lattice_size+1); for walk = 1:num_walks x = lattice_size/2; y = 0; for step = 1:num_steps dx = zeros(1,2); dy = zeros(1,2); dx(rand<=pu+0.5) = -1; dx(rand<=pd+(pu/2)) = 1; dy(rand<=pU+0.5) = -1; dy(rand<=pD+(pU/2)) = 1; x = x+sum(dx); y = y+sum(dy); if x<1 x = 2-x; elseif x>lattice_size x = 2*lattice_size - x - 1; end end x_hist(x+1) = x_hist(x+1) + 1; end subplot(2,3,find([100,1000,10000,100000,1000000] == num_steps)); bar(0:lattice_size, x_hist / num_walks, 'b'); title