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

3 vues (au cours des 30 derniers jours)
Rahul
Rahul le 10 Août 2023
Commenté : Walter Roberson le 11 Août 2023
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

Réponse acceptée

Walter Roberson
Walter Roberson le 10 Août 2023
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 commentaires
Rahul
Rahul le 11 Août 2023
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
Walter Roberson
Walter Roberson le 11 Août 2023
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))

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by