IF-statements not executing correctly
19 次查看(过去 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!