How to use lsqnonlin command for solving a cost function minimization problem which consists of optimization variable?

조회 수: 4 (최근 30일)
Hi,
I'm trying to solve an optimization problem by using 'lsqnonlin' comman in matlab. My objective function consists of optimization variables created by 'optimvar' command. I came across with this error,
"Error using lsqfcnchk (line 80)
If FUN is a MATLAB object, it must have an feval method."
when i tried to use that objective function with lsqnonlin. I read that lsqnonlin only accepts function handles or anonymous functions, not symbolic objects. In this case how can i use my objective function with lsqnonlin? I left a piece of code below to show my problem clearly. By the way, I'm new at optimization toolbox. So I'm open for suggestions to use proper methods.
C = [0 0 0 1
0 1 0 0];
D = [0;0];
x = [zeros(4,1)];
u = optimvar('U',2);
f=0;
% for loop here to create an objective function
for i=1:2
y_ara = C*x + D*u(i);
error1 = 1-y_ara(1,1);
error2 = 1-y_ara(2,1);
f = f + error1^2 + error2^2; % Objective function including optimvar U(1) and U(2)
end
prob = optimproblem;
prob.Objective = f;
% initilization and constraits
x0 = [zeros(1,10)];
lb = [-10*ones(1,2)];
ub = [10*ones(1,2)];
options = optimoptions("lsqnonlin",'Algorithm','levenberg-marquardt');
[sol, fval, exitflag, output] = lsqnonlin(f, x0, lb, ub, options);
Error using lsqfcnchk
If FUN is a MATLAB object, it must have an feval method.

Error in lsqnsetup (line 46)
funfcn = lsqfcnchk(FUN,caller,lengthVarargin,funValCheck,flags.grad);

Error in lsqnonlin (line 207)
JACOB,OUTPUT,earlyTermination] = lsqnsetup(FUN,xCurrent,LB,UB,options,defaultopt, ...
  댓글 수: 2
Matt J
Matt J 2022년 8월 9일
Your ub and lb bounds do not make sense. You have 10 bounds, but only 2 unknowns u(i).
B. Berk
B. Berk 2022년 8월 9일
Its just because i copied a part of whole code and little bit manipulated it to make proper here. It looks like i forgot to change that value, however, the error is still same.

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2022년 8월 9일
편집: Matt J 2022년 8월 9일
If you have an objective created with optimvar variables, you would solve the problem by using the solve command,
C = [0 0 0 1
0 1 0 0];
D = [0;0];
...
prob = optimproblem;
prob.Objective = f;
sol=solve(prob);
In the most basic use case, you would not call lsqnonlin or any other solver directly. The solve() command will choose the appropriate solver for you.
  댓글 수: 6
Matt J
Matt J 2022년 8월 9일
Modeling with a 10th order polynomial sounds like a dangerous thing to do. At the very least, you will need a good initial guess, since polynomials tend to have lots of local minima. Thus, you might need to use.
B. Berk
B. Berk 2022년 8월 9일
Thanks again for your contributions, i will consider your suggestions.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 8월 9일
C = [0 0 0 1
0 1 0 0];
D = [0;0];
x = [zeros(4,1)];
u = optimvar('U',2);
f=0;
% for loop here to create an objective function
for i=1:2
y_ara = C*x + D*u(i);
error1 = 1-y_ara(1,1);
error2 = 1-y_ara(2,1);
f = f + error1^2 + error2^2; % Objective function including optimvar U(1) and U(2)
end
prob = optimproblem;
prob.Objective = f;
% initilization and constraits
x0 = [zeros(1,10)];
lb = [-10*ones(1,2)];
ub = [10*ones(1,2)];
%options = optimoptions("lsqnonlin",'Algorithm','levenberg-marquardt');
[sol, fval, exitflag, output] = solve(prob)
Solving problem using lsqlin.
sol = struct with fields:
U: [2×1 double]
fval = 4.0000
exitflag =
OptimalSolution
output = struct with fields:
iterations: 0 algorithm: 'mldivide' firstorderopt: [] constrviolation: [] cgiterations: [] linearsolver: [] message: '' solver: 'lsqlin'
prob
prob =
OptimizationProblem with properties: Description: '' ObjectiveSense: 'minimize' Variables: [1×1 struct] containing 1 OptimizationVariable Objective: [1×1 OptimizationExpression] Constraints: [0×0 struct] containing 0 OptimizationConstraints See problem formulation with show.
f
f =
You have C*x but your x is all 0. You have D*u but your D is all 0. So your optimization expression becomes a constant.
  댓글 수: 1
B. Berk
B. Berk 2022년 8월 9일
Thank you for your response. 'x' values are updating according to (x_dot = A*x + B*u) equation at every loop in the original of code. I just copied and changed this part to make it proper here. This is why x values are zero and optimization expression becomes zero.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by