Hi I am working with the following code (Brute force method).
“PV_supply” and “WT_supply” and “Demand” are 48×1 size.
what I am trying to do is calculate the “Energy_battery” value for each of the 48 rows. However to do this I need to use the value of “Energy_battery” from the previous row in the calculations of each row which I haven’t figured out how to code and was hoping for some help on this. therefore the equation for “Energy_battery” in row 1 uses the “Energy_battery” value in row 1 for the equation etc
My code is:
for number_panels = 0:5
for number_turbines = 0:3
for h=1:24 %# hours
for d = 1:number_of_days %# which day
n = h + 24*(d-1);
Energy_battery(number_panels + 1, number_turbines + 1,1,1) = 100;
Energy_battery(number_panels + 1, number_turbines + 1,n+1,1) =...
Energy_battery(number_panels + 1, number_turbines + 1,h,1) + ...
((PV_supply(n)*number_panels + WT_supply(n)*number_turbines) - ...
Demand(n)/inverter_efficiency)*battery_charging_efficiency;
This is an extended comment, only an answer to parts of your question, though I think it has relevance to your other recent questions too.
It’s helpful to think of Matlab as an array processing language, the natural ‘unit’ of computation is an array rather than a scalar as in many other languages. If you find yourself writing loops to iterate over the elements of an array stop and think, there is likely to be a more ‘natural’ way of expressing the same calculation without the loops. There’s nothing absolutely wrong with loops but over-reliance on them can have 2 deleterious effects:
So, for example, your statement
could be lifted out of your loop nest entirely and rewritten as
Now, for the main bulk of your code, if I’ve understood it correctly, you want to update each element at
Energy_battery(:,:,n+1,1)based on the values inEnergy_battery(:,:,n,1)and in elementnof the other vectors you have. First, let’s tidy this upcould be rewritten as
for Matlab multiplying an array by a scalar applies the multiplication to each element of the array. And again, this doesn’t need a loop over the values of an index such as
n.I’m afraid I have to go and do some work now, will come back later and finish the lesson if no-one else does. Feel free to edit this answer if you want to.