Running into issue using fsolve

4 views (last 30 days)
Justyn Welsh
Justyn Welsh on 8 Mar 2024
Commented: Matt J on 8 Mar 2024
Trying to solve a problem while using fsolve and fmincon. I am getting correct values, but the solvers are running into a large amount of issues. Getting the following error message:
"Warning: Trust-region-dogleg algorithim of FSOLVE cannot handle non-square systems; using Levenberg-Marquardt algorithim instead."
and
"fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance."
Here is my code:
% solve the optimization problem here
options = optimoptions('fmincon','Algorithm','sqp'); % use SQP algorithm
A = []; % linear inequality constraints - NONE
b = []; % NONE
Aeq = []; % linear equality constraints - NONE
beq = []; % NONE
lb = [0.36, 0.05]; % lower bounds on x
ub = [0.44, 0.06]; % upper bounds on x
f=@(p)obj(p,yData);% objective function
nonlcon=[];
p0 = [0.4,0.055];% initial guess from previous problems
[p,fval,exitflag,output,lambda] = fmincon(f,p0,A,b,Aeq,beq,lb,ub,nonlcon,options);
idata = length(yData);
yModel = zeros(idata,1);
for i = 1:idata
F1 = yData(i,1);
x = [0.33; yData(i,2); 0.33; F1];
x = fsolve(@(x)model(x, F1, p),x);
yModel(i) = x(2);
end
function f = obj(p,yData)
% implement your objective function here - using least-squares method
% p are uncertain model parameters (optimization variables)
% yData is the matrix of experimental data (inputs & outputs)
idata = length(yData);
yModel = zeros(idata,1);
options2 = optimoptions(@fsolve,'Display','off');
for i = 1:idata
F1 = yData(i,1);
x0 = [0.33; 0.33; 0.33; F1];
in = @(x)model(x, F1, p);
out = fsolve(in, x0, options2);
yModel(i) = out(2);
end
f = norm(yData(:,2) - yModel)^2;
end
function h = model(x, F1, p)
% Implement the constraints that define the model
k1 = p(1);
k2 = p(2);
Va = 0.08937;
Vb = 0.1018;
Vc = 0.113;
Ya0 = 1;
Yb0 = 0;
Yc0 = 0;
V = 10;
r1 = (k1*x(1))/(x(1)*Va + x(2)*Vb + x(3)*Vc);
r2 = (k2*x(2))/(x(1)*Va + x(2)*Vb + x(3)*Vc);
h = zeros(1:4);
h(1) = x(1)*x(4) + V*r1 - F1;
h(2) = x(2)*x(4) + V*(r2 - r1);
h(3) = x(3)*x(4) - V*r2;
h(4) = x(1) + x(2) + x(3) - 1;
end
Any help is greatly appreciated!
  1 Comment
Justyn Welsh
Justyn Welsh on 8 Mar 2024
Just to add, the fmincon was not an error, but showing it is satsfied. Issue is that it seems to be using Levenberg-Maarquardt rather than SQP.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 8 Mar 2024
Edited: Matt J on 8 Mar 2024
There are no errors in what you've shown, but it is peculiar that you have one optimization (with fsolve) nested inside another optimization (with fmincon). It would probably be more robust to combine them into one problem, by converting the fsolve equations to fmincon nonlinear equality constraints.
  2 Comments
Justyn Welsh
Justyn Welsh on 8 Mar 2024
Thank you for the input Matt. It is a bit strange, the exercise is dictating for that particular method, but I agree with you.
Matt J
Matt J on 8 Mar 2024
You're welcome, but if your question is resolved, please Accept-click the answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!