Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab: out of bounds.

  1. Mar 6, 2010 #1
    Hey, I have defined
    Code (Text):
    pu = zeros(nx,ny,N);
    pv = zeros(nx,ny,N);
    pu(:,ny,:) = 1;
    and written the loop:
    line 38-40
    Code (Text):
     for i = 2:(nx-1);
            for i = 2:(ny-1);
                ps(i,j) = p(i,j,n) - a1*(pu(i+1,j,n) - pu(i,j,n)) -a2*(pv(i,j+1,n) - pv(i,j,n));
    and I get the error
    Could someone help me with the error?
  2. jcsd
  3. Mar 6, 2010 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well, obviously, you tried to access p(2,11,1) which does not exist.

    Looking at the indexes, it was probably pv(i,j+1,n), since that goes highest int he middle term.

    Now... what range does j iterate over?
  4. Mar 6, 2010 #3
    Thanks for fast reply and solution.
    The code
    Code (Text):
    for i = 2:(nx-1);
            for i = 2:(ny-1);
    was supposed to be
    Code (Text):
    for i = 2:(nx-1);
            for j = 2:(ny-1);
    and I had used the j as variable earlier.
    Last edited: Mar 6, 2010
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook