Array indices must be positive integers or logical values. This error is coming at line:while C(counter)> C(j) && abs(f)>.00001. Please help me out.

2 ビュー (過去 30 日間)
D=800; d=[1 600 09 0.02 300 500; 2 500 10 0.04 100 400; 3 400 06 0.08 050 200]; g=1; P1=400; P2=300; P3=100; del_C1=.04*P1-.16*P3+2; del_C2=.08*P2-.16*P3+4; counter=1; C=15900; while C(counter)> C(j) && abs(f)>.00001 if sum(del_C1,del_C2)<0 && del_C1>del_C2 P1=P1+1; P2=P2; P3=P3-1; P=[P1;P2;P3]; f=D-sum(P); C=d(:,2)+d(:,3).*P+d(:,4).*P.*P; counter=counter+1; j=counter;

採用された回答

Image Analyst
Image Analyst 2018 年 9 月 23 日
You forgot to assign j before the loop. Fix:
D=800;
d=[1 600 09 0.02 300 500;
2 500 10 0.04 100 400;
3 400 06 0.08 050 200];
g=1;
P1=400;
P2=300;
P3=100;
del_C1=.04*P1-.16*P3+2;
del_C2=.08*P2-.16*P3+4;
counter=1;
C=15900;
j=counter;
while C(counter)> C(j) && abs(f)>.00001
if sum(del_C1,del_C2)<0 && del_C1>del_C2
P1=P1+1;
P2=P2;
P3=P3-1;
P=[P1;P2;P3];
f=D-sum(P);
C=d(:,2)+d(:,3).*P+d(:,4).*P.*P;
counter=counter+1;
j=counter;
end
end
msgbox('Done');
Also, read this link so you know how to format your code in Answers.
  1 件のコメント
Rashedul Chy
Rashedul Chy 2018 年 9 月 23 日

Thanks for replying sir. Can u tell me why my loop is not working. Output is showing only the initial values i assigned. Here is the full programme: D=800; d=[1 600 09 0.02 300 500; 2 500 10 0.04 100 400; 3 400 06 0.08 050 200]; g=1; P1=400; P2=300; P3=100; P=[P1;P2;P3]; del_C1=.04*P1-.16*P3+2; del_C2=.08*P2-.16*P3+4; counter=1; C=15900; j=counter; while C(counter)> C(j) && abs(f)>.00001 if sum(del_C1,del_C2)<0 && del_C1>del_C2 P1=P1+1; P2=P2; P3=P3-1; P=[P1;P2;P3]; f=D-sum(P); C=d(:,2)+d(:,3).*P+d(:,4).*P.*P; counter=counter+1; j=counter; elseif sum(del_C1,del_C2)<0 && del_C1<del_C2 P1=P1; P2=P2+1; P3=P3-1; P=[P1;P2;P3]; f=D-sum(P); C=d(:,2)+d(:,3).*P+d(:,4).*P.*P; counter=counter+1; j=counter; elseif sum(del_C1,del_C2)>0 && del_C1>del_C2 P1=P1-1; P2=P2; P3=P3+1; P=[P1;P2;P3]; f=D-sum(P); C=d(:,2)+d(:,3).*P+d(:,4).*P.*P; counter=counter+1; j=counter; elseif sum(del_C1,del_C2)>0 && del_C1<del_C2 P1=P1-1; P2=P2; P3=P3+1; P=[P1;P2;P3]; f=D-sum(P); C=d(:,2)+d(:,3).*P+d(:,4).*P.*P; counter=counter+1; j=counter; end end display(P,'Generated Power in MW') display(C,'Total Cost in $') display(counter,'Iterations numbers')

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by