Matlab linprog错误——A中的行数必须与b的元素个数相同。

时间:2022-03-16 16:12:28

Hi I have the following code for a linprog optimisation.

你好,我有以下代码为linprog优化。

for j = 1:2

 for i = 1:24
   for K = 1:3
      for M = 1:3

   PV_output(:,:,:) = real(PV_power_output(:,:,:));
         WT_output(:,:,:) =  WT_power_output(:,:,:);

         PVenergy = sum(sum(PV_output(:,:,1)));
         WTenergy = sum(sum(WT_power_output(:,:,1)));


          f= [((CRF*CC_PV)/PVenergy)+OM_PV; ((CRF*CC_WT)/WTenergy)+OM_WT];

         A(:,:,:) = [-PV_output(:,:,K) -WT_output(:,:,M)];


            b(:,:) = -Demand(j,i);

           lb = zeros(2,1);


           ub = [max_PV_area/PV_area max_WT_area/WT_area]';

      end
   end
  end
 end

            x(:,j,i,K,M) = linprog(f,A,b,[],[],lb,ub)

Where WT_output and PV_output are 3 dimensional 365x24 arrays and Demand is 365x24

其中WT_output和PV_output是3维365x24数组,需求是365x24 ?

I am trying to optimise x1 and x2 for each of the 365x24 elements of Demand and for each dimension so as to find the optimum K and M combination

我试着对每一个365x24的需求元素和每一个维度优化x1和x2,以找到最佳的K和M组合。

However as the code currently stands I keep getting the error - "The number of rows in A must be the same as the number of elements of b."

但是,由于当前的代码,我不断地得到错误——“A中的行数必须与b的元素个数相同。”

Does anyone have any suggestions?

有人有什么建议吗?

2 个解决方案

#1


0  

What does your workspace say about the sizes of A and B? ChrisJamesC right.. It happened with me too.. I forgot to transpose the matrix while performing operations. Try to check the workspace at each step by placing breakpoints. That might help

你的工作空间对A和B的尺寸有什么看法?ChrisJamesC吧. .这事也发生在我身上。我忘记了在执行操作时矩阵的转置。试着通过放置断点来检查每个步骤的工作空间。这可能帮助

#2


0  

The documentation of linprog states that:

linprog的文档说明:

x = linprog(f,A,b) solves min f'*x such that A*x ≤ b.

x = linprog(f,A,b)解决最小f ' *,* x≤b。

So, clearly the number of rows in A must be the same as the number of elements of b as A can be a matrix whereas b is a vector

显然A的行数必须等于b的元素个数A可以是一个矩阵而b是一个向量。

If your question is "why don't I have the good size?" just try to print the size of the vectors/matrices at each step in order to see where the mistake is (it happens a lot that you forget to transpose a matrix for example)

如果你的问题是“为什么我没有合适的尺寸呢?”试着在每一步打印出矢量/矩阵的大小,这样就可以看出错误在哪里(比如,你忘记了把矩阵转置)

#1


0  

What does your workspace say about the sizes of A and B? ChrisJamesC right.. It happened with me too.. I forgot to transpose the matrix while performing operations. Try to check the workspace at each step by placing breakpoints. That might help

你的工作空间对A和B的尺寸有什么看法?ChrisJamesC吧. .这事也发生在我身上。我忘记了在执行操作时矩阵的转置。试着通过放置断点来检查每个步骤的工作空间。这可能帮助

#2


0  

The documentation of linprog states that:

linprog的文档说明:

x = linprog(f,A,b) solves min f'*x such that A*x ≤ b.

x = linprog(f,A,b)解决最小f ' *,* x≤b。

So, clearly the number of rows in A must be the same as the number of elements of b as A can be a matrix whereas b is a vector

显然A的行数必须等于b的元素个数A可以是一个矩阵而b是一个向量。

If your question is "why don't I have the good size?" just try to print the size of the vectors/matrices at each step in order to see where the mistake is (it happens a lot that you forget to transpose a matrix for example)

如果你的问题是“为什么我没有合适的尺寸呢?”试着在每一步打印出矢量/矩阵的大小,这样就可以看出错误在哪里(比如,你忘记了把矩阵转置)