IF-statements not executing correctly

19 views (last 30 days)
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 Comments
Daniel Catton
Daniel Catton on 1 Feb 2022
Edited: Daniel Catton on 1 Feb 2022
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 on 1 Feb 2022
You're a genius! It worked! That was the most stupid mistake I have made (so far) xD

Sign in to comment.

Accepted Answer

Daniel Catton
Daniel Catton on 1 Feb 2022
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

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!