1
votes

Hi I have this code which keeps giving me a "index exceeds matrix dimensions" error. I am trying to start the loop for h=1, d= 1 for 24 "h" and 3 "d" with a value for the "battery_capacity" matrix =2, but this seems to contradict in terms of the matrix sizing. Any help is appreciated! Could the "h-1" be causing the problem. The error is on the second time the "battery_capacity" is written My code is for idx_number_panels = 1:length(number_panels) % range of PV panel units examined

for number_turbines = 0:2 % range of wind turbine units examined
  for number_batteries = 1:50 % range of battery units examined
    for h=2:25 %# hours
      for d = 1:number_of_days %# which day
        battery_capacity(idx_number_panels, number_turbines+1, ...
                         number_batteries, 1, 1) =  2*number_batteries;
        %% Charging
        battery_charging(idx_number_panels, number_turbines+1, ...
                         number_batteries, h, d) ...
          = hourly_surplus(idx_number_panels, number_turbines+1, ...
                          number_batteries, h, d) ...
            + battery_capacity(idx_number_panels, number_turbines+1, ...
                               number_batteries, h-1,d);
    end
  end
end

DEBUGGER

error line 134 

battery_charging(idx_number_panels, number_turbines+1 ,number_batteries, h,d) =        hourly_surplus(idx_number_panels, number_turbines+1 ,number_batteries, h,d)...
    K>> sz = size(battery_charging)

sz =

 1     1     1     2

K>> index = [idx_number_panels, number_turbines+1 ,number_batteries, h-1,d]

index =

 1     1     1     1     2

K>> ndims(battery_charging)

ans =

  4

Running "battery_charging" in the command line

>> battery_charging

battery_charging(:,:,1,1) =

 0


battery_charging(:,:,1,2) =

  0

1

1 Answers

2
votes

Easier than trying to figure out what the problem is from the code is to debug as follows. First, set the debugger to break on error:

>> dbstop if error

Now, run your code again. When you get to the error, you should be met with the debug prompt:

K>>

You can now inspect the values of the different indexes and the shape of your matrices at the time of your error, and figure out where the problem is coming from.