How can I build a workaround for array bound exceeding?

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)

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

Thanks, thats not exactly it, but the idea of a location vector that does not exceed is good. I forgot to say, DSM is a decision variable. I tried:
L = 2:numel(DSM)-1;
myoptprob.Constraints. = ...
DSM(L) == DSM(L-1) + DSM(L+1);
What now works for my solution is that every DSM Value "in the middle" is the sum of the surrounding positive and negative DSM (can be both) values, which I think is an expression for my problem "you can only give or get to or from your direct neighbours (concerning time i)"
The problem remaining is that the first and the last DSM also need to be as big as their respective neighbours (in absolute values, plus their surrounding), but are not adressed with this formulation.

Sign in to comment.

Asked:

on 6 May 2020

Edited:

on 6 May 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!