How to code a 1D-random walk with wait time?

Hi,
I'm trying to write a code for 1D-random walk with waiting time between jump of particle.
But, the code gives error.
If you kindly advice me the correction, it will help me immensely.
The code is as below with its error:
%Input value for Random walk
defaultValue = 250; %Total number of steps
titleBar = 'Enter the input value';
userPrompt = 'Enter the number of steps to take';
dlgtitle = 'Input';
causerInput = inputdlg(userPrompt,userPrompt,1,{num2str(defaultValue)});
if isempty(causerInput)
return
end
inputValue = round(str2num(cell2mat(causerInput)));
%Check for a valid input
if isnan(inputValue)
inputValue=defaultValue;
message = sprintf('Input value must be an integer. \nI will use %d and continue.', inputValue);
uiwait(warndlg(message));
end
%Turbulence intensity equation for Random Walk
numberofWalks = inputValue;
stepsperWalk = 250;
waitingTime = pause(120.0);
delta_X = rand(stepsperWalk);
delta_T = pause(rand);
xt = zeros(numberofWalks,2);
tic
for step = 2:numberofWalks
if toc < waitingTime
xt(step,1) = xt(step,1) + delta_X(step);
xt(step,2) = xt(step,2) + delta_T(step);
end
end
drift_vel = mean(xt(step,1)/xt(step,2));
diff_coeff = mean(xt(step,1)^2/(2*xt(step,2)));
ERROR:
Index exceeds the number of array elements. Index must not exceed 2.
Error in pde2fshear_v4Perturbed_bistable>pdex2pde (line 641)
xt(step,2) = xt(step,2) + delta_T(step);
With rgds,
rc

 Accepted Answer

waitingTime = pause(120.0);
pause is a MATLAB function that is typically passed a scalar floating point number, and delays execution for that time.
However, when you ask for an output from pause() then it returns without delay, and the return is either 'on' or 'off' according to whether pausing is currently enabled or not. So your waitingTime is going to be either the two-element character vector 'on' or the three-element character vector 'off'
delta_T = pause(rand);
Likewise, your delta_T will be either 'on' or 'off'
xt(step,1) = xt(step,1) + delta_X(step);
There you are trying to index the 'on' or 'off' according to the iteration number. If pauses are enabled, which is the default state, then delta_X would be 'on' and as soon as step became 3, you would be attempting to index the two-element character vector 'on' at the third position.

7 Comments

Hi Walter Roberson,
Thanks for your reply.
But I couldn't find any other logic to apply here. Can you plz avice?
With regards,
rc
Are you asking for real-time waiting, or simulated waiting? Doies the pause(120.0) mean that you expect the user to sit staring at the screen for two minutes waiting for the action to start? Or does it mean that if you graphed position over time, you would want the initial position to show up until t = 120, and then for the graph to show the same y coordinate (current offset) for varying x (cumulative time) ?
Hi Walter Roberson,
Its the later rather than the former, which means, the diffusion of particles are such that its making random jumps from one location to another. While doing so, it performs a jump, then it waits at its new location for sometime(which is random in time), then performs its new jump (spatially random), waits again and so on.
with regards,
rc
stepsperWalk = 250;
std_step = 0.3;
mean_wait = 5; %set as appropriate
delta_X = randn(1,stepsperWalk) * std_step;
delta_T = rand(1,stepsperWalk) * mean_wait;
t = cumsum([0, delta_T]);
x = cumsum([0, delta_X]);
stem(t, x)
xlabel('time'); ylabel('displacement');
Hi Walter,
Thanks a lot for your reply.
with rgds,
rc
Hi,
The following code with some further attempt to modify the same, it seems the matrices (xt and x_total) also (xt and t_total) are not getting added. This may be because they are not identical matrices in rows and columns.
I also tried by transposing the matrix x_total, but it didn't.
Please let me know how can the matrices be added?
stepsperWalk = 250;
std_step = 0.3;
mean_wait = 5; %set as appropriate
delta_X = randn(1,stepsperWalk) * std_step;
delta_T = rand(1,stepsperWalk) * mean_wait;
t_total = cumsum([0, delta_T]);
x_total = cumsum([0, delta_X]);
stem(t_total, x_total)
xlabel('time'); ylabel('displacement');
xt = zeros(stepsperWalk,2);
for step = 2:stepsperWalk
if delta_X(step) < 0.2
xt(step,1) = xt(step,1) + x_total(step);
else
xt(step,1) = xt(step,1) - x_total(step);
end
if delta_T(step) < 3.0
xt(step,2) = xt(step,2) + t_total(step);
else
xt(step,2) = xt(step,2) - t_total(step);
end
end
with regards,
rc
I do not know what you mean about those not getting added.
Your xt(:,2) appears to reflect time somehow, but your code is sometimes subtracting from xt(:,2) which would appear to correspond to subtracting time.
You have no comments about what your xt code is intended to calculate.
stepsperWalk = 250;
std_step = 0.3;
mean_wait = 5; %set as appropriate
delta_X = randn(1,stepsperWalk) * std_step;
delta_T = rand(1,stepsperWalk) * mean_wait;
t_total = cumsum([0, delta_T]);
x_total = cumsum([0, delta_X]);
stem(t_total, x_total)
xlabel('time'); ylabel('displacement');
xt = zeros(stepsperWalk,2);
for step = 2:stepsperWalk
if delta_X(step) < 0.2
xt(step,1) = xt(step,1) + x_total(step);
else
xt(step,1) = xt(step,1) - x_total(step);
end
if delta_T(step) < 3.0
xt(step,2) = xt(step,2) + t_total(step);
else
xt(step,2) = xt(step,2) - t_total(step);
end
end
plot(xt(:,2), xt(:,1))

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!