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

3 visualizaciones (últimos 30 días)
Purva Nagvekar
Purva Nagvekar el 1 de Oct. de 2018
Respondida: KSSV el 1 de Oct. de 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

Respuestas (1)

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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by