Multi-variable fminsearch: Not enough input arguments

I am getting 'Not enough input arguments' as result. Need help.
Function:
function qout = Lobo(d0, L, Tg, nb, uo)
clc
%Fuel flowrate
Ti = 251.9 + 273.15; %Fluid in degK
T0 = 386.1 + 273.15; %Fluid/gas out degK
cpL = 0.32; %Cp fluid KJ/kgdegK
flowL = 500; %flowrate fluid kg/s
THD = flowL.*cpL.*(T0 - Ti);%Total Heat Duty
Ef = 0.9320; %Fuel efficiency
qfuel = THD./Ef;%Heat released by fuel Kw
LHV = 41.05; %Fuel value KJ/kg
mfuel = qfuel./LHV;
%absorptivity
hp = 6.626*(10^-34); %planck's constant m2kg/s
cl = 3*(10^8); %speed of light m2/s
lamda = hp*cl/qfuel; %wavelength
Avog = 6.023*10^24; %Avogadro's number
x1 = 0.9;
M1 = 16;
x2 = 0.1;
M2 = 29;
fatmass = x1*M1 + x2*M2; %fuel molecular mass
nA = mfuel*Avog*fatmass; %number of atoms
pi = 22/7;
alpha = 4.*pi.*nA./lamda;
%Exchange factor
x = pi.*d0/2; %ctc
F = ((sqrt(x.^2 - 4) - x + 2.*sinh(2./x))/2.*pi);
%Heat transfer coefficient
k = 0.25*10^-3; %Thermal conductivity
%rof = 1320; %density
Gm = 920880; %mass flowrate
mu = 130; %Viscosity Pa-s
A0 = pi.*d0.*d0./4;
h0 = ((d0./k).*(0.023.*((Gm.*d0./mu.*A0).^0.8).*((cpL.*mu./k)).^0.3));
%Tube wall temperature
Tref = 41; %Ambient
Tw = Tref + (Ti - T0)/L;
%Air
ea = 0.15; %excess air
cpair = 0.171; %Kcal/kgdegK
Tairin = 114;
atfr = 1.344; %in notebook
mair = mfuel.*atfr.*(1+ea);
%Flame calculation
cpsteam = 4.1855;
cpco2 = 0.918;
cpn2 = 1.044;
xco2 = 0.054;
xsteam = 0.034; %Total should be 8.8
xn2 = 0.66;
cpgas = xsteam.*cpsteam + xco2.*cpco2 + xn2.*cpn2;
kgas = 0.25; %Thermal conductivity
mgas = mfuel + mair;
Lf = 0.0042.*(qfuel.^0.478); %flame length
tf = sqrt(pi.*uo.*Lf.*cpgas./mgas.*kgas); %flame size
db = 1.5.*tf; %Burner tile diameter
dbc = db./(sin(180./nb) + (pi./nb)); %Burner circle diameter
%Cold plane area
cbt = (qfuel.*1.055/4*10^6) + 1.5;
dtc = cbt + dbc; %tube circle diameter
ntubes = pi.*dtc./x;
Ar = L.*ntubes.*x;
%Radiant heat flux
stef = 5.67*10^-8; %Stefan-Boltzmann constant
qr = stef.*alpha.*F.*(Tg.^4 - Tw.^4) + ho.*(Tg - Tw);
%qr = stef*alpha*((sqrt((pi*d0)^2 - 16) - 2*x + 2*sinh(4/pi*d0))/4*pi)*(Tg^4 - (Tref + (Ti - T0)/L)^4) + ((d0/k)*(0.023*((Gm*d0/mu*pi*d0*d0/4)^0.8)*((cpL*mu/k))^0.3))*(Tg - (Tref + (Ti - T0)/L));
%Heat leaving
Qr = qr .* Ar; %total radiant heat absorbed
%Qr = qr * L*pi*(((qfuel*1.055/4*10^6) + 1.5) + ((1.5*(sqrt(pi*uo*Lf*cpgas/mgas*kgas)))/(sin(180/nb) + (pi/nb))));
qair = mair.*cpair.*(Tairin - Tref);
qloss = 0.05.*qfuel;
qout = qfuel + qair - ((stef.*alpha.*((sqrt((pi.*d0).^2 - 16) - 2.*x + 2*sinh(4/pi.*d0))/4*pi).*(Tg.^4 - (Tref + (Ti - T0)/L).^4) + ((d0/k).*(0.023.*((Gm.*d0/mu.*pi.*d0.*d0/4)^0.8).*((cpL.*mu./k)).^0.3)).*(Tg - (Tref + (Ti - T0)/L)).*L.*pi.*(((qfuel.*1.055/4*10^6) + 1.5) + ((1.5.*(sqrt(pi.*uo.*Lf.*cpgas./mgas.*kgas)))/(sin(180/nb) + (pi/nb))))) + qloss);
end
Solution:
%d0 = 0.01:1;
%L = 1:18;
%Tg = 1:1950;
%nb = 8:12;
%uo = 1:50;
x0 = [0.01, 1, 10, 8, 1];
options = optimset('PlotFcns',@optimplotfval);
X = fminsearch(@Lobo, x0, options);
qr = @Lobo;
plot (d0,qr)
xlabel('Tube circle diameter (d0)');
ylabel('flux (qr)');

Answers (1)

X = fminsearch(@Lobo, x0, options);
invokes Lobo with one input vector which is the same length as x0.
You probably want
X = fminsearch(@(x) Lobo(x(1),x(2),x(3),x(4),x(5)), x0, options);

22 Comments

New code:
x(1) = d0;
x(2) = L;
x(3) = Tg;
x(4) = nb;
x(5) = uo;
x0 = [0.01, 1, 10, 8, 1];
options = optimset('PlotFcns',@optimplotfval);
X = fminsearch(@(x)Lobo(x(1),x(2),x(3),x(4),x(5)), x0, options);
Error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in Lobo_Solution (line 2)
x(2) = L;
Delete the first five lines of your new code.
Best wishes
Torsten.
One last thing. How can I get to consider only the positive values of qout?
So you get a solution for x for which "qout" is negative, but a negative "qout" is not feasible ?
Its a fired heater design. Negative qout means it is adding heat to the system. Which is not feasible. We need to reduce qout to lowest value possible but, we have to make sure it is tending to zero. Also, additional condition is that qr should lie between 32000 to 45000.
And qout should always stay positive.
Also, could you tell me, how to add this if else condition:
if qr>32000 && qr<45000
%Heat leaving
Qr = qr .* Ar; %total radiant heat absorbed
%Qr = qr * L*pi*(((qfuel*1.055/4*10^6) + 1.5) + ((1.5*(sqrt(pi*uo*Lf*cpgas/mgas*kgas)))/(sin(180/nb) + (pi/nb))));
qair = mair.*cpair.*(Tairin - Tref);
qloss = 0.05.*qfuel;
qout = (qfuel + qair - ((stef.*alpha.*((sqrt((pi.*d0).^2 - 16) - 2.*x + 2*sinh(4/pi.*d0))/4*pi).*(Tg.^4 - (Tref + (Ti - T0)/L).^4) + ((d0/k).*(0.023.*((Gm.*d0/mu.*pi.*d0.*d0/4)^0.8).*((cpL.*mu./k)).^0.3)).*(Tg - (Tref + (Ti - T0)/L)).*L.*pi.*(((qfuel.*1.055/4*10^6) + 1.5) + ((1.5.*(sqrt(pi.*uo.*Lf.*cpgas./mgas.*kgas)))/(sin(180/nb) + (pi/nb))))) + qloss));
%qout = Qout(Qout>=0);
else
return;
end
Error:
Output argument "qout" (and maybe others) not assigned during call to "C:\Users\Devdatt.thengdi\Desktop\Furnace\Design Method\Lobo.m>Lobo".
If you want to put constraints on some of your design variables, use "fmincon" instead of "fminsearch".
Best wishes
Torsten.
Yes. I have tried it. But, the constraint is not to the variable defined in the objective equation but, a predecessor of it i.e. I am trying to minimize qout (which is the output if the function Lobo) but, the constraint is defined for the qr (which is used to define qout but, isn't included in the equation) Get it?
fmincon, non-linear constraint function. qr can be calculated from the inputs so it can be constraint by a non-linear constraint function.
I suggest extracting all of the logic used to calculate qr into a function that you call from both the objective function and the nonlinear constraint function. This might involve calculating other variables as well in order to be able to calculate qr.
But qr is a function of the design variables, say qr = f(x1,...,x5), so you can constrain it by using the nonlinear constraint function of fmincon to set f(x1,...,x5) <= some value.
Can you give me an example code or something? Can I call the value of qr inequality constraint as @qr<=45000? Is it correct? Also, how do you set a range for it since, it varies between 32000 and 45000?
function [c,ceq] = nonlcon(d0, L, Tg, nb, uo)
%Fuel flowrate
Ti = 251.9 + 273.15; %Fluid in degK
T0 = 386.1 + 273.15; %Fluid/gas out degK
cpL = 0.32; %Cp fluid KJ/kgdegK
flowL = 500; %flowrate fluid kg/s
THD = flowL.*cpL.*(T0 - Ti);%Total Heat Duty
Ef = 0.9320; %Fuel efficiency
qfuel = THD./Ef;%Heat released by fuel Kw
LHV = 41.05; %Fuel value KJ/kg
mfuel = qfuel./LHV;
%absorptivity
hp = 6.626*(10^-34); %planck's constant m2kg/s
cl = 3*(10^8); %speed of light m2/s
lamda = hp*cl/qfuel; %wavelength
Avog = 6.023*10^24; %Avogadro's number
x1 = 0.9;
M1 = 16;
x2 = 0.1;
M2 = 29;
fatmass = x1*M1 + x2*M2; %fuel molecular mass
nA = mfuel*Avog*fatmass; %number of atoms
pi = 22/7;
alpha = 4.*pi.*nA./lamda;
%Exchange factor
x = pi.*d0/2; %ctc
F = ((sqrt(x.^2 - 4) - x + 2.*sinh(2./x))/2.*pi);
%Heat transfer coefficient
k = 0.25*10^-3; %Thermal conductivity
%rof = 1320; %density
Gm = 920880; %mass flowrate
mu = 130; %Viscosity Pa-s
A0 = pi.*d0.*d0./4;
h0 = ((d0./k).*(0.023.*((Gm.*d0./mu.*A0).^0.8).*((cpL.*mu./k)).^0.3));
%Tube wall temperature
Tref = 41; %Ambient
Tw = Tref + (Ti - T0)/L;
%Air
ea = 0.15; %excess air
cpair = 0.171; %Kcal/kgdegK
Tairin = 114;
atfr = 1.344; %in notebook
mair = mfuel.*atfr.*(1+ea);
%Flame calculation
cpsteam = 4.1855;
cpco2 = 0.918;
cpn2 = 1.044;
xco2 = 0.054;
xsteam = 0.034; %Total should be 8.8
xn2 = 0.66;
cpgas = xsteam.*cpsteam + xco2.*cpco2 + xn2.*cpn2;
kgas = 0.25; %Thermal conductivity
mgas = mfuel + mair;
Lf = 0.0042.*(qfuel.^0.478); %flame length
tf = sqrt(pi.*uo.*Lf.*cpgas./mgas.*kgas); %flame size
db = 1.5.*tf; %Burner tile diameter
dbc = db./(sin(180./nb) + (pi./nb)); %Burner circle diameter
%Cold plane area
cbt = (qfuel.*1.055/4*10^6) + 1.5;
dtc = cbt + dbc; %tube circle diameter
ntubes = pi.*dtc./x;
Ar = L.*ntubes.*x;
%Radiant heat flux
stef = 5.67*10^-8; %Stefan-Boltzmann constant
qr = stef.*alpha.*F.*(Tg.^4 - Tw.^4) + ho.*(Tg - Tw);
% Set constraints
ceq = [];
c(1) = qr - 45000; % qr <= 45000
c(2) = -qr + 32000; % qr >= 32000
end
That code uses the undefined variable ho . Perhaps you mean h0 ?
In the line
cbt = (qfuel.*1.055/4*10^6) + 1.5;
could you verify that you want to take 1.055, divide by 4, and multiply the result by 10^6? Or did you want to divide by (4*10^6) ?
Why are you using
pi = 22/7
when more accurate versions are easily available?
%set conditions
ceq=[];
c(1) = qr - 45000;
c(2) = -qr +32000;
x0 = [0.01, 1, 10 + 273.15, 8, 1];
options = optimset('PlotFcns',@optimplotfval);
X = fmincon(@(x)Lobo(x(1),x(2),x(3),x(4),x(5)), x0, [], [], [], [], [], [], @(c)Lobo(c(1), c(2)), options);
disp (X);
Error: Error using Lobo Too many output arguments.
Error in @(c)Lobo(c(1),c(2))
Error in fmincon (line 722) [ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in Lobo_Solution (line 4) X = fmincon(@(x)Lobo(x(1),x(2),x(3),x(4),x(5)), x0, [], [], [], [], [], [], @(c)Lobo(c(1), c(2)), options);
Caused by: Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue.
@WalterRoberson What alternative versions?
MATLAB predefines pi (lowercase, just like you used) to be the most accurate representation of π possible in double precision
Okay. Thanks. Could you please help me with that error?
Your line
X = fmincon(@(x)Lobo(x(1),x(2),x(3),x(4),x(5)), x0, [], [], [], [], [], [], @(c)Lobo(c(1), c(2)), options);
tries to use your objective function, Lobo, as also being the nonlinear constraint function. But the nonlinear constraint function needs to have two outputs and Lobo only has one output, so you cannot use Lobo there. You should be calling your nonlcon in that position before options. And make sure you pass in all of the arguments that your nonlcon needs -- your anonymous function is only passing in the first two elements of the parameters but your nonlcon expects 5 inputs.
Thanks. One Last thing. In a multiobjective function (e.g. gamultiobj) how do you minimize one function and maximize the other?
Set F=[F1,-F2] - then both functions F1 and F2 are to be minimized.
Best wishes
Torsten.

Sign in to comment.

Tags

Asked:

on 7 Mar 2018

Commented:

on 13 Mar 2018

Community Treasure Hunt

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

Start Hunting!