IF-statements not executing correctly
조회 수: 20 (최근 30일)
이전 댓글 표시
This is a code simulating a battery storage (still in very early stages). As you can see I have put counters in every if-statement so that I can see how many times each statement executes. The problem is only the first six (out of 8760) loops executes statements. count1 executes 5 times and count2 1 times, the others 0 times. I cant understand why. If the the four if-statements are not satisfied, at least the else-statement should execute.
I'm new to this forum, but hopefully you will be able to reach hourlydata.mat which is needed to run the code. What I need from this piece of code is the vector "b_lager_last". As of now, the code runs as intended for the first 3 loops, but by the fourth loop something weird happens. Hopefully someone will be able to understand the code and fix my problem. I would be eternally greatful!
medel_kWh = mean(hourlydata);
b_lager_last = zeros(8760, 1);
b_storage_data = zeros(8760, 1);
b_storage_kW = 200;
b_storage = 0;
b_storage_min = 0.2*b_storage_kW;
b_storage_max = 0.8*b_storage_kW;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
count5 = 0;
for i = 1:8760
if (hourlydata(i) <= medel_kWh) && ((b_storage + (medel_kWh - hourlydata(i))) <= b_storage_max)
b_storage = b_storage + (medel_kWh - hourlydata(i));
b_lager_last(i) = medel_kWh;
count1 = count1 + 1;
if (hourlydata(i) <= medel_kWh) && ((b_storage + (medel_kWh - hourlydata(i))) >= b_storage_max)
b_storage = b_storage_max;
b_lager_last(i) = medel_kWh - ((b_storage + (medel_kWh - hourlydata(i))) - b_storage_max);
count2 = count2 + 1;
if (hourlydata(i) >= medel_kWh) && (b_storage >= (hourlydata(i) - medel_kWh))
b_storage = b_storage - (hourlydata(i) - medel_kWh);
b_lager_last(i) = medel_kWh - (hourlydata(i) - medel_kWh);
count3 = count3 + 1;
if (hourlydata(i) >= medel_kWh) && ((hourlydata(i) - medel_kWh) >= b_storage) && (b_storage > 0)
b_storage = 0;
b_lager_last(i) = medel_kWh - b_storage;
count4 = count4 + 1;
else
b_storage = b_storage + medel_kWh;
b_lager_last(i) = medel_kWh;
count5 = count5 + 1;
end
end
end
end
b_storage_data(i) = b_storage;
end
댓글 수: 2
Daniel Catton
2022년 2월 1일
편집: Daniel Catton
2022년 2월 1일
Instead of using multiple "if" statements, try having the first as "if" and the others as "elseif", that may solve your problem (not tried it though!!)
I believe in your code you are saying "if the first statement is true, then run the code and check if the next statement is true, etc."
채택된 답변
Daniel Catton
2022년 2월 1일
Instead of using multiple "if" statements, try having the first as "if" and the others as "elseif", that may solve your problem (not tried it though!!)
I believe in your code you are saying "if the first statement is true, then run the code and check if the next statement is true, etc."
medel_kWh = mean(hourlydata);
b_lager_last = zeros(8760, 1);
b_storage_data = zeros(8760, 1);
b_storage_kW = 200;
b_storage = 0;
b_storage_min = 0.2*b_storage_kW;
b_storage_max = 0.8*b_storage_kW;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
count5 = 0;
for i = 1:8760
if (hourlydata(i) <= medel_kWh) && ((b_storage + (medel_kWh - hourlydata(i))) <= b_storage_max)
b_storage = b_storage + (medel_kWh - hourlydata(i));
b_lager_last(i) = medel_kWh;
count1 = count1 + 1;
elseif (hourlydata(i) <= medel_kWh) && ((b_storage + (medel_kWh - hourlydata(i))) >= b_storage_max)
b_storage = b_storage_max;
b_lager_last(i) = medel_kWh - ((b_storage + (medel_kWh - hourlydata(i))) - b_storage_max);
count2 = count2 + 1;
elseif (hourlydata(i) >= medel_kWh) && (b_storage >= (hourlydata(i) - medel_kWh))
b_storage = b_storage - (hourlydata(i) - medel_kWh);
b_lager_last(i) = medel_kWh - (hourlydata(i) - medel_kWh);
count3 = count3 + 1;
elseif (hourlydata(i) >= medel_kWh) && ((hourlydata(i) - medel_kWh) >= b_storage) && (b_storage > 0)
b_storage = 0;
b_lager_last(i) = medel_kWh - b_storage;
count4 = count4 + 1;
else
b_storage = b_storage + medel_kWh;
b_lager_last(i) = medel_kWh;
count5 = count5 + 1;
end
b_storage_data(i) = b_storage;
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!