# Index out of bound because numel(w)=11

1. Oct 14, 2012

### ria91

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?

2. Oct 14, 2012

### Bill Simpson

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. Oct 15, 2012

### ria91

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