简体   繁体   中英

If Statement within for loop - Matlab

Him I am working on modelling a wind turbine using turbine specific parameters from three manufacturers My code is

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

I have shown all of the code above, but I think that the mistakes start at the line - for K = 1 : size(Array_supply,3) The results I get when the program runs is a set of three matrices (as expected) where the first two are populated with zeros only (incorrect). Also the Grid, Income and Expenses should be 365x 24 matrices (as are the Demand and Array_supply) When I try and just run the Grid(:,:,K), an error appears saying Matlab cant find it!!

Does anyone have any idea where I might have gone wrong? Thanks

First of all, it is always best practice in Matlab to pre-allocate your arrays. If ou know that Grid, Income, Expenses, and Profit are all going to be a 365x24x3 matrix, then you should put this before the loop, and doing likewise for the other variables.

Grid=zeros(365,24,3);

As for your problem, it seems likely that you aren't computing this correctly. Put a break point after the Demand=xlsread(...) statement. Does demand look right? Demand must be a single variable, if it is a matrix, then that is your problem. If it is a matrix, you need to loop through every variable. There are more elegant solutions involving logical masks, feel free to look up the concept if you want to. I'm going to suggest that at least for the time being, you just loop through the whole Demand loop.

Also, I don't think you are using your profit statements correctly. It is only storing a single variable per loop, but you are storing it for the entire matrix... It seems like Profit would be just as well served as a 3x1 matrix, referencing it like Profit(f) instead of Profit(:,:,f).

Oh, and a very minor point, it's Expenses, not Expences...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM