フィルターのクリア

unable to get code to run please help

1 回表示 (過去 30 日間)
Jorge
Jorge 2023 年 10 月 8 日
コメント済み: Walter Roberson 2023 年 10 月 8 日
% Define the objective function to maximize economic impact
function economic_impact = PowerPlantCode2(P)
function fmincon
% Define the optimization problem
options = optimset('fmincon');
options.Display = 'iter';
options.MaxFunEvals = 10;
% Given
electricity_price = 0.47; % $/kWh
water_price = 2; % $/cubic meter
T_warm = 27; % Warm surface water temperature (°C)
T_cold = 4; % Cold deep-ocean water temperature (°C)
dT_condenser = 3; % Terminal temperature difference in the
condenser (C);
% Other probable variables
T_condenser = 12; % degrees Celcius
Run_Time = 1000; % hours
mdot_FC_Discharge = 20; % kg/s
rho_water = 997; % kg/m3
cp = 4.186; % kJ/kg
% Water mass flow rates
mdot_warm = 70; % in kg/s
mdot_cold = 25; % in kg/s
% Electricity generation and desalinated water production
Ideal_power = mdot_warm*cp*(T_warm-T_condenser); % kW
MaxPosOutput = Ideal_power*1-(T_condenser+273)/(T_warm+273); % kW
electricity_usage = Ideal_power*Run_Time/1000; % in kWh
water_volume = (mdot_warm - mdot_FC_Discharge)/rho_water; % in cubic
meters
% Economic impact
electricity_revenue = electricity_usage*electricity_price; % in dollars
water_revenue = water_volume*water_price; % in dollars
economic_impact = electricity_revenue + water_revenue;
% Initial guess and bounds for flash chamber pressure (P)
P0 = 1000; % Initial guess in kPa
lb = 500; % Lower bound in kPa
ub = 10000; % Upper bound in kPa
% Ooptimization
P_optimal, max_economic_impact = fminconPowerPlantCode2; P0; (); (),
(); (); lb; ub; (0);
% Display
fprintf('Optimal Flash Chamber Pressure: %f bar\n', P_optimal);
fprintf('Maximum Economic Impact: $%f\n', max_economic_impact);

採用された回答

Walter Roberson
Walter Roberson 2023 年 10 月 8 日
You had some bad syntax problems in your code.
Your PowerPlantCode2 function does not use the pressure, so it is pointless to optimize with respect to pressure.
Reminder: fmincon() is a minimizer -- so you are minimizing economic impact, but then your output text says that the result is for maximizing the economic impact.
% Initial guess and bounds for flash chamber pressure (P)
P0 = 1000; % Initial guess in kPa
lb = 500; % Lower bound in kPa
ub = 10000; % Upper bound in kPa
% Ooptimization
% Define the optimization problem
options = optimset('fmincon');
options.Display = 'iter';
options.MaxFunEvals = 10;
[P_optimal, max_economic_impact] = fmincon(@PowerPlantCode2, P0, [], [], [], [], lb, ub, [], options);
First-order Norm of Iter F-count f(x) Feasibility optimality step 0 2 2.065891e+03 0.000e+00 0.000e+00 Initial point is a local minimum that satisfies the constraints. Optimization completed because at the initial point, the objective function is non-decreasing in feasible directions to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
% Display
fprintf('Optimal Flash Chamber Pressure: %f bar\n', P_optimal);
Optimal Flash Chamber Pressure: 1000.000000 bar
fprintf('Maximum Economic Impact: $%f\n', max_economic_impact);
Maximum Economic Impact: $2065.891301
% Define the objective function to maximize economic impact
function economic_impact = PowerPlantCode2(P)
% Given
electricity_price = 0.47; % $/kWh
water_price = 2; % $/cubic meter
T_warm = 27; % Warm surface water temperature (°C)
T_cold = 4; % Cold deep-ocean water temperature (°C)
dT_condenser = 3; % Terminal temperature difference in the condenser (C);
% Other probable variables
T_condenser = 12; % degrees Celcius
Run_Time = 1000; % hours
mdot_FC_Discharge = 20; % kg/s
rho_water = 997; % kg/m3
cp = 4.186; % kJ/kg
% Water mass flow rates
mdot_warm = 70; % in kg/s
mdot_cold = 25; % in kg/s
% Electricity generation and desalinated water production
Ideal_power = mdot_warm*cp*(T_warm-T_condenser); % kW
MaxPosOutput = Ideal_power*1-(T_condenser+273)/(T_warm+273); % kW
electricity_usage = Ideal_power*Run_Time/1000; % in kWh
water_volume = (mdot_warm - mdot_FC_Discharge)/rho_water; % in cubic meters
% Economic impact
electricity_revenue = electricity_usage*electricity_price; % in dollars
water_revenue = water_volume*water_price; % in dollars
economic_impact = electricity_revenue + water_revenue;
end
  2 件のコメント
Jorge
Jorge 2023 年 10 月 8 日
Thank you for the speedy reply! i forgot to add. is there a way to incorporate optimal pressure?
Walter Roberson
Walter Roberson 2023 年 10 月 8 日
Well, you would adjust your PowerPlantCode2 to make use of P, your current pressure. But I have no idea what the equations would be for that.
I would have thought that production rate would probably increase with pressure, but that potentially increased pressure had an increased cost (possibly non-linear).
Is increased pressure going to increase the water flow rates?
Water is a little compressible, but it looks like your top end is 10 MPa at which point the change is just starting to become significant, so that probably does not need to be accounted for.

サインインしてコメントする。

その他の回答 (1 件)

Torsten
Torsten 2023 年 10 月 8 日
This should be the rough structure of the optimization code. But you don't use your optimization variable P in the objective function. Thus you always compute the same value for "economic_impact".
% Initial guess and bounds for flash chamber pressure (P)
P0 = 1000; % Initial guess in kPa
lb = 500; % Lower bound in kPa
ub = 10000; % Upper bound in kPa
% Ooptimization
% Define the optimization problem
%options = optimset('fmincon');
options.Display = 'iter';
options.MaxFunEvals = 10;
[P_optimal, max_economic_impact] = fmincon(@PowerPlantCode2, P0, [],[],[],[],lb,ub,[],options)
% Display
fprintf('Optimal Flash Chamber Pressure: %f bar\n', P_optimal);
fprintf('Maximum Economic Impact: $%f\n', max_economic_impact);
% Define the objective function to maximize economic impact
function economic_impact = PowerPlantCode2(P)
% Given
electricity_price = 0.47; % $/kWh
water_price = 2; % $/cubic meter
T_warm = 27; % Warm surface water temperature (°C)
T_cold = 4; % Cold deep-ocean water temperature (°C)
dT_condenser = 3; % Terminal temperature difference in the
% Other probable variables
T_condenser = 12; % degrees Celcius
Run_Time = 1000; % hours
mdot_FC_Discharge = 20; % kg/s
rho_water = 997; % kg/m3
cp = 4.186; % kJ/kg
% Water mass flow rates
mdot_warm = 70; % in kg/s
mdot_cold = 25; % in kg/s
% Electricity generation and desalinated water production
Ideal_power = mdot_warm*cp*(T_warm-T_condenser); % kW
MaxPosOutput = Ideal_power*1-(T_condenser+273)/(T_warm+273); % kW
electricity_usage = Ideal_power*Run_Time/1000; % in kWh
water_volume = (mdot_warm - mdot_FC_Discharge)/rho_water; % in cubic meters
% Economic impact
electricity_revenue = electricity_usage*electricity_price; % in dollars
water_revenue = water_volume*water_price; % in dollars
economic_impact = electricity_revenue + water_revenue;
economic_impact = -economic_impact; % minus because you want to maximize, not minimize
end
  1 件のコメント
Jorge
Jorge 2023 年 10 月 8 日
Thank you for the speedy reply! i forgot to add. is there a way to incorporate optimal pressure?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by