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

2 次查看(过去 30 天)
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