How can I build a workaround for array bound exceeding?
Show older comments
Hi,
i want to build an optimization model for different times i=1,2,3,4.. where something is produced and I have supply and demand for every period which are supposed to be equal. With another variable "DSM" (demand side management), describing the the additional or deducted demand for every i, I allow demand to be shifted in time.
I have a balance constraint over all i
sum(DSM)==0
and now i want to formulate a constraint that demand can only be shifted between two i next to each other. Because I have to consider both shifting directions i thought of:
DSM(i-1) == DSM(i)+ DSM(i+1)
Of course, i get the error "Index exceeds array bounds.", because there is no i=0 or i=5.
How can I solve this? Do i need to adress the min and max t another way or do i need to formulate another variable for "shifted from 2 to 3"
Thanks! Besides: I used array indexing instead of eval or variable indexing, t = 4 and i = (1:t).
Answers (1)
Steven Lord
on 6 May 2020
Let's take an arbitrary DSM vector as an example. Your DSM vector may be different depending on how many random numbers you've generated in your MATLAB session.
>> DSM = randperm(5)
DSM =
3 5 1 2 4
Let's build a location vector L.
>> L = 2:numel(DSM)-1;
Ask for the values of DSM at locations one prior to the locations in L and compare them to values of the sums of the values at locations in L and the values at locations to the right of locations in L.
>> [DSM(L-1); DSM(L)+DSM(L+1)]
ans =
3 5 1
6 3 6
The first 6 is DSM(2)+DSM(3), the 3 in the bottom row is DSM(3)+DSM(4), and the second 6 is DSM(4)+DSM(5).
If you were trying to build constraint matrices (say to pass in as the linear equality constraint matrix Aeq in a call to fmincon) the diag function may be of interest to you, specifically the ability to specify along which diagonal the vector is placed in the resulting matrix.
1 Comment
Daniel Schloß
on 6 May 2020
Edited: Daniel Schloß
on 6 May 2020
Categories
Find more on Direct Search in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!