Index out of bound because numel(w)=11

In summary, the program keeps giving an error saying: ? Attempted to access w(12); index out of bounds because numel(w)=11.
  • #1
ria91
3
0
Hello,,
I try to convert a fortran program to matlab. I want to make an absorbing boundary model. But when I run it, I keep getting an error says:
? Attempted to access w(12); index out of bounds because numel(w)=11.
Error in ==> absorb_bound_coba at 45
w(i)=exp(-1./d.^2.*(x(i)-lim4).^2*w(i));
%input
width=20
n=importdata('node.txt');
x=n(:,2);z=n(:,3);
%define constant
d=0.5*max(x);
w=1;
%computing boundary constant
boundary_width_x=width/100.*(max(x)+abs(min(x)));
boundary_width_z=width/100.*(max(z)+abs(min(z)));

lim1=min(x)+boundary_width_x;
lim2=max(x)-boundary_width_x;
lim3=min(z)+boundary_width_z;
lim4=max(z)-boundary_width_z;

%rectangular model
for i=1:length(x);
if x(i)<=lim1
w(i)=exp(-1./d.^2.*(x(i)-lim1).^2);
elseif x(i)>=lim2
w(i)=exp(-1./d.^2.*(x(i)-lim2).^2);

elseif z(i)<=lim3
w(i)=exp(-1./d.^2.*(x(i)-lim3).^2)*w(i);
elseif x(i)>=lim4

w(i)=exp(-1./d.^2.*(x(i)-lim4).^2)*w(i);
end;
end;

So how I can fix this?
Thanks in advance :D
 
Physics news on Phys.org
  • #2
I am guessing a bit here because I don't think you have shown all the code.

Did you perhaps not make w() large enough when you declared it?
Can you show how you declared w() and perhaps even print that as part of the run?

Another thing that worries me is you have
if x(i)<=lim1
dosomething
elseif x(i)>= lim2
dosomethingelse
elseif x(i)<=lim3
dosomethingdifferent
elseif x(i)>=lim4
dosomethingverydifferent

I'm wondering if you wanted those to be between lim2 and lim3 and between lim3 and lim4 but the code doesn't seem to say that.
 
  • #3
Hello Bill,,
Yeah, I know where's the mistakes now. I didn't declare w(i) inside the loop. And I made some mistakes in typing. I should type z in limit3 and limit4 instead of x. So when I write the script like below
%rectangular model
for i=1:length(x);
w(i)=1;
if x(i)<=lim1
w(i)=exp(-1./d.^2.*(x(i)-lim1).^2);
elseif x(i)>=lim2
w(i)=exp(-1./d.^2.*(x(i)-lim2).^2);
elseif z(i)<=lim3
w(i)=exp(-1./d.^2.*(z(i)-lim3).^2)*w(i);
elseif z(i)>=lim4
w(i)=exp(-1./d.^2.*(z(i)-lim4).^2)*w(i);
end;
end;

and it works! Thanks!

And you're right, I didn't show all the code. I just post a part where problem occurred.
Thanks again
 

1. What does "Index out of bound because numel(w)=11" mean?

This error message indicates that you are trying to access an element in an array or matrix at an index that is outside of its bounds. In this case, the array or matrix has a size of 11 elements, so the valid indices are from 1 to 11. Trying to access an element at an index greater than 11 or less than 1 will result in this error.

2. How do I fix the "Index out of bound because numel(w)=11" error?

To fix this error, you need to ensure that you are using valid indices when accessing elements in your array or matrix. Make sure that the index is within the bounds of the array or matrix (in this case, between 1 and 11). You can also try using the "try-catch" statement to handle the error and prevent your code from crashing.

3. Can this error occur in any programming language?

Yes, this error can occur in any programming language that uses arrays or matrices. It is a common error that can happen when trying to access elements in an array or matrix outside of their bounds.

4. Why is it important to avoid "Index out of bound" errors?

Index out of bound errors can cause your code to crash or produce incorrect results. They can also be difficult to debug and can waste a lot of time. It is important to avoid these errors by carefully checking the indices used when accessing elements in an array or matrix.

5. How can I prevent "Index out of bound" errors from happening?

To prevent "Index out of bound" errors, you can use conditional statements to check if the index is within the bounds of the array or matrix before accessing the element. You can also use built-in functions or libraries that handle indices automatically and prevent out of bound errors. It is also important to carefully review your code and ensure that all indices are correct before running it.

Similar threads

  • Advanced Physics Homework Help
Replies
1
Views
846
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
Replies
1
Views
745
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Calculus and Beyond Homework Help
Replies
14
Views
670
Replies
11
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Replies
9
Views
954
Back
Top