【发布时间】:2012-10-27 01:52:40
【问题描述】:
所以我有一个我无法弄清楚的奇怪问题。 我有一个 while 循环,它根据索引将值传递到向量中。这些值由调用的子函数提供。我的问题是,在第一次迭代之后,向量停止接受来自子函数的值,尽管代码继续运行并且没有崩溃。 这发生在“S(i)=Trials;”。
function Simulation()
N=input('How many trials would you like to run? ');
Tup=input('What is the positive boundary? ');
Tdown=input('What is the negative boundary? ');
StepLength=input('What is the step length? ');
d=0;
i=1;
S=zeros(1,N);
StepLimit=1000000;
if Tup==Tdown
display('Please use boundaries that are not equal to eachother.');
elseif Tup<=d||Tdown>=d
display('Please choose logical boundaries.');
else
while i<=N
S(i)=Trials;
i=i+1;
end
end
x=0:10:max(S);
hist(S,x);
axis tight;
xlabel('Number of Steps');
ylabel('Number of Trials');
function s=Trials()
s=0;
while ~(d<=Tdown)&&~(d>=Tup)
m=StepLength.*RandDir(1);
d=d+m;
s=s+1;
if s>=StepLimit
display('The step limit was reached.');
return
end
end
function out = RandDir(N)
% Generate a random vector from the set {+/- e_1, +/- e_2,..., +/- e_N}
% where e_i is the ith basis vector. N should be an integer.
I = round(ceil(2*N*rand));
if rem(I,2) == 1
sgn = -1;
else
sgn = 1;
end
out = zeros(N,1);
out(ceil(I/2)) = sgn*1;
end
end
end
感谢您的帮助
【问题讨论】:
标签: function matlab error-handling while-loop