Optimization Function for Costs (fminbnd)

조회 수: 2 (최근 30일)
DIP
DIP 2017년 4월 23일
편집: DIP 2017년 4월 23일
Hi,
I have to optimize the cost using the fminbnd function
I am getting a scalar error while the code runs the fminbnd function. Can anyone help me debug
%%An example of Matlab code
clc;clear;
format long;
%%Constants
hf_H2 = 0;
hf_O2 = 0;
hf_H2Og = -244820;
sf_H2 = 130.68;
sf_O2 = 205.14;
sf_H2Og = 188.72;
cp_H2O = 33.7;
cp_H2 = 29.5;
cp_O2 = 329.0;
Tref = 298;
Ru = 8.314;
T = 353;
n = 2;
F = 96487;
p_H2 = 1;
p_O2 = 0.21;
a_H2O = 1;
po = 1;
delT = T-Tref;
%%Calculate Nerst Reversible Cell Potential
% Calculate Gibbs Free Energy
delH = hf_H2Og+cp_H2O*delT - (hf_H2+cp_H2*delT) - 0.5*(hf_O2+cp_O2*delT);
delS = sf_H2Og+cp_H2O*log(T/Tref) - (sf_H2+cp_H2*log(T/Tref)) - 0.5*(sf_O2+cp_O2*log(T/Tref));
delG = delH - (Tref * delS);
% Reversible voltage
Ero = -delG/(n*F);
% Nerst Expression for voltage
Eo=Ero+(Ru*T/n/F)*log((p_H2/po)*(p_O2/po)^0.5);
%%Populating matrix for calculations
V_mdl = @(a,I)(Eo-Ru*T/F*log((I+a(4))/a(1))+Ru*T/2./F*log((a(2)-(I+a(4)))/a(2))-I*a(3));
I_exp = [0,2.5,5,10,20,40,60,80,100,200,400,600,800,1000,1200];
V_exp = [0.986615,0.950774,0.951704,0.916295,0.910731,0.90338,0.857798,0.853067,0.851497,0.799984,...
0.730907,0.689608,0.638525,0.57848,0.440924];
Irange = min(I_exp):1:max(I_exp);
%%Initial guess & Brute-force minimize error function
a(1)=0.5; %Io: Reference exchange current density mA/cm2
a(2)=1200.00; %I_lim: Limiting current density mA/cm2
a(3)=2.5e-4; %total resistance kohm*cm2
a(4)=21.0; %cross-over current density mA/cm2
g = @(a) norm(V_mdl(a,I_exp)-V_exp);
ahat = fminsearch(g,a); % Values Calculated
V_cell = @(I) V_mdl(ahat,I);
%%Plotting and figures
figure(1)
scatter(I_exp,V_exp,'ok','MarkerFaceColor','b')
hold on
plot(Irange,V_cell(Irange),'-r','linewidth',2)
grid on
title('H_{2} - O_{2} Proton Exchange Membrane Fuel Cell - Voltage vs Current Density')
xlabel('Current Density [i, mA/cm^2]')
ylabel('Voltage [V]')
legend('Polarization Curve','Location', 'Best');
R=V_cell(Irange);
%%Calculation of power
i=0;
P_out=zeros;
for j=1:1201
P_out(j)= V_cell(j)*i;
i=i+1;
end
Pmax=max(P_out);
%%Plotting Power
figure(2)
hold on
title('H_{2} - O_{2} Proton Exchange Membrane Fuel Cell Power')
xlabel('Current Density [i, mA/cm^2]');
ylabel('Power Density [W/cm^2]');
plot(Irange,P_out,'-b','linewidth',2);
legend('Power Curve','Location', 'Best');
grid on
axis([0 1200 0 600])
Max_Power=max(P_out)
Max_cdp=Irange(P_out==Max_Power)
%%Calculation of Efficiency
i=0;Eta_tot=zeros;
for j=1:1201
Eta_fc=(V_cell(j)/Eo);
Eta_fuel=(i/(i+ahat(4)));
Eta_tot(j)=Eta_fc*Eta_fuel;
i=i+1;
end
Current=zeros;
for j=1:1201
P_kWh_req=80000*100;
P_kWh_fc=80000/1000*Max_Power;
Area_fc=P_kWh_req/P_kWh_fc;
Install_cost(j)=Area_fc*1500*10000;
Current(j)=j*Area_fc/1000/(j/(j+ahat(4)));
Mol_H2(j)=j*80000*3600/2/96458;
Mass_H2=Mol_H2(j)*2/1000;
Fuel_cost(j)=Mass_H2*4;
end
%%Plotting Efficiency
figure(3)
hold on
title('H_{2} - O_{2} Proton Exchange Membrane Fuel Cell Efficiency')
xlabel('Current Density [i, mA/cm^2]');
ylabel('Efficiency [\eta]');
plot(Irange,Eta_tot,'-r','linewidth',2);
legend('Efficiency Curve','Location', 'Best');
grid on
axis([0 1200 0 1])
Max_Eff=max(Eta_tot)
Max_cde=Irange(Eta_tot==Max_Eff)
%%Optimize(minimize) Cost
for j=1:1201
f=@(j) Install_cost+Fuel_cost ;
[low,high]=fminbnd(f,1,1201)
end

답변 (1개)

John D'Errico
John D'Errico 2017년 4월 23일
편집: John D'Errico 2017년 4월 23일
READ THE ERROR MESSAGE!
fminbnd expects scalar input, and the function MUST return scalar output.
numel(f(1))
ans =
1201
Does f return a scalar? NO.
Return to step 1. READ THE ERROR MESSAGE. Think about what it is telling you. While I have no idea what the function f SHOULD be doing, it is not doing what fminbnd expects.
Admittedly, I also have no idea what you think lines like this are there for:
j=linspace(0,1200,1)
j =
1200
So for someone else to try to debug code that has random statements in it like that is next to impossible.
  댓글 수: 6
DIP
DIP 2017년 4월 23일
Maybe I have defined the function handle wrongly. ive tried all combinations but i keep getting errors
DIP
DIP 2017년 4월 23일
What i basically need is to debug this part of the code
for j=1:1201
f=@(j) Install_cost+Fuel_cost ;
[low,high]=fminbnd(f,1,1201)
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by