How can I update the value of P after each iteration for comparison?

조회 수: 3 (최근 30일)
Purva Nagvekar
Purva Nagvekar 2018년 10월 1일
답변: KSSV 2018년 10월 1일
V=20;
I=8;
P=V*I;
for i=2:4
V(i)=input('Voltage=')
I(i)=input('Current=')
    D(i)=0.5
    d(i)=0.01
    P(i)=V(i)*I(i);
    if P(i)>P
        D(i)=D(i)+d(i)
    elseif P(i)==P
        D(i)=D(i)
    else
        D(i)=D(i)-d(i)
    end
    P(i)
    P=P(i)+P;
end

답변 (1개)

KSSV
KSSV 2018년 10월 1일
V=20;
N = 4 ;
P = zeros(1,N) ;
I = P ;
D = P ;
d = P ;
V = P ;
P(1)=V*I;
I(1) = 8 ;
for i=2:4
    V(i)=input('Voltage=') ;
    I(i)=input('Current=') ;
    D(i)=0.5 ;
    d(i)=0.01 ;
    P(i)=V(i)*I(i);
    if P(i)>P(i-1)
        D(i)=D(i)+d(i)
    elseif P(i)==P
        D(i)=D(i)
    else
        D(i)=D(i)-d(i)
    end
    P(i)
    P(i)=P(i)+P;
end

YOur code is a mess.....what you want can be achieved without loops also.

카테고리

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