Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Index out of bounds error

Subject: Index out of bounds error

From: Nicholas

Date: 5 Feb, 2013 21:32:11

Message: 1 of 8

I was curious if you could figure out why I am getting an index out of bounds error. My code is a finite difference formulation using the crank nicolson method.

Code:
%.....................1-D Heat Equation.................................
%....................Crank Nicolson Method..............................
%
%
clc;clear;
Nx = input('Designate number nodes: ');
L = 1; %Length of domain
nu = 1.5; %nu=(alpha*dt)/dx^2
Beta = 0.5;
dx = L/(Nx-1); %Distance between nodes
dt = nu*(dx^2); %Time step
Nt = input('Designate number of time steps: '); %Number of time steps
t_final = Nt*dt %Final time
% x = 0:dx:L; t = 0:dt:t_final; %u = zeros(Nx,Nt);

u = ones(1,Nx); %initial condition
% u(1,1:Nt+1) = 0;
% u(Nx,1:Nt+1) = 0;
% for i = 1:Nx,
% x(i) = (i-1)*dx;
% end
 
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

a(Nx-1) = (-nu/2);
d(Nx) = (1+nu);
c(2) = (-nu/2);
b(1) = (1-nu)*u_o(1) + (nu/2)*u_o(2);
b(Nx) = (1-nu)*u_o(Nx) + (nu/2)*u_o(Nx-1);

u = CrankTridiagSolver(Nx,d,a,c,b,u);
end
u_cn = u;
-------------------------------------------------------------------------------
My function CrankTridiagSolver is shown below:

function [X] = CrankTridiagSolver(Nx, a, d, c, b, X)
% solve A*x = b
% A is tridiagonal matrix, c is the upper diagonal, d is the lower diagonal
Nx = length(d)
for i = 2:Nx
    X = a(i-1)/d(i-1);
    d(i) = d(i) - X*c(i);
    b(i) = b(i) - X*b(i-1);
end
X(Nx) = b(Nx)/d(Nx);
for i = Nx-1:-1:1 %back substitution
    X(i) = (b(i) - c(i+1)*X(i+1))/d(i);
end

Any help would be greatly appreciated.

Subject: Index out of bounds error

From: Nicholas

Date: 5 Feb, 2013 21:41:17

Message: 2 of 8

"Nicholas " <nsbakes2@gmail.com> wrote in message <kertor$pv$1@newscl01ah.mathworks.com>...
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);

Subject: Index out of bounds error

From: dpb

Date: 6 Feb, 2013 00:40:54

Message: 3 of 8

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
>

...

 From your followup posting...

> 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 :) ).

--

Subject: Index out of bounds error

From: Nicholas

Date: 6 Feb, 2013 05:57:08

Message: 4 of 8

I'm extremely new to programming and have virtually no experience with matlab. I've tried going through the debug mode with no luck. The b variable is the matrix "b" in Ax =b. A is the tridiagonal matrix. My code is telling me Nx =50 when it runs even though I'm specifying 51 as the input.

Subject: Index out of bounds error

From: Nicholas

Date: 6 Feb, 2013 06:02:05

Message: 5 of 8

I'm extremely new to programming and have very limited experience in matlab. I've gone through the debug mode with no luck. It doesn't make sense that, even though I'm inputting 51 as the number of nodes, my code is telling me Nx = 50.

Subject: Index out of bounds error

From: dpb

Date: 6 Feb, 2013 14:10:39

Message: 6 of 8

On 2/6/2013 12:02 AM, Nicholas wrote:
> I'm extremely new to programming and have very limited experience in
> matlab. I've gone through the debug mode with no luck. It doesn't make
> sense that, even though I'm inputting 51 as the number of nodes, my code
> is telling me Nx = 50.

You must have modified it somewhere then...set a breakpoint at the point
after it's been entered and then step and see where it's modified. Or,
put in some strategic disp() statements and/or remove trailing ';' to
have output echo'ed to the console to see where things go south.

Check it's what you're actually running that you think it is...try

which (your_script/function_name)

at the command line to be certain it's not an aliased copy accidentally
saved earlier. If you've modified it, be sure you're running the
modified copy to be sure you've saved and in proper subdirectory and then

clear (your_script/function_name)

to make the new version be loaded instead of, perhaps a cached old
version still in memory...

--

Subject: Index out of bounds error

From: dpb

Date: 6 Feb, 2013 19:32:27

Message: 7 of 8

On 2/6/2013 12:02 AM, Nicholas wrote:
> I'm extremely new to programming and have very limited experience in
> matlab. I've gone through the debug mode with no luck. It doesn't make
> sense that, even though I'm inputting 51 as the number of nodes, my code
> is telling me Nx = 50.

Well, it's not actually telling you that, precisely. The actual error
is that numel(u_0) == 50. That's not _quite_ the same thing as Nx==50.
  Perhaps inadvertently in your code an element in u_0 was deleted so
the array isn't the same length as Nx.

Remember in debugging to look at precisely what the code says w/o
allowing yourself to read into it what you intended. Somewhere I'll bet
you've made a simple "off-by-one" mistake if the problem wasn't traced
back to the previous suggestion of perhaps the code you're running isn't
the same as that you're looking at.

If you still are having trouble and can make a self-contained case that
shows the problem that cs-sm readers can cut'n paste into their command
line, then surely somebody will be able to pick up on where the problem
is. Unfortunately, in what you've posted so far it does appear as
though it should be ok (at least to my old eyes)...

--

Subject: Index out of bounds error

From: Nicholas

Date: 8 Feb, 2013 20:50:11

Message: 8 of 8

Thanks for the suggestions. The probelm ended up being that the upper and lower diagonals defined in the tridiagonal matrix solver were missing an element.
 
dpb <none@non.net> wrote in message <keub3k$jl6$1@speranza.aioe.org>...
> On 2/6/2013 12:02 AM, Nicholas wrote:
> > I'm extremely new to programming and have very limited experience in
> > matlab. I've gone through the debug mode with no luck. It doesn't make
> > sense that, even though I'm inputting 51 as the number of nodes, my code
> > is telling me Nx = 50.
>
> Well, it's not actually telling you that, precisely. The actual error
> is that numel(u_0) == 50. That's not _quite_ the same thing as Nx==50.
> Perhaps inadvertently in your code an element in u_0 was deleted so
> the array isn't the same length as Nx.
>
> Remember in debugging to look at precisely what the code says w/o
> allowing yourself to read into it what you intended. Somewhere I'll bet
> you've made a simple "off-by-one" mistake if the problem wasn't traced
> back to the previous suggestion of perhaps the code you're running isn't
> the same as that you're looking at.
>
> If you still are having trouble and can make a self-contained case that
> shows the problem that cs-sm readers can cut'n paste into their command
> line, then surely somebody will be able to pick up on where the problem
> is. Unfortunately, in what you've posted so far it does appear as
> though it should be ok (at least to my old eyes)...
>
> --

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us