Array indices must be positive integers or logical values.

5 次查看(过去 30 天)
I keep getting the error "Array indices must be positive integers or logical values" on every equation within the for loop and I'm not sure what I'm doing wrong. I've looked through other posts with this error and I can't seem to find my error
clear
clc
% Set known variables
rho = 800; % kg/m^3
mu = 0.00035; % N*s/m^2
Q = 2; % m^3/s
n = 25; % unitless
g = 9.81; % m/s^2
L = 160934; % m
m_dot = rho*Q;
H = L/(m_dot*g);
D = 1:0.1:10;
for n = 1:length(D)
P(D) = (128*mu*(Q^2)*L)./(pi*(D.^4));
CQ(D) = (P)./(rho*(n^3)*(D.^5));
CH(D) = (g*H)./((n^2)*(D.^2));
CQ(D) = Q./(n*(D.^3));
end

回答(1 个)

madhan ravi
madhan ravi 2020-6-2
Left hand side of the equation (n) and in right hand side D(n).
And Ofcourse you don’t need a loop here ;)
  2 个评论
madhan ravi
madhan ravi 2020-6-2
% Set known variables
rho = 800; % kg/m^3
mu = 0.00035; % N*s/m^2
Q = 2; % m^3/s
n = 25; % unitless
g = 9.81; % m/s^2
L = 160934; % m
m_dot = rho*Q;
H = L/(m_dot*g);
D = 1:0.1:10;
n = 1:numel(D);
P = (128*mu*(Q^2)*L)./(pi*(D.^4));
CQ = P ./(rho*(n.^3).*(D.^5));
CH = (g*H)./((n.^2).*(D.^2));
CQ = Q./(n.*(D.^3));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Customize Object Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by