필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by