error of Subscript indices must either be real positive integers or logicals, index exceeds matrix dimension,

1 view (last 30 days)
I am a total noob in matlab and i am trying to write a code for water hammer simulation in a series pipe system, and the error i am getting is index exceeds matrix dimension, apparently for this line of code v1(1,t+dt1)=((((-a1)^2)*v1(2,t+dt1))/(h1(2,t+dt1)-h1(1,t+dt1))*g-a1^2); and also i am getting the error of Subscript indices must either be real positive integers or logicals, for the code h1(1,t+dt1)=Zu; this are both inlet boundary conditions. Zu has been defined earlier with a value of 40.
would be grateful if anyone can guide me through this or point towards something that can help me.
%property
L1=2720;% m, length of pipe 1
L2=2652;% m, length of pipe 2
ts=500; % s, final simulation time
dia1=0.5;%m, dia of pipe 1
t=0;
dia2=0.4;%m, dia of pipe 2
a1=1000;%m/s, wavespeed in pipe 1
a2=1200; %m/s, wavespeed in pipe 2
Q=0.05; %m^3/s, discharge in pipe
Zu=40;%m, u/s constant water level
v1=4*Q/(pi*dia1^2); %m/s, flow velocity in pipe 1
v2=4*Q/(pi*dia2^2); %m/s, flow velocity in pipe 2
dx1=100; %m, spacing for pipe 1
n=2; % no of elements
dt1=dx1/(a1*n); %s, time step
if dx1>=(a1+v1)*dt1
message1= sprintf('dx1 is ok');
else
message2= sprintf('change dx1 value');
end
dx2=100; %m, spacing for pipe 2
dt2=dx2/(a2*n);%s, time step for pipe 2
if dx2>=(a2+v2)*dt2
message1= sprintf('dx2 is ok');
else
message2= sprintf('change dx2 value');
end
x1=0:dx1:L1;
x2=0:dx2:L2;
x=[x1 x2];
t1=0:dt1:ts;
t2=0:dt2:ts;
%connection section boundary
h1(1)=Zu; %initial head, constant head level
v1(1,t+dt1)=((((-a1)^2)*v1(2,t+dt1))/(h1(2,t+dt1)-h1(1,t+dt1))*g-a1^2);

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 26 Mar 2019
Edited: KALYAN ACHARJYA on 26 Mar 2019
There may be multiple error, I have noticed one immeditely
You have defined dt1 as
dt1=dx1/(a1*n);
It return the value
dt1 =
0.050000000000000
Again
You have defined v1 as following, which is scalar
>> v1=4*Q/(pi*dia1^2)
v1 =
0.254647908947033
At the last line (Numerator) calling the v1 as vector
*v1(2,t+dt1)
%at initial t define as
t=0;
What does it mean? See it becomes v1(2,0+0.05) , which goes to v1(2,0.5)
Here two issue
  1. As your v1 is scalar based on the data as you have defined, there is no menaing of v1(?,?)
  2. If v1 is vector, please ensure that v1(r,c), these r and c values always greater that 0 (real positive) within the specified rannge,
If r and c are not real and positive integers than Matlab shows the error
error of Subscript indices must either be real positive integers or logicals
And if the r and c value out of range (dimension), Matlab shows the error-
index exceeds matrix dimension.
Please debug the code step by step and find the issue. Indeed this is easy, hope you can do it.
Good Luck & Hope it Helps!

More Answers (0)

Community Treasure Hunt

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

Start Hunting!