2

So I have this bizarre problem that I cannot figure out. I have a while loop that is passing values into a vector according to an index. These values are provided by a subfunction that is called. My problem is that after the first iteration, the vector ceases to accept values from the subfunction, though the code keeps running and doesn't break down. This is happening at '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

Thank you for any help

4

2 回答 2

1

without getting into the semantics, here's what you're doing:

Step 1: You call a function Trials... Change d until you get a desired values.

Step 2: Call Trials again, but keep the same d as previously used. Does it meet the previous end condition? yes. Stop Trials

Step 3: Call Trials again, but keep the same d as previously used. Does it meet the previous end condition? yes. Stop Trials

Step 4: Call Trials again, but keep the same d as previously used. Does it meet the previous end condition? yes. Stop Trials

solution:

in your Trials function reinitialize d:

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

I agree on the re-initialization of your s and d variables. The reason you are finding it difficult to debug is that you are using global variables. Please don't do that unless you absolutely have to!

Here is a version without global variables:

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;

Why did you use a while loop in the main function (have i broken your code with the for loop)?

于 2012-11-07T08:13:31.577 回答