【问题标题】:If Statement within for loop - Matlabif 循环内的语句 - Matlab
【发布时间】:2012-02-02 00:23:45
【问题描述】:

他我正在使用来自三个制造商的涡轮机特定参数对风力涡轮机进行建模 我的代码是

Site_speed = xlsread('test.xlsx','Sheet1'); % Wind speed data recorded on site
air_density = xlsread('test.xlsx','Sheet2'); % Air density data recorded on site

Turbine_parameters = xlsread('windparameters.xlsx'); % Wind turbine unit database

Ref_wind_speed = Turbine_parameters(:,1); % Wind speed from wind turbine unit database file Turbine_parameters
Ref_output = Turbine_parameters(:,2:4); % Power output from wind turbine unit database file Turbine_parameters

Density_correct = (air_density./air_density_ref);

for K = 1 : size(Ref_output, 2)

    power_out(:,:,K) = Density_correct.* interp1( Ref_wind_speed, Ref_output(:,K), Site_speed, 'nearest');

% xlswrite('this_file2.xlsx', power_out(:,:,1), 'sheet1');
% xlswrite('this_file2.xlsx', power_out(:,:,2), 'sheet2');
% xlswrite('this_file2.xlsx', power_out(:,:,3), 'sheet3');

%% WIND TURBINE FINANCIAL ANALYSIS + OPERATIONAL EMISSIONS

Array_supply(:,:,K) = (1-Losses) .*  power_out(:,:,K) .* Turbines;
Total_array(:,:,K) = sum(Array_supply(:));
Array_OM_cost(:,:,K) = sum(sum(Total_array(:,:,K) * Wind_OM));

% % Grid connected system with internal load
end

for K = 1 : size(Array_supply,3)
    Demand = xlsread('demandtest.xlsx');
    if Demand >= Array_supply(:,:,K)
        Grid(:,:,K) = Demand - Array_supply(:,:,K)
        Income(:,:,K)= (Array_supply(:,:,K)*FIT_wind) + Array_supply(:,:,K)*Grid_cost);
        Expences(:,:,K) = (Array_OM_cost(:,:,K)) + sum(sum((Grid(:,:,K)*Grid_cost)));
        Profit(:,:,K) = sum(sum(Income(:,:,K))) -  sum(sum(Expences(:,:,K)));
    else
        Income(:,:,K) = (Demand*FIT_wind) + (Demand*Xe_wind)+(Demand*Grid_cost);
        Expences(:,:,K) = Array_OM_cost(:,:,K);
        Profit(:,:,K) = sum(sum(Income(:,:,K))) -  sum(sum(Expences(:,:,K)));
    end
end

我已经展示了上面的所有代码,但我认为错误从以下行开始 - 对于 K = 1:大小(Array_supply,3) 程序运行时我得到的结果是一组三个矩阵(如预期的那样),其中前两个矩阵仅填充零(不正确)。 网格、收入和费用也应该是 365x 24 矩阵(需求和 Array_supply 也是如此) 当我尝试运行 Grid(:,:,K) 时,出现一个错误,提示 Matlab 找不到它!

有人知道我哪里出错了吗? 谢谢

【问题讨论】:

    标签: matlab if-statement for-loop


    【解决方案1】:

    首先,在 Matlab 中预先分配数组始终是最佳实践。如果您知道 Grid、Income、Expenses 和 Profit 都将是一个 365x24x3 矩阵,那么您应该将它放在循环之前,并且对其他变量也这样做。

    Grid=zeros(365,24,3);
    

    至于您的问题,您似乎计算不正确。在Demand=xlsread(...) 语句之后放置一个断点。需求看起来对吗?需求必须是单个变量,如果是矩阵,那就是你的问题。如果是矩阵,则需要遍历每个变量。还有更多涉及逻辑掩码的优雅解决方案,如果您愿意,请随时查找该概念。我将建议至少在目前,您只需循环整个需​​求循环。

    另外,我认为您没有正确使用利润表。每个循环它只存储一个变量,但你将它存储在整个矩阵中......看起来 Profit 就像一个 3x1 矩阵一样好,引用它就像 Profit(f) 而不是 Profit(:, :,f)。

    哦,还有一个小问题,是费用,不是费用……

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多