how to identify the mistakes in loop in the given following code?

2 次查看(过去 30 天)
I have to find q, n value and TC. we need to find the these three values in iteration way. How to solve it?
clc
clear all
pr=7;
p=2500;
D=1600;
z=4;
zr=4;
m=10;
hr=5;
h1=12;
h2=11;
h3=13;
cL=4;
c=20;
c0=2;
c1=3;
c2=2;
ce=15;
k=0.1;
alpha=0.1;
beta=0.9;
u=1-alpha+alpha.*beta;
s=250;
A=110;
g0=15;
g1=20;
theta1=1;
theta2=0.6;
w0=0.3;
w1=0.2;
TC1=inf
TC1 = Inf
q=1:10:191
q = 1×20
1 11 21 31 41 51 61 71 81 91 101 111 121 131 141 151 161 171 181 191
n=1:1:20
n = 1×20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
j=1;
l=1;
for i=1:length(q)
n=sqrt((D.*zr.*m+s+c.*g0+(A.*((q(i).*h2)/2))))/(q(i).*(((hr+cL.*k).*((D.*q(i))/(m.*p)))+((h1.*q(i)).*((1/2)-(((2.*u-1).*D)/(2.*p.*u))-D))+(D.*ce.*g1.*theta1)+(ce.*((w1.*(1-(((2.*u-1).*D)./(2.*D.*u))).*q(i))-(q(i)./2)).*theta2)));
q=sqrt((2.*D.*(zr.*m+s+g0.*(c+n(i).*ce.*theta1)+A+n(i).*h3))./(n(i).^2.*(((hr+cL.*k).*(D./m.*p))+(h1.*(1-(((2.*u-1).*D)./(2.*p.*u)))+(ce.*(w1.*(2-(((2.*u-1).*D)./(p.*u))-1).*theta2))))));
N(i+1)=n;
Q(i+1)=q;
j=j+1;
end
Index exceeds the number of array elements. Index must not exceed 1.
N
Q
z1=1;
for z=2:1:20
if ((N(z)~=N(z-1)) | Q(z)~=Q(z-1))
z1=z1+1;
else
break
end
end
z1
j=1;
for i=1:20
Q=Q(i);
N=N(i);
TC1=(pr.*D)+(z.*(m.*D./N(i).*Q(i)))+((hr+cL.*k).*(D.*N(i).*Q(i)./2.*m.*p))+(h1.*(1-(((2.*u-1).*D)./(2.*p.*u))).*N(i).*Q(i))-(h1.*N(i).*Q(i)/2)+(s.*D./N(i).*Q(i))+(D.*A./N(i).*Q(i))+(D.*h3./Q(i))+(D.*h2./2.*N(i))+((D./u).*(c0+(c1.*alpha)+(c0.*alpha)+(c2.*(1-beta).*alpha)))+((c.*D./N(i).*Q(i)).*(g0+g1.*N(i).*Q(i)))+(((D.*ce.*N(i).*theta1)./(N(i).*Q(i))).*(g0+g1.*N(i).*Q(i)))+((w0+w1.*(((1-(((2.*u-1).*D)./(2.*p.*u))).*N(i).*Q(i))-(N(i).*Q(i)/2))).*ce.*theta2);
TC(i)=TC1;
j=j+1;
end
round(TC)
for m=2:5
if(TC1 >=TC)
TC1=TC;
else
ansn = m+1;
break
end
end
round(TC)
  6 个评论
Jan
Jan 2022-10-10
@M.Rameswari Sudha: Codes like this must fail:
for i=1:20
Q=Q(i);
N=N(i);
...
end
In the first iteration the arrays Q and N are set to the scalar value of their first elements. Then there is no 2nd element in Q and N in the next iteration. The same problem here:
for i=1:length(q)
n = sqrt((D.*zr.*m+s+c.*g0+(A.*((q(i) ...
q = ... n(i) ...
% Now q and n are scalars and in the 2nd iteration "q(i)" and "n(i)" will fail.
end
By the way, the code can be made much more readable. Comapre
j = 1;
for i=1:length(q)
n=sqrt((D.*zr.*m+s+c.*g0+(A.*((q(i).*h2)/2))))/(q(i).*(((hr+cL.*k).*((D.*q(i))/(m.*p)))+((h1.*q(i)).*((1/2)-(((2.*u-1).*D)/(2.*p.*u))-D))+(D.*ce.*g1.*theta1)+(ce.*((w1.*(1-(((2.*u-1).*D)./(2.*D.*u))).*q(i))-(q(i)./2)).*theta2)));
q=sqrt((2.*D.*(zr.*m+s+g0.*(c+n(i).*ce.*theta1)+A+n(i).*h3))./(n(i).^2.*(((hr+cL.*k).*(D./m.*p))+(h1.*(1-(((2.*u-1).*D)./(2.*p.*u)))+(ce.*(w1.*(2-(((2.*u-1).*D)./(p.*u))-1).*theta2))))));
N(i+1)=n;
Q(i+1)=q;
j=j+1;
end
with
R = (2 * u - 1) * D;
S = R / (2 * p * u);
for i = 1:length(q)
N(i+1) = sqrt(D * zr * m + s + c * g0 + A * q(i) * h2 / 2) / ...
(q(i) * ((hr + cL * k) * D * q(i) / (m * p) + ...
h1 * q(i) * (0.5 - S - D) + ...
D * ce * g1 * theta1 + (ce * q(i) * (w1 * (1 - R / (2 * D * u)) - ...
0.5) * theta2)));
Q(i+1) = sqrt((2 * D * (zr * m + s + g0 * (c + n(i) * ce * theta1) + A + ...
n(i) * h3)) / (n(i)^2 * ((hr + cL * k) * D / m * p + ...
h1 * (1 - S) + (ce * w1 * (1 - 2 * S) * theta2))));
end
j = 1 + length(q);

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2022-10-10
for i=1:length(q)
n=sqrt((D.*zr.*m+s+c.*g0+(A.*((q(i).*h2)/2))))/(q(i).*(((hr+cL.*k).*((D.*q(i))/(m.*p)))+((h1.*q(i)).*((1/2)-(((2.*u-1).*D)/(2.*p.*u))-D))+(D.*ce.*g1.*theta1)+(ce.*((w1.*(1-(((2.*u-1).*D)./(2.*D.*u))).*q(i))-(q(i)./2)).*theta2)));
% ^ ^ .... (and more)
q=sqrt((2.*D.*(zr.*m+s+g0.*(c+n(i).*ce.*theta1)+A+n(i).*h3))./(n(i).^2.*(((hr+cL.*k).*(D./m.*p))+(h1.*(1-(((2.*u-1).*D)./(2.*p.*u)))+(ce.*(w1.*(2-(((2.*u-1).*D)./(p.*u))-1).*theta2))))));
% ^ oops!
N(i+1)=n;
Q(i+1)=q;
j=j+1;
end
You're indexing q(i) assuming q is a vector which is the correct assumption on the first iteration of the loop. But then you're overwriting q and replacing the vector with a scalar. So when you index q(i) and i>1, you get an indexing error.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2009a

Community Treasure Hunt

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

Start Hunting!

Translated by