Array indices must be positive integers or logical values.

1 view (last 30 days)
I just simply want to use previous Re, f and V values in the next one and trying to stop iteration when it reaches the criteria i made. But for the many times i defined my array differently i couldnt solve this error message. It gives the error every time at line 31. Here is my code, any help would be much appreciated.
clear all
clc
close all
%Variables
H=8; %m, =H1
D=0.3; %m
ks=0.0001; %m
nu=0.001; %Ns/m^2
rho=1000; %kg/m^3
g=9.81; %m/s^2
gamma=rho*g;
deltat=0.02; %s
T=60; %s
E=0.01; %criteria for steady state
N=10^10; %time to iterate until finding stady-state velocity
%1-) Steady State Velocity, Vs
%writing Bernoulli Eq. between (1) and (3);
Vsteady=sqrt(H*2*g); %m/s,steady-state velocity
Resteady=Vsteady*D*rho/nu; %Reynolds Number
fsteady=0.25/(log((ks/3.7*D)+(5.74/Resteady^0.9)))^2;
Qsteady=Vsteady*(pi()*D^2)/4;
for j=1
V(j)=Vsteady;
Re(j)=Resteady;
f(j)=fsteady;
for i=1:deltat:N
for L=50
Re(i)=V(i)*D*rho/nu; %Reynolds Number
f(i)=0.25/(log((ks/3.7*D)+(5.74/Re(i)^0.9)))^2;
V(i+1)=V(i)+deltat.*(H-(1+(f(i)*L*V(i)^2)./(D*2*g))).*(g./L);
Q=(V(i+1))*(pi()*D^2)/4; %m^3/s
if abs((V(i+1)-V(i)) ./ (V(i)))==E
break
end
%writing Bernoulli Eq. between (1) and (2);
P2=gamma*H-(gamma*Vsteady^2)/(2*g)-(fsteady*L*Vsteady^2*gamma)/(D*2*g); %H1=H2
end
end
end

Answers (2)

Sindar
Sindar on 16 Feb 2020
You are trying to access Re(i),V(i),etc. with i=1.02. There doesn't seem to be anywhere that you use i as a number rather than index, so try replacing
for i=1:deltat:N
with:
for i=1:N

James Heselden
James Heselden on 16 Feb 2020
So the error comes from the line:
Re(i)=V(i)*D*rho/nu; %Reynolds Number
Where your i value on the second iteration is 1.02
i =
1.020
You cannot take the 1.02 index of an array, this must be a integer. the issue isnt with your array but with your for loop on line 29:
for i=1:deltat:N
...
end
I do not know much on the specifics in what your code is for, but you have 2 options to solve the issue:
1. Take the floor() / ceil() / round() of your index.
Re(i)=V(i)*D*rho/nu; %Reynolds Number
%becomes
Re(floor(i))=V(floor(i))*D*rho/nu; %Reynolds Number
2. Add a secondary index to identify the location in your array. and rpelace every instance of indexing with (i) to (idx).
idx = 0; %Initialise index to 0
for i=1:deltat:N
idx=idx+1; %Increment index
...
Re(idx)=V(idx)*D*rho/nu; %Reynolds Number
...
Given the context the latter would be the most appropriate.
Hope that helps :)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!