I have a code, but it keeps saying "Supplied objective function must return a scalar value.'
Mostrar comentarios más antiguos
function Moles = Conv_func(H2, CO2, CO, CH4, H2O, T0, Pt, e11, e12, e13, e14)
% Call Sh_func to obtain entropy values
Sh_H2 = Sh_func('H2', T0);
Sh_CO2 = Sh_func('CO2', T0);
Sh_CO = Sh_func('CO', T0);
Sh_CH4 = Sh_func('CH4', T0);
Sh_H2O = Sh_func('H2O', T0);
% Compute standard entropies of reaction
Sr1 = (Sh_CH4(1) + Sh_CO2(1)) - (2*Sh_CO(1) + 2*Sh_H2(1)); % 1st reaction
Sr2 = (Sh_CO2(1) + Sh_H2(1)) - (Sh_CO(1) + Sh_H2O(1)); % 2nd reaction
Sr3 = Sh_CH4(1) - (2*Sh_H2(1)); % 3rd reaction
Sr4 = Sh_CO2(1) - (2*Sh_CO(1)); % 4th reaction
% Compute standard enthalpies of reaction
Hr1 = (Sh_CH4(2) + Sh_CO2(2)) - (2*Sh_CO(2) + 2*Sh_H2(2)); % 1st reaction
Hr2 = (Sh_CO2(2) + Sh_H2(2)) - (Sh_CO(2) + Sh_H2O(2)); % 2nd reaction
Hr3 = Sh_CH4(2) - (2*Sh_H2(2)); % 3rd reaction
Hr4 = Sh_CO2(2) - (2*Sh_CO(2)); % 4th reaction
% Compute equilibrium constants
k1 = exp(-Hr1*1000/(8.314*T0) + Sr1/8.314); % 1st reaction
k2 = exp(-Hr2*1000/(8.314*T0) + Sr2/8.314); % 2nd reaction
k3 = exp(-Hr3*1000/(8.314*T0) + Sr3/8.314); % 3rd reaction
k4 = exp(-Hr4*1000/(8.314*T0) + Sr4/8.314); % 4th reaction
% Solve for conversion using fminunc optimizer
e0 = [e11; e12; e13; e14]; % Initial condition for solver
r = fminunc(@(e) Sol_func(e, H2, CO2, CO, CH4, H2O, k1, k2, k3, k4, Pt), e0);
% Compute moles of species
Moles(1) = H2 + 2*r(1) - r(2) + 2*r(3);
Moles(2) = CO2 - r(1) - r(2) - r(4);
Moles(3) = CO + 2*r(1) + r(2) + 2*r(4);
Moles(4) = CH4 - r(1)- r(3);
Moles(5) = H2O - r(2);
end
my sol_func is :
function z = Sol_func(e, H2, CO2, CO, CH4, H2O, k1, k2, k3, k4, Pt)
e1 = e(1);
e2 = e(2);
e3 = e(3);
e4 = e(4);
ntot = H2 + CO2 + CH4 + CO + H2O; %Total number of moles
z = zeros(4, 1);
z(1) = (CH4 - e1 - e3) * (CO2 - e1 - e2 - e3) * k1 - (ntot + 2*e1+ e3+ e4)^-2 * (CO + 2*e1 + e2 + 2*e4)^2 * (H2 + 2*e1 + 2*e3)^2 * (Pt)^(-2);
z(2) = (H2 + 2*e1 + 2*e3) * (CO2 - e1 - e2 - e3) * k2 - (CO + 2*e1 + e2 + 2*e4) * (H2O + e2) * (Pt)^(-2);
z(3) = (CH4 - e1 - e3) * k3 - (ntot + 2*e1+ e3+ e4)^-1 * (H2 + 2*e1 + 2*e3)^2 * (Pt)^(-2);
z(4) = (CO2 - e1 - e2 - e3) * k4 - (ntot + 2*e1+ e3+ e4)^-1 * (CO + 2*e1 + e2 + 2*e4)^2 * (Pt)^(-2);
end
Respuestas (1)
I have a code, but it keeps saying "Supplied objective function must return a scalar value.'
Yes, that's true. Your function "Sol_func" must return a scalar value instead of a 4x1 vector. "fminunc" will then try to minimize this scalar value. Maybe you want to have the z(i) equal to 0 ; then you should return sum(z.^2).
Categorías
Más información sobre Simulate Responses to Biological Variability and Doses en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!