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);
2 个评论
Matt J
2022-8-9
Your ub and lb bounds do not make sense. You have 10 bounds, but only 2 unknowns u(i).
采纳的回答
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
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.
更多回答(1 个)
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)
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!