Search code examples
matlabloopsfor-loopmathematical-optimization

'for' loop won't loop


I have the following code

for i = 1:8760
    A = [PVtech(i,:) WTtech(i,:)];
    b = demand(i);
    f = [CRF * PVtechcost(i,:) .* PVcap(i,:) ./ PVtech(i,:) CRF*WTtechcost(i,:) .* WTcap(i,:) ./ WTtech(i,:)];
    x(i) = linprog(f, A,b,[], [], lb);
end

I'm trying to optimize linprog over the 8760 data set but can't seem to get the loop going for each row.
When I run it I get a size of `A to be 1x30 (when it should be 8760 by 30).

Does anyone see where I have coded wrongly?


Solution

  • Yes, every time you run it you are overwriting A with the single row [PVtech(i,:) WTtech(i,:)]

    Try this: A = [A; PVtech(i,:) WTtech(i,:)]; i.e. vertical concatenation

    With preallocation your code would look like this:

    numRows = 8760;
    A = zeros(numRows, 30);
    
    for i = 1:numRows
    
        A(i,:) = [PVtech(i,:) WTtech(i,:)];
    
        b = demand(i);
    
        f = [CRF*PVtechcost(i,:).*PVcap(i,:)./PVtech(i,:) CRF*WTtechcost(i,:).*WTcap(i,:)./WTtech(i,:)];
    
        x(i) = linprog(f, A,b,[], [], lb);
    
    end