sizing microgrid using for and if statement

4 views (last 30 days)
Hello,
I am currently doing a project where my task is to size the components in micro-grid and calculate the LCC. I extracted the data för one day. Data I have are: Battery voltage, Battery current, Inverter power( reprecent the consumption), Solar power (represent the production).I am a beginner in matlab and hope that some one can help me here. I want to write a code that loop through all the hours in the day and calculate the following:
for all hours
if (production > consumption) and (battery power <= 80%*Battery max capacity)
charge the battery= Production-consumption
elseif (production > consumption) and (battery power >= 80%*Battery max capacity)
wasted power= Production-consumption
elseif (consumption > production) and (battery power >= 20%*Battery max capacity)
use the battery=consumption-production
else (consumption > production) and (battery power <= 20%*Battery max capacity)
increase battery capacity = consumption-production
end
I am already stuck by looping through all the hours and dont know how to do it
so thankful for help
  1 Comment
Guillaume
Guillaume on 24 Apr 2019
In all likelyhood you don't need a loop. Your test can probably be carried out at once on all the data.
For us to be able to give you an answer that works for you, it would be very useful to see an example of the data (is it a table, cell array, one or more matrix? How is the time stored? As a datetime? datenum? datestr?, etc.). So please attach a demo mat file to your question.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 25 Apr 2019
It sounds like you have several problems. The first one being the data generation which you currently do with some form of digitisation. I don't think it's something I can help with. Then you have the problem of processing your data once you've generated it.
I don't really understand what you're doing with your datenum, datevec, datestr. One thing for sure, it probably doesn't do what you want since at one point you're using hours/minutes/seconds in place of year/month/day. In any case, you shouldn't be using any of these types. Use datetime which is so much more practical. E.g. if you want to generate dates in a day in interval of 1 minutes:
d = datetime('2019-03-30 00:00:00') + minutes(0:24*60-1);
As for your data, I recommend you store it in a table or even better timetable, eg.
solardata = timetable(sometimevector, B_v(:), P_s(:), P_inv(:), 'VariableNames', {'BatteryVoltage', 'SolarPower', 'InverterPower'});
You could then retime your timetable to make it hourly if that's what you need.
After that you can store the state of your system (I think that's what you want to do, it's not clear) in a new column, eg.:
solardata.State(solardata.SolarPower > solardata.InverterPower & solardata.BatteryVoltage <= 0.8*SOCmax) = "charging";
solardata.State(solardata.SolarPower > solardata.InverterPower & solardata.BatteryVoltage > 0.8*SOCmax) = "wasting power";
solardata.State(solardata.SolarPower <= solardata.InverterPower & solardata.BatteryVoltage >= 0.2*SOCmax) = "discharging";
solardata.State(solardata.SolarPower <= solardata.InverterPower & solardata.BatteryVoltage < 0.2*SOCmax) = "low battery";
  3 Comments
Guillaume
Guillaume on 30 Apr 2019
That last error you get is because the Time is not considered a variable in a timetable and therefore you should only specify four variable names.
Summer_week_data=timetable(interpt(:),interpBat_V(:),interpBat_C(:),interpSol_p(:),interpInv_P(:),'VariableNames',{Battery_volt','Battery_current','Solar_power','Inverter_power'});
It seems you have figured out how to resolve your different length issue.
Guillaume
Guillaume on 30 Apr 2019
Edited: Guillaume on 30 Apr 2019
Please comment on the answer rather than starting new answers.
I thought the way you interpolated your data was fine, so I'm not sure why you're no longer doing that. Another option is to start with one timetable per variable and then synchronize them all. Since all your variables have the same duration, create one timetable per variable with a linspace'd time vector:
starttime = datetime(2018,07,01,14,0,0);
endtime = datetime(2018,07,05,14,0,0);
tVbat = timetable(linspace(starttime, endtime, numel(Bat_V)', Bat_V, 'VariableNames', {'Battery_volt'}))
tCbat = timetable(linspace(starttime, endtime, numel(Bat_C)', Bat_C, 'VariableNames', {'Battery_current'}))
tbat = synchronize(tVbat, tCbat, 'Uniform', 'Interval', minutes(1));
%... etc for other variables.

Sign in to comment.

More Answers (1)

Abm
Abm on 30 Apr 2019
Thank you so much for responding !! I kept thinking in this problem from yesterday.
I will continue as you suggest for the rest of the solution.
Best regards

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!