How to code a 1D-random walk with wait time?
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2023-8-10
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 个评论
Walter Roberson
2023-8-11
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))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!