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.
Running into issue using fsolve
조회 수: 5 (최근 30일)
이전 댓글 표시
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!
채택된 답변
Matt J
2024년 3월 8일
편집: Matt J
2024년 3월 8일
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
Matt J
2024년 3월 8일
You're welcome, but if your question is resolved, please Accept-click the answer.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!