Failure in initial objective function evaluation. FSOLVE cannot continue.

4 views (last 30 days)
Right after the second while loop in the code below, I am calling fsolve on a function that solves for 5 unknowns. I keep getting the error "Failure in initial objective function evaluation. FSOLVE cannot continue." I have looked at several other posts with this issue, but wasn't able to figure out what is wrong with my code. I also have another code that is very similar to this one and works just fine.
p1 = 0.01; %atm (pressure @ 1)
T1 = 200; %K (temp @ 1)
u1 = 500; %m/s (speed @ 1)
n_N2 = 0.79; %mols
n_O2 = 0.21; %mols
M_N2 = 28.014; %kg/kmol
M_O2 = 31.998; %kg/kmol
M_N = 14.007; %kg/kmol
M_O = 15.999; %kg/kmol
M_NO = 30.006; %kg/kmol
R = 8.314459848; %kJ/K*kmol
rho1 = p1*(n_N2*M_N2 + n_O2*M_O2)/((n_N2 + n_O2)*R*T1);
h1 = n_N2*cp_n2(T1)*T1 + n_O2*cp_o2(T1)*T1;
rho2_g = rho1/0.1;
counter = 0;
error = 1.0;
x0 = [0.1, 0.1, 0.1, 0.1, 0.1];
while error > 0.0001 || counter <= 10000
p2 = p1 + rho1*u1^2*(1 - rho1/rho2_g);
h2 = h1 + (u1^2/2.0)*(1 - (rho1/rho2_g)^2);
T2_g = 300.00;
err = 1.0;
count = 0;
while err < 0.001 || count <=10000
X = fsolve(@(x)Xi_eflow(x, p2, T2_g), x0);
h2_g = X(1)*cp_n2(T2_g)*T2_g + X(2)*cp_o2(T2_g)*T2_g + ...
X(3)*cp_o(T2_g)*T2_g + X(4)*cp_n(T2_g)*T2_g + ...
X(5)*cp_no(T2_g)*T2_g;
err = abs(h2 - h2_g);
if err > 0.0001
T2_g = (T2 + T2_g)/2.0;
end
if count == 10000
z = count
end
end
rho2 = p2*(Xn2*M_N2 + Xo2*M_O2 + Xn*M_N + Xo*M_O + Xno*M_NO)...
/((Xn2 + Xo2 + Xn + Xo + Xno)*R*T2_g);
if error > 0.001
rho2_g = (rho2_g + rho2)/2
end
if counter == 10000
y = counter
end
end
Below is the function that fsolve is used on.
function F = Xi_eflow(x, P, T)
R = 8.314459848; %kJ/kmol*K
Xn2 = x(1);
Xo2 = x(2);
Xo = x(3);
Xn = x(4);
Xno = x(5);
Go = -1.011066E-17*T^6 + 1.816726E-13*T^5 - 1.317613E-9*T^4 + ...
4.993102E-6*T^3 - 1.081611E-2*T^2 - 53.86166*T + 248642;
Gn = -9.090730E-18*T^6 + 1.667190E-13*T^5 - 1.243770E-9*T^4 + ...
4.874266E-6*T^3 - 1.115735E-2*T^2 - 52.39920*T + 471978.9;
Gno = 2*(7.363399E-20*T^6 - 1.268925E-15*T^5 + 3.387909E-12*T^4 + ...
5.794901E-8*T^3 - 2.905865E-4*T^2 - 12.27637*T + 90286.60);
F(1) = Xn2 + Xo2 + Xo + XN + Xno - 1;
F(2) = (2*Xn2 + Xn + Xno)/(2*Xo2 + Xo + Xno) - (0.79/0.23);
F(3) = exp(-Go/(R*T)) - Xo*P^(0.5)/Xo2^(0.5);
F(4) = exp(-Gn/(R*T)) - Xn*P^(0.5)/Xn2^(0.5);
F(5) = exp(-Gno/(R*T)) - Xno^2/(Xn2*Xo2);
end
Any help is appreciated. Thank you!
Edit: Forgot to put some other functions on here.
function y = cp_n(x)
y = 9.982621E-22*x^6 - 1.872004E-17*x^5 + 1.321452E-13*x^4 - ...
3.827231E-10*x^3 + 4.821461E-7*x^2 - 2.469692E-4*x + 20.82571;
end
function y = cp_n2(x)
y = 2.693757E-20*x^6 - 4.651184E-16*x^5 + 3.103549E-12*x^4 - ...
9.831534E-9*x^3 + 1.383600E-5*x^2 - 2.998751E-3*x + 28.88362;
end
function y = cp_no(x)
y = 2.489814E-20*x^6 - 4.154027E-16*x^5 + 2.638902E-12*x^4 - ...
7.714111E-9*x^3 + 9.019032E-6*x^2 + 1.658686E-3*x + 28.56333;
end
function y = cp_o(x)
y = 1.232504E-20*x^6 - 2.139706E-16*x^5 + 1.462356E-12*x^4 - ...
4.982123E-9*x^3 + 8.915414E-6*x^2 - 7.957834E-3*x + 23.62581;
end
function y = cp_o2(x)
y = 9.818398E-22*x^6 + 1.336256E-17*x^5 - 3.741595E-13*x^4 + ...
2.662210E-9*x^3 - 8.689588E-6*x^2 + 1.570265E-2*x + 25.37744;
end
  2 Comments
Matt J
Matt J on 19 Apr 2019
Here's what I get when I run your code,
Undefined function or variable 'cp_n2'.
Error in test (line 13)
h1 = n_N2*cp_n2(T1)*T1 + n_O2*cp_o2(T1)*T1;
Andrew
Andrew on 19 Apr 2019
Sorry, I forgot to give the functions for my cp values. I have added them above. Sorry about that.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 19 Apr 2019
Instead of XN, in this line
F(1) = Xn2 + Xo2 + Xo + XN + Xno - 1;
should it be Xn?

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!