MATLAB Why is my Matlab infinite loop not working properly?

AI Thread Summary
The discussion centers around troubleshooting a MATLAB program that encounters an infinite loop and issues with random number generation. The initial problem was resolved by changing an 'or' statement to an 'and' statement in the while loop condition. However, the user faced a new issue where the random number generator consistently produced the same value, leading to incorrect results for the expected value variable. It was suggested that the user avoid overwriting the built-in `rand` function by using a different variable name and to clear any previous definitions of `rand`. The expectation of the expected value being close to zero was debated, with some participants noting that the matrix's structure may not support this assumption.
Poisonous
Messages
26
Reaction score
0
EDIT:: I figured out the problem with the loop was that I needed an 'and' statement, not an 'or' statement.. BUT I'm still having the problem where my random number is always the same.

I can't figure out why this is loop is infinite (I think it's the while loop). I'm new to Matlab specifically, so it might be something in the syntax.

The point of this program is to start in the middle of the array (at point M(3,3)), then move around the matrix until it hits one of the boundary -1's or 1's. Then the while loop stops and adds either the 1 or -1 to the expecVal variable. Based off the matrix, the variable expecVal should be about 0 at the end. I know there are more efficient ways of writing this program, but the point was just to write a quick simulation, and this is the first way I thought to do it.

Code:
M = zeros(5);

M(1,3) = -1;
M(2,2) = -1;
M(2,4) = 1;
M(3,1) = 1;
M(3,5) = 1;
M(4,1) = -1;
M(4,5) = -1;
M(5,2) = 1;
M(5,3) = -1;
M(5,4) = 1;

rand = rand();
expecVal = 0;
z = 3;
x = 3;

for i=1:10
    while M(z,x) ~= 1 || M(z,x) ~= -1
        rand = rand();
        if rand < .25
            z=z+1;
        elseif .25 < rand < .5
            z=z-1;
        elseif .5 < rand < .75
            x=x+1;
        else
            x=x-1;
        end
    end
    expecVal = expecVal + M(z,x);
    z = 3;
    x = 3;
end

expecVal
 
Last edited:
Physics news on Phys.org
Ok, so I figured out that all I need to do is switch the || to be an &&, but I'm also having another weird problem.

I've also noticed that my Matlab is only ever generating the same random number (8.147) every single time I call the rand function. So, the problem always returns the expecVal 1, since that's what M(3,1) is and it picks that every time.
 
Last edited:
Ok, so here it is as fixed as I have it so far. I had to restart my computer so that MATLAB wouldn't generate other random numbers.. weird. The only problem is that the values I get aren't always very close to 0, and the variance seems a little bit large.

Code:
M = zeros(5);

M(1,3) = -1;
M(2,2) = -1;
M(2,4) = 1;
M(3,1) = 1;
M(3,5) = 1;
M(4,1) = -1;
M(4,5) = -1;
M(5,2) = 1;
M(5,3) = -1;
M(5,4) = 1;

ranNum = rand;
expecVal = 0;
z = 3;
x = 3;

for i=1:1000
    while M(z,x) ~= 1 && M(z,x) ~= -1
        ranNum = rand;
        if ranNum < .25
            z=z+1;
        elseif ranNum < .5
            z=z-1;
        elseif ranNum < .75
            x=x+1;
        elseif ranNum < 1
            x=x-1;
        end
    end
    expecVal = expecVal + M(z,x);
    z = 3;
    x = 3;
end

expecVal
 
I don't know why you're writing "rand = rand()", that would fix the number by overwriting the rand.m function with a variable which you call "rand", and explain why you have to restart MATLAB to fix it. A quicker way is "clear rand".
Edit - the code in the 3rd post appears to work, for me at least. Why do you think the expectation of expected val should be 0? The matrix doesn't appear (at first glance) to show the symmetry required. My results are generally in the range -30 to 50, after 1000 iterations that is not a particularly high variance...
Code:
M =

     0     0    -1     0     0
     0    -1     0     1     0
     1     0     0     0     1
    -1     0     0     0    -1
     0     1    -1     1     0
 
Last edited:
From relaxations I knew you were equally likely to hit either 1 or -1 from the starting point, so i figured you should be really close to 0 after a bunch of tries. I guess I was being too optimistic.
 
I'm no good at statistics (never learnt), but when judging the variance shouldn't you take into account the number of samples? Trying this code with i = 1,000,000 and it's still pretty low numbers for the most part, 52, 423, 149...Plus, if it's equally likely to hit -1 as 1, why not just replace this code with a coinflip?
 
Back
Top