Quasi Newton optimization for a function

조회 수: 47 (최근 30일)
Manfrid Loper
Manfrid Loper 2020년 3월 2일
댓글: J. Alex Lee 2020년 3월 3일
Im trying to implement Quasi Newton method to optimize a function. the code so far has two problematic issues that I pointed out in the script (where alpha need to be calculated and also B and Binve be updated), Can someone please help me implement formulas mentioned in Optimization_problem, so i can run the script successfully. Here what i have so far. Thanks very much!
clc;clear;
% objective function, its gradient and Hessian
f = @(x1,x2) -4*x1 - 2*x2 - x1.^2 + 2*x1.^4 - 2*x1.*x2 + 3*x2.^2;
Gradient = @(x) [-4-2*x(1)+8*x(1)^3-2*x(2); -2-2*x(1)+6*x(2);];
%Hessian = @(x) [-2+24*x(1)^2, -2; -2; 6];
% plot contour lines
[X, Y] = meshgrid(-0.25:0.01:1.75, -0.25:0.0025:1.75);
contour(X,Y,f(X,Y),[-4.34 -4.3 -4.2 -4.1 -4.0 -3 -2 -1 0],'ShowText','On'), hold on;
grid on;
% initialization
x0 = [0; 0;];
df0 = Gradient(x0);
B = eye(2);
invB = eye(2);
% store intermediate points
Xs(:,1) = x0;
for i = 1:500
% compute step size "alpha"
%
% %% problematic part
% update x
x1 = x0 - alpha*invB*df0;
df1 = Gradient(x1);
fprintf('It. %i: f(%e,%e) = %e.\n', i, x1(1), x1(2), f(x1(1),x1(2)));
Xs(:,i+1) = x1;
% check the stopping criterion.
if norm(x1-x0)/norm(x1)<1e-4
break;
end
% update B and invB
%
%problematic part
%
% update x0 and df0
x0 = x1;
df0 = df1;
end
plot(Xs(1,:), Xs(2,:),'o-');
daspect([1 1 1]);
hold off;
  댓글 수: 1
J. Alex Lee
J. Alex Lee 2020년 3월 3일
you've left out the problematic bits, it seems...what are your issues? Are you having trouble writing the vector equations down into code?

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Nonlinear Optimization에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by