Borrar filtros
Borrar filtros

The if loop is continuing even after the condition is false.

3 visualizaciones (últimos 30 días)
I have to find the max value of R in specified range of the SOC values and the answer should be 0.2554 but the if loop is giving answer as 0.2552. The loop is iterating for the values of 'j' even after the condition is false. I have attached the matlab code and the data file.
clear all
clc
load data
Ezero=(0.0026.*temp)+(7.9387*10^(-4).*temp.*SOC)+(0.0016.*temp.*(SOC).^2)+2.5625; %Open circuit voltage
R=V-Ezero; %Battery internal resistance
figure (1)
plot(SOC,V,'-r','LineWidth',1); %Plot of voltage vs SOC
xlabel('SOC(%)');
ylabel('Voltage(Volts)');
title('Voltage vs SOC plot');
grid on
figure (2)
plot(SOC,Ezero,'-k','LineWidth',1)
xlabel('SOC(%)');
ylabel('Open circuit voltage(Volts)');
title('Open circuit voltage vs SOC plot'); %Plot of Open circuit voltage vs SOC
grid on
figure(3)
plot(SOC,R,'-b','LineWidth',1)
xlabel('SOC(%)');
ylabel('Internal Resistance(Ohm)');
title('Internal Resistance vs SOC plot'); %Plot of Resistance vs SOC
grid on
%% RUn
socl= find(SOC>=0.6 & SOC<=0.8);
RS=R(socl);
for j=2:length(RS)
if(RS(j)>RS(j-1)) %this loop here is giving wrong value
Rmax=RS(j);
end
end

Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 28 de Sept. de 2019
Editada: KALYAN ACHARJYA el 28 de Sept. de 2019
It's seems perfectly working:
>> RS
RS =
Columns 1 through 12
0.2217 0.2226 0.2235 0.2244 0.2252 0.2254 0.2253 0.2252 0.2250 0.2248 0.2245 0.2242
Columns 13 through 24
0.2239 0.2236 0.2232 0.2228 0.2224 0.2220 0.2218 0.2216 0.2218 0.2222 0.2225 0.2228
Columns 25 through 33
0.2230 0.2233 0.2235 0.2243 0.2252 0.2252 0.2245 0.2237 0.2228
>> length(RS)
ans =
33
Please note that you have comparing present value with privious one, ant from the array results it shows 15 times present value is greater than privious.
I changed the code here to count how many numbers if condition is true
count=0;
for j=2:length(RS)
if(RS(j)>RS(j-1)) %this loop here is giving wrong value
Rmax=RS(j);
count=count+1;
end
end
Important: Some cases both number (R(j) and R(j-1)) are same till 4th decimal point, in such case I request you to read about floating number representation
One Example:
>> A=2.3456
A =
2.3456
>> B=3.3456
B =
3.3456
>> A==B % Checking wheather A and B equal?
ans =
logical
0
  3 comentarios
KALYAN ACHARJYA
KALYAN ACHARJYA el 28 de Sept. de 2019
Editada: KALYAN ACHARJYA el 28 de Sept. de 2019
Why do think loop should be stop? "if" any current condition is false, it will check for next R(j) value because of for loop. It doesnot matter whether the codition is true or not, it will check all R(J) values as for loop is running.
Any condition is false, if you want to exit from for loop, please use break statement.
Deep Shah
Deep Shah el 28 de Sept. de 2019
I got the necessary output, Thank you for the help!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by