Just to add, the error is "nonlinear constraint function is undefined at initial point. fmincon cannot continue."
Getting an fmincon error at initial point
13 views (last 30 days)
Show older comments
Justyn Welsh
on 27 Feb 2024
Commented: Justyn Welsh
on 29 Feb 2024
Below is my code. I am assuming issue is some negative number getting caught up where it should be positive but I have not been able to figure it out:
% solve the optimization problem from here
f = @(x)obj(x); % declare objective function
lb = [0, 0, -Inf, -Inf, -Inf, 0, 0, -Inf]; % lower bounds on x - CHANGE WITH UNCERTAINTY
ub = []; % upper bounds on x
A = []; % linear inequality constraints - NONE
b = []; % NONE
Aeq = []; % linear equality constraints - NONE
beq = []; % NONE
p = [20, 22]; % parameters for nonlinear constraints
nonlcon = @(x)model(x,p); % model for nonlinear constraints
% initial guess and algorithm
x0 = [10;2;3;3;21;30;90;120]; % x = (L, r, vt, vs, T_t(out), T_s(in), T_s,(out), T_t(in)
options = optimoptions(@fmincon, 'Algorithm', 'SQP'); % use SQP algorithm
% call the SQP solver to find solution to the problem
[x,fval,exitflag,output,lambdab] = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
function f = obj(x) % objective function (radius and length to be minimized)
f = ((0.10*x(2))+1)*(15*x(1))-150;
end
function [g,h] = model(x,p)
% Implement the constraints that define the model
% Use h for equality constraints and g for inequality constraints
cpt = 2.220; % specific heat of gasoline [kJ/kgK] - will be an UNCERTAINTY VALUE
cps = 4.184; % specific heat of water [kJ/kgK]
rt = 730; % density of gasoline [kg/m^3]
rs = 997; % density of water [kg/m^3]
ut = 20; % dynamic viscosity of gasoline [mPa*s] - this value is at roughly 120 C for gasoline (uncertainity spans this range)
us = 1; % dynamic viscosity of water [mPa*s] - this value is at the temperature of my initial guess for water
kt = 120; % thermal conductivity of gasoline [W/m*K] - maybe uncertainty?
ks = 0.598; % thermal conductivity of water [W/m*K]
h_f = 0.001; % thermal resistance of the fouling [K/W] - provided constant based off average fouling value for stainless steel heat exchangers
% all given values
t_LM = (((x(8)-x(7))-(x(6)-x(5)))/log((x(8)-x(7))/x(6)-x(5))); % deltaTLM calcuation - REMEMBER X(9) IS UNCERTAINTY ALSO FIX THE TEMP VALUES
Re_t = (rt*x(3)*x(1))/ut; % Reynolds # of gasoline
Re_s = (rs*x(4)*x(1))/us; % Reynolds # of water
Pr_t = (ut*cpt)/kt; % Prandtl # of gasoline
Pr_s = (us*cps)/ks; % Prandtl # of water
Nu_t = 0.023*(Re_t^0.8)*(Pr_t^0.4); % Nusselt # of gasoline
Nu_s = 0.023*(Re_s^0.8)*(Pr_s^0.4); % Nusselt # of water
h_t = (Nu_t*kt)/x(1); % thermal coefficient of gasoline
h_s = (Nu_s*ks)/x(1); % thermal coefficient of water
R_t = 1/(h_t*pi*(x(2)^2)); % thermal resistance of the tube
R_s = 1/(h_s*pi*(x(2)^2)); % thermal resistance of the shell
R_f = 1/(h_f*pi*(x(2)^2)); % thermal resistance of the fouling
R_total = 1/((1/R_t) + (1/R_s) + (1/R_f)); % total thermal resistance
U = 1/R_total; % overall heat transfer coefficient
Q = U*pi*(x(2)^2)*t_LM; % heat value
% all relevant equations to the system
h = zeros(1,1); % equality constraints
h(1) = (pi*(x(1)^2)*cpt)*(x(5)-x(8)) - (pi*(x(1)^2)*cps)*(x(6)-x(7)) - Q; % energy balance
g = zeros(2,1); % inequality constraints - p is referenced in fmincon setup
g(1) = p(1) - x(5); % final temperature of gasoline cannot exceed 22
g(2) = x(5) - p(2); % final temperature of gasoline must exceed 20
end
Thank you!
Accepted Answer
Walter Roberson
on 27 Feb 2024
t_LM = (((x(8)-x(7))-(x(6)-x(5)))/log((x(8)-x(7))/x(6)-x(5))); % deltaTLM calcuation - REMEMBER X(9) IS UNCERTAINTY ALSO FIX THE TEMP VALUES
That is coming out complex-valued at x0, which gives the result that h (the nonlinear constraint) becomes complex-valued.
2 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!