Why is my Matlab infinite loop not working properly?

In summary, the problem with the code is that it is infinite because an and statement should have been used, not an or statement. The program is trying to move around the matrix randomly, but it always hits the same number - 8.147. The program can be fixed by changing the code so that a coinflip is used instead of an and statement.
  • #1
Poisonous
26
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
  • #2
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:
  • #3
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
 
  • #4
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     [COLOR="Red"]0[/COLOR]     0     1
    -1     0     0     0    -1
     0     1    -1     1     0
 
Last edited:
  • #5
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.
 
  • #6
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?
 

Related to Why is my Matlab infinite loop not working properly?

1. What is an infinite loop in Matlab?

An infinite loop in Matlab is a loop that continues to run endlessly without ever terminating. This can happen if the loop condition is always true or if there is no condition at all. It can also occur if the loop is not properly incremented or if there are no break statements to exit the loop.

2. How do I stop an infinite loop in Matlab?

To stop an infinite loop in Matlab, you can press the "Ctrl+C" keys on your keyboard. This will interrupt the program and break out of the loop. You can also use the "break" statement within the loop to specify a condition that will terminate the loop.

3. Why is my Matlab code stuck in an infinite loop?

There are several reasons why your Matlab code may be stuck in an infinite loop. It could be due to a logical error in your loop condition or a mistake in the loop increment. It could also be caused by a large dataset or a slow algorithm that takes a long time to execute, making it seem like it is stuck in a loop.

4. How can I prevent infinite loops in Matlab?

To prevent infinite loops in Matlab, it is important to carefully check your loop conditions and make sure they are properly defined. You should also include break statements if necessary to ensure that the loop will terminate. Additionally, you can use debugging tools to step through your code and identify any potential issues.

5. Can infinite loops cause damage to my computer?

Infinite loops can cause your computer's CPU to run at full capacity, which can lead to overheating and potentially damage your computer. However, modern systems have safeguards in place to prevent this from happening. It is still important to address and fix infinite loops in your code to improve its efficiency and prevent any potential issues.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
850
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
373
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Back
Top