How to use lsqnonlin command for solving a cost function minimization problem which consists of optimization variable?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
B. Berk
el 9 de Ag. de 2022
Comentada: B. Berk
el 9 de Ag. de 2022
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);
2 comentarios
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).
Respuesta aceptada
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
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.
Más respuestas (1)
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)
prob
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.
Ver también
Categorías
Más información sobre Systems of Nonlinear Equations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!