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

3 views (last 30 days)
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

Answers (1)

KSSV
KSSV on 1 Oct 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.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!