user643469
user643469

Reputation: 187

Use value in previous cell of matlab matrix for use in equation

Hi I am working with the following code (Brute force method). "PV_supply" and "WT_supply" and "Demand" are 48x1 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;

Upvotes: 1

Views: 988

Answers (1)

High Performance Mark
High Performance Mark

Reputation: 78334

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:

  1. Matlab code without loops is often (much) faster than the equivalent with loops;
  2. Code with loops is often more cluttered, and requires more variables to be used, than the equivalent without loops. The clutter is a hindrance when it comes to understanding the code

So, for example, your statement

Energy_battery(number_panels + 1, number_turbines + 1,1,1) = 100;

could be lifted out of your loop nest entirely and rewritten as

Energy_battery(:,:,1,1) = 100;

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 in Energy_battery(:,:,n,1) and in element n of the other vectors you have. First, let's tidy this up

((PV_supply(n)*number_panels + WT_supply(n)*number_turbines) - Demand(n)/inverter_efficiency)*battery_charging_efficiency

could be rewritten as

((PV_supply*number_panels + WT_supply*number_turbines) - Demand/inverter_efficiency)*battery_charging_efficiency

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.

Upvotes: 1

Related Questions