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

3 visualizaciones (últimos 30 días)
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 comentarios
Matt J
Matt J el 9 de Ag. de 2022
Your ub and lb bounds do not make sense. You have 10 bounds, but only 2 unknowns u(i).
B. Berk
B. Berk el 9 de Ag. de 2022
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.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 9 de Ag. de 2022
Editada: Matt J el 9 de Ag. de 2022
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 comentarios
Matt J
Matt J el 9 de Ag. de 2022
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.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 9 de Ag. de 2022
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 comentario
B. Berk
B. Berk el 9 de Ag. de 2022
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by