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

3 Ansichten (letzte 30 Tage)
Purva Nagvekar
Purva Nagvekar am 1 Okt. 2018
Beantwortet: KSSV am 1 Okt. 2018
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

Antworten (1)

KSSV
KSSV am 1 Okt. 2018
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.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by