IF-statements not executing correctly

23 ビュー (過去 30 日間)
Alexander BS
Alexander BS 2022 年 2 月 1 日
コメント済み: Alexander BS 2022 年 2 月 1 日
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
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."
Alexander BS
Alexander BS 2022 年 2 月 1 日
You're a genius! It worked! That was the most stupid mistake I have made (so far) xD

サインインしてコメントする。

採用された回答

Daniel Catton
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 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by