Quasi Newton optimization for a function
47 次查看(过去 30 天)
显示 更早的评论
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
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 Center 和 File Exchange 中查找有关 Nonlinear Optimization 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!