Index out of bound because numel(w)=11

  • Thread starter Thread starter ria91
  • Start date Start date
  • Tags Tags
    Bound Index
Click For Summary
SUMMARY

The forum discussion addresses an "index out of bounds" error encountered while converting a Fortran program to MATLAB for an absorbing boundary model. The error arises from attempting to access an element of the array 'w' that has not been properly initialized, specifically when 'w(i)' is referenced without being declared in the loop. The user resolves the issue by correctly initializing 'w(i)' within the loop and replacing 'x' with 'z' in the limit conditions for 'lim3' and 'lim4'. The corrected code successfully executes without errors.

PREREQUISITES
  • Familiarity with MATLAB programming and syntax
  • Understanding of array indexing and bounds in MATLAB
  • Basic knowledge of Fortran programming concepts
  • Experience with mathematical modeling and boundary conditions
NEXT STEPS
  • Review MATLAB array initialization techniques
  • Learn about error handling in MATLAB to manage runtime errors
  • Explore mathematical modeling in MATLAB for boundary value problems
  • Investigate the differences between Fortran and MATLAB syntax for variable declarations
USEFUL FOR

This discussion is beneficial for MATLAB programmers, software developers transitioning from Fortran, and researchers working on mathematical modeling and simulations involving boundary conditions.

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
 

Similar threads

Replies
5
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
11
Views
3K