Quasi Newton optimization for a function

46 Ansichten (letzte 30 Tage)
Manfrid Loper
Manfrid Loper am 2 Mär. 2020
Kommentiert: J. Alex Lee am 3 Mär. 2020
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 Kommentar
J. Alex Lee
J. Alex Lee am 3 Mär. 2020
you've left out the problematic bits, it seems...what are your issues? Are you having trouble writing the vector equations down into code?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Nonlinear Optimization finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by