2

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

助けてくれてありがとう

4

2 に答える 2

1

セマンティクスに入ることなく、ここであなたがやっていることは次のとおりです。

ステップ 1: 関数 Trials を呼び出します... 必要な値が得られるまで d を変更します。

ステップ 2: Trials を再度呼び出しますが、以前と同じ d を保持します。前回の終了条件を満たしていますか?はい。トライアルを停止

ステップ 3: Trials を再度呼び出しますが、以前と同じ d を保持します。前回の終了条件を満たしていますか?はい。トライアルを停止

ステップ 4: Trials を再度呼び出しますが、以前と同じ d を保持します。前回の終了条件を満たしていますか?はい。トライアルを停止

解決:

あなたのTrials関数で reinitialize d

function s=Trials()
    d=0;
    s=0;
...
于 2012-11-07T03:48:46.580 に答える
0

s および d 変数の再初期化に同意します。デバッグが難しい理由は、グローバル変数を使用しているためです。絶対に必要でない限り、それをしないでください。

グローバル変数のないバージョンは次のとおりです。

function foo()
N           = 10;
Tup         = 10;
Tdown       = -10;
StepLength  = 1;
d           = 0;
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
    for i_trial= 1:N
        S(i_trial) = Trials(Tdown, Tup, StepLength, StepLimit);
    end
end

x           = 0:10:max(S);
hist(S,x);
axis tight;
xlabel('Number of Steps');
ylabel('Number of Trials');

function s=Trials(Tdown, Tup, StepLength, StepLimit)
s = 0;
d = 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;

main 関数で while ループを使用したのはなぜですか (for ループでコードを壊してしまったのでしょうか)。

于 2012-11-07T08:13:31.577 に答える