why is there an error about index out?

I think my code is fine and check for several times. But there is still a problem about index. Can someone help me? I am a beginner. The code is as below:
clear all; close all;
ns=20;%step number
dx=-100;
dz=0.05;
Q=200;
b=50;
g=9.81;
n=0.025;
S=0.0005;
x_0=0;
y_0=4.62;
z_0=0;
A_0=y_0*b;
P_0=2*y_0+b;
R_0=A_0/P_0;
V_0=Q/A_0;
H1_0=y_0+z_0+V_0^2/2/g;
Sf_0=V_0^2*n^2/R_0^(4/3);
k=1:ns+1;
x=dx*(k-1);
z=dz*(k-1);
Sf=zeros(1,length(k));Sf(1)=Sf_0;
H1=zeros(1,length(k));H1(1)=H1_0;
yy=zeros(1,length(k));yy(1)=y_0;
for i=1:ns;
for j=1:100;
y=y_0-j*0.01;
A=y*b;
P=2*y+b;
R=A/P;
V=Q/A;
H1(i+1)=z(i+1)+y+V^2/2/g;
Sf1=V^2*n^2/R^(4/3);
Sf(i+1)=Sf1;
Sf_avg=(Sf(i)+Sf(i+1))/2;
avg=Sf_avg*dx;
H2=H1(i)-avg;
while abs(H2-H1(i+1))<0.005
yy(i+1)=y;
i=i+1;
end
end
end

3 Comments

Which line does the error occur on?
on the line: while abs(H2-H1(i+1))<0.005
If you omit the clear all, you can set a breakpoint in the failing line or let Matlab stop there automatically:
dbstop if error
Then you can investigate the reasons of the problem much easier.
Killing the breakpoints by clear all is a bad idea, because the debugger is the friend of the programmer.

Answers (2)

While you are increasing i in the while-loop there is the possibility of reaching an i such that i+1 is beyond the end of array y in the line "yy(i+1)=y;" or beyond the end of H1 in the condition "abs(H2-H1(i+1))<0.005". This would produce an error message about improper indexing.
You should be aware that changing i within the for-loop which indexes with i is not good programming practice. Mathworks' documentation states "Avoid assigning a value to the index variable within the body of a loop. The for statement overrides any changes made to the index within the loop."

3 Comments

Thank you! That should be the problem. Can you tell me way to revise it? What can I do if I don't change i in the loop?
I suggest you use another index, ii, in place of i which starts out at the current i value for indexing within the while-loop, and place the additional condition that ii cannot reenter the while-loop with a value greater than ns. That would guarantee that your particular indexing difficulty could not occur. I have used the "short circuit" && to avoid trouble with H1(ii+1) in that while condition.
I cannot promise that other difficulties will not become apparent when the indexing problem is corrected. I am uncertain as to what your code is meant to accomplish.
.......
H2=H1(i)-avg;
ii = i; % Change i over to the index ii
while ii<=ns && abs(H2-H1(ii+1))<0.005 % First check that ii<=ns
yy(ii+1)=y;
ii=ii+1;
end
.....
Thank you all the same! But when I do this, the loop doesn't work well any more. However, since the data I need is yy, I could simply save yy in a mat file in the loop. But the solution to this mistake still confuses me a lot.
Dongyu
Dongyu on 15 Nov 2013
I found out that if I put i<ns-1 as a condition, while i<ns-1 && abs(H2-H1(i+1))<0.005 it will be lack of the last term, but if I construct a new loop, only to calculate the last term, the program works well. It's kinda stupid though.

This question is closed.

Tags

Asked:

on 15 Nov 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!