Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Index out of bounds error
Replies: 4   Last Post: Feb 6, 2013 2:32 PM

 Messages: [ Previous | Next ]
 dpb Posts: 9,850 Registered: 6/7/07
Re: Index out of bounds error
Posted: Feb 5, 2013 7:40 PM

On 2/5/2013 3:32 PM, Nicholas wrote:
> I was curious if you could figure out why I am getting an index out of
> bounds error. ...

...
> Nx = input('Designate number nodes: ');
...
> u = ones(1,Nx); %initial condition
...
> for tstep = 1:Nt
> u_o = u;
>
> d(1:Nx) = (1+nu); %diagonal

a(1:Nx-1) = (-nu/2); %lower diagonal
> c(1:Nx-1) = (-nu/2); %upper diagonal
>
> for i=2:Nx-1
> b(i) = (1-nu)*u_o(i) + (nu/2)*u_o(i-1) + (nu/2)*u_o(i+1);
> end
>

...

> The inputs are 51 nodes and 560 time steps
> error:
> Attempted to access u_o(51); index out of bounds because numel(u_o)=50.
> Error in hw3_CN (line 35)
> b(i) = (1-nu)*u_o(i) + (nu/2)*u_o(i-1) + (nu/2)*u_o(i+1);

Well, superficially it looks as though if that were the case numel(u_0)
would be 51, not 50 in the error message so one must presume either
it's not the code posted, precisely, or the inputs aren't as described.

Set

debug on error

and then you can see what's going on when it aborts and the problem will
likely become embarrassingly clear (coding or input errors frequently
are self-humbling :) ).

--

Date Subject Author
2/5/13 dpb
2/6/13 dpb
2/6/13 dpb