The if loop is continuing even after the condition is false.
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
KALYAN ACHARJYA
2019-9-28
编辑:KALYAN ACHARJYA
2019-9-28
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 个评论
KALYAN ACHARJYA
2019-9-28
编辑:KALYAN ACHARJYA
2019-9-28
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!