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

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.
  2 Comments
Andy
Andy on 22 Mar 2019
With only this line of code to examine, all that I can say is that it looks like t or dt1 are combining to produce an invalid number. Without seeing the rest of the code it is impossible to say how. If I get this sort of error I normally insert a statement disp(t) or leave the ; off the end of the line that sets the variable causing it to show in the command window. That usually points me in the right direction for further investigation.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 22 Mar 2019
Edited: KSSV on 22 Mar 2019
v1(1,t+dt1)=((((-a1)^2)*v1(2,t+dt1))/(h1(2,t+dt1)-h1(1,t+dt1))*g-a1^2)
In the above I could say that dt1 is a fraction...and you are tryigng to index arrays with fractions/ negatives. Note that MATLAB accepts only posittive integers and logicals as index.
A = rand(10) ;
A(1) % allowed, no error
A(10) % allowed, no error
A(1,2) % allowed, no error
A(-10) % error, index cannot be negative
A(0) % error, not allowed index cannot be zero unless it is logical.
A(1.25) % not allowed
You need to re think on your code.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!