だから私は理解できないこの奇妙な問題を抱えています。インデックスに従ってベクトルに値を渡す 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
助けてくれてありがとう