Problem with While loop

Hello friends, I want to ask for help in the following code, and I use the while loop to determine the vectors must be below a value but I generated will last value that is not less, that I make only store the while smaller and open. Thank you.
clear
clc
H=[0 250 500 750 1000 1250 1500 1750 2000 2250 2500 2750 3000 3500 3750 4000 5000];
B=[0 0.4 0.8 1 1.15 1.25 1.32 1.38 1.41 1.44 1.46 1.48 1.5 1.53 1.54 1.55 1.6];
%******************************************
A=27e-4;
l=[0.8 0.8 0.6];
N=2000;
b=length(B);
fmax=B(b)*A;
flujo1=0:fmax/30:fmax;
B1=flujo1/A;
H1=interp1(B,H,B1,'pchip');
H2=(H1*l(1))/l(2);
B2=interp1(H,B,H2,'pchip');
f2=B2*A;
f3=flujo1+f2;
B3=f3/A;
H3=interp1(B,H,B3,'pchip');
I=(H3*l(3)+H2*l(2))/N;
i=1;e=0;
while e<max(B)
Fr1(i)=flujo1(i);
Fr2(i)=f2(i);
Fr3(i)=f3(i);
Ir(i)=I(i);
e=B3(i);
i=i+1;
end
hold on
plot(Ir,Fr1,'+')
plot(Ir,Fr2,'r')
plot(Ir,Fr3,'-o')
grid on
legend ('Flujol','Flujo2','Flujo3')
title('CURVA DE EXCITACIÓN CORRIENTE vs FLUJO')
xlabel('Corriente(A)')
ylabel('Flujo(Wb)')
pause
hold off

Respuestas (1)

Walter Roberson
Walter Roberson el 20 de Sept. de 2015

0 votos

last_e = e;
while e<max(B)
Fr1(i)=flujo1(i);
Fr2(i)=f2(i);
Fr3(i)=f3(i);
Ir(i)=I(i);
last_e = e;
e=B3(i);
i=i+1;
end
e = last_e;
Now after the loop, e will be the last value for which the condition was true, rather than being the first value for which the condition was false.

3 comentarios

fabian
fabian el 20 de Sept. de 2015
It does not work, what should it only store values to condicon, not one after the condition, you must store 16 values, not 17 as it is doing. Thank you
Walter Roberson
Walter Roberson el 20 de Sept. de 2015
The condition is not changed until you do
e=B3(i);
so the code is storing only the values before the condition.
Perhaps you wanted to change your condition.
Fr1 = [];
Fr2 = [];
Fr3 = [];
Ir = [];
i=1;
while B3(i) < max(B)
Fr1(i) = flujo1(i);
Fr2(i) = f2(i);
Fr3(i) = f3(i);
Ir(i) = I(i);
i=i+1;
end
You could make this much more compact:
idx = find(B3 >= max(B), 1, 'first');
i = idx - 1;
Fr1 = flujo1(1:i);
Fr2 = f2(1:i);
Fr3 = f3(1:i);
Ir = I(1:i);
fabian
fabian el 20 de Sept. de 2015
Thank you !!!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 20 de Sept. de 2015

Comentada:

el 20 de Sept. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by