Index exceeds matrix dimensions

% flow m3/s
Q = 20;
% slope m/m
So = 0.0001;
% side slope m/m
z = 2;
% manning number
n = 0.015;
b = 10; % width of channel m
g = 9.81; % gravity force m/s2
N = 4;
ys = 1;
yend = 2;
dy = abs((ys-yend)/N);
y(1) = ys;
% A(1) = ;x
for i = 1:N+1
if i == 1
y(i) = ys;
else
y(i)= y(i-1) + dy;
end
A(i) = (b + y(i)*z)*(y(i));
P(i) = b + 2*y(i)*sqrt(1+z^2);
R(i) = A(i) / P(i);
V(i) = Q / A(i);
E(i) = y(i) + V(i)^2 / (2*g);
if i == 1;
dE(i) = 0;
else
dE(i) = E(i)-E(i-1);
end
dE
Se(i) = (V(i)^2)* (n^2)/(R(i)^(4/3))
if i > 1
Sav(i) = (Se(i) + Se(i+1))/2
else
Sav(i) = 0
end
if i > 1
dx = dE(i)/(So - Sav(i))
else
dx = 0
end
if i > 1
x (i) = dx(i) + dx(i-1);
else
x(i)= 0
end
end
its giving me on line where dE(i) =E(i) -E(i+1)
Sav(i) =(Se(i) + Se(i+1))/2
index exceed matrix dimension
How do i solve the issue???
thank you

6 Comments

When sections of your code are highlighted like in the image below, click on those sections and after a second or so, a message will appear that suggests improvements. If the message doesn't appear, you can press ctrl+m. Pay attention to those messages because there are lots of messy sections of the code.
190929 115610-MATLAB R2019b - academic use.png
The problem is here
E(i+1)
E only has 2 elements but i+1 equals 3 which means you're asking for the 3rd element.
Adam Danz,
thank you for the precious comment.
I already figure it out and replaced E(i+1) with E(i-1) to calculate the average of E at different y
but how do i tranform my data or the variable which are y(i) , A(i), p(i) into matrics and i need a code to plot the values of x vs. y
and thank you
After you make the E(i-1) change you also need to make the Se(i-1) change. Then your code runs without error. Then 'y' 'A' and 'P' are all vectors of length 5. 'x' don't exist in your code.
Thank u mr. Adam Yes i know about the Se also. Regarding x, i will continue my code and see ur opinion.
Mr. Adam,
i already correct the continued and finished it.
if i want to put the variable of
y A P R V E dE Se Sav dx x
in a matrix in for of a table .. what is the required code.??
this process is called water surface profile using direct step method.(Civil Engineering).
also i need to plot the vales of x with respect to y.???
if you just help me i will be gratefull.
See answer.

Sign in to comment.

 Accepted Answer

E(i) = y(i) + V(i)^2 / (2*g);
if i == 1;
dE(i) = 0;
else
,dE(i) = E(i)-E(i+1); ,
end
You are defining E(i) in the for loop, and later in the same for loop you are referencing to E(i+1) (before it was definied). Where the hell prograsm should know what E(i+1) would be in the next circle when you will define it?
This code is incorrect.

7 Comments

Thank u for ur time. This code is correct but needs to be edited.
It's up to you.
Mr. Csaba
i already correct the continued and finished it.
if i want to put the variable of
y A P R V E dE Se Sav dx x
in a matrix in a form of a table .. what is the required code.??
this process is called water surface profile using direct step method.(Civil Engineering).
also i need to plot the vales of x with respect to y.???
if you just help me i will be gratefull.
Csaba
Csaba on 30 Sep 2019
Edited: Csaba on 30 Sep 2019
I guess you want something like this!
% flow m3/s
Q = 20;
% slope m/m
So = 0.0001;
% side slope m/m
z = 2;
% manning number
n = 0.015;
b = 10; % width of channel m
g = 9.81; % gravity force m/s2
N = 4;
ys = 1;
yend = 2;
dy = abs((ys-yend)/N);
y(1) = ys;
dE(1) = 0;
Sav(1) = 0;
% A(1) = ;x
% for i = 1:N+1
% if i == 1
% y(i) = ys;
% else
% y(i)= y(i-1) + dy;
% end
for i=2:N+1
y(i)= y(i-1) + dy;
A(i) = (b + y(i)*z)*(y(i));
P(i) = b + 2*y(i)*sqrt(1+z^2);
R(i) = A(i) / P(i);
V(i) = Q / A(i);
E(i) = y(i) + V(i)^2 / (2*g);
dE(i) = E(i)-E(i-1);
Se(i) = (V(i)^2)* (n^2)/(R(i)^(4/3))
Sav(i) = (Se(i) + Se(i-1))/2;
dx(i) = dE(i)/(So - Sav(i)); % I guess you want dx(i) intead of just dx
x (i) = dx(i) + dx(i-1);
% You do not need all of this below because now (after my corrections)
% i runs from 2 and all i=1 values are assigned before the for cycle!
% all others are now listed above!
% if i == 1;
% dE(i) = 0;
% else
% dE(i) = E(i)-E(i-1);
% end
% dE
% Se(i) = (V(i)^2)* (n^2)/(R(i)^(4/3))
% if i > 1
% Sav(i) = (Se(i) + Se(i+1))/2
% else
% Sav(i) = 0
% end
% if i > 1
% dx = dE(i)/(So - Sav(i))
% else
% dx = 0
% end
% if i > 1
% x (i) = dx(i) + dx(i-1);
% else
% x(i)= 0
% end
end
plot(x,y);
thank you very much for ur correction mr Csaba.
i appreciate ur time helping me with this code.
above a fortran code
how to transfer to matlab code?
thank you

Sign in to comment.

More Answers (1)

"if i want to put the variable of
y A P R V E dE Se Sav dx x
in a matrix in for of a table .. what is the required code.??"
Those variables are row vectors of equal length and can be concatenated vertically into a matrix like this:
out = [y;A;P;R;V;E;dE;Se;Sav;dx;x];
But to follow the principles of Tidy Data, each column of a matrix should be a variable and each row an observation. The matrix would be transposed.
out = [y;A;P;R;V;E;dE;Se;Sav;dx;x].';
% ^^
Better yet, I recommend using a table
T = array2table([y;A;P;R;V;E;dE;Se;Sav;dx;x].',...
'VariableNames',{'y','A','P','R','V','E','dE','Se','Sav','dx','x'})
"also i need to plot the vales of x with respect to y."
Assuming you've created the table above,
plot(T.x,T.y,'o-')

2 Comments

Thank you mr Adam for the help.
but regarding the last code which is plot,
what is o-???
It's the linespec of the plot
It plots with lines (-) and circle markers (o).

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Release

R2016a

Tags

Asked:

on 29 Sep 2019

Commented:

on 29 Nov 2019

Community Treasure Hunt

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

Start Hunting!