Clear Filters
Clear Filters

Getting an fmincon error at initial point

3 views (last 30 days)
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)
Error using sqpInterface
Nonlinear constraint function is undefined at initial point. Fmincon cannot continue.

Error in fmincon (line 900)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = sqpInterface(funfcn,X,full(A),full(B),full(Aeq),full(Beq), ...
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!
  1 Comment
Justyn Welsh
Justyn Welsh on 27 Feb 2024
Just to add, the error is "nonlinear constraint function is undefined at initial point. fmincon cannot continue."

Sign in to comment.

Accepted Answer

Walter Roberson
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
Justyn Welsh
Justyn Welsh on 29 Feb 2024
thank you! this solved my issue - but I've ran into something new I wouldn't mind extra eyes in. I now have attempt to set up a Monte Carlo analysis. I am now getting a complex value, yet by running the numbers by hand for my range of values (MC(1)), it should be a standard integer. I know that somewhere the MC(1) is getting an incorrect value, but I am unsure of where:
% sample the uncertainty set and solve the optimization problem at each
% value of uncertainty
meanVal = [125; 2.22]; % mean value for temperature of incoming gasoline & specific heat of gasoline
sigma = [1; 0.03]; % sigma values relating to the above values
iterations = 2000; % arbitrary iteration value
OptimalLR = zeros(iterations, 1); % setting up zeros to hold our data
for i = 1:iterations % iterations setup
MC = mcarlo(meanVal, sigma); % pulling monte carlo info
LRopt = LR(MC); % getting updated values
OptimalLR(i) = LRopt(1); % instructing a new variable to take the 6th parameter (V) within the zeros we set up earlier
end
function f = obj(x) % objective function (cost pipiing (radius and length) to be minimized)
f = ((0.10*x(2))+1)*(15*x(1));
end
function [g,h] = model(x,MC)
% Implement the constraints that define the model
% Use h for equality constraints and g for inequality constraints
cpt = MC(2); % specific heat of gasoline [kJ/kgK] - MC(2) is specific heat of gasoline uncertainity
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 constant values
t_LM = ((MC(1) - x(7)) - (x(5) - x(6))) / log((MC(1) - x(7)) / (x(5) - x(6))); % deltaTLM calcuation - MC(1) is temperature incoming of gasoline uncertainty
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
p = [20, 22, 150]; % parameters for nonlinear constraints
% all relevant equations to the system
h = zeros(2,1); % equality constraints
h(1) = (pi*(x(1)^2)*cpt)*(x(5)-MC(1)) - (pi*(x(1)^2)*cps)*(x(6)-x(7)) - Q; % energy balance
g = zeros(3,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
g(3) = p(3) - ((0.10*x(2))+1)*(15*x(1)); % total cost of piping cannot exceed $150 based on piping length being $15/m and radius adding 10% to the cost to scale
end
function MC = mcarlo(meanVal, sigma) % setting up monte carlo method
MC = meanVal + sigma.*randn(size(meanVal)); % inspired from p=sigma.*randn+kmean from lecture
MC = min(max(MC, [120, 130]), [2.1534, 2.2866]);
end
function LRopt = LR(MC) % this becomes our new optimization problem now that we are subjecting our variables to uncertainty
% solve the optimization problem from here
f = @(x)obj(x); % declare objective function
lb = [0, 0, -Inf, -Inf, -Inf, 0, 0]; % lower bounds on x
ub = []; % upper bounds on x
A = []; % linear inequality constraints - NONE
b = []; % NONE
Aeq = []; % linear equality constraints - NONE
beq = []; % NONE
nonlcon = @(x)model(x,MC); % model for nonlinear constraints
% initial guess and algorithm
x0 = [10;2;3;3;30;20;80]; % x = (L, r, vt, vs, T_t(out), T_s(in), T_s,(out))
options = optimoptions(@fmincon, 'Algorithm', 'SQP'); % use SQP algorithm
% call the SQP solver to find solution to the problem
LRopt = fmincon(f,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
end
Justyn Welsh
Justyn Welsh on 29 Feb 2024
Actually, I have realized my own issue here. My values were screwed up in the MC(min(max). Now it is converging to an infeasible point. I will need to troubleshoot. Thank you for the help!

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!