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

3 views (last 30 days)
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 Comments
B. Berk
B. Berk on 9 Aug 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.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 9 Aug 2022
Edited: Matt J on 9 Aug 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 Comments
Matt J
Matt J on 9 Aug 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.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 9 Aug 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 Comment
B. Berk
B. Berk on 9 Aug 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.

Sign in to comment.

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!