Loop is not working. My while loop is not working. output is initial values here. Can anyone help??

조회 수: 5 (최근 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;
P=[P1;P2;P3];
f=1;
del_C1=.04*P1-.16*P3+2;
del_C2=.08*P2-.16*P3+4;
counter=1;
C=15900;
j=.1;
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;
        del_C1=.04*P1-.16*P3+2;
        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
    del_C1=.04*P1-.16*P3+2;
    del_C2=.08*P2-.16*P3+4;
end
display(P,'Generated Power in MW')
display(C,'Total Cost in $')
display(counter,'Iterations numbers')

채택된 답변

Dennis
Dennis 2018년 9월 24일
편집: Dennis 2018년 9월 24일
C is not a vector/matrix it is a scalar. C(counter) can not work for any value of counter >1.
C(0.1) will not work either.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by