Modify the MatLab code below to answer the following prompt. Adjust pw by gradually…
- User
Modify the MatLab code below to answer the following prompt.
Adjust pw by gradually increasing it from 0 to near 1. As you do this, repeat number 7 in the previous problem for different values of pw. Determine the appropriate way to translate number of steps to time by modifying the relationship: t = n*𝜏 (the new formula will be a function of pw ). Now, consider two kinds of media. Media A fills the pipe for x ≥ 0, while media B fills the pipe for x < 0. Suppose the diffusion constant for media A is 10^-7 cm^2 / sec and the diffusion constant for media B is 10^-8 cm^2 / sec. By using different pw for media A and B, answer the following questions.
- 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_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]);
- Assistant
- User
Error in the following line: unrecognized function or variable "pw§".
current_pw = pw§;
- Assistant
Apologies for the error. It looks like there is a typo in the code. The correct line should be:
current_pw = pw(p);
Please replace the line "current_pw = pw§;" with "current_pw = pw(p);"
- User
§ does not exist
- Assistant
Apologies for the confusion. It looks like there was another typographical error. Please replace the line "current_pw = pw§;" with "current_pw = pw(p);". The correct line should be:
current_pw = pw(p);
This will assign the value of pw at index p to the variable current_pw.
- User
Invalid text character §
- Assistant
I apologize for the confusion. It seems that there was a mistake in my previous response. To fix the issue, replace the line "current_pw = pw§;" with "current_pw = pw(p);". The correct line should be:
current_pw = pw(p);
This will assign the value of pw at index p to the variable current_pw.
- User
How to say "pw at index p" in MatLab
- Assistant
In MATLAB, you can access the value of an element at a specific index in an array using parentheses. To access the element of
pwat indexp, you would use the following notation:current_pw = pw(p);
This will assign the value of
pwat indexpto the variablecurrent_pw. - User
Can you use dot indexing instead?
- Assistant
Yes, you can use dot indexing in MATLAB to access the value of an element at a specific index in an array. However, dot indexing is typically used for accessing properties or fields of a structure or object, rather than accessing array elements.
Since
pwis a regular array and not a structure or object, dot indexing is not appropriate in this case. Instead, you should use parentheses indexing as mentioned earlier:current_pw = pw(p);
This will assign the value of
pwat indexpto the variablecurrent_pw. - User
MatLab is saying § is an invalid text character
- Assistant
Apologies for the confusion. You are correct, the notation "§" is not recognized in MATLAB. To access the element of
pwat indexp, you need to use parentheses indexing, as mentioned earlier:current_pw = pw(p);
This will assign the value of
pwat indexpto the variablecurrent_pw. Once again, I apologize for any confusion caused. - User
That is not parentheses indexing
- Assistant
Apologies for the confusion. In MATLAB, we use parentheses indexing to access elements in an array. The correct syntax to access the element of
pwat indexpis:current_pw = pw(p);
Please replace the line "current_pw = pw§;" with "current_pw = pw(p);" to correctly access the value of
pwat indexp.