Why is my Matlab infinite loop not working properly?

  • Context: MATLAB 
  • Thread starter Thread starter Poisonous
  • Start date Start date
  • Tags Tags
    Infinite Loop Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB program that simulates random movement within a matrix until a boundary condition is met. Participants explore issues related to infinite loops, random number generation, and the expected value of outcomes based on the matrix configuration. The conversation includes technical aspects of coding in MATLAB and statistical reasoning regarding the results of the simulation.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant identifies that changing the logical operator from 'or' to 'and' resolves part of the infinite loop issue.
  • Another participant notes that the random number generator consistently produces the same value, affecting the outcome of the simulation.
  • A later post suggests that restarting MATLAB was necessary to generate different random numbers, indicating a potential issue with variable naming.
  • Concerns are raised about the expected value of the simulation results, with one participant questioning the assumption that the expectation should be close to zero based on the matrix's symmetry.
  • Another participant reflects on the variance of the results, suggesting that the number of samples should be considered when evaluating statistical outcomes.
  • One participant proposes that if hitting -1 or 1 is equally likely, a simpler method like a coin flip could replace the current simulation approach.

Areas of Agreement / Disagreement

Participants express differing views on the expected value of the simulation results and the implications of variance. There is no consensus on whether the results should be close to zero or on the best method for achieving the simulation's goals.

Contextual Notes

Participants mention issues related to variable naming that may affect the random number generation, as well as the need for a clearer understanding of statistical variance in relation to sample size.

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?
 

Similar threads

Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K