How do I use fminunc with two variables?

조회 수: 9 (최근 30일)
Sohyeon
Sohyeon 2024년 4월 1일
댓글: Sohyeon 2024년 4월 2일
I am trying to minize the follwing function q given x and muk where muk is each element of the array mu.
I first created the function q and dq, which is the gradient of q with two variables x and muk to be general.
Then, when I am work with the for loop over the size of mu, I was trying to plug in mu particularly and minize the function, q_, which is now a function of the variable x only. However, my code fails and if you guys can look at it. I'll very much appreciate it.
-------------------------------- Code is here -----------------------------------------------
mu = [1, 10, 100, 1000];
tau = 1 ./ mu;
q = @(x, muk) x(1) + x(2) + (muk / 2) * (x(1)^2 + x(2)^2 - 2)^2;
dq = @(x, muk) [1 + muk * 2 * x(1) * (x(1)^2 + x(2)^2 - 2); 1 + muk * 2 * x(2) * (x(1)^2 + x(2)^2 - 2)];
options = optimoptions('fminunc', 'Algorithm', 'trust-region', 'HessianApproximation', 'bfgs', 'SpecifyObjectiveGradient', true);
x0 = [1; 0];
xk = x0;
for k = 1:length(mu)
a = mu(k);
q_ = @(x)(q(x, a));
dq_ = @(x)(dq(x, a));
dq_2 = dq_(xk);
while norm(dq_2) > tau(k)
[x,fval] = fminunc(q_,xk,options)
xk = x;
dq_2 = dq_(xk);
end
xk
end
And I got these error at [x,fval] = fminunc(q_,xk,options) saying
Error using +
Too many output arguments.
Error in HW4_3>@(x,muk)x(1)+x(2)+(muk/2)*(x(1)^2+x(2)^2-2)^2 (line 6)
q = @(x, muk) x(1) + x(2) + (muk / 2) * (x(1)^2 + x(2)^2 - 2)^2;
Error in HW4_3>@(x)(q(x,a)) (line 18)
q_ = @(x)(q(x, a));
Error in fminunc (line 345)
[f,GRAD] = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINUNC cannot continue.

답변 (1개)

Torsten
Torsten 2024년 4월 2일
편집: Torsten 2024년 4월 2일
You tell "fminunc" that you supply the gradient of the objective, but you don't do it.
mu = [1, 10, 100, 1000];
tau = 1 ./ mu;
q = @(x, muk) x(1) + x(2) + (muk / 2) * (x(1)^2 + x(2)^2 - 2)^2;
dq = @(x, muk) [1 + muk * 2 * x(1) * (x(1)^2 + x(2)^2 - 2); 1 + muk * 2 * x(2) * (x(1)^2 + x(2)^2 - 2)];
options = optimoptions('fminunc', 'Algorithm', 'trust-region', 'HessianApproximation', 'bfgs', 'SpecifyObjectiveGradient', true);
x0 = [1; 0];
%xk = x0;
for k = 1:length(mu)
a = mu(k);
%q_ = @(x)(q(x, a));
%dq_ = @(x)(dq(x, a));
%dq_2 = dq_(xk);
%while norm(dq_2) > tau(k)
[x,fval] = fminunc(@(x)q_(x,a,q,dq),x0,options)
%xk = x;
%dq_2 = dq_(xk);
%end
x
end
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
x = 2x1
-1.1072 -1.1072
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = -2.1123
x = 2x1
-1.1072 -1.1072
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Local minimum possible. fminunc stopped because the final change in function value relative to its initial value is less than the value of the function tolerance.
x = 2x1
-1.0123 -1.0123
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = -2.0123
x = 2x1
-1.0123 -1.0123
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Local minimum possible. fminunc stopped because the final change in function value relative to its initial value is less than the value of the function tolerance.
x = 2x1
-1.0012 -1.0013
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = -2.0012
x = 2x1
-1.0012 -1.0013
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Local minimum possible. fminunc stopped because the final change in function value relative to its initial value is less than the value of the function tolerance.
x = 2x1
-1.0001 -1.0001
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval = -2.0001
x = 2x1
-1.0001 -1.0001
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function [z gz] = q_(x,a,q,dq)
z = q(x,a);
if nargout > 1
gz = dq(x,a);
end
end
  댓글 수: 1
Sohyeon
Sohyeon 2024년 4월 2일
Thank you for your answer! It works perfectly!

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

카테고리

Help CenterFile Exchange에서 Simulink Coder에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by