Index out of bound because numel(w)=11

  • Thread starter Thread starter ria91
  • Start date Start date
  • Tags Tags
    Bound Index
AI Thread Summary
The discussion revolves around converting a Fortran program to MATLAB for an absorbing boundary model. The user encountered an "index out of bounds" error when trying to access an element of the array 'w'. The issue was identified as a result of not properly initializing 'w(i)' within the loop and mistakenly using 'x' instead of 'z' in the boundary conditions. After correcting these mistakes by initializing 'w(i)' and adjusting the limits to use 'z', the code functioned correctly. The user expressed gratitude for the assistance received.
ria91
Messages
3
Reaction score
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
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.
 
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
 
Back
Top