Why do I get the error “Index exceeds matrix dimensions.”?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ananthareddy Kunreddy
el 1 de Feb. de 2020
Comentada: Ananthareddy Kunreddy
el 1 de Feb. de 2020
function [solution] = demand_limit_optimization_with_thresholdSOC...
(load_profile, pv_generation, C_bat, SOC_0, P_batMin, P_batMax, SOC_min, SOC_max, ...
eta_bat, threshold, hour, SOC_trajectory_day, dischargeLimitation, chargeLimitation)
%%Optimization for demand limit minimization
load matlab
pv_generation = 1:24;
n = length(pv_generation);
hour=n;
prob = optimproblem('ObjectiveSense','minimize');
%Define variables, length of array (n) and upper/lower bounds
load_profile = Pload
panelArea = 50;
panelEff = 0.16;
numDays = 1; % Number of consecutive days
FinalWeight = 1; % Final weight on energy storage
timeOptimize = 1; % Time step for optimization [hour]
stepAdjust = 1;
Ppv = panelArea*panelEff*repmat(solar(1:stepAdjust:end),numDays,1);
pv_generation = Ppv;
C_bat = 5;
SOC_0 = 0.5;
SOC_trajectory_day = [SOC_0,0,0,0,0,0,0,0,0,0,0];
P_batMin = -3; P_batMax = 3;
SOC_min = 0.1;
SOC_max = 1;
eta_bat = 0.9;
threshold = 6;
P_system = optimvar('P_system', n); %P_PV + P_bat
P_bat = optimvar('P_bat',n,'LowerBound',P_batMin,'UpperBound', P_batMax); %Max and Min power of the battery
SOC = optimvar('SOC', n,'LowerBound',SOC_min,'UpperBound',SOC_max); %SOC level
P_PV = optimvar('P_PV', n); %Forecasted PV generation
P_dmd = optimvar('P_dmd', n); %Forecasted load
threshold = optimvar('threshold', n, 'UpperBound', threshold); %added load threshold which cant be crossed
%%Define objective function
f = 0;
for t=1:n
if (pv_generation(t) < load_profile(t)) && (load_profile(t) >= 0)
f = f + (P_dmd(t) - P_system(t)); %P_dmd - (P_PV - P_PVcharge + discharge)
else
f = 0;
end
end
prob.Objective = f;
%%Define constraints
P_system_calculate = optimconstr(n,1); %Constraint to calculate the Power of the PV + Battery system
SOC_level = optimconstr(n,1); % Constraint to calculate the SOC level
initialCharge = optimconstr(n,1); %Constraint to define the inital SOC level
threshold_limitation = optimconstr(n,1);
PV_feed = optimconstr(n,1); %Constraint to implement input PV
load_profile_feed = optimconstr(n,1); %Constraint to implement input load
pvDischarge_limitation = optimconstr(n,1); %Constraint to battery discharge into grid
pvCharge_limitation = optimconstr(n,1);%Constraint to battery charge from grid
SOC_trajectory = optimconstr(n,1);%Constraint to assure, the optimization includes already made steps
%Time depending constraints (which need to be calculated in every time
%step)
for t=1:n
%1: Calculating the energy which flows from/into the PV-battery system
%P_system(t) = P_PV(t) + P_bat(t)
P_system_calculate(t) = P_PV(t) + P_bat(t) == P_system(t);
%2: Calculation of the time depending charging level SOC
if t>1
SOC_level(t) = SOC(t) == SOC(t-1) - ((P_bat(t) / 4) * eta_bat) / (C_bat / 4); %Devided by 4 bc 15 minutes
else
initialCharge = SOC(1) == SOC_0; %Initial charge level of the batter: SOC(1) = SOC_0;
end
%Threshold limitation
threshold_limitation(t) = threshold(t) >= P_dmd(t) - P_system(t);
%3&4: Setting the input data of the generation and the load profiles
PV_feed(t) = P_PV(t) == pv_generation(t);
load_profile_feed(t) = P_dmd(t) == load_profile(t);
%Constraint to forbid feed in charge
pvDischarge_limitation(t) = P_bat(t) <= P_dmd(t) - P_PV(t);
pvCharge_limitation(t) = - P_bat(t) <= P_PV(t);
end
for i=1:hour
SOC_trajectory((i-1)*4 + 1) = SOC((i-1)*4 + 1) == SOC_trajectory_day((i-1)*4 + 1);
SOC_trajectory((i-1)*4 + 2) = SOC((i-1)*4 + 2) == SOC_trajectory_day((i-1)*4 + 2);
SOC_trajectory((i-1)*4 + 3) = SOC((i-1)*4 + 3) == SOC_trajectory_day((i-1)*4 + 3);
SOC_trajectory((i-1)*4 + 4) = SOC((i-1)*4 + 4) == SOC_trajectory_day((i-1)*4 + 4);
end
%Add constraints to problem
prob.Constraints.P_system_calculate = P_system_calculate;
prob.Constraints.SOC_level = SOC_level;
prob.Constraints.PV_feed = PV_feed;
prob.Constraints.load_profile_feed = load_profile_feed;
prob.Constraints.initialCharge = initialCharge;
prob.Constraints.threshold_limitation = threshold_limitation;
prob.Constraints.SOC_trajectory = SOC_trajectory;
if dischargeLimitation
prob.Constraints.pvDischarge_limitation = pvDischarge_limitation;
end
if chargeLimitation
prob.Constraints.pvCharge_limitation = pvCharge_limitation;
end
%%solve optimization problem
solution = solve(prob);
%showproblem(prob) %See full optimization problem with all constraints and
%boundaries
2 comentarios
Ioannis Andreou
el 1 de Feb. de 2020
Can you show the whole Error-message including the line that produces it?
Respuestas (1)
Ioannis Andreou
el 1 de Feb. de 2020
you set
hour = n;
and then construct
SOC_trajectory = optimconstr (n, 1);
and then finally you iterate
for i = 1:hour
SOC_trajectory((i-1)*4 + 4) =..
end
So since i goes up to n, you at some point have (i-1)*4+4 > n, and thus the index exceeds the matrix dimension.
You either have to construct SOC_trajectory larger or change the iteration to
for i = 1:hour/4
SOC_trajectory((i-1)*4 + 4) =..
end
2 comentarios
Walter Roberson
el 1 de Feb. de 2020
Also SOC is being accessed up to (24-1)*4+4 when it is only 24 long.
Have you considered constructing 2 dimensional optimization variables with the second dimension 4?
Ver también
Categorías
Más información sobre Optimization Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!